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" |
Reid Kleckner | 04f9bca | 2018-03-07 22:48:35 +0000 | [diff] [blame] | 34 | #include "clang/Sema/ScopeInfo.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 35 | #include "clang/Sema/SemaInternal.h" |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 40 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 41 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 42 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 43 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 44 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 45 | C, |
| 46 | Cpp, |
| 47 | ObjC |
| 48 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 49 | } // end namespace AttributeLangSupport |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Helper functions |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 55 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 56 | /// type (function or function-typed variable) or an Objective-C |
| 57 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 58 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 59 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 60 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 61 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 62 | /// Return true if the given decl has function type (function or |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 63 | /// function-typed variable) or an Objective-C method or a block. |
| 64 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 65 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 66 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 67 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 68 | /// Return true if the given decl has a declarator that should have |
| 69 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 71 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 73 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 76 | /// hasFunctionProto - Return true if the given decl has a argument |
| 77 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 78 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 80 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 81 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 82 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 85 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 86 | /// 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] | 87 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 88 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 89 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 90 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 91 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 92 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 96 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 97 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 98 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 99 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 100 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 101 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 102 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 105 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 106 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 107 | return FD->getParamDecl(Idx)->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 108 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 109 | return MD->parameters()[Idx]->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 110 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 111 | return BD->getParamDecl(Idx)->getSourceRange(); |
| 112 | return SourceRange(); |
| 113 | } |
| 114 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 116 | if (const FunctionType *FnTy = D->getFunctionType()) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 117 | return FnTy->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 118 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 121 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 122 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 123 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 124 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 125 | return MD->getReturnTypeSourceRange(); |
| 126 | return SourceRange(); |
| 127 | } |
| 128 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 130 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 131 | return cast<FunctionProtoType>(FnTy)->isVariadic(); |
| 132 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 133 | return BD->isVariadic(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 134 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | static bool isInstanceMethod(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 138 | if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 139 | return MethodDecl->isInstance(); |
| 140 | return false; |
| 141 | } |
| 142 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 143 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 144 | const auto *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 145 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 146 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 147 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 148 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 149 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 150 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 151 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 152 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 154 | // FIXME: Should we walk the chain of classes? |
| 155 | return ClsName == &Ctx.Idents.get("NSString") || |
| 156 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 157 | } |
| 158 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 159 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 160 | const auto *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 161 | if (!PT) |
| 162 | return false; |
| 163 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 164 | const auto *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 165 | if (!RT) |
| 166 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 168 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 169 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 170 | return false; |
| 171 | |
| 172 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 173 | } |
| 174 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 175 | static unsigned getNumAttributeArgs(const ParsedAttr &AL) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 176 | // FIXME: Include the type in the argument list. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 177 | return AL.getNumArgs() + AL.hasParsedType(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 180 | template <typename Compare> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 181 | static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 182 | unsigned Num, unsigned Diag, |
| 183 | Compare Comp) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 184 | if (Comp(getNumAttributeArgs(AL), Num)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 185 | S.Diag(AL.getLoc(), Diag) << AL << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 192 | /// Check if the attribute has exactly as many args as Num. May |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 193 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 194 | static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 195 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 196 | diag::err_attribute_wrong_number_arguments, |
| 197 | std::not_equal_to<unsigned>()); |
| 198 | } |
| 199 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 200 | /// Check if the attribute has at least as many args as Num. May |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 201 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 202 | static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 203 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 204 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 205 | diag::err_attribute_too_few_arguments, |
| 206 | std::less<unsigned>()); |
| 207 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 208 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 209 | /// Check if the attribute has at most as many args as Num. May |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 210 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 211 | static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL, |
| 212 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 213 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 214 | diag::err_attribute_too_many_arguments, |
| 215 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 218 | /// A helper function to provide Attribute Location for the Attr types |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 219 | /// AND the ParsedAttr. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 220 | template <typename AttrInfo> |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 221 | static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 222 | SourceLocation>::type |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 223 | getAttrLoc(const AttrInfo &AL) { |
| 224 | return AL.getLocation(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 225 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 226 | static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); } |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 227 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 228 | /// If Expr is a valid integer constant, get the value of the integer |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 229 | /// expression and return success or failure. May output an error. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 230 | template <typename AttrInfo> |
| 231 | static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 232 | uint32_t &Val, unsigned Idx = UINT_MAX) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 233 | llvm::APSInt I(32); |
| 234 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 235 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 236 | if (Idx != UINT_MAX) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 237 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 238 | << AI << Idx << AANT_ArgumentIntegerConstant |
| 239 | << Expr->getSourceRange(); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 240 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 241 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 242 | << AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange(); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 245 | |
| 246 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 247 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 248 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
| 251 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 252 | Val = (uint32_t)I.getZExtValue(); |
| 253 | return true; |
| 254 | } |
| 255 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 256 | /// Wrapper around checkUInt32Argument, with an extra check to be sure |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 257 | /// that the result will fit into a regular (signed) int. All args have the same |
| 258 | /// purpose as they do in checkUInt32Argument. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 259 | template <typename AttrInfo> |
| 260 | static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 261 | int &Val, unsigned Idx = UINT_MAX) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 262 | uint32_t UVal; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 263 | if (!checkUInt32Argument(S, AI, Expr, UVal, Idx)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 264 | return false; |
| 265 | |
George Burgess IV | a804957 | 2016-12-22 19:00:31 +0000 | [diff] [blame] | 266 | if (UVal > (uint32_t)std::numeric_limits<int>::max()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 267 | llvm::APSInt I(32); // for toString |
| 268 | I = UVal; |
| 269 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 270 | << I.toString(10, false) << 32 << /* Unsigned */ 0; |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | Val = UVal; |
| 275 | return true; |
| 276 | } |
| 277 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 278 | /// Diagnose mutually exclusive attributes when present on a given |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 279 | /// declaration. Returns true if diagnosed. |
| 280 | template <typename AttrTy> |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 281 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 282 | if (const auto *A = D->getAttr<AttrTy>()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 283 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A; |
| 284 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
| 285 | return true; |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | template <typename AttrTy> |
| 291 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) { |
| 292 | if (const auto *A = D->getAttr<AttrTy>()) { |
| 293 | S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 294 | << A; |
| 295 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 296 | return true; |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 301 | /// Check if IdxExpr is a valid parameter index for a function or |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 302 | /// instance method D. May output an error. |
| 303 | /// |
| 304 | /// \returns true if IdxExpr is a valid index. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 305 | template <typename AttrInfo> |
| 306 | static bool checkFunctionOrMethodParameterIndex( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 307 | Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 308 | const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 309 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 310 | |
| 311 | // In C++ the implicit 'this' function parameter also counts. |
| 312 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 313 | bool HP = hasFunctionProto(D); |
| 314 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 315 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 316 | unsigned NumParams = |
| 317 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 318 | |
| 319 | llvm::APSInt IdxInt; |
| 320 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 321 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 322 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 323 | << &AI << AttrArgNum << AANT_ArgumentIntegerConstant |
| 324 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 328 | unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX); |
| 329 | if (IdxSource < 1 || (!IV && IdxSource > NumParams)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 330 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 331 | << &AI << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 332 | return false; |
| 333 | } |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 334 | if (HasImplicitThisParam && !CanIndexImplicitThis) { |
| 335 | if (IdxSource == 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 336 | S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument) |
| 337 | << &AI << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 338 | return false; |
| 339 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 342 | Idx = ParamIdx(IdxSource, D); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 346 | /// Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 347 | /// If not emit an error and return false. If the argument is an identifier it |
| 348 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 349 | /// literal. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 350 | bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum, |
| 351 | StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 352 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 353 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 354 | if (AL.isArgIdent(ArgNum)) { |
| 355 | IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 356 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 357 | << AL << AANT_ArgumentString |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 358 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 359 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 360 | Str = Loc->Ident->getName(); |
| 361 | if (ArgLocation) |
| 362 | *ArgLocation = Loc->Loc; |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | // Now check for an actual string literal. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 367 | Expr *ArgExpr = AL.getArgAsExpr(ArgNum); |
| 368 | const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 369 | if (ArgLocation) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 370 | *ArgLocation = ArgExpr->getBeginLoc(); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 371 | |
| 372 | if (!Literal || !Literal->isAscii()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 373 | Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 374 | << AL << AANT_ArgumentString; |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
| 378 | Str = Literal->getString(); |
| 379 | return true; |
| 380 | } |
| 381 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 382 | /// Applies the given attribute to the Decl without performing any |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 383 | /// additional semantic checking. |
| 384 | template <typename AttrType> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 385 | static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 386 | D->addAttr(::new (S.Context) AttrType(AL.getRange(), S.Context, |
| 387 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 390 | template <typename AttrType> |
| 391 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 392 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 393 | handleSimpleAttribute<AttrType>(S, D, AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 396 | /// Applies the given attribute to the Decl so long as the Decl doesn't |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 397 | /// already have one of the given incompatible attributes. |
| 398 | template <typename AttrType, typename IncompatibleAttrType, |
| 399 | typename... IncompatibleAttrTypes> |
| 400 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 401 | const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 402 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL)) |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 403 | return; |
| 404 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 405 | AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 408 | /// Check if the passed-in expression is of type int or bool. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 409 | static bool isIntOrBool(Expr *Exp) { |
| 410 | QualType QT = Exp->getType(); |
| 411 | return QT->isBooleanType() || QT->isIntegerType(); |
| 412 | } |
| 413 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 414 | |
| 415 | // Check to see if the type is a smart pointer of some kind. We assume |
| 416 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 417 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 418 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 419 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 420 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 421 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 422 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 423 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 424 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 425 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 426 | return false; |
| 427 | |
| 428 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 431 | /// Check if passed in Decl is a pointer type. |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 432 | /// Note that this function may produce an error message. |
| 433 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 434 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 435 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 436 | const auto *VD = cast<ValueDecl>(D); |
| 437 | QualType QT = VD->getType(); |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 438 | if (QT->isAnyPointerType()) |
| 439 | return true; |
| 440 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 441 | if (const auto *RT = QT->getAs<RecordType>()) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 442 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 443 | // (We don't want to force template instantiation if we can avoid it, |
| 444 | // since that would alter the order in which templates are instantiated.) |
| 445 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 446 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 447 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 448 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 449 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 450 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 451 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 452 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 453 | return false; |
| 454 | } |
| 455 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 456 | /// Checks that the passed in QualType either is of RecordType or points |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 457 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 458 | static const RecordType *getRecordType(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 459 | if (const auto *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 460 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 461 | |
| 462 | // Now check if we point to record type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 463 | if (const auto *PT = QT->getAs<PointerType>()) |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 464 | return PT->getPointeeType()->getAs<RecordType>(); |
| 465 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 466 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 469 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 470 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 471 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 472 | if (!RT) |
| 473 | return false; |
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 | // 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] | 476 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 477 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 478 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 479 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 480 | // FIXME -- Check the type that the smart pointer points to. |
| 481 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 482 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 483 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 484 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 485 | RecordDecl *RD = RT->getDecl(); |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 486 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 487 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 488 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 489 | // Else check if any base classes have a capability. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 490 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 491 | CXXBasePaths BPaths(false, false); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 492 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 493 | const auto *Type = BS->getType()->getAs<RecordType>(); |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 494 | return Type->getDecl()->hasAttr<CapabilityAttr>(); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 495 | }, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 496 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 497 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 501 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 502 | const auto *TD = Ty->getAs<TypedefType>(); |
| 503 | if (!TD) |
| 504 | return false; |
| 505 | |
| 506 | TypedefNameDecl *TN = TD->getDecl(); |
| 507 | if (!TN) |
| 508 | return false; |
| 509 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 510 | return TN->hasAttr<CapabilityAttr>(); |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 513 | static bool typeHasCapability(Sema &S, QualType Ty) { |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 514 | if (checkTypedefTypeForCapability(Ty)) |
| 515 | return true; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 516 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 517 | if (checkRecordTypeForCapability(S, Ty)) |
| 518 | return true; |
| 519 | |
| 520 | return false; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 523 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 524 | // Capability expressions are simple expressions involving the boolean logic |
| 525 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 526 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 527 | // is a capability or not. |
| 528 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 529 | if (const auto *E = dyn_cast<CastExpr>(Ex)) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 530 | return isCapabilityExpr(S, E->getSubExpr()); |
| 531 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 532 | return isCapabilityExpr(S, E->getSubExpr()); |
| 533 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 534 | if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf || |
| 535 | E->getOpcode() == UO_Deref) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 536 | return isCapabilityExpr(S, E->getSubExpr()); |
| 537 | return false; |
| 538 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 539 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 540 | return isCapabilityExpr(S, E->getLHS()) && |
| 541 | isCapabilityExpr(S, E->getRHS()); |
| 542 | return false; |
| 543 | } |
| 544 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 545 | return typeHasCapability(S, Ex->getType()); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 548 | /// Checks that all attribute arguments, starting from Sidx, resolve to |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 549 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 550 | /// \param Sidx The attribute argument index to start checking with. |
| 551 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 552 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 553 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 554 | const ParsedAttr &AL, |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 555 | SmallVectorImpl<Expr *> &Args, |
| 556 | int Sidx = 0, |
| 557 | bool ParamIdxOk = false) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 558 | for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) { |
| 559 | Expr *ArgExp = AL.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 560 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 561 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 562 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 563 | Args.push_back(ArgExp); |
| 564 | continue; |
| 565 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 566 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 567 | if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 568 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 569 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 570 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 571 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 572 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 573 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 574 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 575 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 576 | // We allow constant strings to be used as a placeholder for expressions |
| 577 | // that are not valid C++ syntax, but warn that they are ignored. |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 578 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 579 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 580 | continue; |
| 581 | } |
| 582 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 583 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 584 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 585 | // A pointer to member expression of the form &MyClass::mu is treated |
| 586 | // specially -- we need to look at the type of the member. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 587 | if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 588 | if (UOp->getOpcode() == UO_AddrOf) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 589 | if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 590 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 591 | ArgTy = DRE->getDecl()->getType(); |
| 592 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 593 | // 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] | 594 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 595 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 596 | // Now check if we index into a record type function param. |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 597 | if(!RT && ParamIdxOk) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 598 | const auto *FD = dyn_cast<FunctionDecl>(D); |
| 599 | const auto *IL = dyn_cast<IntegerLiteral>(ArgExp); |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 600 | if(FD && IL) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 601 | unsigned int NumParams = FD->getNumParams(); |
| 602 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 603 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 604 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 605 | if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
| 606 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 607 | << AL << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 608 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 609 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 610 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 614 | // If the type does not have a capability, see if the components of the |
| 615 | // expression have capabilities. This allows for writing C code where the |
| 616 | // capability may be on the type, and the expression is a capability |
| 617 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 618 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 619 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 620 | << AL << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 621 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 622 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 623 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 626 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 627 | // Attribute Implementations |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 630 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 631 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 632 | return; |
| 633 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 634 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 635 | PtGuardedVarAttr(AL.getRange(), S.Context, |
| 636 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 639 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 640 | Expr *&Arg) { |
| 641 | SmallVector<Expr *, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 642 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 643 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 644 | unsigned Size = Args.size(); |
| 645 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 646 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 647 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 648 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 649 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 650 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 653 | static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 654 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 655 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 656 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 657 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 658 | D->addAttr(::new (S.Context) GuardedByAttr( |
| 659 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 662 | static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 663 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 664 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | return; |
| 666 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 667 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 668 | return; |
| 669 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 670 | D->addAttr(::new (S.Context) PtGuardedByAttr( |
| 671 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 674 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 675 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 676 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 677 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 678 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 679 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 680 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 681 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 682 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL; |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 683 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 684 | } |
| 685 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 686 | // Check that all arguments are lockable objects. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 687 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 688 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 689 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 690 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 691 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 694 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 695 | SmallVector<Expr *, 1> Args; |
| 696 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 697 | return; |
| 698 | |
| 699 | Expr **StartArg = &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 700 | D->addAttr(::new (S.Context) AcquiredAfterAttr( |
| 701 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 702 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 705 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 706 | SmallVector<Expr *, 1> Args; |
| 707 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 708 | return; |
| 709 | |
| 710 | Expr **StartArg = &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 711 | D->addAttr(::new (S.Context) AcquiredBeforeAttr( |
| 712 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 713 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 716 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 717 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 718 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 719 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 720 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 721 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 722 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 725 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 726 | SmallVector<Expr *, 1> Args; |
| 727 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 728 | return; |
| 729 | |
| 730 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 731 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 732 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 733 | AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size, |
| 734 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 738 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 739 | SmallVector<Expr *, 1> Args; |
| 740 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 741 | return; |
| 742 | |
| 743 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 744 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 745 | D->addAttr(::new (S.Context) AssertExclusiveLockAttr( |
| 746 | AL.getRange(), S.Context, StartArg, Size, |
| 747 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 750 | /// Checks to be sure that the given parameter number is in bounds, and |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 751 | /// is an integral type. Will emit appropriate diagnostics if this returns |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 752 | /// false. |
| 753 | /// |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 754 | /// AttrArgNo is used to actually retrieve the argument, so it's base-0. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 755 | template <typename AttrInfo> |
| 756 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 757 | const AttrInfo &AI, unsigned AttrArgNo) { |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 758 | assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument"); |
| 759 | Expr *AttrArg = AI.getArgAsExpr(AttrArgNo); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 760 | ParamIdx Idx; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 761 | if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 762 | Idx)) |
| 763 | return false; |
| 764 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 765 | const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 766 | if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 767 | SourceLocation SrcLoc = AttrArg->getBeginLoc(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 768 | S.Diag(SrcLoc, diag::err_attribute_integers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 769 | << AI << Param->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 770 | return false; |
| 771 | } |
| 772 | return true; |
| 773 | } |
| 774 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 775 | static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 776 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 777 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 778 | return; |
| 779 | |
| 780 | const auto *FD = cast<FunctionDecl>(D); |
| 781 | if (!FD->getReturnType()->isPointerType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 782 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 783 | return; |
| 784 | } |
| 785 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 786 | const Expr *SizeExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 787 | int SizeArgNoVal; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 788 | // Parameter indices are 1-indexed, hence Index=1 |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 789 | if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 790 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 791 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 792 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 793 | ParamIdx SizeArgNo(SizeArgNoVal, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 794 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 795 | ParamIdx NumberArgNo; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 796 | if (AL.getNumArgs() == 2) { |
| 797 | const Expr *NumberExpr = AL.getArgAsExpr(1); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 798 | int Val; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 799 | // Parameter indices are 1-based, hence Index=2 |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 800 | if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 801 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 802 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 803 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 804 | NumberArgNo = ParamIdx(Val, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 807 | D->addAttr(::new (S.Context) |
| 808 | AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo, |
| 809 | AL.getAttributeSpellingListIndex())); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 810 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 811 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 812 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 813 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 814 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 815 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 816 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 817 | if (!isIntOrBool(AL.getArgAsExpr(0))) { |
| 818 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 819 | << AL << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 820 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 824 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 825 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 826 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 829 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 830 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 831 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 832 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 833 | return; |
| 834 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 835 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr( |
| 836 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(), |
| 837 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 840 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 841 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 842 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 843 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 844 | return; |
| 845 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 846 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 847 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), |
| 848 | Args.size(), AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 851 | static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 852 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 853 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 854 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 855 | unsigned Size = Args.size(); |
| 856 | if (Size == 0) |
| 857 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 858 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 859 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 860 | LockReturnedAttr(AL.getRange(), S.Context, Args[0], |
| 861 | AL.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 864 | static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 865 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 866 | return; |
| 867 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 868 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 869 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 870 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 871 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 872 | if (Size == 0) |
| 873 | return; |
| 874 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 875 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 876 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 877 | LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size, |
| 878 | AL.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 881 | static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 882 | Expr *&Cond, StringRef &Msg) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 883 | Cond = AL.getArgAsExpr(0); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 884 | if (!Cond->isTypeDependent()) { |
| 885 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 886 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 887 | return false; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 888 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 891 | if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 892 | return false; |
| 893 | |
| 894 | if (Msg.empty()) |
| 895 | Msg = "<no message provided>"; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 896 | |
| 897 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 898 | if (isa<FunctionDecl>(D) && !Cond->isValueDependent() && |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 899 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 900 | Diags)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 901 | S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL; |
George Burgess IV | 0d54653 | 2016-11-10 21:47:12 +0000 | [diff] [blame] | 902 | for (const PartialDiagnosticAt &PDiag : Diags) |
| 903 | S.Diag(PDiag.first, PDiag.second); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 904 | return false; |
| 905 | } |
| 906 | return true; |
| 907 | } |
| 908 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 909 | static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 910 | S.Diag(AL.getLoc(), diag::ext_clang_enable_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 911 | |
| 912 | Expr *Cond; |
| 913 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 914 | if (checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 915 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 916 | EnableIfAttr(AL.getRange(), S.Context, Cond, Msg, |
| 917 | AL.getAttributeSpellingListIndex())); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | namespace { |
| 921 | /// Determines if a given Expr references any of the given function's |
| 922 | /// ParmVarDecls, or the function's implicit `this` parameter (if applicable). |
| 923 | class ArgumentDependenceChecker |
| 924 | : public RecursiveASTVisitor<ArgumentDependenceChecker> { |
| 925 | #ifndef NDEBUG |
| 926 | const CXXRecordDecl *ClassType; |
| 927 | #endif |
| 928 | llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms; |
| 929 | bool Result; |
| 930 | |
| 931 | public: |
| 932 | ArgumentDependenceChecker(const FunctionDecl *FD) { |
| 933 | #ifndef NDEBUG |
| 934 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 935 | ClassType = MD->getParent(); |
| 936 | else |
| 937 | ClassType = nullptr; |
| 938 | #endif |
| 939 | Parms.insert(FD->param_begin(), FD->param_end()); |
| 940 | } |
| 941 | |
| 942 | bool referencesArgs(Expr *E) { |
| 943 | Result = false; |
| 944 | TraverseStmt(E); |
| 945 | return Result; |
| 946 | } |
| 947 | |
| 948 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 949 | assert(E->getType()->getPointeeCXXRecordDecl() == ClassType && |
| 950 | "`this` doesn't refer to the enclosing class?"); |
| 951 | Result = true; |
| 952 | return false; |
| 953 | } |
| 954 | |
| 955 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 956 | if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 957 | if (Parms.count(PVD)) { |
| 958 | Result = true; |
| 959 | return false; |
| 960 | } |
| 961 | return true; |
| 962 | } |
| 963 | }; |
| 964 | } |
| 965 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 966 | static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 967 | S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 968 | |
| 969 | Expr *Cond; |
| 970 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 971 | if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 972 | return; |
| 973 | |
| 974 | StringRef DiagTypeStr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 975 | if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 976 | return; |
| 977 | |
| 978 | DiagnoseIfAttr::DiagnosticType DiagType; |
| 979 | if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 980 | S.Diag(AL.getArgAsExpr(2)->getBeginLoc(), |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 981 | diag::err_diagnose_if_invalid_diagnostic_type); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 982 | return; |
| 983 | } |
| 984 | |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 985 | bool ArgDependent = false; |
Argyrios Kyrtzidis | 5f0c0aa | 2017-05-24 18:35:01 +0000 | [diff] [blame] | 986 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 987 | ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 988 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 989 | AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent, |
| 990 | cast<NamedDecl>(D), AL.getAttributeSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 993 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 994 | if (D->hasAttr<PassObjectSizeAttr>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 995 | S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 996 | return; |
| 997 | } |
| 998 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 999 | Expr *E = AL.getArgAsExpr(0); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1000 | uint32_t Type; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1001 | if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1)) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | |
| 1004 | // pass_object_size's argument is passed in as the second argument of |
| 1005 | // __builtin_object_size. So, it has the same constraints as that second |
| 1006 | // argument; namely, it must be in the range [0, 3]. |
| 1007 | if (Type > 3) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1008 | S.Diag(E->getBeginLoc(), diag::err_attribute_argument_outof_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1009 | << AL << 0 << 3 << E->getSourceRange(); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | // pass_object_size is only supported on constant pointer parameters; as a |
| 1014 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 1015 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 1016 | // definition, so we defer the constness check until later. |
| 1017 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1018 | S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1022 | D->addAttr(::new (S.Context) PassObjectSizeAttr( |
| 1023 | AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex())); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1026 | static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1027 | ConsumableAttr::ConsumedState DefaultState; |
| 1028 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1029 | if (AL.isArgIdent(0)) { |
| 1030 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1031 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1032 | DefaultState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1033 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1034 | << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1035 | return; |
| 1036 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1037 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1038 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1039 | << AL << AANT_ArgumentIdentifier; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1040 | return; |
| 1041 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1042 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1043 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1044 | ConsumableAttr(AL.getRange(), S.Context, DefaultState, |
| 1045 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1049 | const ParsedAttr &AL) { |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1050 | ASTContext &CurrContext = S.getASTContext(); |
| 1051 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1052 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1053 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1054 | if (!RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1055 | S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1056 | RD->getNameAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1057 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1058 | return false; |
| 1059 | } |
| 1060 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1061 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
| 1064 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1065 | static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1066 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1067 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1068 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1069 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1070 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1071 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1072 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1073 | for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1074 | CallableWhenAttr::ConsumedState CallableState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1075 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1076 | StringRef StateString; |
| 1077 | SourceLocation Loc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1078 | if (AL.isArgIdent(ArgIndex)) { |
| 1079 | IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex); |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1080 | StateString = Ident->Ident->getName(); |
| 1081 | Loc = Ident->Loc; |
| 1082 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1083 | if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc)) |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1084 | return; |
| 1085 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1086 | |
| 1087 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1088 | CallableState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1089 | S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1090 | return; |
| 1091 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1092 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1093 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1094 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1095 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1096 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1097 | CallableWhenAttr(AL.getRange(), S.Context, States.data(), |
| 1098 | States.size(), AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1101 | static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1102 | ParamTypestateAttr::ConsumedState ParamState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1103 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1104 | if (AL.isArgIdent(0)) { |
| 1105 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1106 | StringRef StateString = Ident->Ident->getName(); |
| 1107 | |
| 1108 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1109 | ParamState)) { |
| 1110 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1111 | << AL << StateString; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1112 | return; |
| 1113 | } |
| 1114 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1115 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1116 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1117 | return; |
| 1118 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1119 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1120 | // FIXME: This check is currently being done in the analysis. It can be |
| 1121 | // enabled here only after the parser propagates attributes at |
| 1122 | // template specialization definition, not declaration. |
| 1123 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1124 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1125 | // |
| 1126 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1127 | // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1128 | // ReturnType.getAsString(); |
| 1129 | // return; |
| 1130 | //} |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1131 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1132 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1133 | ParamTypestateAttr(AL.getRange(), S.Context, ParamState, |
| 1134 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1137 | static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1138 | ReturnTypestateAttr::ConsumedState ReturnState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1139 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1140 | if (AL.isArgIdent(0)) { |
| 1141 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1142 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1143 | ReturnState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1144 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1145 | << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1146 | return; |
| 1147 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1148 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1149 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1150 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1151 | return; |
| 1152 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1153 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1154 | // FIXME: This check is currently being done in the analysis. It can be |
| 1155 | // enabled here only after the parser propagates attributes at |
| 1156 | // template specialization definition, not declaration. |
| 1157 | //QualType ReturnType; |
| 1158 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1159 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1160 | // ReturnType = Param->getType(); |
| 1161 | // |
| 1162 | //} else if (const CXXConstructorDecl *Constructor = |
| 1163 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1164 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1165 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1166 | //} else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1167 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1168 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1169 | //} |
| 1170 | // |
| 1171 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1172 | // |
| 1173 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1174 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1175 | // ReturnType.getAsString(); |
| 1176 | // return; |
| 1177 | //} |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1178 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1179 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1180 | ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState, |
| 1181 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1184 | static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1185 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1186 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1187 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1188 | SetTypestateAttr::ConsumedState NewState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1189 | if (AL.isArgIdent(0)) { |
| 1190 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1191 | StringRef Param = Ident->Ident->getName(); |
| 1192 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1193 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1194 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1195 | return; |
| 1196 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1197 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1198 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1199 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1200 | return; |
| 1201 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1202 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1203 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1204 | SetTypestateAttr(AL.getRange(), S.Context, NewState, |
| 1205 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1208 | static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1209 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1210 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1211 | |
| 1212 | TestTypestateAttr::ConsumedState TestState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1213 | if (AL.isArgIdent(0)) { |
| 1214 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1215 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1216 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1217 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1218 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1219 | return; |
| 1220 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1221 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1222 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1223 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1224 | return; |
| 1225 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1226 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1227 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1228 | TestTypestateAttr(AL.getRange(), S.Context, TestState, |
| 1229 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1232 | static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1233 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1234 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1237 | static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1238 | if (auto *TD = dyn_cast<TagDecl>(D)) |
| 1239 | TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context, |
| 1240 | AL.getAttributeSpellingListIndex())); |
| 1241 | else if (auto *FD = dyn_cast<FieldDecl>(D)) { |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1242 | bool BitfieldByteAligned = (!FD->getType()->isDependentType() && |
| 1243 | !FD->getType()->isIncompleteType() && |
| 1244 | FD->isBitField() && |
| 1245 | S.Context.getTypeAlign(FD->getType()) <= 8); |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1246 | |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1247 | if (S.getASTContext().getTargetInfo().getTriple().isPS4()) { |
| 1248 | if (BitfieldByteAligned) |
| 1249 | // The PS4 target needs to maintain ABI backwards compatibility. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1250 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1251 | << AL << FD->getType(); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1252 | else |
| 1253 | FD->addAttr(::new (S.Context) PackedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1254 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1255 | } else { |
| 1256 | // Report warning about changed offset in the newer compiler versions. |
| 1257 | if (BitfieldByteAligned) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1258 | S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1259 | |
| 1260 | FD->addAttr(::new (S.Context) PackedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1261 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1264 | } else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1265 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1268 | static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1269 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1270 | // variables or properties of Objective-C classes. The outlet must also |
| 1271 | // have an object reference type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1272 | if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1273 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1274 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1275 | << AL << VD->getType() << 0; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1276 | return false; |
| 1277 | } |
| 1278 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1279 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1280 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1281 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1282 | << AL << PD->getType() << 1; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1283 | return false; |
| 1284 | } |
| 1285 | } |
| 1286 | else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1287 | S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1288 | return false; |
| 1289 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1290 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1291 | return true; |
| 1292 | } |
| 1293 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1294 | static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1295 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1296 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1297 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1298 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1299 | IBOutletAttr(AL.getRange(), S.Context, |
| 1300 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1303 | static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1304 | |
| 1305 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1306 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1307 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1308 | return; |
| 1309 | } |
| 1310 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1311 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1312 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1313 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1314 | ParsedType PT; |
| 1315 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1316 | if (AL.hasParsedType()) |
| 1317 | PT = AL.getTypeArg(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1318 | else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1319 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1320 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1321 | if (!PT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1322 | S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1323 | return; |
| 1324 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1325 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1326 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1327 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1328 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1329 | if (!QTLoc) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1330 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1331 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1332 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1333 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1334 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1335 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1336 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1337 | S.Diag(AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1338 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1339 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1340 | return; |
| 1341 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1342 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1343 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1344 | IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc, |
| 1345 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1348 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1349 | if (RefOkay) { |
| 1350 | if (T->isReferenceType()) |
| 1351 | return true; |
| 1352 | } else { |
| 1353 | T = T.getNonReferenceType(); |
| 1354 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1355 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1356 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1357 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1358 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1359 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1360 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1361 | for (const auto *I : UD->fields()) { |
| 1362 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1363 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1364 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1372 | static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1373 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1374 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1375 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1376 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1377 | if (isReturnValue) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1378 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1379 | << AL << AttrParmRange << TypeRange; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1380 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1381 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1382 | << AL << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
| 1385 | return true; |
| 1386 | } |
| 1387 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1388 | static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1389 | SmallVector<ParamIdx, 8> NonNullArgs; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1390 | for (unsigned I = 0; I < AL.getNumArgs(); ++I) { |
| 1391 | Expr *Ex = AL.getArgAsExpr(I); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1392 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1393 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1394 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1397 | if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) && |
| 1398 | !attrNonNullArgCheck( |
| 1399 | S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL, |
| 1400 | Ex->getSourceRange(), |
| 1401 | getFunctionOrMethodParamRange(D, Idx.getASTIndex()))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1402 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1403 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1404 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1405 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1406 | |
| 1407 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1408 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1409 | // check if the attribute came from a macro expansion or a template |
| 1410 | // instantiation. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1411 | if (NonNullArgs.empty() && AL.getLoc().isFileID() && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1412 | !S.inTemplateInstantiation()) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1413 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1414 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1415 | I != E && !AnyPointers; ++I) { |
| 1416 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1417 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1418 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1419 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1420 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1421 | if (!AnyPointers) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1422 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1423 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1424 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1425 | ParamIdx *Start = NonNullArgs.data(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1426 | unsigned Size = NonNullArgs.size(); |
| 1427 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1428 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1429 | NonNullAttr(AL.getRange(), S.Context, Start, Size, |
| 1430 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1433 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1434 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1435 | if (AL.getNumArgs() > 0) { |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1436 | if (D->getFunctionType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1437 | handleNonNullAttr(S, D, AL); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1438 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1439 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1440 | << D->getSourceRange(); |
| 1441 | } |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | // Is the argument a pointer type? |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1446 | if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(), |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1447 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1448 | return; |
| 1449 | |
| 1450 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1451 | NonNullAttr(AL.getRange(), S.Context, nullptr, 0, |
| 1452 | AL.getAttributeSpellingListIndex())); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1455 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1456 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1457 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1458 | if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1459 | /* isReturnValue */ true)) |
| 1460 | return; |
| 1461 | |
| 1462 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1463 | ReturnsNonNullAttr(AL.getRange(), S.Context, |
| 1464 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1467 | static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1468 | if (D->isInvalidDecl()) |
| 1469 | return; |
| 1470 | |
| 1471 | // noescape only applies to pointer types. |
| 1472 | QualType T = cast<ParmVarDecl>(D)->getType(); |
| 1473 | if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1474 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1475 | << AL << AL.getRange() << 0; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | D->addAttr(::new (S.Context) NoEscapeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1480 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1483 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1484 | Expr *E = AL.getArgAsExpr(0), |
| 1485 | *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr; |
| 1486 | S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE, |
| 1487 | AL.getAttributeSpellingListIndex()); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1490 | static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1491 | S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 1492 | AL.getAttributeSpellingListIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1495 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1496 | Expr *OE, unsigned SpellingListIndex) { |
| 1497 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1498 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1499 | |
| 1500 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1501 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1502 | |
| 1503 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1504 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1505 | << &TmpAttr << AttrRange << SR; |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | if (!E->isValueDependent()) { |
| 1510 | llvm::APSInt I(64); |
| 1511 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1512 | if (OE) |
| 1513 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1514 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1515 | << E->getSourceRange(); |
| 1516 | else |
| 1517 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1518 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1519 | << E->getSourceRange(); |
| 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | if (!I.isPowerOf2()) { |
| 1524 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1525 | << E->getSourceRange(); |
| 1526 | return; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | if (OE) { |
| 1531 | if (!OE->isValueDependent()) { |
| 1532 | llvm::APSInt I(64); |
| 1533 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1534 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1535 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1536 | << OE->getSourceRange(); |
| 1537 | return; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | D->addAttr(::new (Context) |
| 1543 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1544 | } |
| 1545 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1546 | void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr, |
| 1547 | unsigned SpellingListIndex) { |
| 1548 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1549 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1550 | AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1551 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1552 | |
| 1553 | if (!ResultType->isDependentType() && |
| 1554 | !isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1555 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1556 | << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D); |
| 1557 | return; |
| 1558 | } |
| 1559 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1560 | ParamIdx Idx; |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1561 | const auto *FuncDecl = cast<FunctionDecl>(D); |
| 1562 | if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1563 | /*AttrArgNo=*/1, ParamExpr, Idx)) |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1564 | return; |
| 1565 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1566 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1567 | if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1568 | Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1569 | << &TmpAttr |
| 1570 | << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | } |
| 1573 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1574 | D->addAttr(::new (Context) |
| 1575 | AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex)); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1578 | /// Normalize the attribute, __foo__ becomes foo. |
| 1579 | /// Returns true if normalization was applied. |
| 1580 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1581 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1582 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1583 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1584 | return true; |
| 1585 | } |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1589 | static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1590 | // This attribute must be applied to a function declaration. The first |
| 1591 | // argument to the attribute must be an identifier, the name of the resource, |
| 1592 | // for example: malloc. The following arguments must be argument indexes, the |
| 1593 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1594 | // 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] | 1595 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1596 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1597 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1598 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1599 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1600 | << AL << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1601 | return; |
| 1602 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1603 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1604 | // Figure out our Kind. |
| 1605 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1606 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1607 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1608 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1609 | // Check arguments. |
| 1610 | switch (K) { |
| 1611 | case OwnershipAttr::Takes: |
| 1612 | case OwnershipAttr::Holds: |
| 1613 | if (AL.getNumArgs() < 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1614 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1615 | return; |
| 1616 | } |
| 1617 | break; |
| 1618 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1619 | if (AL.getNumArgs() > 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1620 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1621 | return; |
| 1622 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1623 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1626 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1627 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1628 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1629 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1630 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1631 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1632 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1633 | SmallVector<ParamIdx, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1634 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1635 | Expr *Ex = AL.getArgAsExpr(i); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1636 | ParamIdx Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1637 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1638 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1639 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1640 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1641 | QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1642 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1643 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1644 | case OwnershipAttr::Takes: |
| 1645 | case OwnershipAttr::Holds: |
| 1646 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1647 | Err = 0; |
| 1648 | break; |
| 1649 | case OwnershipAttr::Returns: |
| 1650 | if (!T->isIntegerType()) |
| 1651 | Err = 1; |
| 1652 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1653 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1654 | if (-1 != Err) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1655 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err |
| 1656 | << Ex->getSourceRange(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1657 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1658 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1659 | |
| 1660 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1661 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1662 | // Cannot have two ownership attributes of different kinds for the same |
| 1663 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1664 | if (I->getOwnKind() != K && I->args_end() != |
| 1665 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1666 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1667 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1668 | } else if (K == OwnershipAttr::Returns && |
| 1669 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1670 | // A returns attribute conflicts with any other returns attribute using |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1671 | // a different index. |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1672 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1673 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1674 | << I->args_begin()->getSourceIndex(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1675 | if (I->args_size()) |
| 1676 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1677 | << Idx.getSourceIndex() << Ex->getSourceRange(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1678 | return; |
| 1679 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1680 | } |
| 1681 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1682 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1685 | ParamIdx *Start = OwnershipArgs.data(); |
| 1686 | unsigned Size = OwnershipArgs.size(); |
| 1687 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1688 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1689 | OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size, |
| 1690 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1693 | static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1694 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1695 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1696 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1697 | return; |
| 1698 | } |
| 1699 | |
| 1700 | // gcc rejects |
| 1701 | // class c { |
| 1702 | // static int a __attribute__((weakref ("v2"))); |
| 1703 | // static int b() __attribute__((weakref ("f3"))); |
| 1704 | // }; |
| 1705 | // and ignores the attributes of |
| 1706 | // void f(void) { |
| 1707 | // static int a __attribute__((weakref ("v2"))); |
| 1708 | // } |
| 1709 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1710 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1711 | if (!Ctx->isFileContext()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1712 | S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1713 | << cast<NamedDecl>(D); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1714 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | // The GCC manual says |
| 1718 | // |
| 1719 | // At present, a declaration to which `weakref' is attached can only |
| 1720 | // be `static'. |
| 1721 | // |
| 1722 | // It also says |
| 1723 | // |
| 1724 | // Without a TARGET, |
| 1725 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1726 | // equivalent to `weak'. |
| 1727 | // |
| 1728 | // gcc 4.4.1 will accept |
| 1729 | // int a7 __attribute__((weakref)); |
| 1730 | // as |
| 1731 | // int a7 __attribute__((weak)); |
| 1732 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1733 | // it if this behaviour is actually used. |
| 1734 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1735 | // GCC rejects |
| 1736 | // static ((alias ("y"), weakref)). |
| 1737 | // Should we? How to check that weakref is before or after alias? |
| 1738 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1739 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1740 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1741 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1742 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1743 | if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1744 | // GCC will accept anything as the argument of weakref. Should we |
| 1745 | // check for an existing decl? |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1746 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1747 | AL.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1748 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1749 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1750 | WeakRefAttr(AL.getRange(), S.Context, |
| 1751 | AL.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1754 | static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1755 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1756 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1757 | return; |
| 1758 | |
| 1759 | // Aliases should be on declarations, not definitions. |
| 1760 | const auto *FD = cast<FunctionDecl>(D); |
| 1761 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1762 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1763 | return; |
| 1764 | } |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1765 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1766 | D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str, |
| 1767 | AL.getAttributeSpellingListIndex())); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1770 | static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1771 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1772 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1773 | return; |
| 1774 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1775 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1776 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin); |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1777 | return; |
| 1778 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1779 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1780 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx); |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1781 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1782 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1783 | // Aliases should be on declarations, not definitions. |
| 1784 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1785 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1786 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1787 | return; |
| 1788 | } |
| 1789 | } else { |
| 1790 | const auto *VD = cast<VarDecl>(D); |
| 1791 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1792 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1793 | return; |
| 1794 | } |
| 1795 | } |
| 1796 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1797 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1798 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1799 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1800 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1803 | static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1804 | StringRef Model; |
| 1805 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1806 | // Check that it is a string. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1807 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1808 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1809 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1810 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1811 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1812 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1813 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1814 | return; |
| 1815 | } |
| 1816 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1817 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1818 | TLSModelAttr(AL.getRange(), S.Context, Model, |
| 1819 | AL.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1822 | static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1823 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1824 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1825 | D->addAttr(::new (S.Context) RestrictAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1826 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1827 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1830 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1831 | << AL << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1834 | static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1835 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 1836 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 1837 | return; |
| 1838 | |
| 1839 | SmallVector<IdentifierInfo *, 8> CPUs; |
| 1840 | for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) { |
| 1841 | if (!AL.isArgIdent(ArgNo)) { |
| 1842 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1843 | << AL << AANT_ArgumentIdentifier; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1844 | return; |
| 1845 | } |
| 1846 | |
| 1847 | IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo); |
| 1848 | StringRef CPUName = CPUArg->Ident->getName().trim(); |
| 1849 | |
| 1850 | if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) { |
| 1851 | S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value) |
| 1852 | << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch); |
| 1853 | return; |
| 1854 | } |
| 1855 | |
| 1856 | const TargetInfo &Target = S.Context.getTargetInfo(); |
| 1857 | if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) { |
| 1858 | return Target.CPUSpecificManglingCharacter(CPUName) == |
| 1859 | Target.CPUSpecificManglingCharacter(Cur->getName()); |
| 1860 | })) { |
| 1861 | S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries); |
| 1862 | return; |
| 1863 | } |
| 1864 | CPUs.push_back(CPUArg->Ident); |
| 1865 | } |
| 1866 | |
| 1867 | FD->setIsMultiVersion(true); |
| 1868 | if (AL.getKind() == ParsedAttr::AT_CPUSpecific) |
| 1869 | D->addAttr(::new (S.Context) CPUSpecificAttr( |
| 1870 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 1871 | AL.getAttributeSpellingListIndex())); |
| 1872 | else |
| 1873 | D->addAttr(::new (S.Context) CPUDispatchAttr( |
| 1874 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 1875 | AL.getAttributeSpellingListIndex())); |
| 1876 | } |
| 1877 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1878 | static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1879 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1880 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1881 | << AL << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1882 | return; |
| 1883 | } |
| 1884 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1885 | if (CommonAttr *CA = S.mergeCommonAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1886 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1889 | static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1890 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL)) |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1891 | return; |
| 1892 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1893 | if (AL.isDeclspecAttribute()) { |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 1894 | const auto &Triple = S.getASTContext().getTargetInfo().getTriple(); |
| 1895 | const auto &Arch = Triple.getArch(); |
| 1896 | if (Arch != llvm::Triple::x86 && |
| 1897 | (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1898 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1899 | << AL << Triple.getArchName(); |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 1900 | return; |
| 1901 | } |
| 1902 | } |
| 1903 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1904 | D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context, |
| 1905 | AL.getAttributeSpellingListIndex())); |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1908 | static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1909 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1910 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1911 | if (!isa<ObjCMethodDecl>(D)) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 1912 | S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1913 | << Attrs << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1914 | return; |
| 1915 | } |
| 1916 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1917 | D->addAttr(::new (S.Context) NoReturnAttr( |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 1918 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1921 | static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 1922 | if (!S.getLangOpts().CFProtectionBranch) |
| 1923 | S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored); |
| 1924 | else |
| 1925 | handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1928 | bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 1929 | if (!checkAttributeNumArgs(*this, Attrs, 0)) { |
| 1930 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1931 | return true; |
| 1932 | } |
| 1933 | |
| 1934 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1937 | bool Sema::CheckAttrTarget(const ParsedAttr &AL) { |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1938 | // Check whether the attribute is valid on the current target. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1939 | if (!AL.existsInTarget(Context.getTargetInfo())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1940 | Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1941 | AL.setInvalid(); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1942 | return true; |
| 1943 | } |
| 1944 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1945 | return false; |
| 1946 | } |
| 1947 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1948 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1949 | |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1950 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1951 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 1952 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1953 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1954 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1955 | !VD->getType()->isFunctionPointerType())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1956 | S.Diag(AL.getLoc(), AL.isCXX11Attribute() |
| 1957 | ? diag::err_attribute_wrong_decl_type |
| 1958 | : diag::warn_attribute_wrong_decl_type) |
| 1959 | << AL << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1960 | return; |
| 1961 | } |
| 1962 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1963 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1964 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1965 | AnalyzerNoReturnAttr(AL.getRange(), S.Context, |
| 1966 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1969 | // PS3 PPU-specific. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1970 | static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1971 | /* |
| 1972 | Returning a Vector Class in Registers |
| 1973 | |
| 1974 | According to the PPU ABI specifications, a class with a single member of |
| 1975 | vector type is returned in memory when used as the return value of a |
| 1976 | function. |
| 1977 | This results in inefficient code when implementing vector classes. To return |
| 1978 | the value in a single vector register, add the vecreturn attribute to the |
| 1979 | class definition. This attribute is also applicable to struct types. |
| 1980 | |
| 1981 | Example: |
| 1982 | |
| 1983 | struct Vector |
| 1984 | { |
| 1985 | __vector float xyzw; |
| 1986 | } __attribute__((vecreturn)); |
| 1987 | |
| 1988 | Vector Add(Vector lhs, Vector rhs) |
| 1989 | { |
| 1990 | Vector result; |
| 1991 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1992 | return result; // This will be returned in a register |
| 1993 | } |
| 1994 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1995 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1996 | S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1997 | return; |
| 1998 | } |
| 1999 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2000 | const auto *R = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2001 | int count = 0; |
| 2002 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2003 | if (!isa<CXXRecordDecl>(R)) { |
| 2004 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2005 | return; |
| 2006 | } |
| 2007 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2008 | if (!cast<CXXRecordDecl>(R)->isPOD()) { |
| 2009 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2010 | return; |
| 2011 | } |
| 2012 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2013 | for (const auto *I : R->fields()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2014 | if ((count == 1) || !I->getType()->isVectorType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2015 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2016 | return; |
| 2017 | } |
| 2018 | count++; |
| 2019 | } |
| 2020 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2021 | D->addAttr(::new (S.Context) VecReturnAttr( |
| 2022 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2025 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2026 | const ParsedAttr &AL) { |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2027 | if (isa<ParmVarDecl>(D)) { |
| 2028 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 2029 | // parameter of a function declaration or lambda. |
| 2030 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2031 | S.Diag(AL.getLoc(), |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2032 | diag::err_carries_dependency_param_not_function_decl); |
| 2033 | return; |
| 2034 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2035 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2036 | |
| 2037 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2038 | AL.getRange(), S.Context, |
| 2039 | AL.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2042 | static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2043 | bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName(); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2044 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2045 | // If this is spelled as the standard C++17 attribute, but not in C++17, warn |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2046 | // about using it as an extension. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2047 | if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2048 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2049 | |
| 2050 | D->addAttr(::new (S.Context) UnusedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2051 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2054 | static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2055 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2056 | if (AL.getNumArgs() && |
| 2057 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2058 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2059 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2060 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2061 | ConstructorAttr(AL.getRange(), S.Context, priority, |
| 2062 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2065 | static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 2066 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2067 | if (AL.getNumArgs() && |
| 2068 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2069 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2070 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2071 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2072 | DestructorAttr(AL.getRange(), S.Context, priority, |
| 2073 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2076 | template <typename AttrTy> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2077 | static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2078 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2079 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2080 | if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2081 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2082 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2083 | D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str, |
| 2084 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2087 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2088 | const ParsedAttr &AL) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2089 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2090 | S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2091 | << AL << AL.getRange(); |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 2092 | return; |
| 2093 | } |
| 2094 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2095 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2096 | ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context, |
| 2097 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2100 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2101 | IdentifierInfo *Platform, |
| 2102 | VersionTuple Introduced, |
| 2103 | VersionTuple Deprecated, |
| 2104 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2105 | StringRef PlatformName |
| 2106 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2107 | if (PlatformName.empty()) |
| 2108 | PlatformName = Platform->getName(); |
| 2109 | |
| 2110 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2111 | // of these steps are needed). |
| 2112 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2113 | !(Introduced <= Deprecated)) { |
| 2114 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2115 | << 1 << PlatformName << Deprecated.getAsString() |
| 2116 | << 0 << Introduced.getAsString(); |
| 2117 | return true; |
| 2118 | } |
| 2119 | |
| 2120 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2121 | !(Introduced <= Obsoleted)) { |
| 2122 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2123 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2124 | << 0 << Introduced.getAsString(); |
| 2125 | return true; |
| 2126 | } |
| 2127 | |
| 2128 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2129 | !(Deprecated <= Obsoleted)) { |
| 2130 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2131 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2132 | << 1 << Deprecated.getAsString(); |
| 2133 | return true; |
| 2134 | } |
| 2135 | |
| 2136 | return false; |
| 2137 | } |
| 2138 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2139 | /// Check whether the two versions match. |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2140 | /// |
| 2141 | /// If either version tuple is empty, then they are assumed to match. If |
| 2142 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2143 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2144 | bool BeforeIsOkay) { |
| 2145 | if (X.empty() || Y.empty()) |
| 2146 | return true; |
| 2147 | |
| 2148 | if (X == Y) |
| 2149 | return true; |
| 2150 | |
| 2151 | if (BeforeIsOkay && X < Y) |
| 2152 | return true; |
| 2153 | |
| 2154 | return false; |
| 2155 | } |
| 2156 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2157 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2158 | IdentifierInfo *Platform, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2159 | bool Implicit, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2160 | VersionTuple Introduced, |
| 2161 | VersionTuple Deprecated, |
| 2162 | VersionTuple Obsoleted, |
| 2163 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2164 | StringRef Message, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2165 | bool IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2166 | StringRef Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2167 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2168 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2169 | VersionTuple MergedIntroduced = Introduced; |
| 2170 | VersionTuple MergedDeprecated = Deprecated; |
| 2171 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2172 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2173 | bool OverrideOrImpl = false; |
| 2174 | switch (AMK) { |
| 2175 | case AMK_None: |
| 2176 | case AMK_Redeclaration: |
| 2177 | OverrideOrImpl = false; |
| 2178 | break; |
| 2179 | |
| 2180 | case AMK_Override: |
| 2181 | case AMK_ProtocolImplementation: |
| 2182 | OverrideOrImpl = true; |
| 2183 | break; |
| 2184 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2185 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2186 | if (D->hasAttrs()) { |
| 2187 | AttrVec &Attrs = D->getAttrs(); |
| 2188 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2189 | const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2190 | if (!OldAA) { |
| 2191 | ++i; |
| 2192 | continue; |
| 2193 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2194 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2195 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2196 | if (OldPlatform != Platform) { |
| 2197 | ++i; |
| 2198 | continue; |
| 2199 | } |
| 2200 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2201 | // If there is an existing availability attribute for this platform that |
| 2202 | // is explicit and the new one is implicit use the explicit one and |
| 2203 | // discard the new implicit attribute. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2204 | if (!OldAA->isImplicit() && Implicit) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2205 | return nullptr; |
| 2206 | } |
| 2207 | |
| 2208 | // If there is an existing attribute for this platform that is implicit |
| 2209 | // and the new attribute is explicit then erase the old one and |
| 2210 | // continue processing the attributes. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2211 | if (!Implicit && OldAA->isImplicit()) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2212 | Attrs.erase(Attrs.begin() + i); |
| 2213 | --e; |
| 2214 | continue; |
| 2215 | } |
| 2216 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2217 | FoundAny = true; |
| 2218 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2219 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2220 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2221 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2223 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2224 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2225 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2226 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2227 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2228 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2229 | int Which = -1; |
| 2230 | VersionTuple FirstVersion; |
| 2231 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2232 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2233 | Which = 0; |
| 2234 | FirstVersion = OldIntroduced; |
| 2235 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2236 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2237 | Which = 1; |
| 2238 | FirstVersion = Deprecated; |
| 2239 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2240 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2241 | Which = 2; |
| 2242 | FirstVersion = Obsoleted; |
| 2243 | SecondVersion = OldObsoleted; |
| 2244 | } |
| 2245 | |
| 2246 | if (Which == -1) { |
| 2247 | Diag(OldAA->getLocation(), |
| 2248 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2249 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2250 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2251 | } else { |
| 2252 | Diag(OldAA->getLocation(), |
| 2253 | diag::warn_mismatched_availability_override) |
| 2254 | << Which |
| 2255 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2256 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2257 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2258 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2259 | if (AMK == AMK_Override) |
| 2260 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2261 | else |
| 2262 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2263 | } else { |
| 2264 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2265 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2266 | } |
| 2267 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2268 | Attrs.erase(Attrs.begin() + i); |
| 2269 | --e; |
| 2270 | continue; |
| 2271 | } |
| 2272 | |
| 2273 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2274 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2275 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2276 | |
| 2277 | if (MergedIntroduced2.empty()) |
| 2278 | MergedIntroduced2 = OldIntroduced; |
| 2279 | if (MergedDeprecated2.empty()) |
| 2280 | MergedDeprecated2 = OldDeprecated; |
| 2281 | if (MergedObsoleted2.empty()) |
| 2282 | MergedObsoleted2 = OldObsoleted; |
| 2283 | |
| 2284 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2285 | MergedIntroduced2, MergedDeprecated2, |
| 2286 | MergedObsoleted2)) { |
| 2287 | Attrs.erase(Attrs.begin() + i); |
| 2288 | --e; |
| 2289 | continue; |
| 2290 | } |
| 2291 | |
| 2292 | MergedIntroduced = MergedIntroduced2; |
| 2293 | MergedDeprecated = MergedDeprecated2; |
| 2294 | MergedObsoleted = MergedObsoleted2; |
| 2295 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2296 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | if (FoundAny && |
| 2300 | MergedIntroduced == Introduced && |
| 2301 | MergedDeprecated == Deprecated && |
| 2302 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2303 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2304 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2305 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2306 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2307 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2308 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2309 | !OverrideOrImpl) { |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2310 | auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2311 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2312 | Obsoleted, IsUnavailable, Message, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2313 | IsStrict, Replacement, |
| 2314 | AttrSpellingListIndex); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2315 | Avail->setImplicit(Implicit); |
| 2316 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2317 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2318 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2321 | static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2322 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2323 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2324 | IdentifierLoc *Platform = AL.getArgAsIdent(0); |
| 2325 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2326 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2327 | IdentifierInfo *II = Platform->Ident; |
| 2328 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2329 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2330 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2331 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2332 | auto *ND = dyn_cast<NamedDecl>(D); |
Alex Lorenz | 472cc79 | 2017-04-20 09:35:02 +0000 | [diff] [blame] | 2333 | if (!ND) // We warned about this already, so just return. |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2334 | return; |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2335 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2336 | AvailabilityChange Introduced = AL.getAvailabilityIntroduced(); |
| 2337 | AvailabilityChange Deprecated = AL.getAvailabilityDeprecated(); |
| 2338 | AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted(); |
| 2339 | bool IsUnavailable = AL.getUnavailableLoc().isValid(); |
| 2340 | bool IsStrict = AL.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2341 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2342 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2343 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2344 | StringRef Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2345 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr())) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2346 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2347 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2348 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2349 | false/*Implicit*/, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2350 | Introduced.Version, |
| 2351 | Deprecated.Version, |
| 2352 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2353 | IsUnavailable, Str, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2354 | IsStrict, Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2355 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2356 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2357 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2358 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2359 | |
| 2360 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2361 | // matches before the start of the watchOS platform. |
| 2362 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2363 | IdentifierInfo *NewII = nullptr; |
| 2364 | if (II->getName() == "ios") |
| 2365 | NewII = &S.Context.Idents.get("watchos"); |
| 2366 | else if (II->getName() == "ios_app_extension") |
| 2367 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2368 | |
| 2369 | if (NewII) { |
| 2370 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2371 | if (Version.empty()) |
| 2372 | return Version; |
| 2373 | auto Major = Version.getMajor(); |
| 2374 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2375 | if (NewMajor >= 2) { |
| 2376 | if (Version.getMinor().hasValue()) { |
| 2377 | if (Version.getSubminor().hasValue()) |
| 2378 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2379 | Version.getSubminor().getValue()); |
| 2380 | else |
| 2381 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | return VersionTuple(2, 0); |
| 2386 | }; |
| 2387 | |
| 2388 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2389 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2390 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2391 | |
| 2392 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2393 | AL.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2394 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2395 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2396 | NewIntroduced, |
| 2397 | NewDeprecated, |
| 2398 | NewObsoleted, |
| 2399 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2400 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2401 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2402 | Sema::AMK_None, |
| 2403 | Index); |
| 2404 | if (NewAttr) |
| 2405 | D->addAttr(NewAttr); |
| 2406 | } |
| 2407 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2408 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2409 | // matches before the start of the tvOS platform. |
| 2410 | IdentifierInfo *NewII = nullptr; |
| 2411 | if (II->getName() == "ios") |
| 2412 | NewII = &S.Context.Idents.get("tvos"); |
| 2413 | else if (II->getName() == "ios_app_extension") |
| 2414 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2415 | |
| 2416 | if (NewII) { |
| 2417 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2418 | AL.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2419 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2420 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2421 | Introduced.Version, |
| 2422 | Deprecated.Version, |
| 2423 | Obsoleted.Version, |
| 2424 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2425 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2426 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2427 | Sema::AMK_None, |
| 2428 | Index); |
| 2429 | if (NewAttr) |
| 2430 | D->addAttr(NewAttr); |
| 2431 | } |
| 2432 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2435 | static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2436 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2437 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2438 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2439 | assert(checkAttributeAtMostNumArgs(S, AL, 3) && |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2440 | "Invalid number of arguments in an external_source_symbol attribute"); |
| 2441 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2442 | StringRef Language; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2443 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2444 | Language = SE->getString(); |
| 2445 | StringRef DefinedIn; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2446 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2447 | DefinedIn = SE->getString(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2448 | bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2449 | |
| 2450 | D->addAttr(::new (S.Context) ExternalSourceSymbolAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2451 | AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration, |
| 2452 | AL.getAttributeSpellingListIndex())); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2455 | template <class T> |
| 2456 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2457 | typename T::VisibilityType value, |
| 2458 | unsigned attrSpellingListIndex) { |
| 2459 | T *existingAttr = D->getAttr<T>(); |
| 2460 | if (existingAttr) { |
| 2461 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2462 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2463 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2464 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2465 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2466 | D->dropAttr<T>(); |
| 2467 | } |
| 2468 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2469 | } |
| 2470 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2471 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2472 | VisibilityAttr::VisibilityType Vis, |
| 2473 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2474 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2475 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2478 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2479 | TypeVisibilityAttr::VisibilityType Vis, |
| 2480 | unsigned AttrSpellingListIndex) { |
| 2481 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2482 | AttrSpellingListIndex); |
| 2483 | } |
| 2484 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2485 | static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2486 | bool isTypeVisibility) { |
| 2487 | // Visibility attributes don't mean anything on a typedef. |
| 2488 | if (isa<TypedefNameDecl>(D)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2489 | S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2490 | return; |
| 2491 | } |
| 2492 | |
| 2493 | // 'type_visibility' can only go on a type or namespace. |
| 2494 | if (isTypeVisibility && |
| 2495 | !(isa<TagDecl>(D) || |
| 2496 | isa<ObjCInterfaceDecl>(D) || |
| 2497 | isa<NamespaceDecl>(D))) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2498 | S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2499 | << AL << ExpectedTypeOrNamespace; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2500 | return; |
| 2501 | } |
| 2502 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2503 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2504 | StringRef TypeStr; |
| 2505 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2506 | if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2507 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2508 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2509 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2510 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2511 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL |
| 2512 | << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2513 | return; |
| 2514 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2515 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2516 | // Complain about attempts to use protected visibility on targets |
| 2517 | // (like Darwin) that don't support it. |
| 2518 | if (type == VisibilityAttr::Protected && |
| 2519 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2520 | S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2521 | type = VisibilityAttr::Default; |
| 2522 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2523 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2524 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 2525 | Attr *newAttr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2526 | if (isTypeVisibility) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2527 | newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(), |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2528 | (TypeVisibilityAttr::VisibilityType) type, |
| 2529 | Index); |
| 2530 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2531 | newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2532 | } |
| 2533 | if (newAttr) |
| 2534 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2537 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2538 | const auto *M = cast<ObjCMethodDecl>(D); |
| 2539 | if (!AL.isArgIdent(0)) { |
| 2540 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2541 | << AL << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2544 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2545 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2546 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2547 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2548 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2549 | return; |
| 2550 | } |
| 2551 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2552 | if (F == ObjCMethodFamilyAttr::OMF_init && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2553 | !M->getReturnType()->isObjCObjectPointerType()) { |
| 2554 | S.Diag(M->getLocation(), diag::err_init_method_bad_return_type) |
| 2555 | << M->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2556 | // Ignore the attribute. |
| 2557 | return; |
| 2558 | } |
| 2559 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2560 | D->addAttr(new (S.Context) ObjCMethodFamilyAttr( |
| 2561 | AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2564 | static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2565 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2566 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2567 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2568 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2569 | return; |
| 2570 | } |
| 2571 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2572 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2573 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2574 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2575 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2576 | return; |
| 2577 | } |
| 2578 | } |
| 2579 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2580 | // It is okay to include this attribute on properties, e.g.: |
| 2581 | // |
| 2582 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2583 | // |
| 2584 | // In this case it follows tradition and suppresses an error in the above |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2585 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2586 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2587 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2588 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2589 | ObjCNSObjectAttr(AL.getRange(), S.Context, |
| 2590 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2593 | static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2594 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2595 | QualType T = TD->getUnderlyingType(); |
| 2596 | if (!T->isObjCObjectPointerType()) { |
| 2597 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2598 | return; |
| 2599 | } |
| 2600 | } else { |
| 2601 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2602 | return; |
| 2603 | } |
| 2604 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2605 | ObjCIndependentClassAttr(AL.getRange(), S.Context, |
| 2606 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2609 | static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2610 | if (!AL.isArgIdent(0)) { |
| 2611 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2612 | << AL << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2615 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2616 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2617 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2618 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2619 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2620 | return; |
| 2621 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2622 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2623 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2624 | BlocksAttr(AL.getRange(), S.Context, type, |
| 2625 | AL.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2628 | static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2629 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2630 | if (AL.getNumArgs() > 0) { |
| 2631 | Expr *E = AL.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2632 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2633 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2634 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2635 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2636 | << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2637 | return; |
| 2638 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2639 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2640 | if (Idx.isSigned() && Idx.isNegative()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2641 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2642 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2643 | return; |
| 2644 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2645 | |
| 2646 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2649 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2650 | if (AL.getNumArgs() > 1) { |
| 2651 | Expr *E = AL.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2652 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2653 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2654 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2655 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2656 | << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2657 | return; |
| 2658 | } |
| 2659 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2660 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2661 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2662 | // FIXME: This error message could be improved, it would be nice |
| 2663 | // to say what the bounds actually are. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2664 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2665 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2666 | return; |
| 2667 | } |
| 2668 | } |
| 2669 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2670 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2671 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2672 | if (isa<FunctionNoProtoType>(FT)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2673 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2674 | return; |
| 2675 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2676 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2677 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2678 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2679 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2680 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2681 | } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2682 | if (!MD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2683 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2684 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2685 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2686 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2687 | if (!BD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2688 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2689 | return; |
| 2690 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2691 | } else if (const auto *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2692 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2693 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2694 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2695 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2696 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2697 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2698 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2699 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2700 | return; |
| 2701 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2702 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2703 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2704 | << AL << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2707 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2708 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2709 | << AL << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2710 | return; |
| 2711 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2712 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2713 | SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos, |
| 2714 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2717 | static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2718 | if (D->getFunctionType() && |
| 2719 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2720 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2723 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2724 | if (MD->getReturnType()->isVoidType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2725 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1; |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2726 | return; |
| 2727 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2728 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2729 | // If this is spelled as the standard C++17 attribute, but not in C++17, warn |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2730 | // about using it as an extension. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2731 | if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() && |
| 2732 | !AL.getScopeName()) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2733 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2734 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2735 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2736 | WarnUnusedResultAttr(AL.getRange(), S.Context, |
| 2737 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2740 | static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2741 | // weak_import only applies to variable & function declarations. |
| 2742 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2743 | if (!D->canBeWeakImported(isDef)) { |
| 2744 | if (isDef) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2745 | S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2746 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2747 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2748 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2749 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2750 | // Nothing to warn about here. |
| 2751 | } else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2752 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2753 | << AL << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2754 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2755 | return; |
| 2756 | } |
| 2757 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2758 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2759 | WeakImportAttr(AL.getRange(), S.Context, |
| 2760 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2763 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2764 | template <typename WorkGroupAttr> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2765 | static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2766 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2767 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2768 | const Expr *E = AL.getArgAsExpr(i); |
| 2769 | if (!checkUInt32Argument(S, AL, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2770 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2771 | if (WGSize[i] == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2772 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2773 | << AL << E->getSourceRange(); |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2774 | return; |
| 2775 | } |
| 2776 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2777 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2778 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2779 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2780 | Existing->getYDim() == WGSize[1] && |
| 2781 | Existing->getZDim() == WGSize[2])) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2782 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2783 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2784 | D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context, |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2785 | WGSize[0], WGSize[1], WGSize[2], |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2786 | AL.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2789 | // Handles intel_reqd_sub_group_size. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2790 | static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2791 | uint32_t SGSize; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2792 | const Expr *E = AL.getArgAsExpr(0); |
| 2793 | if (!checkUInt32Argument(S, AL, E, SGSize)) |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2794 | return; |
| 2795 | if (SGSize == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2796 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2797 | << AL << E->getSourceRange(); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2798 | return; |
| 2799 | } |
| 2800 | |
| 2801 | OpenCLIntelReqdSubGroupSizeAttr *Existing = |
| 2802 | D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>(); |
| 2803 | if (Existing && Existing->getSubGroupSize() != SGSize) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2804 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2805 | |
| 2806 | D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2807 | AL.getRange(), S.Context, SGSize, |
| 2808 | AL.getAttributeSpellingListIndex())); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2811 | static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2812 | if (!AL.hasParsedType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2813 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2814 | return; |
| 2815 | } |
| 2816 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2817 | TypeSourceInfo *ParmTSI = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2818 | QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2819 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2820 | |
| 2821 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2822 | (ParmType->isBooleanType() || |
| 2823 | !ParmType->isIntegralType(S.getASTContext()))) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2824 | S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint) |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2825 | << ParmType; |
| 2826 | return; |
| 2827 | } |
| 2828 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2829 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2830 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2831 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2832 | return; |
| 2833 | } |
| 2834 | } |
| 2835 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2836 | D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2837 | ParmTSI, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2838 | AL.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2841 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2842 | StringRef Name, |
| 2843 | unsigned AttrSpellingListIndex) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2844 | // Explicit or partial specializations do not inherit |
| 2845 | // the section attribute from the primary template. |
| 2846 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 2847 | if (AttrSpellingListIndex == SectionAttr::Declspec_allocate && |
| 2848 | FD->isFunctionTemplateSpecialization()) |
| 2849 | return nullptr; |
| 2850 | } |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2851 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2852 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2853 | return nullptr; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2854 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 2855 | << 1 /*section*/; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2856 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2857 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2858 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2859 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2860 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2863 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2864 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2865 | if (!Error.empty()) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2866 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2867 | << 1 /*'section'*/; |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2868 | return false; |
| 2869 | } |
| 2870 | return true; |
| 2871 | } |
| 2872 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2873 | static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2874 | // Make sure that there is a string literal as the sections's single |
| 2875 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2876 | StringRef Str; |
| 2877 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2878 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2879 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2881 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2882 | return; |
| 2883 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2884 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2885 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2886 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2887 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2888 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2889 | return; |
| 2890 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2892 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 2893 | SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2894 | if (NewAttr) |
| 2895 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2898 | static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) { |
| 2899 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName); |
| 2900 | if (!Error.empty()) { |
| 2901 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
| 2902 | << 0 /*'code-seg'*/; |
| 2903 | return false; |
| 2904 | } |
| 2905 | return true; |
| 2906 | } |
| 2907 | |
| 2908 | CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range, |
| 2909 | StringRef Name, |
| 2910 | unsigned AttrSpellingListIndex) { |
| 2911 | // Explicit or partial specializations do not inherit |
| 2912 | // the code_seg attribute from the primary template. |
| 2913 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 2914 | if (FD->isFunctionTemplateSpecialization()) |
| 2915 | return nullptr; |
| 2916 | } |
| 2917 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 2918 | if (ExistingAttr->getName() == Name) |
| 2919 | return nullptr; |
| 2920 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 2921 | << 0 /*codeseg*/; |
| 2922 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2923 | return nullptr; |
| 2924 | } |
| 2925 | return ::new (Context) CodeSegAttr(Range, Context, Name, |
| 2926 | AttrSpellingListIndex); |
| 2927 | } |
| 2928 | |
| 2929 | static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2930 | StringRef Str; |
| 2931 | SourceLocation LiteralLoc; |
| 2932 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
| 2933 | return; |
| 2934 | if (!checkCodeSegName(S, LiteralLoc, Str)) |
| 2935 | return; |
| 2936 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 2937 | if (!ExistingAttr->isImplicit()) { |
| 2938 | S.Diag(AL.getLoc(), |
| 2939 | ExistingAttr->getName() == Str |
| 2940 | ? diag::warn_duplicate_codeseg_attribute |
| 2941 | : diag::err_conflicting_codeseg_attribute); |
| 2942 | return; |
| 2943 | } |
| 2944 | D->dropAttr<CodeSegAttr>(); |
| 2945 | } |
| 2946 | if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str, |
| 2947 | AL.getAttributeSpellingListIndex())) |
| 2948 | D->addAttr(CSA); |
| 2949 | } |
| 2950 | |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 2951 | // Check for things we'd like to warn about. Multiversioning issues are |
| 2952 | // handled later in the process, once we know how many exist. |
| 2953 | bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 2954 | enum FirstParam { Unsupported, Duplicate }; |
| 2955 | enum SecondParam { None, Architecture }; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2956 | for (auto Str : {"tune=", "fpmath="}) |
| 2957 | if (AttrStr.find(Str) != StringRef::npos) |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 2958 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 2959 | << Unsupported << None << Str; |
| 2960 | |
| 2961 | TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr); |
| 2962 | |
| 2963 | if (!ParsedAttrs.Architecture.empty() && |
| 2964 | !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture)) |
| 2965 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 2966 | << Unsupported << Architecture << ParsedAttrs.Architecture; |
| 2967 | |
| 2968 | if (ParsedAttrs.DuplicateArchitecture) |
| 2969 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 2970 | << Duplicate << None << "arch="; |
| 2971 | |
| 2972 | for (const auto &Feature : ParsedAttrs.Features) { |
| 2973 | auto CurFeature = StringRef(Feature).drop_front(); // remove + or -. |
| 2974 | if (!Context.getTargetInfo().isValidFeatureName(CurFeature)) |
| 2975 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 2976 | << Unsupported << None << CurFeature; |
| 2977 | } |
| 2978 | |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 2979 | return false; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2982 | static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2983 | StringRef Str; |
| 2984 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2985 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) || |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 2986 | S.checkTargetAttr(LiteralLoc, Str)) |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2987 | return; |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 2988 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2989 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2990 | TargetAttr *NewAttr = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2991 | ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2992 | D->addAttr(NewAttr); |
| 2993 | } |
| 2994 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2995 | static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 2996 | Expr *E = AL.getArgAsExpr(0); |
| 2997 | uint32_t VecWidth; |
| 2998 | if (!checkUInt32Argument(S, AL, E, VecWidth)) { |
| 2999 | AL.setInvalid(); |
| 3000 | return; |
| 3001 | } |
| 3002 | |
| 3003 | MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>(); |
| 3004 | if (Existing && Existing->getVectorWidth() != VecWidth) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3005 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3006 | return; |
| 3007 | } |
| 3008 | |
| 3009 | D->addAttr(::new (S.Context) |
| 3010 | MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth, |
| 3011 | AL.getAttributeSpellingListIndex())); |
| 3012 | } |
| 3013 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3014 | static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3015 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3016 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3017 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3018 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3019 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3020 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 3021 | // will warn the user. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3022 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3023 | if (DRE->hasQualifier()) |
| 3024 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3025 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3026 | NI = DRE->getNameInfo(); |
| 3027 | if (!FD) { |
| 3028 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 3029 | << NI.getName(); |
| 3030 | return; |
| 3031 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3032 | } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3033 | if (ULE->hasExplicitTemplateArgs()) |
| 3034 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3035 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 3036 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 3037 | if (!FD) { |
| 3038 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 3039 | << NI.getName(); |
| 3040 | if (ULE->getType() == S.Context.OverloadTy) |
| 3041 | S.NoteAllOverloadCandidates(ULE); |
| 3042 | return; |
| 3043 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3044 | } else { |
| 3045 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3046 | return; |
| 3047 | } |
| 3048 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3049 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3050 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3051 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3052 | return; |
| 3053 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3054 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3055 | // We're currently more strict than GCC about what function types we accept. |
| 3056 | // If this ever proves to be a problem it should be easy to fix. |
Aaron Ballman | 3b70e75 | 2017-12-01 16:53:49 +0000 | [diff] [blame] | 3057 | QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType()); |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3058 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3059 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3060 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3061 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 3062 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3063 | return; |
| 3064 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3065 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3066 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3067 | CleanupAttr(AL.getRange(), S.Context, FD, |
| 3068 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3071 | static void handleEnumExtensibilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3072 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3073 | if (!AL.isArgIdent(0)) { |
| 3074 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3075 | << AL << 0 << AANT_ArgumentIdentifier; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3076 | return; |
| 3077 | } |
| 3078 | |
| 3079 | EnumExtensibilityAttr::Kind ExtensibilityKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3080 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3081 | if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(), |
| 3082 | ExtensibilityKind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3083 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3084 | return; |
| 3085 | } |
| 3086 | |
| 3087 | D->addAttr(::new (S.Context) EnumExtensibilityAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3088 | AL.getRange(), S.Context, ExtensibilityKind, |
| 3089 | AL.getAttributeSpellingListIndex())); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3092 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3093 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3094 | static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3095 | Expr *IdxExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3096 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3097 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3098 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3099 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3100 | // Make sure the format string is really a string. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3101 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3102 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3103 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 3104 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3105 | !isCFStringType(Ty, S.Context) && |
| 3106 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3107 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3108 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3109 | << "a string type" << IdxExpr->getSourceRange() |
| 3110 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3111 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3112 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3113 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3114 | if (!isNSStringType(Ty, S.Context) && |
| 3115 | !isCFStringType(Ty, S.Context) && |
| 3116 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3117 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3118 | S.Diag(AL.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3119 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 3120 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3121 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3124 | D->addAttr(::new (S.Context) FormatArgAttr( |
| 3125 | AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3128 | enum FormatAttrKind { |
| 3129 | CFStringFormat, |
| 3130 | NSStringFormat, |
| 3131 | StrftimeFormat, |
| 3132 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3133 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3134 | InvalidFormat |
| 3135 | }; |
| 3136 | |
| 3137 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3138 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3139 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3140 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3141 | // Check for formats that get handled specially. |
| 3142 | .Case("NSString", NSStringFormat) |
| 3143 | .Case("CFString", CFStringFormat) |
| 3144 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3145 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3146 | // Otherwise, check for supported formats. |
| 3147 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3148 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3149 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 3150 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 3151 | .Case("os_trace", SupportedFormat) |
| 3152 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3153 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3154 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3155 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3158 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3159 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3160 | static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3161 | if (!S.getLangOpts().CPlusPlus) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3162 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3163 | return; |
| 3164 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3165 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3166 | if (S.getCurFunctionOrMethodDecl()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3167 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3168 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3169 | return; |
| 3170 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3171 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3172 | if (S.Context.getAsArrayType(T)) |
| 3173 | T = S.Context.getBaseElementType(T); |
| 3174 | if (!T->getAs<RecordType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3175 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3176 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3177 | return; |
| 3178 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3179 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3180 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3181 | uint32_t prioritynum; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3182 | if (!checkUInt32Argument(S, AL, E, prioritynum)) { |
| 3183 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3184 | return; |
| 3185 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3186 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3187 | if (prioritynum < 101 || prioritynum > 65535) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3188 | S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3189 | << E->getSourceRange() << AL << 101 << 65535; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3190 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3191 | return; |
| 3192 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3193 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3194 | InitPriorityAttr(AL.getRange(), S.Context, prioritynum, |
| 3195 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3198 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3199 | IdentifierInfo *Format, int FormatIdx, |
| 3200 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3201 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3202 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3203 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3204 | if (F->getType() == Format && |
| 3205 | F->getFormatIdx() == FormatIdx && |
| 3206 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3207 | // If we don't have a valid location for this attribute, adopt the |
| 3208 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3209 | if (F->getLocation().isInvalid()) |
| 3210 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3211 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3212 | } |
| 3213 | } |
| 3214 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3215 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3216 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3219 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3220 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3221 | static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3222 | if (!AL.isArgIdent(0)) { |
| 3223 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3224 | << AL << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3225 | return; |
| 3226 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3227 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3228 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3229 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3230 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3231 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3232 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3233 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3234 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3235 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3236 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3237 | // If we've modified the string name, we need a new identifier for it. |
| 3238 | II = &S.Context.Idents.get(Format); |
| 3239 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3240 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3241 | // Check for supported formats. |
| 3242 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3243 | |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3244 | if (Kind == IgnoredFormat) |
| 3245 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3246 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3247 | if (Kind == InvalidFormat) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3248 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3249 | << AL << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3250 | return; |
| 3251 | } |
| 3252 | |
| 3253 | // checks for the 2nd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3254 | Expr *IdxExpr = AL.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3255 | uint32_t Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3256 | if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3257 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3258 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3259 | if (Idx < 1 || Idx > NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3260 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3261 | << AL << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3262 | return; |
| 3263 | } |
| 3264 | |
| 3265 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3266 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3267 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3268 | if (HasImplicitThisParam) { |
| 3269 | if (ArgIdx == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3270 | S.Diag(AL.getLoc(), |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3271 | diag::err_format_attribute_implicit_this_format_string) |
| 3272 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3273 | return; |
| 3274 | } |
| 3275 | ArgIdx--; |
| 3276 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3278 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3279 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3280 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3281 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3282 | if (!isCFStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3283 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3284 | << "a CFString" << IdxExpr->getSourceRange() |
| 3285 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3286 | return; |
| 3287 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3288 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3289 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3290 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3291 | if (!isNSStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3292 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3293 | << "an NSString" << IdxExpr->getSourceRange() |
| 3294 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3295 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3296 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3297 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3298 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3299 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3300 | << "a string type" << IdxExpr->getSourceRange() |
| 3301 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3302 | return; |
| 3303 | } |
| 3304 | |
| 3305 | // check the 3rd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3306 | Expr *FirstArgExpr = AL.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3307 | uint32_t FirstArg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3308 | if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3309 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3310 | |
| 3311 | // check if the function is variadic if the 3rd argument non-zero |
| 3312 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3313 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3314 | ++NumArgs; // +1 for ... |
| 3315 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3316 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3317 | return; |
| 3318 | } |
| 3319 | } |
| 3320 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3321 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3322 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3323 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3324 | if (FirstArg != 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3325 | S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3326 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3327 | return; |
| 3328 | } |
| 3329 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3330 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3331 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3332 | << AL << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3333 | return; |
| 3334 | } |
| 3335 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3336 | FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3337 | Idx, FirstArg, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3338 | AL.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3339 | if (NewAttr) |
| 3340 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3343 | static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3344 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3345 | RecordDecl *RD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3346 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3347 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3348 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3349 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3350 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3351 | |
| 3352 | if (!RD || !RD->isUnion()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3353 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL |
| 3354 | << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3355 | return; |
| 3356 | } |
| 3357 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3358 | if (!RD->isCompleteDefinition()) { |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3359 | if (!RD->isBeingDefined()) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3360 | S.Diag(AL.getLoc(), |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3361 | diag::warn_transparent_union_attribute_not_definition); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3362 | return; |
| 3363 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3364 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3365 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3366 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3367 | if (Field == FieldEnd) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3368 | S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3369 | return; |
| 3370 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3371 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3372 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3373 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3374 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3375 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3376 | diag::warn_transparent_union_attribute_floating) |
| 3377 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3378 | return; |
| 3379 | } |
| 3380 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3381 | if (FirstType->isIncompleteType()) |
| 3382 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3383 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3384 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3385 | for (; Field != FieldEnd; ++Field) { |
| 3386 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3387 | if (FieldType->isIncompleteType()) |
| 3388 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3389 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3390 | // members of the union would all have the same calling convention as the |
| 3391 | // first member of the union. Checking just the size and alignment isn't |
| 3392 | // sufficient (consider structs passed on the stack instead of in registers |
| 3393 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3394 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3395 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3396 | // Warn if we drop the attribute. |
| 3397 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3398 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3399 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3400 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3401 | diag::warn_transparent_union_attribute_field_size_align) |
| 3402 | << isSize << Field->getDeclName() << FieldBits; |
| 3403 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3404 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3405 | diag::note_transparent_union_first_field_size_align) |
| 3406 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3407 | return; |
| 3408 | } |
| 3409 | } |
| 3410 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3411 | RD->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3412 | TransparentUnionAttr(AL.getRange(), S.Context, |
| 3413 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3416 | static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3417 | // Make sure that there is a string literal as the annotation's single |
| 3418 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3419 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3420 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3421 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3422 | |
| 3423 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3424 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3425 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3426 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3427 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3428 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3429 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3430 | AnnotateAttr(AL.getRange(), S.Context, Str, |
| 3431 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3434 | static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3435 | S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 3436 | AL.getAttributeSpellingListIndex()); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3440 | unsigned SpellingListIndex) { |
| 3441 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3442 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3443 | |
| 3444 | QualType T; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3445 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3446 | T = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3447 | else if (const auto *VD = dyn_cast<ValueDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3448 | T = VD->getType(); |
| 3449 | else |
| 3450 | llvm_unreachable("Unknown decl type for align_value"); |
| 3451 | |
| 3452 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3453 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3454 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3455 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3456 | return; |
| 3457 | } |
| 3458 | |
| 3459 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3460 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3461 | ExprResult ICE |
| 3462 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3463 | diag::err_align_value_attribute_argument_not_int, |
| 3464 | /*AllowFold*/ false); |
| 3465 | if (ICE.isInvalid()) |
| 3466 | return; |
| 3467 | |
| 3468 | if (!Alignment.isPowerOf2()) { |
| 3469 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3470 | << E->getSourceRange(); |
| 3471 | return; |
| 3472 | } |
| 3473 | |
| 3474 | D->addAttr(::new (Context) |
| 3475 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3476 | SpellingListIndex)); |
| 3477 | return; |
| 3478 | } |
| 3479 | |
| 3480 | // Save dependent expressions in the AST to be instantiated. |
| 3481 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3484 | static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3485 | // check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3486 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3487 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3488 | return; |
| 3489 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3490 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3491 | if (AL.getNumArgs() == 0) { |
| 3492 | D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context, |
| 3493 | true, nullptr, AL.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3494 | return; |
| 3495 | } |
| 3496 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3497 | Expr *E = AL.getArgAsExpr(0); |
| 3498 | if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3499 | S.Diag(AL.getEllipsisLoc(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3500 | diag::err_pack_expansion_without_parameter_packs); |
| 3501 | return; |
| 3502 | } |
| 3503 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3504 | if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3505 | return; |
| 3506 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3507 | S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(), |
| 3508 | AL.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3512 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3513 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3514 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3515 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3516 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3517 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3518 | // C++11 [dcl.align]p1: |
| 3519 | // An alignment-specifier may be applied to a variable or to a class |
| 3520 | // data member, but it shall not be applied to a bit-field, a function |
| 3521 | // parameter, the formal parameter of a catch clause, or a variable |
| 3522 | // declared with the register storage class specifier. An |
| 3523 | // alignment-specifier may also be applied to the declaration of a class |
| 3524 | // or enumeration type. |
| 3525 | // C11 6.7.5/2: |
| 3526 | // An alignment attribute shall not be specified in a declaration of |
| 3527 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3528 | // object declared with the register storage-class specifier. |
| 3529 | int DiagKind = -1; |
| 3530 | if (isa<ParmVarDecl>(D)) { |
| 3531 | DiagKind = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3532 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3533 | if (VD->getStorageClass() == SC_Register) |
| 3534 | DiagKind = 1; |
| 3535 | if (VD->isExceptionVariable()) |
| 3536 | DiagKind = 2; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3537 | } else if (const auto *FD = dyn_cast<FieldDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3538 | if (FD->isBitField()) |
| 3539 | DiagKind = 3; |
| 3540 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3541 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3542 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3543 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3544 | return; |
| 3545 | } |
| 3546 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3547 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3548 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3549 | return; |
| 3550 | } |
| 3551 | } |
| 3552 | |
Richard Smith | 90ae967 | 2018-06-20 23:36:55 +0000 | [diff] [blame] | 3553 | if (E->isValueDependent()) { |
| 3554 | // We can't support a dependent alignment on a non-dependent type, |
| 3555 | // because we have no way to model that a type is "alignment-dependent" |
| 3556 | // but not dependent in any other way. |
| 3557 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3558 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3559 | Diag(AttrLoc, diag::err_alignment_dependent_typedef_name) |
| 3560 | << E->getSourceRange(); |
| 3561 | return; |
| 3562 | } |
| 3563 | } |
| 3564 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3565 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3566 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3567 | AA->setPackExpansion(IsPackExpansion); |
| 3568 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3569 | return; |
| 3570 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3571 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3572 | // FIXME: Cache the number on the AL object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3573 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3574 | ExprResult ICE |
| 3575 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3576 | diag::err_aligned_attribute_argument_not_int, |
| 3577 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3578 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3579 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3580 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3581 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3582 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3583 | // C++11 [dcl.align]p2: |
| 3584 | // -- if the constant expression evaluates to zero, the alignment |
| 3585 | // specifier shall have no effect |
| 3586 | // C11 6.7.5p6: |
| 3587 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3588 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3589 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3590 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3591 | << E->getSourceRange(); |
| 3592 | return; |
| 3593 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3594 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3595 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3596 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3597 | unsigned MaxValidAlignment = |
| 3598 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3599 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3600 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3601 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3602 | << E->getSourceRange(); |
| 3603 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3604 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3605 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3606 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3607 | unsigned MaxTLSAlign = |
| 3608 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3609 | .getQuantity(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3610 | const auto *VD = dyn_cast<VarDecl>(D); |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3611 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3612 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3613 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3614 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3615 | return; |
| 3616 | } |
| 3617 | } |
| 3618 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3619 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3620 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3621 | AA->setPackExpansion(IsPackExpansion); |
| 3622 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3625 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3626 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3627 | // FIXME: Cache the number on the AL object if non-dependent? |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3628 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3629 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3630 | SpellingListIndex); |
| 3631 | AA->setPackExpansion(IsPackExpansion); |
| 3632 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3633 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3634 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3635 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3636 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3637 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3638 | QualType UnderlyingTy, DiagTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3639 | if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3640 | UnderlyingTy = DiagTy = VD->getType(); |
| 3641 | } else { |
| 3642 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3643 | if (const auto *ED = dyn_cast<EnumDecl>(D)) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3644 | UnderlyingTy = ED->getIntegerType(); |
| 3645 | } |
| 3646 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3647 | return; |
| 3648 | |
| 3649 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3650 | // The combined effect of all alignment attributes in a declaration shall |
| 3651 | // not specify an alignment that is less strict than the alignment that |
| 3652 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3653 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3654 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3655 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3656 | if (I->isAlignmentDependent()) |
| 3657 | return; |
| 3658 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3659 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3660 | Align = std::max(Align, I->getAlignment(Context)); |
| 3661 | } |
| 3662 | |
| 3663 | if (AlignasAttr && Align) { |
| 3664 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3665 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3666 | if (NaturalAlign > RequestedAlign) |
| 3667 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3668 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3672 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3673 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3674 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3675 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3676 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3677 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3678 | // have to wait until the record is defined to catch any mismatches. |
| 3679 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3680 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3681 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3682 | // The unspecified model never matches what a definition could need. |
| 3683 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3684 | return false; |
| 3685 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3686 | if (BestCase) { |
| 3687 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3688 | return false; |
| 3689 | } else { |
| 3690 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3691 | return false; |
| 3692 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3693 | |
| 3694 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3695 | << 0 /*definition*/; |
| 3696 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3697 | << RD->getNameAsString(); |
| 3698 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3701 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3702 | /// attribute. |
| 3703 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3704 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3705 | IntegerMode = true; |
| 3706 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3707 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3708 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3709 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3710 | case 'Q': |
| 3711 | DestWidth = 8; |
| 3712 | break; |
| 3713 | case 'H': |
| 3714 | DestWidth = 16; |
| 3715 | break; |
| 3716 | case 'S': |
| 3717 | DestWidth = 32; |
| 3718 | break; |
| 3719 | case 'D': |
| 3720 | DestWidth = 64; |
| 3721 | break; |
| 3722 | case 'X': |
| 3723 | DestWidth = 96; |
| 3724 | break; |
| 3725 | case 'T': |
| 3726 | DestWidth = 128; |
| 3727 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3728 | } |
| 3729 | if (Str[1] == 'F') { |
| 3730 | IntegerMode = false; |
| 3731 | } else if (Str[1] == 'C') { |
| 3732 | IntegerMode = false; |
| 3733 | ComplexMode = true; |
| 3734 | } else if (Str[1] != 'I') { |
| 3735 | DestWidth = 0; |
| 3736 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3737 | break; |
| 3738 | case 4: |
| 3739 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3740 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3741 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3742 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3743 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3744 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3745 | break; |
| 3746 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3747 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3748 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3749 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3750 | case 11: |
| 3751 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3752 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3753 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3754 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3758 | /// type. |
| 3759 | /// |
| 3760 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3761 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3762 | /// HImode, not an intermediate pointer. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3763 | static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3764 | // This attribute isn't documented, but glibc uses it. It changes |
| 3765 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3766 | if (!AL.isArgIdent(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3767 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 3768 | << AL << AANT_ArgumentIdentifier; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3769 | return; |
| 3770 | } |
| 3771 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3772 | IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3773 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3774 | S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex()); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 3778 | unsigned SpellingListIndex, bool InInstantiation) { |
| 3779 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3780 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3781 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3782 | |
| 3783 | unsigned DestWidth = 0; |
| 3784 | bool IntegerMode = true; |
| 3785 | bool ComplexMode = false; |
| 3786 | llvm::APInt VectorSize(64, 0); |
| 3787 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3788 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3789 | size_t StrSize = Str.size(); |
| 3790 | size_t VectorStringLength = 0; |
| 3791 | while ((VectorStringLength + 1) < StrSize && |
| 3792 | isdigit(Str[VectorStringLength + 1])) |
| 3793 | ++VectorStringLength; |
| 3794 | if (VectorStringLength && |
| 3795 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3796 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3797 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3798 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3799 | // Avoid duplicate warning from template instantiation. |
| 3800 | if (!InInstantiation) |
| 3801 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3802 | } else { |
| 3803 | VectorSize = 0; |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3808 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 3809 | |
| 3810 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3811 | // and friends, at least with glibc. |
| 3812 | // FIXME: Make sure floating-point mappings are accurate |
| 3813 | // FIXME: Support XF and TF types |
| 3814 | if (!DestWidth) { |
| 3815 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 3816 | return; |
| 3817 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3818 | |
| 3819 | QualType OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3820 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3821 | OldTy = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3822 | else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3823 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 3824 | // Try to get type from enum declaration, default to int. |
| 3825 | OldTy = ED->getIntegerType(); |
| 3826 | if (OldTy.isNull()) |
| 3827 | OldTy = Context.IntTy; |
| 3828 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3829 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3830 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3831 | if (OldTy->isDependentType()) { |
| 3832 | D->addAttr(::new (Context) |
| 3833 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 3834 | return; |
| 3835 | } |
| 3836 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3837 | // Base type can also be a vector type (see PR17453). |
| 3838 | // Distinguish between base type and base element type. |
| 3839 | QualType OldElemTy = OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3840 | if (const auto *VT = OldTy->getAs<VectorType>()) |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3841 | OldElemTy = VT->getElementType(); |
| 3842 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3843 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 3844 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 3845 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 3846 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 3847 | VectorSize.getBoolValue()) { |
| 3848 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 3849 | return; |
| 3850 | } |
| 3851 | bool IntegralOrAnyEnumType = |
| 3852 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 3853 | |
| 3854 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 3855 | !IntegralOrAnyEnumType) |
| 3856 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3857 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3858 | if (!IntegralOrAnyEnumType) |
| 3859 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3860 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3861 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3862 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3863 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3864 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3865 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3868 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3869 | |
| 3870 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3871 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 3872 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3873 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3874 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3875 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3876 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3877 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3878 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3881 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3882 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3886 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3887 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3888 | VectorType::GenericVector); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3889 | } else if (const auto *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3890 | // Complex machine mode does not support base vector types. |
| 3891 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3892 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3893 | return; |
| 3894 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3895 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3896 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3897 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3898 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3899 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3903 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3904 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | // Install the new type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3908 | if (auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3909 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3910 | else if (auto *ED = dyn_cast<EnumDecl>(D)) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3911 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3912 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3913 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3914 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3915 | D->addAttr(::new (Context) |
| 3916 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3917 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3918 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3919 | static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3920 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3921 | NoDebugAttr(AL.getRange(), S.Context, |
| 3922 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3925 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3926 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3927 | unsigned AttrSpellingListIndex) { |
| 3928 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3929 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3930 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3931 | return nullptr; |
| 3932 | } |
| 3933 | |
| 3934 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3935 | return nullptr; |
| 3936 | |
| 3937 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3938 | AttrSpellingListIndex); |
| 3939 | } |
| 3940 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3941 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) { |
| 3942 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3943 | return nullptr; |
| 3944 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3945 | return ::new (Context) |
| 3946 | CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3949 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) { |
| 3950 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
| 3951 | return nullptr; |
| 3952 | |
| 3953 | return ::new (Context) |
| 3954 | CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
| 3955 | } |
| 3956 | |
| 3957 | InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D, |
| 3958 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3959 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3960 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 3961 | // ImplicitParm or VarTemplateSpecialization). |
| 3962 | if (VD->getKind() != Decl::Var) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3963 | Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3964 | << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 3965 | : ExpectedVariableOrFunction); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3966 | return nullptr; |
| 3967 | } |
| 3968 | // Attribute does not apply to non-static local variables. |
| 3969 | if (VD->hasLocalStorage()) { |
| 3970 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 3971 | return nullptr; |
| 3972 | } |
| 3973 | } |
| 3974 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3975 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
| 3976 | return nullptr; |
| 3977 | |
| 3978 | return ::new (Context) InternalLinkageAttr( |
| 3979 | AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
| 3980 | } |
| 3981 | InternalLinkageAttr * |
| 3982 | Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) { |
| 3983 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 3984 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 3985 | // ImplicitParm or VarTemplateSpecialization). |
| 3986 | if (VD->getKind() != Decl::Var) { |
| 3987 | Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type) |
| 3988 | << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 3989 | : ExpectedVariableOrFunction); |
| 3990 | return nullptr; |
| 3991 | } |
| 3992 | // Attribute does not apply to non-static local variables. |
| 3993 | if (VD->hasLocalStorage()) { |
| 3994 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 3995 | return nullptr; |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4000 | return nullptr; |
| 4001 | |
| 4002 | return ::new (Context) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4003 | InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4006 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 4007 | unsigned AttrSpellingListIndex) { |
| 4008 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 4009 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 4010 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4011 | return nullptr; |
| 4012 | } |
| 4013 | |
| 4014 | if (D->hasAttr<MinSizeAttr>()) |
| 4015 | return nullptr; |
| 4016 | |
| 4017 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 4018 | } |
| 4019 | |
| 4020 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 4021 | unsigned AttrSpellingListIndex) { |
| 4022 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 4023 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 4024 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4025 | D->dropAttr<AlwaysInlineAttr>(); |
| 4026 | } |
| 4027 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 4028 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 4029 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4030 | D->dropAttr<MinSizeAttr>(); |
| 4031 | } |
| 4032 | |
| 4033 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 4034 | return nullptr; |
| 4035 | |
| 4036 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 4037 | AttrSpellingListIndex); |
| 4038 | } |
| 4039 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4040 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4041 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL)) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 4042 | return; |
| 4043 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4044 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4045 | D, AL.getRange(), AL.getName(), |
| 4046 | AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4047 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4050 | static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4051 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4052 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4053 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4056 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4057 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4058 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4059 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4062 | static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4063 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL)) |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4064 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4065 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4066 | if (!VD->hasGlobalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4067 | S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4068 | return; |
| 4069 | } |
| 4070 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4071 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4074 | static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4075 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL)) |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4076 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4077 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 4078 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 4079 | // "int x[]"). |
Jonas Hahnfeld | ee47d8c | 2018-02-14 16:04:03 +0000 | [diff] [blame] | 4080 | if (!S.getLangOpts().CUDARelocatableDeviceCode && VD->hasExternalStorage() && |
| 4081 | !isa<IncompleteArrayType>(VD->getType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4082 | S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4083 | return; |
| 4084 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4085 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4086 | S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared) |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4087 | << S.CurrentCUDATarget()) |
| 4088 | return; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4089 | D->addAttr(::new (S.Context) CUDASharedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4090 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4093 | static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4094 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) || |
| 4095 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 4096 | return; |
| 4097 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4098 | const auto *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4099 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4100 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 4101 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4102 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4103 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 4104 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4105 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4106 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4107 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 4108 | if (Method->isInstance()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4109 | S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method) |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4110 | << Method; |
| 4111 | return; |
| 4112 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4113 | S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method; |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4114 | } |
| 4115 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 4116 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4117 | S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4118 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4119 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4120 | CUDAGlobalAttr(AL.getRange(), S.Context, |
| 4121 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4124 | static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4125 | const auto *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 4126 | if (!Fn->isInlineSpecified()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4127 | S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 4128 | return; |
| 4129 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4130 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4131 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4132 | GNUInlineAttr(AL.getRange(), S.Context, |
| 4133 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4136 | static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4137 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4138 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4139 | // Diagnostic is emitted elsewhere: here we store the (valid) AL |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4140 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 4141 | CallingConv CC; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4142 | if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4143 | return; |
| 4144 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4145 | if (!isa<ObjCMethodDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4146 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4147 | << AL << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4148 | return; |
| 4149 | } |
| 4150 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4151 | switch (AL.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4152 | case ParsedAttr::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4153 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4154 | FastCallAttr(AL.getRange(), S.Context, |
| 4155 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4156 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4157 | case ParsedAttr::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4158 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4159 | StdCallAttr(AL.getRange(), S.Context, |
| 4160 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4161 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4162 | case ParsedAttr::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4163 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4164 | ThisCallAttr(AL.getRange(), S.Context, |
| 4165 | AL.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 4166 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4167 | case ParsedAttr::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4168 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4169 | CDeclAttr(AL.getRange(), S.Context, |
| 4170 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4171 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4172 | case ParsedAttr::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4173 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4174 | PascalAttr(AL.getRange(), S.Context, |
| 4175 | AL.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4176 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4177 | case ParsedAttr::AT_SwiftCall: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4178 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4179 | SwiftCallAttr(AL.getRange(), S.Context, |
| 4180 | AL.getAttributeSpellingListIndex())); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4181 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4182 | case ParsedAttr::AT_VectorCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4183 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4184 | VectorCallAttr(AL.getRange(), S.Context, |
| 4185 | AL.getAttributeSpellingListIndex())); |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4186 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4187 | case ParsedAttr::AT_MSABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4188 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4189 | MSABIAttr(AL.getRange(), S.Context, |
| 4190 | AL.getAttributeSpellingListIndex())); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4191 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4192 | case ParsedAttr::AT_SysVABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4193 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4194 | SysVABIAttr(AL.getRange(), S.Context, |
| 4195 | AL.getAttributeSpellingListIndex())); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4196 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4197 | case ParsedAttr::AT_RegCall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4198 | D->addAttr(::new (S.Context) RegCallAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4199 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4200 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4201 | case ParsedAttr::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4202 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4203 | switch (CC) { |
| 4204 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4205 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4206 | break; |
| 4207 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4208 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4209 | break; |
| 4210 | default: |
| 4211 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4214 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4215 | PcsAttr(AL.getRange(), S.Context, PCS, |
| 4216 | AL.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4217 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4218 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4219 | case ParsedAttr::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4220 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4221 | IntelOclBiccAttr(AL.getRange(), S.Context, |
| 4222 | AL.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4223 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4224 | case ParsedAttr::AT_PreserveMost: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4225 | D->addAttr(::new (S.Context) PreserveMostAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4226 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4227 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4228 | case ParsedAttr::AT_PreserveAll: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4229 | D->addAttr(::new (S.Context) PreserveAllAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4230 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4231 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4232 | default: |
| 4233 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4234 | } |
| 4235 | } |
| 4236 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4237 | static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4238 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4239 | return; |
| 4240 | |
| 4241 | std::vector<StringRef> DiagnosticIdentifiers; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4242 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4243 | StringRef RuleName; |
| 4244 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4245 | if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4246 | return; |
| 4247 | |
| 4248 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 4249 | // clang-tidy knows about available rules. |
| 4250 | DiagnosticIdentifiers.push_back(RuleName); |
| 4251 | } |
| 4252 | D->addAttr(::new (S.Context) SuppressAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4253 | AL.getRange(), S.Context, DiagnosticIdentifiers.data(), |
| 4254 | DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex())); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4257 | bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC, |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4258 | const FunctionDecl *FD) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4259 | if (Attrs.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4260 | return true; |
| 4261 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4262 | if (Attrs.hasProcessingCache()) { |
| 4263 | CC = (CallingConv) Attrs.getProcessingCache(); |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4264 | return false; |
| 4265 | } |
| 4266 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4267 | unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4268 | if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) { |
| 4269 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4270 | return true; |
| 4271 | } |
| 4272 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4273 | // TODO: diagnose uses of these conventions on the wrong target. |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4274 | switch (Attrs.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4275 | case ParsedAttr::AT_CDecl: |
| 4276 | CC = CC_C; |
| 4277 | break; |
| 4278 | case ParsedAttr::AT_FastCall: |
| 4279 | CC = CC_X86FastCall; |
| 4280 | break; |
| 4281 | case ParsedAttr::AT_StdCall: |
| 4282 | CC = CC_X86StdCall; |
| 4283 | break; |
| 4284 | case ParsedAttr::AT_ThisCall: |
| 4285 | CC = CC_X86ThisCall; |
| 4286 | break; |
| 4287 | case ParsedAttr::AT_Pascal: |
| 4288 | CC = CC_X86Pascal; |
| 4289 | break; |
| 4290 | case ParsedAttr::AT_SwiftCall: |
| 4291 | CC = CC_Swift; |
| 4292 | break; |
| 4293 | case ParsedAttr::AT_VectorCall: |
| 4294 | CC = CC_X86VectorCall; |
| 4295 | break; |
| 4296 | case ParsedAttr::AT_RegCall: |
| 4297 | CC = CC_X86RegCall; |
| 4298 | break; |
| 4299 | case ParsedAttr::AT_MSABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4300 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 4301 | CC_Win64; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4302 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4303 | case ParsedAttr::AT_SysVABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4304 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4305 | CC_C; |
| 4306 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4307 | case ParsedAttr::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 4308 | StringRef StrRef; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4309 | if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) { |
| 4310 | Attrs.setInvalid(); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4311 | return true; |
| 4312 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4313 | if (StrRef == "aapcs") { |
| 4314 | CC = CC_AAPCS; |
| 4315 | break; |
| 4316 | } else if (StrRef == "aapcs-vfp") { |
| 4317 | CC = CC_AAPCS_VFP; |
| 4318 | break; |
| 4319 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4320 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4321 | Attrs.setInvalid(); |
| 4322 | Diag(Attrs.getLoc(), diag::err_invalid_pcs); |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4323 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4324 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4325 | case ParsedAttr::AT_IntelOclBicc: |
| 4326 | CC = CC_IntelOclBicc; |
| 4327 | break; |
| 4328 | case ParsedAttr::AT_PreserveMost: |
| 4329 | CC = CC_PreserveMost; |
| 4330 | break; |
| 4331 | case ParsedAttr::AT_PreserveAll: |
| 4332 | CC = CC_PreserveAll; |
| 4333 | break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4334 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4337 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4338 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4339 | if (A != TargetInfo::CCCR_OK) { |
| 4340 | if (A == TargetInfo::CCCR_Warning) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4341 | Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4342 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4343 | // This convention is not valid for the target. Use the default function or |
| 4344 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 4345 | bool IsCXXMethod = false, IsVariadic = false; |
| 4346 | if (FD) { |
| 4347 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4348 | IsVariadic = FD->isVariadic(); |
| 4349 | } |
| 4350 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4353 | Attrs.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4354 | return false; |
| 4355 | } |
| 4356 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4357 | /// Pointer-like types in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4358 | static bool isValidSwiftContextType(QualType Ty) { |
| 4359 | if (!Ty->hasPointerRepresentation()) |
| 4360 | return Ty->isDependentType(); |
| 4361 | return Ty->getPointeeType().getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | /// Pointers and references in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4365 | static bool isValidSwiftIndirectResultType(QualType Ty) { |
| 4366 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4367 | Ty = PtrType->getPointeeType(); |
| 4368 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4369 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4370 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4371 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4372 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4373 | return Ty.getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | /// Pointers and references to pointers in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4377 | static bool isValidSwiftErrorResultType(QualType Ty) { |
| 4378 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4379 | Ty = PtrType->getPointeeType(); |
| 4380 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4381 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4382 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4383 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4384 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4385 | if (!Ty.getQualifiers().empty()) |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4386 | return false; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4387 | return isValidSwiftContextType(Ty); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4390 | static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs, |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4391 | ParameterABI Abi) { |
| 4392 | S.AddParameterABIAttr(Attrs.getRange(), D, Abi, |
| 4393 | Attrs.getAttributeSpellingListIndex()); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 4397 | unsigned spellingIndex) { |
| 4398 | |
| 4399 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4400 | |
| 4401 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4402 | if (existingAttr->getABI() != abi) { |
| 4403 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4404 | << getParameterABISpelling(abi) << existingAttr; |
| 4405 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4406 | return; |
| 4407 | } |
| 4408 | } |
| 4409 | |
| 4410 | switch (abi) { |
| 4411 | case ParameterABI::Ordinary: |
| 4412 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4413 | |
| 4414 | case ParameterABI::SwiftContext: |
| 4415 | if (!isValidSwiftContextType(type)) { |
| 4416 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4417 | << getParameterABISpelling(abi) |
| 4418 | << /*pointer to pointer */ 0 << type; |
| 4419 | } |
| 4420 | D->addAttr(::new (Context) |
| 4421 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4422 | return; |
| 4423 | |
| 4424 | case ParameterABI::SwiftErrorResult: |
| 4425 | if (!isValidSwiftErrorResultType(type)) { |
| 4426 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4427 | << getParameterABISpelling(abi) |
| 4428 | << /*pointer to pointer */ 1 << type; |
| 4429 | } |
| 4430 | D->addAttr(::new (Context) |
| 4431 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4432 | return; |
| 4433 | |
| 4434 | case ParameterABI::SwiftIndirectResult: |
| 4435 | if (!isValidSwiftIndirectResultType(type)) { |
| 4436 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4437 | << getParameterABISpelling(abi) |
| 4438 | << /*pointer*/ 0 << type; |
| 4439 | } |
| 4440 | D->addAttr(::new (Context) |
| 4441 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4442 | return; |
| 4443 | } |
| 4444 | llvm_unreachable("bad parameter ABI attribute"); |
| 4445 | } |
| 4446 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4447 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4448 | /// otherwise setting numParams to the appropriate value. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4449 | bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4450 | if (AL.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4451 | return true; |
| 4452 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4453 | if (!checkAttributeNumArgs(*this, AL, 1)) { |
| 4454 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4455 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4456 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4457 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4458 | uint32_t NP; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4459 | Expr *NumParamsExpr = AL.getArgAsExpr(0); |
| 4460 | if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) { |
| 4461 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4462 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4465 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4466 | Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4467 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4468 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4469 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4472 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4473 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4474 | Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4475 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4476 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4477 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4480 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4483 | // Checks whether an argument of launch_bounds attribute is |
| 4484 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4485 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4486 | // and may output an error. |
| 4487 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4488 | const CUDALaunchBoundsAttr &AL, |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4489 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4490 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4491 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4492 | |
| 4493 | // Accept template arguments for now as they depend on something else. |
| 4494 | // We'll get to check them when they eventually get instantiated. |
| 4495 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4496 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4497 | |
| 4498 | llvm::APSInt I(64); |
| 4499 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4500 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4501 | << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4502 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4503 | } |
| 4504 | // Make sure we can fit it in 32 bits. |
| 4505 | if (!I.isIntN(32)) { |
| 4506 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4507 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4508 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4509 | } |
| 4510 | if (I < 0) |
| 4511 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4512 | << &AL << Idx << E->getSourceRange(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4513 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4514 | // We may need to perform implicit conversion of the argument. |
| 4515 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4516 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4517 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4518 | assert(!ValArg.isInvalid() && |
| 4519 | "Unexpected PerformCopyInitialization() failure."); |
| 4520 | |
| 4521 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
| 4524 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4525 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4526 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4527 | SpellingListIndex); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4528 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4529 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4530 | return; |
| 4531 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4532 | if (MinBlocks) { |
| 4533 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4534 | if (MinBlocks == nullptr) |
| 4535 | return; |
| 4536 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4537 | |
| 4538 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4539 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4540 | } |
| 4541 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4542 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4543 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 4544 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4545 | return; |
| 4546 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4547 | S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 4548 | AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr, |
| 4549 | AL.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4552 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4553 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4554 | if (!AL.isArgIdent(0)) { |
| 4555 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4556 | << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4557 | return; |
| 4558 | } |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4559 | |
| 4560 | ParamIdx ArgumentIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4561 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4562 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4563 | return; |
| 4564 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4565 | ParamIdx TypeTagIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4566 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4567 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4568 | return; |
| 4569 | |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 4570 | bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag"; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4571 | if (IsPointer) { |
| 4572 | // Ensure that buffer has a pointer type. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4573 | unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex(); |
| 4574 | if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) || |
| 4575 | !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType()) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4576 | S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 4579 | D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr( |
| 4580 | AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx, |
| 4581 | TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
| 4584 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4585 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4586 | if (!AL.isArgIdent(0)) { |
| 4587 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4588 | << AL << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4589 | return; |
| 4590 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4591 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4592 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4593 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4594 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4595 | if (!isa<VarDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4596 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4597 | << AL << ExpectedVariable; |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4598 | return; |
| 4599 | } |
| 4600 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4601 | IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4602 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4603 | S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4604 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4605 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4606 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4607 | TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4608 | MatchingCTypeLoc, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4609 | AL.getLayoutCompatible(), |
| 4610 | AL.getMustBeNull(), |
| 4611 | AL.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4614 | static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4615 | ParamIdx ArgCount; |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4616 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4617 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0), |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4618 | ArgCount, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4619 | true /* CanIndexImplicitThis */)) |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4620 | return; |
| 4621 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4622 | // ArgCount isn't a parameter index [0;n), it's a count [1;n] |
| 4623 | D->addAttr(::new (S.Context) XRayLogArgsAttr( |
| 4624 | AL.getRange(), S.Context, ArgCount.getSourceIndex(), |
| 4625 | AL.getAttributeSpellingListIndex())); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4628 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4629 | // Checker-specific attribute handlers. |
| 4630 | //===----------------------------------------------------------------------===// |
| 4631 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4632 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) { |
| 4633 | return QT->isDependentType() || QT->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4636 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) { |
| 4637 | return QT->isDependentType() || QT->isObjCObjectPointerType() || |
| 4638 | S.Context.isObjCNSObjectType(QT); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4639 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4640 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4641 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) { |
| 4642 | return QT->isDependentType() || QT->isPointerType() || |
| 4643 | isValidSubjectOfNSAttribute(S, QT); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4646 | static void handleNSConsumedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4647 | S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(), |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4648 | AL.getKind() == ParsedAttr::AT_NSConsumed, |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4649 | /*template instantiation*/ false); |
| 4650 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4651 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4652 | void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D, |
| 4653 | unsigned SpellingIndex, bool IsNSConsumed, |
| 4654 | bool IsTemplateInstantiation) { |
| 4655 | const auto *Param = cast<ParmVarDecl>(D); |
| 4656 | bool TypeOK; |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4657 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4658 | if (IsNSConsumed) |
| 4659 | TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType()); |
| 4660 | else |
| 4661 | TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4662 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4663 | if (!TypeOK) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4664 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 4665 | // is significant. Allow non-dependent code to contain inappropriate |
| 4666 | // attributes even in ARC, but require template instantiations to be |
| 4667 | // set up correctly. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4668 | Diag(D->getBeginLoc(), (IsTemplateInstantiation && IsNSConsumed && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4669 | getLangOpts().ObjCAutoRefCount |
| 4670 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4671 | : diag::warn_ns_attribute_wrong_parameter_type)) |
| 4672 | << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed") |
| 4673 | << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4674 | return; |
| 4675 | } |
| 4676 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4677 | if (IsNSConsumed) |
| 4678 | D->addAttr(::new (Context) |
| 4679 | NSConsumedAttr(AttrRange, Context, SpellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4680 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4681 | D->addAttr(::new (Context) |
| 4682 | CFConsumedAttr(AttrRange, Context, SpellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4685 | bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) { |
| 4686 | if (isValidSubjectOfNSReturnsRetainedAttribute(QT)) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4687 | return false; |
| 4688 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4689 | Diag(Loc, diag::warn_ns_attribute_wrong_return_type) |
| 4690 | << "'ns_returns_retained'" << 0 << 0; |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4691 | return true; |
| 4692 | } |
| 4693 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4694 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4695 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4696 | QualType ReturnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4697 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4698 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 4699 | ReturnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4700 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4701 | (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4702 | return; // ignore: was handled as a type attribute |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4703 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4704 | ReturnType = PD->getType(); |
| 4705 | else if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 4706 | ReturnType = FD->getReturnType(); |
| 4707 | else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4708 | ReturnType = Param->getType()->getPointeeType(); |
| 4709 | if (ReturnType.isNull()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4710 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4711 | << AL << /*pointer-to-CF*/ 2 << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4712 | return; |
| 4713 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4714 | } else if (AL.isUsedAsTypeAttr()) { |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4715 | return; |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4716 | } else { |
| 4717 | AttributeDeclKind ExpectedDeclKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4718 | switch (AL.getKind()) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4719 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4720 | case ParsedAttr::AT_NSReturnsRetained: |
| 4721 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 4722 | case ParsedAttr::AT_NSReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4723 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4724 | break; |
| 4725 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4726 | case ParsedAttr::AT_CFReturnsRetained: |
| 4727 | case ParsedAttr::AT_CFReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4728 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4729 | break; |
| 4730 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4731 | S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4732 | << AL.getRange() << AL << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4733 | return; |
| 4734 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4735 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4736 | bool TypeOK; |
| 4737 | bool Cf; |
| 4738 | switch (AL.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4739 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4740 | case ParsedAttr::AT_NSReturnsRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4741 | TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType); |
| 4742 | Cf = false; |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4743 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4744 | |
| 4745 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 4746 | case ParsedAttr::AT_NSReturnsNotRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4747 | TypeOK = isValidSubjectOfNSAttribute(S, ReturnType); |
| 4748 | Cf = false; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4749 | break; |
| 4750 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4751 | case ParsedAttr::AT_CFReturnsRetained: |
| 4752 | case ParsedAttr::AT_CFReturnsNotRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4753 | TypeOK = isValidSubjectOfCFAttribute(S, ReturnType); |
| 4754 | Cf = true; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4755 | break; |
| 4756 | } |
| 4757 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4758 | if (!TypeOK) { |
| 4759 | if (AL.isUsedAsTypeAttr()) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4760 | return; |
| 4761 | |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4762 | if (isa<ParmVarDecl>(D)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4763 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4764 | << AL << /*pointer-to-CF*/ 2 << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4765 | } else { |
| 4766 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4767 | enum : unsigned { |
| 4768 | Function, |
| 4769 | Method, |
| 4770 | Property |
| 4771 | } SubjectKind = Function; |
| 4772 | if (isa<ObjCMethodDecl>(D)) |
| 4773 | SubjectKind = Method; |
| 4774 | else if (isa<ObjCPropertyDecl>(D)) |
| 4775 | SubjectKind = Property; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4776 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4777 | << AL << SubjectKind << Cf << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4778 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4779 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4780 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4781 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4782 | switch (AL.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4783 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4784 | llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4785 | case ParsedAttr::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4786 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4787 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4788 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4789 | case ParsedAttr::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4790 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4791 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4792 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4793 | case ParsedAttr::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4794 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4795 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4796 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4797 | case ParsedAttr::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4798 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4799 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4800 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4801 | case ParsedAttr::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4802 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4803 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4804 | return; |
| 4805 | }; |
| 4806 | } |
| 4807 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4808 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4809 | const ParsedAttr &Attrs) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4810 | const int EP_ObjCMethod = 1; |
| 4811 | const int EP_ObjCProperty = 2; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4812 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4813 | SourceLocation loc = Attrs.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4814 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4815 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4816 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4817 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4818 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4819 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4820 | if (!resultType->isReferenceType() && |
| 4821 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4822 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4823 | << SourceRange(loc) << Attrs |
| 4824 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
| 4825 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4826 | |
| 4827 | // Drop the attribute. |
| 4828 | return; |
| 4829 | } |
| 4830 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4831 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4832 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4835 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4836 | const ParsedAttr &Attrs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4837 | const auto *Method = cast<ObjCMethodDecl>(D); |
| 4838 | |
| 4839 | const DeclContext *DC = Method->getDeclContext(); |
| 4840 | if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4841 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4842 | << 0; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4843 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4844 | return; |
| 4845 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4846 | if (Method->getMethodFamily() == OMF_dealloc) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4847 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4848 | << 1; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4849 | return; |
| 4850 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4851 | |
| 4852 | D->addAttr(::new (S.Context) ObjCRequiresSuperAttr( |
| 4853 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4856 | static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4857 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4858 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4859 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4860 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4861 | return; |
| 4862 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4863 | |
| 4864 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4865 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4866 | if (!Parm->Ident->isStr("id")) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4867 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL; |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4868 | return; |
| 4869 | } |
| 4870 | |
| 4871 | // Only allow 'cv void *'. |
| 4872 | QualType T = TD->getUnderlyingType(); |
| 4873 | if (!T->isVoidPointerType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4874 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4875 | return; |
| 4876 | } |
| 4877 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4878 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4879 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4880 | ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident, |
| 4881 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4884 | static void handleObjCBridgeMutableAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4885 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4886 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4887 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4888 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4889 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4890 | return; |
| 4891 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4892 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4893 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4894 | ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident, |
| 4895 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4898 | static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4899 | const ParsedAttr &AL) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4900 | IdentifierInfo *RelatedClass = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4901 | AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4902 | if (!RelatedClass) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4903 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4904 | return; |
| 4905 | } |
| 4906 | IdentifierInfo *ClassMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4907 | AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4908 | IdentifierInfo *InstanceMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4909 | AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4910 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4911 | ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4912 | ClassMethod, InstanceMethod, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4913 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4916 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4917 | const ParsedAttr &AL) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4918 | ObjCInterfaceDecl *IFace; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4919 | if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4920 | IFace = CatDecl->getClassInterface(); |
| 4921 | else |
| 4922 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4923 | |
| 4924 | if (!IFace) |
| 4925 | return; |
| 4926 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4927 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4928 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4929 | ObjCDesignatedInitializerAttr(AL.getRange(), S.Context, |
| 4930 | AL.getAttributeSpellingListIndex())); |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4933 | static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4934 | StringRef MetaDataName; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4935 | if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName)) |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4936 | return; |
| 4937 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4938 | ObjCRuntimeNameAttr(AL.getRange(), S.Context, |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4939 | MetaDataName, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4940 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 4943 | // When a user wants to use objc_boxable with a union or struct |
| 4944 | // but they don't have access to the declaration (legacy/third-party code) |
| 4945 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4946 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4947 | static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4948 | bool notify = false; |
| 4949 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4950 | auto *RD = dyn_cast<RecordDecl>(D); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4951 | if (RD && RD->getDefinition()) { |
| 4952 | RD = RD->getDefinition(); |
| 4953 | notify = true; |
| 4954 | } |
| 4955 | |
| 4956 | if (RD) { |
| 4957 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4958 | ObjCBoxableAttr(AL.getRange(), S.Context, |
| 4959 | AL.getAttributeSpellingListIndex()); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4960 | RD->addAttr(BoxableAttr); |
| 4961 | if (notify) { |
| 4962 | // we need to notify ASTReader/ASTWriter about |
| 4963 | // modification of existing declaration |
| 4964 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 4965 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 4966 | } |
| 4967 | } |
| 4968 | } |
| 4969 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4970 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4971 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4972 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4973 | S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4974 | << AL.getRange() << AL << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4977 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4978 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4979 | const auto *VD = cast<ValueDecl>(D); |
| 4980 | QualType QT = VD->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4981 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4982 | if (!QT->isDependentType() && |
| 4983 | !QT->isObjCLifetimeType()) { |
| 4984 | S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
| 4985 | << QT; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4986 | return; |
| 4987 | } |
| 4988 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4989 | Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4990 | |
| 4991 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4992 | // going to infer. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4993 | if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType()) |
| 4994 | Lifetime = QT->getObjCARCImplicitLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4995 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4996 | switch (Lifetime) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4997 | case Qualifiers::OCL_None: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4998 | assert(QT->isDependentType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4999 | "didn't infer lifetime for non-dependent type?"); |
| 5000 | break; |
| 5001 | |
| 5002 | case Qualifiers::OCL_Weak: // meaningful |
| 5003 | case Qualifiers::OCL_Strong: // meaningful |
| 5004 | break; |
| 5005 | |
| 5006 | case Qualifiers::OCL_ExplicitNone: |
| 5007 | case Qualifiers::OCL_Autoreleasing: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5008 | S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
| 5009 | << (Lifetime == Qualifiers::OCL_Autoreleasing); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5010 | break; |
| 5011 | } |
| 5012 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5013 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5014 | ObjCPreciseLifetimeAttr(AL.getRange(), S.Context, |
| 5015 | AL.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5018 | //===----------------------------------------------------------------------===// |
| 5019 | // Microsoft specific attribute handlers. |
| 5020 | //===----------------------------------------------------------------------===// |
| 5021 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5022 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 5023 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 5024 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 5025 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5026 | return nullptr; |
| 5027 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 5028 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 5029 | D->dropAttr<UuidAttr>(); |
| 5030 | } |
| 5031 | |
| 5032 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 5033 | } |
| 5034 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5035 | static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5036 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5037 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5038 | << AL << AttributeLangSupport::C; |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5039 | return; |
| 5040 | } |
| 5041 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5042 | StringRef StrRef; |
| 5043 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5044 | if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5045 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5046 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5047 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 5048 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5049 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 5050 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5051 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5052 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5053 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5054 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5055 | return; |
| 5056 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 5057 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5058 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5059 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5060 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5061 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5062 | return; |
| 5063 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5064 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5065 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5066 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5067 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5068 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5069 | |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5070 | // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's |
| 5071 | // the only thing in the [] list, the [] too), and add an insertion of |
| 5072 | // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas |
| 5073 | // separating attributes nor of the [ and the ] are in the AST. |
Nico Weber | 0a23404 | 2017-05-05 17:15:08 +0000 | [diff] [blame] | 5074 | // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc" |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5075 | // on cfe-dev. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5076 | if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling. |
| 5077 | S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated); |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5078 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5079 | UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(), |
| 5080 | AL.getAttributeSpellingListIndex(), StrRef); |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5081 | if (UA) |
| 5082 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5085 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5086 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5087 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5088 | << AL << AttributeLangSupport::C; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5089 | return; |
| 5090 | } |
| 5091 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5092 | D, AL.getRange(), /*BestCase=*/true, |
| 5093 | AL.getAttributeSpellingListIndex(), |
| 5094 | (MSInheritanceAttr::Spelling)AL.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5095 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5096 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5097 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 5098 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5101 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5102 | const auto *VD = cast<VarDecl>(D); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5103 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5104 | S.Diag(AL.getLoc(), diag::err_thread_unsupported); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5105 | return; |
| 5106 | } |
| 5107 | if (VD->getTSCSpec() != TSCS_unspecified) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5108 | S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5109 | return; |
| 5110 | } |
| 5111 | if (VD->hasLocalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5112 | S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5113 | return; |
| 5114 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5115 | D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context, |
| 5116 | AL.getAttributeSpellingListIndex())); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5119 | static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5120 | SmallVector<StringRef, 4> Tags; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5121 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5122 | StringRef Tag; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5123 | if (!S.checkStringLiteralArgumentAttr(AL, I, Tag)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5124 | return; |
| 5125 | Tags.push_back(Tag); |
| 5126 | } |
| 5127 | |
| 5128 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 5129 | if (!NS->isInline()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5130 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5131 | return; |
| 5132 | } |
| 5133 | if (NS->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5134 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5135 | return; |
| 5136 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5137 | if (AL.getNumArgs() == 0) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5138 | Tags.push_back(NS->getName()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5139 | } else if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5140 | return; |
| 5141 | |
| 5142 | // Store tags sorted and without duplicates. |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 5143 | llvm::sort(Tags.begin(), Tags.end()); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5144 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 5145 | |
| 5146 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5147 | AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(), |
| 5148 | AL.getAttributeSpellingListIndex())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5151 | static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5152 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5153 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5154 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5155 | return; |
| 5156 | } |
| 5157 | |
| 5158 | StringRef Str; |
| 5159 | SourceLocation ArgLoc; |
| 5160 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5161 | if (AL.getNumArgs() == 0) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5162 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5163 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5164 | return; |
| 5165 | |
| 5166 | ARMInterruptAttr::InterruptType Kind; |
| 5167 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5168 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5169 | << ArgLoc; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5170 | return; |
| 5171 | } |
| 5172 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5173 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5174 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5175 | ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5178 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5179 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5180 | return; |
| 5181 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5182 | if (!AL.isArgExpr(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5183 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 5184 | << AL << AANT_ArgumentIntegerConstant; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5185 | return; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
| 5188 | // FIXME: Check for decl - it should be void ()(void). |
| 5189 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5190 | Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5191 | llvm::APSInt NumParams(32); |
| 5192 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5193 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5194 | << AL << AANT_ArgumentIntegerConstant |
| 5195 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5196 | return; |
| 5197 | } |
| 5198 | |
| 5199 | unsigned Num = NumParams.getLimitedValue(255); |
| 5200 | if ((Num & 1) || Num > 30) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5201 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5202 | << AL << (int)NumParams.getSExtValue() |
| 5203 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5204 | return; |
| 5205 | } |
| 5206 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5207 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5208 | MSP430InterruptAttr(AL.getLoc(), S.Context, Num, |
| 5209 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5210 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5213 | static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5214 | // Only one optional argument permitted. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5215 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5216 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5217 | return; |
| 5218 | } |
| 5219 | |
| 5220 | StringRef Str; |
| 5221 | SourceLocation ArgLoc; |
| 5222 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5223 | if (AL.getNumArgs() == 0) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5224 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5225 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5226 | return; |
| 5227 | |
| 5228 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 5229 | // a) Must be a function. |
| 5230 | // b) Must have no parameters. |
| 5231 | // c) Must have the 'void' return type. |
| 5232 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 5233 | // lacks the 'eret' instruction. |
| 5234 | // e) The attribute itself must either have no argument or one of the |
| 5235 | // valid interrupt types, see [MipsInterruptDocs]. |
| 5236 | |
| 5237 | if (!isFunctionOrMethod(D)) { |
| 5238 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5239 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5240 | return; |
| 5241 | } |
| 5242 | |
| 5243 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5244 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5245 | << 0; |
| 5246 | return; |
| 5247 | } |
| 5248 | |
| 5249 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5250 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5251 | << 1; |
| 5252 | return; |
| 5253 | } |
| 5254 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5255 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5256 | return; |
| 5257 | |
| 5258 | MipsInterruptAttr::InterruptType Kind; |
| 5259 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5260 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5261 | << AL << "'" + std::string(Str) + "'"; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5262 | return; |
| 5263 | } |
| 5264 | |
| 5265 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5266 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5269 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5270 | // Semantic checks for a function with the 'interrupt' attribute. |
| 5271 | // a) Must be a function. |
| 5272 | // b) Must have the 'void' return type. |
| 5273 | // c) Must take 1 or 2 arguments. |
| 5274 | // d) The 1st argument must be a pointer. |
| 5275 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 5276 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5277 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5278 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5279 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5280 | << AL << ExpectedFunctionWithProtoType; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5281 | return; |
| 5282 | } |
| 5283 | // Interrupt handler must have void return type. |
| 5284 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5285 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5286 | diag::err_anyx86_interrupt_attribute) |
| 5287 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5288 | ? 0 |
| 5289 | : 1) |
| 5290 | << 0; |
| 5291 | return; |
| 5292 | } |
| 5293 | // Interrupt handler must have 1 or 2 parameters. |
| 5294 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5295 | if (NumParams < 1 || NumParams > 2) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5296 | S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute) |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5297 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5298 | ? 0 |
| 5299 | : 1) |
| 5300 | << 1; |
| 5301 | return; |
| 5302 | } |
| 5303 | // The first argument must be a pointer. |
| 5304 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5305 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5306 | diag::err_anyx86_interrupt_attribute) |
| 5307 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5308 | ? 0 |
| 5309 | : 1) |
| 5310 | << 2; |
| 5311 | return; |
| 5312 | } |
| 5313 | // The second argument, if present, must be an unsigned integer. |
| 5314 | unsigned TypeSize = |
| 5315 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5316 | ? 64 |
| 5317 | : 32; |
| 5318 | if (NumParams == 2 && |
| 5319 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5320 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5321 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5322 | diag::err_anyx86_interrupt_attribute) |
| 5323 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5324 | ? 0 |
| 5325 | : 1) |
| 5326 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 5327 | return; |
| 5328 | } |
| 5329 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5330 | AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex())); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5331 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5332 | } |
| 5333 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5334 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5335 | if (!isFunctionOrMethod(D)) { |
| 5336 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5337 | << "'interrupt'" << ExpectedFunction; |
| 5338 | return; |
| 5339 | } |
| 5340 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5341 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5342 | return; |
| 5343 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5344 | handleSimpleAttribute<AVRInterruptAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5347 | static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5348 | if (!isFunctionOrMethod(D)) { |
| 5349 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5350 | << "'signal'" << ExpectedFunction; |
| 5351 | return; |
| 5352 | } |
| 5353 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5354 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5355 | return; |
| 5356 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5357 | handleSimpleAttribute<AVRSignalAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5360 | |
| 5361 | static void handleRISCVInterruptAttr(Sema &S, Decl *D, |
| 5362 | const ParsedAttr &AL) { |
| 5363 | // Warn about repeated attributes. |
| 5364 | if (const auto *A = D->getAttr<RISCVInterruptAttr>()) { |
| 5365 | S.Diag(AL.getRange().getBegin(), |
| 5366 | diag::warn_riscv_repeated_interrupt_attribute); |
| 5367 | S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute); |
| 5368 | return; |
| 5369 | } |
| 5370 | |
| 5371 | // Check the attribute argument. Argument is optional. |
| 5372 | if (!checkAttributeAtMostNumArgs(S, AL, 1)) |
| 5373 | return; |
| 5374 | |
| 5375 | StringRef Str; |
| 5376 | SourceLocation ArgLoc; |
| 5377 | |
| 5378 | // 'machine'is the default interrupt mode. |
| 5379 | if (AL.getNumArgs() == 0) |
| 5380 | Str = "machine"; |
| 5381 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5382 | return; |
| 5383 | |
| 5384 | // Semantic checks for a function with the 'interrupt' attribute: |
| 5385 | // - Must be a function. |
| 5386 | // - Must have no parameters. |
| 5387 | // - Must have the 'void' return type. |
| 5388 | // - The attribute itself must either have no argument or one of the |
| 5389 | // valid interrupt types, see [RISCVInterruptDocs]. |
| 5390 | |
| 5391 | if (D->getFunctionType() == nullptr) { |
| 5392 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5393 | << "'interrupt'" << ExpectedFunction; |
| 5394 | return; |
| 5395 | } |
| 5396 | |
| 5397 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5398 | S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0; |
| 5399 | return; |
| 5400 | } |
| 5401 | |
| 5402 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5403 | S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1; |
| 5404 | return; |
| 5405 | } |
| 5406 | |
| 5407 | RISCVInterruptAttr::InterruptType Kind; |
| 5408 | if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5409 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5410 | << ArgLoc; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5411 | return; |
| 5412 | } |
| 5413 | |
| 5414 | D->addAttr(::new (S.Context) RISCVInterruptAttr( |
| 5415 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
| 5416 | } |
| 5417 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5418 | static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5419 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5420 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5421 | case llvm::Triple::msp430: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5422 | handleMSP430InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5423 | break; |
| 5424 | case llvm::Triple::mipsel: |
| 5425 | case llvm::Triple::mips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5426 | handleMipsInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5427 | break; |
| 5428 | case llvm::Triple::x86: |
| 5429 | case llvm::Triple::x86_64: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5430 | handleAnyX86InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5431 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5432 | case llvm::Triple::avr: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5433 | handleAVRInterruptAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5434 | break; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5435 | case llvm::Triple::riscv32: |
| 5436 | case llvm::Triple::riscv64: |
| 5437 | handleRISCVInterruptAttr(S, D, AL); |
| 5438 | break; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5439 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5440 | handleARMInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5441 | break; |
| 5442 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5443 | } |
| 5444 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5445 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5446 | const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5447 | uint32_t Min = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5448 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 5449 | if (!checkUInt32Argument(S, AL, MinExpr, Min)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5450 | return; |
| 5451 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5452 | uint32_t Max = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5453 | Expr *MaxExpr = AL.getArgAsExpr(1); |
| 5454 | if (!checkUInt32Argument(S, AL, MaxExpr, Max)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5455 | return; |
| 5456 | |
| 5457 | if (Min == 0 && Max != 0) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5458 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5459 | return; |
| 5460 | } |
| 5461 | if (Min > Max) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5462 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5463 | return; |
| 5464 | } |
| 5465 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5466 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5467 | AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max, |
| 5468 | AL.getAttributeSpellingListIndex())); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5471 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5472 | uint32_t Min = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5473 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 5474 | if (!checkUInt32Argument(S, AL, MinExpr, Min)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5475 | return; |
| 5476 | |
| 5477 | uint32_t Max = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5478 | if (AL.getNumArgs() == 2) { |
| 5479 | Expr *MaxExpr = AL.getArgAsExpr(1); |
| 5480 | if (!checkUInt32Argument(S, AL, MaxExpr, Max)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5481 | return; |
| 5482 | } |
| 5483 | |
| 5484 | if (Min == 0 && Max != 0) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5485 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5486 | return; |
| 5487 | } |
| 5488 | if (Max != 0 && Min > Max) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5489 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5490 | return; |
| 5491 | } |
| 5492 | |
| 5493 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5494 | AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max, |
| 5495 | AL.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5496 | } |
| 5497 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5498 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5499 | uint32_t NumSGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5500 | Expr *NumSGPRExpr = AL.getArgAsExpr(0); |
| 5501 | if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5502 | return; |
| 5503 | |
| 5504 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5505 | AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR, |
| 5506 | AL.getAttributeSpellingListIndex())); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5509 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5510 | uint32_t NumVGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5511 | Expr *NumVGPRExpr = AL.getArgAsExpr(0); |
| 5512 | if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5513 | return; |
| 5514 | |
| 5515 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5516 | AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR, |
| 5517 | AL.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5520 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5521 | const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5522 | // If we try to apply it to a function pointer, don't warn, but don't |
| 5523 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 5524 | // special about calling a force_align_arg_pointer function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5525 | const auto *VD = dyn_cast<ValueDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5526 | if (VD && VD->getType()->isFunctionPointerType()) |
| 5527 | return; |
| 5528 | // Also don't warn on function pointer typedefs. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5529 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5530 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 5531 | TD->getUnderlyingType()->isFunctionType())) |
| 5532 | return; |
| 5533 | // Attribute can only be applied to function types. |
| 5534 | if (!isa<FunctionDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5535 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5536 | << AL << ExpectedFunction; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5537 | return; |
| 5538 | } |
| 5539 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5540 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5541 | X86ForceAlignArgPointerAttr(AL.getRange(), S.Context, |
| 5542 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5545 | static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5546 | uint32_t Version; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5547 | Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
| 5548 | if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version)) |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5549 | return; |
| 5550 | |
| 5551 | // TODO: Investigate what happens with the next major version of MSVC. |
| 5552 | if (Version != LangOptions::MSVC2015) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5553 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5554 | << AL << Version << VersionExpr->getSourceRange(); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5555 | return; |
| 5556 | } |
| 5557 | |
| 5558 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5559 | LayoutVersionAttr(AL.getRange(), S.Context, Version, |
| 5560 | AL.getAttributeSpellingListIndex())); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5563 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 5564 | unsigned AttrSpellingListIndex) { |
| 5565 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5566 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5567 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5571 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5572 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5573 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5574 | } |
| 5575 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5576 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 5577 | unsigned AttrSpellingListIndex) { |
| 5578 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5579 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5580 | D->dropAttr<DLLImportAttr>(); |
| 5581 | } |
| 5582 | |
| 5583 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5584 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5585 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5586 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5589 | static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5590 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 5591 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5592 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A; |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5593 | return; |
| 5594 | } |
| 5595 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5596 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5597 | if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport && |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5598 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5599 | // MinGW doesn't allow dllimport on inline functions. |
| 5600 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5601 | << A; |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5602 | return; |
| 5603 | } |
| 5604 | } |
| 5605 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5606 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5607 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5608 | MD->getParent()->isLambda()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5609 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A; |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5610 | return; |
| 5611 | } |
| 5612 | } |
| 5613 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5614 | unsigned Index = A.getAttributeSpellingListIndex(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5615 | Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5616 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 5617 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5618 | if (NewAttr) |
| 5619 | D->addAttr(NewAttr); |
| 5620 | } |
| 5621 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5622 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5623 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5624 | unsigned AttrSpellingListIndex, |
| 5625 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 5626 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 5627 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5628 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5629 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 5630 | << 1 /*previous declaration*/; |
| 5631 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 5632 | D->dropAttr<MSInheritanceAttr>(); |
| 5633 | } |
| 5634 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5635 | auto *RD = cast<CXXRecordDecl>(D); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5636 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5637 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 5638 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5639 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5640 | } |
| 5641 | } else { |
| 5642 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 5643 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5644 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5645 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5646 | } |
| 5647 | if (RD->getDescribedClassTemplate()) { |
| 5648 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5649 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5650 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5651 | } |
| 5652 | } |
| 5653 | |
| 5654 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5655 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5656 | } |
| 5657 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5658 | static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5659 | // The capability attributes take a single string parameter for the name of |
| 5660 | // the capability they represent. The lockable attribute does not take any |
| 5661 | // parameters. However, semantically, both attributes represent the same |
| 5662 | // concept, and so they use the same semantic attribute. Eventually, the |
| 5663 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5664 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 5665 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5666 | // literal will be considered a "mutex." |
| 5667 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5668 | SourceLocation LiteralLoc; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5669 | if (AL.getKind() == ParsedAttr::AT_Capability && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5670 | !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5671 | return; |
| 5672 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5673 | // Currently, there are only two names allowed for a capability: role and |
| 5674 | // mutex (case insensitive). Diagnose other capability names. |
| 5675 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 5676 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 5677 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5678 | D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N, |
| 5679 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5682 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 5683 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5684 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 5685 | return; |
| 5686 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5687 | D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context, |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 5688 | Args.data(), Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5689 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5693 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5694 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5695 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5696 | return; |
| 5697 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5698 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5699 | S.Context, |
| 5700 | Args.data(), Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5701 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5705 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5706 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5707 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5708 | return; |
| 5709 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5710 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5711 | S.Context, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5712 | AL.getArgAsExpr(0), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5713 | Args.data(), |
| 5714 | Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5715 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5719 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5720 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5721 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5722 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5723 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5724 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5725 | AL.getRange(), S.Context, Args.data(), Args.size(), |
| 5726 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5729 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5730 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5731 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5732 | return; |
| 5733 | |
| 5734 | // check that all arguments are lockable objects |
| 5735 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5736 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5737 | if (Args.empty()) |
| 5738 | return; |
| 5739 | |
| 5740 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5741 | RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(), |
| 5742 | Args.size(), AL.getAttributeSpellingListIndex()); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5743 | |
| 5744 | D->addAttr(RCA); |
| 5745 | } |
| 5746 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5747 | static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5748 | if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5749 | if (NSD->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5750 | S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace); |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5751 | // Do not want to attach the attribute to the namespace because that will |
| 5752 | // cause confusing diagnostic reports for uses of declarations within the |
| 5753 | // namespace. |
| 5754 | return; |
| 5755 | } |
| 5756 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5757 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5758 | // Handle the cases where the attribute has a text message. |
| 5759 | StringRef Str, Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5760 | if (AL.isArgExpr(0) && AL.getArgAsExpr(0) && |
| 5761 | !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5762 | return; |
| 5763 | |
| 5764 | // Only support a single optional message for Declspec and CXX11. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5765 | if (AL.isDeclspecAttribute() || AL.isCXX11Attribute()) |
| 5766 | checkAttributeAtMostNumArgs(S, AL, 1); |
| 5767 | else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) && |
| 5768 | !S.checkStringLiteralArgumentAttr(AL, 1, Replacement)) |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5769 | return; |
| 5770 | |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5771 | if (!S.getLangOpts().CPlusPlus14) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5772 | if (AL.isCXX11Attribute() && |
| 5773 | !(AL.hasScope() && AL.getScopeName()->isStr("gnu"))) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5774 | S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL; |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5775 | |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5776 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5777 | DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement, |
| 5778 | AL.getAttributeSpellingListIndex())); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | static bool isGlobalVar(const Decl *D) { |
| 5782 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 5783 | return S->hasGlobalStorage(); |
| 5784 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5787 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5788 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5789 | return; |
| 5790 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5791 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5792 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5793 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5794 | StringRef SanitizerName; |
| 5795 | SourceLocation LiteralLoc; |
| 5796 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5797 | if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5798 | return; |
| 5799 | |
| 5800 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 5801 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5802 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 5803 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5804 | << AL << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5805 | Sanitizers.push_back(SanitizerName); |
| 5806 | } |
| 5807 | |
| 5808 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5809 | AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 5810 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5814 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5815 | StringRef AttrName = AL.getName()->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 5816 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5817 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 5818 | .Case("no_address_safety_analysis", "address") |
| 5819 | .Case("no_sanitize_address", "address") |
| 5820 | .Case("no_sanitize_thread", "thread") |
| 5821 | .Case("no_sanitize_memory", "memory"); |
| 5822 | if (isGlobalVar(D) && SanitizerName != "address") |
| 5823 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5824 | << AL << ExpectedFunction; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5825 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5826 | NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1, |
| 5827 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5830 | static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5831 | if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5832 | D->addAttr(Internal); |
| 5833 | } |
| 5834 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5835 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5836 | if (S.LangOpts.OpenCLVersion != 200) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5837 | S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5838 | << AL << "2.0" << 0; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5839 | else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5840 | S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL |
| 5841 | << "2.0"; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5842 | } |
| 5843 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5844 | /// Handles semantic checking for features that are common to all attributes, |
| 5845 | /// such as checking whether a parameter was properly specified, or the correct |
| 5846 | /// number of arguments were passed, etc. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5847 | static bool handleCommonAttributeFeatures(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5848 | const ParsedAttr &AL) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5849 | // Several attributes carry different semantics than the parsing requires, so |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5850 | // those are opted out of the common argument checks. |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5851 | // |
| 5852 | // We also bail on unknown and ignored attributes because those are handled |
| 5853 | // as part of the target-specific handling logic. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5854 | if (AL.getKind() == ParsedAttr::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5855 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5856 | // Check whether the attribute requires specific language extensions to be |
| 5857 | // enabled. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5858 | if (!AL.diagnoseLangOpts(S)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5859 | return true; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5860 | // Check whether the attribute appertains to the given subject. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5861 | if (!AL.diagnoseAppertainsTo(S, D)) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5862 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5863 | if (AL.hasCustomParsing()) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5864 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5865 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5866 | if (AL.getMinArgs() == AL.getMaxArgs()) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5867 | // If there are no optional arguments, then checking for the argument count |
| 5868 | // is trivial. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5869 | if (!checkAttributeNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5870 | return true; |
| 5871 | } else { |
| 5872 | // There are optional arguments, so checking is slightly more involved. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5873 | if (AL.getMinArgs() && |
| 5874 | !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5875 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5876 | else if (!AL.hasVariadicArg() && AL.getMaxArgs() && |
| 5877 | !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5878 | return true; |
| 5879 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5880 | |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 5881 | if (S.CheckAttrTarget(AL)) |
| 5882 | return true; |
| 5883 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5884 | return false; |
| 5885 | } |
| 5886 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5887 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5888 | if (D->isInvalidDecl()) |
| 5889 | return; |
| 5890 | |
| 5891 | // Check if there is only one access qualifier. |
| 5892 | if (D->hasAttr<OpenCLAccessAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5893 | S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5894 | << D->getSourceRange(); |
| 5895 | D->setInvalidDecl(true); |
| 5896 | return; |
| 5897 | } |
| 5898 | |
| 5899 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 5900 | // image object can be read and written. |
| 5901 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 5902 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 5903 | // qualifier is a compilation error. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5904 | if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5905 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5906 | if (AL.getName()->getName().find("read_write") != StringRef::npos) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5907 | if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5908 | S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5909 | << AL << PDecl->getType() << DeclTy->isImageType(); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5910 | D->setInvalidDecl(true); |
| 5911 | return; |
| 5912 | } |
| 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5917 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5920 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5921 | // Top Level Sema Entry Points |
| 5922 | //===----------------------------------------------------------------------===// |
| 5923 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5924 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5925 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 5926 | /// silently ignore it if a GNU attribute. |
| 5927 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5928 | const ParsedAttr &AL, |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5929 | bool IncludeCXX11Attributes) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5930 | if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5931 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 5932 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5933 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5934 | // instead. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5935 | if (AL.isCXX11Attribute() && !IncludeCXX11Attributes) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5936 | return; |
| 5937 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5938 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 5939 | // which do not apply to the current target architecture are treated as |
| 5940 | // though they were unknown attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5941 | if (AL.getKind() == ParsedAttr::UnknownAttribute || |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5942 | !AL.existsInTarget(S.Context.getTargetInfo())) { |
| 5943 | S.Diag(AL.getLoc(), AL.isDeclspecAttribute() |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5944 | ? diag::warn_unhandled_ms_attribute_ignored |
| 5945 | : diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5946 | << AL; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5947 | return; |
| 5948 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5949 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5950 | if (handleCommonAttributeFeatures(S, D, AL)) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5951 | return; |
| 5952 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5953 | switch (AL.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5954 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5955 | if (!AL.isStmtAttr()) { |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5956 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5957 | assert(AL.isTypeAttr() && "Non-type attribute not handled"); |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5958 | break; |
| 5959 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5960 | S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5961 | << AL << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5962 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5963 | case ParsedAttr::AT_Interrupt: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5964 | handleInterruptAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5965 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5966 | case ParsedAttr::AT_X86ForceAlignArgPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5967 | handleX86ForceAlignArgPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5968 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5969 | case ParsedAttr::AT_DLLExport: |
| 5970 | case ParsedAttr::AT_DLLImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5971 | handleDLLAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5972 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5973 | case ParsedAttr::AT_Mips16: |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 5974 | handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5975 | MipsInterruptAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5976 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5977 | case ParsedAttr::AT_NoMips16: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5978 | handleSimpleAttribute<NoMips16Attr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5979 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5980 | case ParsedAttr::AT_MicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5981 | handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 5982 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5983 | case ParsedAttr::AT_NoMicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5984 | handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 5985 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5986 | case ParsedAttr::AT_MipsLongCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5987 | handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5988 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5989 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5990 | case ParsedAttr::AT_MipsShortCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5991 | handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5992 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5993 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5994 | case ParsedAttr::AT_AMDGPUFlatWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5995 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5996 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5997 | case ParsedAttr::AT_AMDGPUWavesPerEU: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5998 | handleAMDGPUWavesPerEUAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5999 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6000 | case ParsedAttr::AT_AMDGPUNumSGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6001 | handleAMDGPUNumSGPRAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6002 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6003 | case ParsedAttr::AT_AMDGPUNumVGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6004 | handleAMDGPUNumVGPRAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6005 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6006 | case ParsedAttr::AT_AVRSignal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6007 | handleAVRSignalAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 6008 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6009 | case ParsedAttr::AT_IBAction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6010 | handleSimpleAttribute<IBActionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6011 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6012 | case ParsedAttr::AT_IBOutlet: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6013 | handleIBOutlet(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6014 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6015 | case ParsedAttr::AT_IBOutletCollection: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6016 | handleIBOutletCollection(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6017 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6018 | case ParsedAttr::AT_IFunc: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6019 | handleIFuncAttr(S, D, AL); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 6020 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6021 | case ParsedAttr::AT_Alias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6022 | handleAliasAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6023 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6024 | case ParsedAttr::AT_Aligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6025 | handleAlignedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6026 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6027 | case ParsedAttr::AT_AlignValue: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6028 | handleAlignValueAttr(S, D, AL); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 6029 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6030 | case ParsedAttr::AT_AllocSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6031 | handleAllocSizeAttr(S, D, AL); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6032 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6033 | case ParsedAttr::AT_AlwaysInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6034 | handleAlwaysInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6035 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6036 | case ParsedAttr::AT_Artificial: |
Aaron Ballman | 736c09b | 2018-02-15 16:28:10 +0000 | [diff] [blame] | 6037 | handleSimpleAttribute<ArtificialAttr>(S, D, AL); |
Erich Keane | 293a055 | 2018-02-14 00:14:07 +0000 | [diff] [blame] | 6038 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6039 | case ParsedAttr::AT_AnalyzerNoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6040 | handleAnalyzerNoReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6041 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6042 | case ParsedAttr::AT_TLSModel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6043 | handleTLSModelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6044 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6045 | case ParsedAttr::AT_Annotate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6046 | handleAnnotateAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6047 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6048 | case ParsedAttr::AT_Availability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6049 | handleAvailabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6050 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6051 | case ParsedAttr::AT_CarriesDependency: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6052 | handleDependencyAttr(S, scope, D, AL); |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6053 | break; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 6054 | case ParsedAttr::AT_CPUDispatch: |
| 6055 | case ParsedAttr::AT_CPUSpecific: |
| 6056 | handleCPUSpecificAttr(S, D, AL); |
| 6057 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6058 | case ParsedAttr::AT_Common: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6059 | handleCommonAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6060 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6061 | case ParsedAttr::AT_CUDAConstant: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6062 | handleConstantAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6063 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6064 | case ParsedAttr::AT_PassObjectSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6065 | handlePassObjectSizeAttr(S, D, AL); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6066 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6067 | case ParsedAttr::AT_Constructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6068 | handleConstructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6069 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6070 | case ParsedAttr::AT_CXX11NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6071 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6072 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6073 | case ParsedAttr::AT_Deprecated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6074 | handleDeprecatedAttr(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6075 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6076 | case ParsedAttr::AT_Destructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6077 | handleDestructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6078 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6079 | case ParsedAttr::AT_EnableIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6080 | handleEnableIfAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6081 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6082 | case ParsedAttr::AT_DiagnoseIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6083 | handleDiagnoseIfAttr(S, D, AL); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6084 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6085 | case ParsedAttr::AT_ExtVectorType: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6086 | handleExtVectorTypeAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6087 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6088 | case ParsedAttr::AT_ExternalSourceSymbol: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6089 | handleExternalSourceSymbolAttr(S, D, AL); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 6090 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6091 | case ParsedAttr::AT_MinSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6092 | handleMinSizeAttr(S, D, AL); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6093 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6094 | case ParsedAttr::AT_OptimizeNone: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6095 | handleOptimizeNoneAttr(S, D, AL); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 6096 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6097 | case ParsedAttr::AT_FlagEnum: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6098 | handleSimpleAttribute<FlagEnumAttr>(S, D, AL); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 6099 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6100 | case ParsedAttr::AT_EnumExtensibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6101 | handleEnumExtensibilityAttr(S, D, AL); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 6102 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6103 | case ParsedAttr::AT_Flatten: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6104 | handleSimpleAttribute<FlattenAttr>(S, D, AL); |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 6105 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6106 | case ParsedAttr::AT_Format: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6107 | handleFormatAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6108 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6109 | case ParsedAttr::AT_FormatArg: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6110 | handleFormatArgAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6111 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6112 | case ParsedAttr::AT_CUDAGlobal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6113 | handleGlobalAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6114 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6115 | case ParsedAttr::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6116 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6117 | AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6118 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6119 | case ParsedAttr::AT_CUDAHost: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6120 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6121 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6122 | case ParsedAttr::AT_GNUInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6123 | handleGNUInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6124 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6125 | case ParsedAttr::AT_CUDALaunchBounds: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6126 | handleLaunchBoundsAttr(S, D, AL); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 6127 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6128 | case ParsedAttr::AT_Restrict: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6129 | handleRestrictAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6130 | break; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6131 | case ParsedAttr::AT_LifetimeBound: |
| 6132 | handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL); |
| 6133 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6134 | case ParsedAttr::AT_MayAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6135 | handleSimpleAttribute<MayAliasAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6136 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6137 | case ParsedAttr::AT_Mode: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6138 | handleModeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6139 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6140 | case ParsedAttr::AT_NoAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6141 | handleSimpleAttribute<NoAliasAttr>(S, D, AL); |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 6142 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6143 | case ParsedAttr::AT_NoCommon: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6144 | handleSimpleAttribute<NoCommonAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6145 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6146 | case ParsedAttr::AT_NoSplitStack: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6147 | handleSimpleAttribute<NoSplitStackAttr>(S, D, AL); |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 6148 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6149 | case ParsedAttr::AT_NonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6150 | if (auto *PVD = dyn_cast<ParmVarDecl>(D)) |
| 6151 | handleNonNullAttrParameter(S, PVD, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6152 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6153 | handleNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6154 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6155 | case ParsedAttr::AT_ReturnsNonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6156 | handleReturnsNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6157 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6158 | case ParsedAttr::AT_NoEscape: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6159 | handleNoEscapeAttr(S, D, AL); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 6160 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6161 | case ParsedAttr::AT_AssumeAligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6162 | handleAssumeAlignedAttr(S, D, AL); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 6163 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6164 | case ParsedAttr::AT_AllocAlign: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6165 | handleAllocAlignAttr(S, D, AL); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 6166 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6167 | case ParsedAttr::AT_Overloadable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6168 | handleSimpleAttribute<OverloadableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6169 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6170 | case ParsedAttr::AT_Ownership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6171 | handleOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6172 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6173 | case ParsedAttr::AT_Cold: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6174 | handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6175 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6176 | case ParsedAttr::AT_Hot: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6177 | handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6178 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6179 | case ParsedAttr::AT_Naked: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6180 | handleNakedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6181 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6182 | case ParsedAttr::AT_NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6183 | handleNoReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6184 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6185 | case ParsedAttr::AT_AnyX86NoCfCheck: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 6186 | handleNoCfCheckAttr(S, D, AL); |
| 6187 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6188 | case ParsedAttr::AT_NoThrow: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6189 | handleSimpleAttribute<NoThrowAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6190 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6191 | case ParsedAttr::AT_CUDAShared: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6192 | handleSharedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6193 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6194 | case ParsedAttr::AT_VecReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6195 | handleVecReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6196 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6197 | case ParsedAttr::AT_ObjCOwnership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6198 | handleObjCOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6199 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6200 | case ParsedAttr::AT_ObjCPreciseLifetime: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6201 | handleObjCPreciseLifetimeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6202 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6203 | case ParsedAttr::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6204 | handleObjCReturnsInnerPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6205 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6206 | case ParsedAttr::AT_ObjCRequiresSuper: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6207 | handleObjCRequiresSuperAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6208 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6209 | case ParsedAttr::AT_ObjCBridge: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6210 | handleObjCBridgeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6211 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6212 | case ParsedAttr::AT_ObjCBridgeMutable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6213 | handleObjCBridgeMutableAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6214 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6215 | case ParsedAttr::AT_ObjCBridgeRelated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6216 | handleObjCBridgeRelatedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6217 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6218 | case ParsedAttr::AT_ObjCDesignatedInitializer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6219 | handleObjCDesignatedInitializer(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6220 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6221 | case ParsedAttr::AT_ObjCRuntimeName: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6222 | handleObjCRuntimeName(S, D, AL); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6223 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6224 | case ParsedAttr::AT_ObjCRuntimeVisible: |
| 6225 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL); |
| 6226 | break; |
| 6227 | case ParsedAttr::AT_ObjCBoxable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6228 | handleObjCBoxable(S, D, AL); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 6229 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6230 | case ParsedAttr::AT_CFAuditedTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6231 | handleSimpleAttributeWithExclusions<CFAuditedTransferAttr, |
| 6232 | CFUnknownTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6233 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6234 | case ParsedAttr::AT_CFUnknownTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6235 | handleSimpleAttributeWithExclusions<CFUnknownTransferAttr, |
| 6236 | CFAuditedTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6237 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6238 | case ParsedAttr::AT_CFConsumed: |
| 6239 | case ParsedAttr::AT_NSConsumed: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6240 | handleNSConsumedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6241 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6242 | case ParsedAttr::AT_NSConsumesSelf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6243 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6244 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6245 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 6246 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 6247 | case ParsedAttr::AT_CFReturnsNotRetained: |
| 6248 | case ParsedAttr::AT_NSReturnsRetained: |
| 6249 | case ParsedAttr::AT_CFReturnsRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6250 | handleNSReturnsRetainedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6251 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6252 | case ParsedAttr::AT_WorkGroupSizeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6253 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6254 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6255 | case ParsedAttr::AT_ReqdWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6256 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6257 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6258 | case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6259 | handleSubGroupSize(S, D, AL); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 6260 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6261 | case ParsedAttr::AT_VecTypeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6262 | handleVecTypeHint(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6263 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6264 | case ParsedAttr::AT_RequireConstantInit: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6265 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL); |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 6266 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6267 | case ParsedAttr::AT_InitPriority: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6268 | handleInitPriorityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6269 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6270 | case ParsedAttr::AT_Packed: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6271 | handlePackedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6272 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6273 | case ParsedAttr::AT_Section: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6274 | handleSectionAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6275 | break; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 6276 | case ParsedAttr::AT_CodeSeg: |
| 6277 | handleCodeSegAttr(S, D, AL); |
| 6278 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6279 | case ParsedAttr::AT_Target: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6280 | handleTargetAttr(S, D, AL); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 6281 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6282 | case ParsedAttr::AT_MinVectorWidth: |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 6283 | handleMinVectorWidthAttr(S, D, AL); |
| 6284 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6285 | case ParsedAttr::AT_Unavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6286 | handleAttrWithMessage<UnavailableAttr>(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6287 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6288 | case ParsedAttr::AT_ArcWeakrefUnavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6289 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6290 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6291 | case ParsedAttr::AT_ObjCRootClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6292 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6293 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6294 | case ParsedAttr::AT_ObjCSubclassingRestricted: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6295 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL); |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 6296 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6297 | case ParsedAttr::AT_ObjCExplicitProtocolImpl: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6298 | handleObjCSuppresProtocolAttr(S, D, AL); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 6299 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6300 | case ParsedAttr::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6301 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6302 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6303 | case ParsedAttr::AT_Unused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6304 | handleUnusedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6305 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6306 | case ParsedAttr::AT_ReturnsTwice: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6307 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6308 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6309 | case ParsedAttr::AT_NotTailCalled: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6310 | handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>( |
| 6311 | S, D, AL); |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 6312 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6313 | case ParsedAttr::AT_DisableTailCalls: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6314 | handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D, |
| 6315 | AL); |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 6316 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6317 | case ParsedAttr::AT_Used: |
Aaron Ballman | 1a3901c | 2018-03-03 21:02:09 +0000 | [diff] [blame] | 6318 | handleSimpleAttribute<UsedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6319 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6320 | case ParsedAttr::AT_Visibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6321 | handleVisibilityAttr(S, D, AL, false); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 6322 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6323 | case ParsedAttr::AT_TypeVisibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6324 | handleVisibilityAttr(S, D, AL, true); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 6325 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6326 | case ParsedAttr::AT_WarnUnused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6327 | handleSimpleAttribute<WarnUnusedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6328 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6329 | case ParsedAttr::AT_WarnUnusedResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6330 | handleWarnUnusedResult(S, D, AL); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 6331 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6332 | case ParsedAttr::AT_Weak: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6333 | handleSimpleAttribute<WeakAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6334 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6335 | case ParsedAttr::AT_WeakRef: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6336 | handleWeakRefAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6337 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6338 | case ParsedAttr::AT_WeakImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6339 | handleWeakImportAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6340 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6341 | case ParsedAttr::AT_TransparentUnion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6342 | handleTransparentUnionAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6343 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6344 | case ParsedAttr::AT_ObjCException: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6345 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6346 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6347 | case ParsedAttr::AT_ObjCMethodFamily: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6348 | handleObjCMethodFamilyAttr(S, D, AL); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 6349 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6350 | case ParsedAttr::AT_ObjCNSObject: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6351 | handleObjCNSObject(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6352 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6353 | case ParsedAttr::AT_ObjCIndependentClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6354 | handleObjCIndependentClass(S, D, AL); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 6355 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6356 | case ParsedAttr::AT_Blocks: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6357 | handleBlocksAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6358 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6359 | case ParsedAttr::AT_Sentinel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6360 | handleSentinelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6361 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6362 | case ParsedAttr::AT_Const: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6363 | handleSimpleAttribute<ConstAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6364 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6365 | case ParsedAttr::AT_Pure: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6366 | handleSimpleAttribute<PureAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6367 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6368 | case ParsedAttr::AT_Cleanup: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6369 | handleCleanupAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6370 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6371 | case ParsedAttr::AT_NoDebug: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6372 | handleNoDebugAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6373 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6374 | case ParsedAttr::AT_NoDuplicate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6375 | handleSimpleAttribute<NoDuplicateAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6376 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6377 | case ParsedAttr::AT_Convergent: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6378 | handleSimpleAttribute<ConvergentAttr>(S, D, AL); |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 6379 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6380 | case ParsedAttr::AT_NoInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6381 | handleSimpleAttribute<NoInlineAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6382 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6383 | case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6384 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6385 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6386 | case ParsedAttr::AT_NoStackProtector: |
Manoj Gupta | 4fbf84c | 2018-05-09 21:41:18 +0000 | [diff] [blame] | 6387 | // Interacts with -fstack-protector options. |
| 6388 | handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL); |
| 6389 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6390 | case ParsedAttr::AT_StdCall: |
| 6391 | case ParsedAttr::AT_CDecl: |
| 6392 | case ParsedAttr::AT_FastCall: |
| 6393 | case ParsedAttr::AT_ThisCall: |
| 6394 | case ParsedAttr::AT_Pascal: |
| 6395 | case ParsedAttr::AT_RegCall: |
| 6396 | case ParsedAttr::AT_SwiftCall: |
| 6397 | case ParsedAttr::AT_VectorCall: |
| 6398 | case ParsedAttr::AT_MSABI: |
| 6399 | case ParsedAttr::AT_SysVABI: |
| 6400 | case ParsedAttr::AT_Pcs: |
| 6401 | case ParsedAttr::AT_IntelOclBicc: |
| 6402 | case ParsedAttr::AT_PreserveMost: |
| 6403 | case ParsedAttr::AT_PreserveAll: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6404 | handleCallConvAttr(S, D, AL); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 6405 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6406 | case ParsedAttr::AT_Suppress: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6407 | handleSuppressAttr(S, D, AL); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 6408 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6409 | case ParsedAttr::AT_OpenCLKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6410 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6411 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6412 | case ParsedAttr::AT_OpenCLAccess: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6413 | handleOpenCLAccessAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6414 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6415 | case ParsedAttr::AT_OpenCLNoSVM: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6416 | handleOpenCLNoSVMAttr(S, D, AL); |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 6417 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6418 | case ParsedAttr::AT_SwiftContext: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6419 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6420 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6421 | case ParsedAttr::AT_SwiftErrorResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6422 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6423 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6424 | case ParsedAttr::AT_SwiftIndirectResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6425 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6426 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6427 | case ParsedAttr::AT_InternalLinkage: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6428 | handleInternalLinkageAttr(S, D, AL); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 6429 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6430 | case ParsedAttr::AT_LTOVisibilityPublic: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6431 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL); |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 6432 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6433 | |
| 6434 | // Microsoft attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6435 | case ParsedAttr::AT_EmptyBases: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6436 | handleSimpleAttribute<EmptyBasesAttr>(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6437 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6438 | case ParsedAttr::AT_LayoutVersion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6439 | handleLayoutVersion(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6440 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6441 | case ParsedAttr::AT_TrivialABI: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6442 | handleSimpleAttribute<TrivialABIAttr>(S, D, AL); |
Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 6443 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6444 | case ParsedAttr::AT_MSNoVTable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6445 | handleSimpleAttribute<MSNoVTableAttr>(S, D, AL); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 6446 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6447 | case ParsedAttr::AT_MSStruct: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6448 | handleSimpleAttribute<MSStructAttr>(S, D, AL); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6449 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6450 | case ParsedAttr::AT_Uuid: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6451 | handleUuidAttr(S, D, AL); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 6452 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6453 | case ParsedAttr::AT_MSInheritance: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6454 | handleMSInheritanceAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6455 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6456 | case ParsedAttr::AT_SelectAny: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6457 | handleSimpleAttribute<SelectAnyAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6458 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6459 | case ParsedAttr::AT_Thread: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6460 | handleDeclspecThreadAttr(S, D, AL); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 6461 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6462 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6463 | case ParsedAttr::AT_AbiTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6464 | handleAbiTagAttr(S, D, AL); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 6465 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6466 | |
| 6467 | // Thread safety attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6468 | case ParsedAttr::AT_AssertExclusiveLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6469 | handleAssertExclusiveLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 6470 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6471 | case ParsedAttr::AT_AssertSharedLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6472 | handleAssertSharedLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 6473 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6474 | case ParsedAttr::AT_GuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6475 | handleSimpleAttribute<GuardedVarAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6476 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6477 | case ParsedAttr::AT_PtGuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6478 | handlePtGuardedVarAttr(S, D, AL); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6479 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6480 | case ParsedAttr::AT_ScopedLockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6481 | handleSimpleAttribute<ScopedLockableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6482 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6483 | case ParsedAttr::AT_NoSanitize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6484 | handleNoSanitizeAttr(S, D, AL); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6485 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6486 | case ParsedAttr::AT_NoSanitizeSpecific: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6487 | handleNoSanitizeSpecificAttr(S, D, AL); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 6488 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6489 | case ParsedAttr::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6490 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 6491 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6492 | case ParsedAttr::AT_GuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6493 | handleGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6494 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6495 | case ParsedAttr::AT_PtGuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6496 | handlePtGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6497 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6498 | case ParsedAttr::AT_ExclusiveTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6499 | handleExclusiveTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6500 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6501 | case ParsedAttr::AT_LockReturned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6502 | handleLockReturnedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6503 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6504 | case ParsedAttr::AT_LocksExcluded: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6505 | handleLocksExcludedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6506 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6507 | case ParsedAttr::AT_SharedTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6508 | handleSharedTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6509 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6510 | case ParsedAttr::AT_AcquiredBefore: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6511 | handleAcquiredBeforeAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6512 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6513 | case ParsedAttr::AT_AcquiredAfter: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6514 | handleAcquiredAfterAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6515 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6516 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6517 | // Capability analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6518 | case ParsedAttr::AT_Capability: |
| 6519 | case ParsedAttr::AT_Lockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6520 | handleCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6521 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6522 | case ParsedAttr::AT_RequiresCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6523 | handleRequiresCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6524 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6525 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6526 | case ParsedAttr::AT_AssertCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6527 | handleAssertCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6528 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6529 | case ParsedAttr::AT_AcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6530 | handleAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6531 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6532 | case ParsedAttr::AT_ReleaseCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6533 | handleReleaseCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6534 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6535 | case ParsedAttr::AT_TryAcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6536 | handleTryAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6537 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6538 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 6539 | // Consumed analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6540 | case ParsedAttr::AT_Consumable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6541 | handleConsumableAttr(S, D, AL); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 6542 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6543 | case ParsedAttr::AT_ConsumableAutoCast: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6544 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6545 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6546 | case ParsedAttr::AT_ConsumableSetOnRead: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6547 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6548 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6549 | case ParsedAttr::AT_CallableWhen: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6550 | handleCallableWhenAttr(S, D, AL); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6551 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6552 | case ParsedAttr::AT_ParamTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6553 | handleParamTypestateAttr(S, D, AL); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 6554 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6555 | case ParsedAttr::AT_ReturnTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6556 | handleReturnTypestateAttr(S, D, AL); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 6557 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6558 | case ParsedAttr::AT_SetTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6559 | handleSetTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6560 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6561 | case ParsedAttr::AT_TestTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6562 | handleTestTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6563 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6564 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6565 | // Type safety attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6566 | case ParsedAttr::AT_ArgumentWithTypeTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6567 | handleArgumentWithTypeTagAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6568 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6569 | case ParsedAttr::AT_TypeTagForDatatype: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6570 | handleTypeTagForDatatypeAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6571 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6572 | case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 6573 | handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 6574 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6575 | case ParsedAttr::AT_RenderScriptKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6576 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 6577 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6578 | // XRay attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6579 | case ParsedAttr::AT_XRayInstrument: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6580 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL); |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6581 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6582 | case ParsedAttr::AT_XRayLogArgs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6583 | handleXRayLogArgsAttr(S, D, AL); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 6584 | break; |
Martin Bohme | 4e1293b | 2018-08-13 14:11:03 +0000 | [diff] [blame^] | 6585 | |
| 6586 | // Move semantics attribute. |
| 6587 | case ParsedAttr::AT_Reinitializes: |
| 6588 | handleSimpleAttribute<ReinitializesAttr>(S, D, AL); |
| 6589 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6590 | } |
| 6591 | } |
| 6592 | |
| 6593 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 6594 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 6595 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6596 | const ParsedAttributesView &AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6597 | bool IncludeCXX11Attributes) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6598 | if (AttrList.empty()) |
| 6599 | return; |
| 6600 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6601 | for (const ParsedAttr &AL : AttrList) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6602 | ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6603 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6604 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6605 | // GCC accepts |
| 6606 | // static int a9 __attribute__((weakref)); |
| 6607 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6608 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6609 | Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias) |
| 6610 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 6611 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6612 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6613 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6614 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 6615 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 6616 | // good to have a way to specify "these attributes must appear as a group", |
| 6617 | // for these. Additionally, it would be good to have a way to specify "these |
| 6618 | // 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] | 6619 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 6620 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6621 | if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6622 | // FIXME: This emits a different error message than |
| 6623 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6624 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6625 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6626 | } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6627 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6628 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6629 | } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6630 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6631 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6632 | } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 6633 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 6634 | D->setInvalidDecl(); |
Yaxun Liu | aa24601 | 2018-06-12 23:58:59 +0000 | [diff] [blame] | 6635 | } else if (!D->hasAttr<CUDAGlobalAttr>()) { |
| 6636 | if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 6637 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6638 | << A << ExpectedKernelFunction; |
| 6639 | D->setInvalidDecl(); |
| 6640 | } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 6641 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6642 | << A << ExpectedKernelFunction; |
| 6643 | D->setInvalidDecl(); |
| 6644 | } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6645 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6646 | << A << ExpectedKernelFunction; |
| 6647 | D->setInvalidDecl(); |
| 6648 | } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6649 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6650 | << A << ExpectedKernelFunction; |
| 6651 | D->setInvalidDecl(); |
| 6652 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6653 | } |
| 6654 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
Hiroshi Inoue | 939d932 | 2017-06-30 05:40:31 +0000 | [diff] [blame] | 6657 | // Helper for delayed processing TransparentUnion attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6658 | void Sema::ProcessDeclAttributeDelayed(Decl *D, |
| 6659 | const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6660 | for (const ParsedAttr &AL : AttrList) |
| 6661 | if (AL.getKind() == ParsedAttr::AT_TransparentUnion) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6662 | handleTransparentUnionAttr(*this, D, AL); |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 6663 | break; |
| 6664 | } |
| 6665 | } |
| 6666 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6667 | // Annotation attributes are the only attributes allowed after an access |
| 6668 | // specifier. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6669 | bool Sema::ProcessAccessDeclAttributeList( |
| 6670 | AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6671 | for (const ParsedAttr &AL : AttrList) { |
| 6672 | if (AL.getKind() == ParsedAttr::AT_Annotate) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6673 | ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6674 | } else { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6675 | Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6676 | return true; |
| 6677 | } |
| 6678 | } |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6679 | return false; |
| 6680 | } |
| 6681 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6682 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 6683 | /// contains any decl attributes that we should warn about. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6684 | static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6685 | for (const ParsedAttr &AL : A) { |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6686 | // Only warn if the attribute is an unignored, non-type attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6687 | if (AL.isUsedAsTypeAttr() || AL.isInvalid()) |
| 6688 | continue; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6689 | if (AL.getKind() == ParsedAttr::IgnoredAttribute) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6690 | continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6691 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6692 | if (AL.getKind() == ParsedAttr::UnknownAttribute) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6693 | S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6694 | << AL << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6695 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6696 | S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL |
| 6697 | << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6698 | } |
| 6699 | } |
| 6700 | } |
| 6701 | |
| 6702 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 6703 | /// used to build a declaration, complain about any decl attributes |
| 6704 | /// which might be lying around on it. |
| 6705 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6706 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes()); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6707 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 6708 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 6709 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 6710 | } |
| 6711 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6712 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6713 | /// \#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] | 6714 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 6715 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6716 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6717 | NamedDecl *NewD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6718 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6719 | FunctionDecl *NewFD; |
| 6720 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6721 | // FIXME: Mangling? |
| 6722 | // FIXME: Is the qualifier info correct? |
| 6723 | // FIXME: Is the DeclContext correct? |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6724 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 6725 | Loc, Loc, DeclarationName(II), |
| 6726 | FD->getType(), FD->getTypeSourceInfo(), |
| 6727 | SC_None, false/*isInlineSpecified*/, |
| 6728 | FD->hasPrototype(), |
| 6729 | false/*isConstexprSpecified*/); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6730 | NewD = NewFD; |
| 6731 | |
| 6732 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6733 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6734 | |
| 6735 | // Fake up parameter variables; they are declared as if this were |
| 6736 | // a typedef. |
| 6737 | QualType FDTy = FD->getType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6738 | if (const auto *FT = FDTy->getAs<FunctionProtoType>()) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6739 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6740 | for (const auto &AI : FT->param_types()) { |
| 6741 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6742 | Param->setScopeInfo(0, Params.size()); |
| 6743 | Params.push_back(Param); |
| 6744 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6745 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6746 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6747 | } else if (auto *VD = dyn_cast<VarDecl>(ND)) { |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6748 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6749 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6750 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6751 | VD->getStorageClass()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6752 | if (VD->getQualifier()) |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame] | 6753 | cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc()); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6754 | } |
| 6755 | return NewD; |
| 6756 | } |
| 6757 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6758 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6759 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6760 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6761 | if (W.getUsed()) return; // only do this once |
| 6762 | W.setUsed(true); |
| 6763 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 6764 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6765 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6766 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 6767 | W.getLocation())); |
| 6768 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6769 | WeakTopLevelDecl.push_back(NewD); |
| 6770 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 6771 | // to insert Decl at TU scope, sorry. |
| 6772 | DeclContext *SavedContext = CurContext; |
| 6773 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 6774 | NewD->setDeclContext(CurContext); |
| 6775 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6776 | PushOnScopeChains(NewD, S); |
| 6777 | CurContext = SavedContext; |
| 6778 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6779 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6780 | } |
| 6781 | } |
| 6782 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6783 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 6784 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 6785 | // have to do this. |
| 6786 | LoadExternalWeakUndeclaredIdentifiers(); |
| 6787 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6788 | NamedDecl *ND = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6789 | if (auto *VD = dyn_cast<VarDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6790 | if (VD->isExternC()) |
| 6791 | ND = VD; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6792 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6793 | if (FD->isExternC()) |
| 6794 | ND = FD; |
| 6795 | if (ND) { |
| 6796 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 6797 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6798 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 6799 | WeakInfo W = I->second; |
| 6800 | DeclApplyPragmaWeak(S, ND, W); |
| 6801 | WeakUndeclaredIdentifiers[Id] = W; |
| 6802 | } |
| 6803 | } |
| 6804 | } |
| 6805 | } |
| 6806 | } |
| 6807 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6808 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 6809 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 6810 | /// 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] | 6811 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6812 | // Apply decl attributes from the DeclSpec if present. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6813 | if (!PD.getDeclSpec().getAttributes().empty()) |
| 6814 | ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6815 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6816 | // Walk the declarator structure, applying decl attributes that were in a type |
| 6817 | // position to the decl itself. This handles cases like: |
| 6818 | // int *__attr__(x)** D; |
| 6819 | // when X is a decl attribute. |
| 6820 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6821 | ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(), |
| 6822 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6823 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6824 | // Finally, apply any attributes on the decl itself. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6825 | ProcessDeclAttributeList(S, D, PD.getAttributes()); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 6826 | |
| 6827 | // Apply additional attributes specified by '#pragma clang attribute'. |
| 6828 | AddPragmaAttributes(S, D); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6829 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6830 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6831 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6832 | /// If so, it'll still be annotated with an attribute that makes it |
| 6833 | /// illegal to actually use. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6834 | static bool isForbiddenTypeAllowed(Sema &S, Decl *D, |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6835 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6836 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6837 | // Private ivars are always okay. Unfortunately, people don't |
| 6838 | // always properly make their ivars private, even in system headers. |
| 6839 | // Plus we need to make fields okay, too. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6840 | if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) && |
| 6841 | !isa<FunctionDecl>(D)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6842 | return false; |
| 6843 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6844 | // Silently accept unsupported uses of __weak in both user and system |
| 6845 | // declarations when it's been disabled, for ease of integration with |
| 6846 | // -fno-objc-arc files. We do have to take some care against attempts |
| 6847 | // to define such things; for now, we've only done that for ivars |
| 6848 | // and properties. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6849 | if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6850 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 6851 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 6852 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 6853 | return true; |
| 6854 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6855 | } |
| 6856 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6857 | // Allow all sorts of things in system headers. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6858 | if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6859 | // Currently, all the failures dealt with this way are due to ARC |
| 6860 | // restrictions. |
| 6861 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 6862 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6863 | } |
| 6864 | |
| 6865 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | /// Handle a delayed forbidden-type diagnostic. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6869 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD, |
| 6870 | Decl *D) { |
| 6871 | auto Reason = UnavailableAttr::IR_None; |
| 6872 | if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) { |
| 6873 | assert(Reason && "didn't set reason?"); |
| 6874 | D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6875 | return; |
| 6876 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6877 | if (S.getLangOpts().ObjCAutoRefCount) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6878 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 6879 | // FIXME: we may want to suppress diagnostics for all |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6880 | // kind of forbidden type messages on unavailable functions. |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6881 | if (FD->hasAttr<UnavailableAttr>() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6882 | DD.getForbiddenTypeDiagnostic() == |
| 6883 | diag::err_arc_array_param_no_ownership) { |
| 6884 | DD.Triggered = true; |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6885 | return; |
| 6886 | } |
| 6887 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6888 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6889 | S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic()) |
| 6890 | << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument(); |
| 6891 | DD.Triggered = true; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6892 | } |
| 6893 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6894 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 6895 | const Decl *D) { |
| 6896 | // Check each AvailabilityAttr to find the one for this platform. |
| 6897 | for (const auto *A : D->attrs()) { |
| 6898 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 6899 | // FIXME: this is copied from CheckAvailability. We should try to |
| 6900 | // de-duplicate. |
| 6901 | |
| 6902 | // Check if this is an App Extension "platform", and if so chop off |
| 6903 | // the suffix for matching with the actual platform. |
| 6904 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 6905 | StringRef RealizedPlatform = ActualPlatform; |
| 6906 | if (Context.getLangOpts().AppExt) { |
| 6907 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 6908 | if (suffix != StringRef::npos) |
| 6909 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 6910 | } |
| 6911 | |
| 6912 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 6913 | |
| 6914 | // Match the platform name. |
| 6915 | if (RealizedPlatform == TargetPlatform) |
| 6916 | return Avail; |
| 6917 | } |
| 6918 | } |
| 6919 | return nullptr; |
| 6920 | } |
| 6921 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 6922 | /// The diagnostic we should emit for \c D, and the declaration that |
| 6923 | /// originated it, or \c AR_Available. |
| 6924 | /// |
| 6925 | /// \param D The declaration to check. |
| 6926 | /// \param Message If non-null, this will be populated with the message from |
| 6927 | /// the availability attribute that is selected. |
| 6928 | static std::pair<AvailabilityResult, const NamedDecl *> |
| 6929 | ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) { |
| 6930 | AvailabilityResult Result = D->getAvailability(Message); |
| 6931 | |
| 6932 | // For typedefs, if the typedef declaration appears available look |
| 6933 | // to the underlying type to see if it is more restrictive. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6934 | while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 6935 | if (Result == AR_Available) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6936 | if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 6937 | D = TT->getDecl(); |
| 6938 | Result = D->getAvailability(Message); |
| 6939 | continue; |
| 6940 | } |
| 6941 | } |
| 6942 | break; |
| 6943 | } |
| 6944 | |
| 6945 | // Forward class declarations get their attributes from their definition. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6946 | if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 6947 | if (IDecl->getDefinition()) { |
| 6948 | D = IDecl->getDefinition(); |
| 6949 | Result = D->getAvailability(Message); |
| 6950 | } |
| 6951 | } |
| 6952 | |
| 6953 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 6954 | if (Result == AR_Available) { |
| 6955 | const DeclContext *DC = ECD->getDeclContext(); |
| 6956 | if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) { |
| 6957 | Result = TheEnumDecl->getAvailability(Message); |
| 6958 | D = TheEnumDecl; |
| 6959 | } |
| 6960 | } |
| 6961 | |
| 6962 | return {Result, D}; |
| 6963 | } |
| 6964 | |
| 6965 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6966 | /// whether we should emit a diagnostic for \c K and \c DeclVersion in |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6967 | /// the context of \c Ctx. For example, we should emit an unavailable diagnostic |
| 6968 | /// in a deprecated context, but not the other way around. |
| 6969 | static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 6970 | VersionTuple DeclVersion, |
| 6971 | Decl *Ctx) { |
| 6972 | assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 6973 | |
| 6974 | // Checks if we should emit the availability diagnostic in the context of C. |
| 6975 | auto CheckContext = [&](const Decl *C) { |
| 6976 | if (K == AR_NotYetIntroduced) { |
| 6977 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 6978 | if (AA->getIntroduced() >= DeclVersion) |
| 6979 | return true; |
| 6980 | } else if (K == AR_Deprecated) |
| 6981 | if (C->isDeprecated()) |
| 6982 | return true; |
| 6983 | |
| 6984 | if (C->isUnavailable()) |
| 6985 | return true; |
| 6986 | return false; |
| 6987 | }; |
| 6988 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6989 | do { |
| 6990 | if (CheckContext(Ctx)) |
| 6991 | return false; |
| 6992 | |
| 6993 | // An implementation implicitly has the availability of the interface. |
Steven Wu | 3bb4aa5 | 2018-04-16 23:34:18 +0000 | [diff] [blame] | 6994 | // Unless it is "+load" method. |
| 6995 | if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx)) |
| 6996 | if (MethodD->isClassMethod() && |
| 6997 | MethodD->getSelector().getAsString() == "load") |
| 6998 | return true; |
| 6999 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7000 | if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7001 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 7002 | if (CheckContext(Interface)) |
| 7003 | return false; |
| 7004 | } |
| 7005 | // A category implicitly has the availability of the interface. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7006 | else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7007 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 7008 | if (CheckContext(Interface)) |
| 7009 | return false; |
| 7010 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 7011 | |
| 7012 | return true; |
| 7013 | } |
| 7014 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7015 | static bool |
| 7016 | shouldDiagnoseAvailabilityByDefault(const ASTContext &Context, |
| 7017 | const VersionTuple &DeploymentVersion, |
| 7018 | const VersionTuple &DeclVersion) { |
| 7019 | const auto &Triple = Context.getTargetInfo().getTriple(); |
| 7020 | VersionTuple ForceAvailabilityFromVersion; |
| 7021 | switch (Triple.getOS()) { |
| 7022 | case llvm::Triple::IOS: |
| 7023 | case llvm::Triple::TvOS: |
| 7024 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11); |
| 7025 | break; |
| 7026 | case llvm::Triple::WatchOS: |
| 7027 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4); |
| 7028 | break; |
| 7029 | case llvm::Triple::Darwin: |
| 7030 | case llvm::Triple::MacOSX: |
| 7031 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13); |
| 7032 | break; |
| 7033 | default: |
| 7034 | // New targets should always warn about availability. |
| 7035 | return Triple.getVendor() == llvm::Triple::Apple; |
| 7036 | } |
| 7037 | return DeploymentVersion >= ForceAvailabilityFromVersion || |
| 7038 | DeclVersion >= ForceAvailabilityFromVersion; |
| 7039 | } |
| 7040 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7041 | static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) { |
| 7042 | for (Decl *Ctx = OrigCtx; Ctx; |
| 7043 | Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) { |
| 7044 | if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx)) |
| 7045 | return cast<NamedDecl>(Ctx); |
| 7046 | if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) { |
| 7047 | if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx)) |
| 7048 | return Imp->getClassInterface(); |
| 7049 | return CD; |
| 7050 | } |
| 7051 | } |
| 7052 | |
| 7053 | return dyn_cast<NamedDecl>(OrigCtx); |
| 7054 | } |
| 7055 | |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7056 | namespace { |
| 7057 | |
| 7058 | struct AttributeInsertion { |
| 7059 | StringRef Prefix; |
| 7060 | SourceLocation Loc; |
| 7061 | StringRef Suffix; |
| 7062 | |
| 7063 | static AttributeInsertion createInsertionAfter(const NamedDecl *D) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7064 | return {" ", D->getEndLoc(), ""}; |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7065 | } |
| 7066 | static AttributeInsertion createInsertionAfter(SourceLocation Loc) { |
| 7067 | return {" ", Loc, ""}; |
| 7068 | } |
| 7069 | static AttributeInsertion createInsertionBefore(const NamedDecl *D) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7070 | return {"", D->getBeginLoc(), "\n"}; |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7071 | } |
| 7072 | }; |
| 7073 | |
| 7074 | } // end anonymous namespace |
| 7075 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7076 | /// Tries to parse a string as ObjC method name. |
| 7077 | /// |
| 7078 | /// \param Name The string to parse. Expected to originate from availability |
| 7079 | /// attribute argument. |
| 7080 | /// \param SlotNames The vector that will be populated with slot names. In case |
| 7081 | /// of unsuccessful parsing can contain invalid data. |
| 7082 | /// \returns A number of method parameters if parsing was successful, None |
| 7083 | /// otherwise. |
| 7084 | static Optional<unsigned> |
| 7085 | tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames, |
| 7086 | const LangOptions &LangOpts) { |
| 7087 | // Accept replacements starting with - or + as valid ObjC method names. |
| 7088 | if (!Name.empty() && (Name.front() == '-' || Name.front() == '+')) |
| 7089 | Name = Name.drop_front(1); |
| 7090 | if (Name.empty()) |
| 7091 | return None; |
| 7092 | Name.split(SlotNames, ':'); |
| 7093 | unsigned NumParams; |
| 7094 | if (Name.back() == ':') { |
| 7095 | // Remove an empty string at the end that doesn't represent any slot. |
| 7096 | SlotNames.pop_back(); |
| 7097 | NumParams = SlotNames.size(); |
| 7098 | } else { |
| 7099 | if (SlotNames.size() != 1) |
| 7100 | // Not a valid method name, just a colon-separated string. |
| 7101 | return None; |
| 7102 | NumParams = 0; |
| 7103 | } |
| 7104 | // Verify all slot names are valid. |
| 7105 | bool AllowDollar = LangOpts.DollarIdents; |
| 7106 | for (StringRef S : SlotNames) { |
| 7107 | if (S.empty()) |
| 7108 | continue; |
| 7109 | if (!isValidIdentifier(S, AllowDollar)) |
| 7110 | return None; |
| 7111 | } |
| 7112 | return NumParams; |
| 7113 | } |
| 7114 | |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7115 | /// Returns a source location in which it's appropriate to insert a new |
| 7116 | /// attribute for the given declaration \D. |
| 7117 | static Optional<AttributeInsertion> |
| 7118 | createAttributeInsertion(const NamedDecl *D, const SourceManager &SM, |
| 7119 | const LangOptions &LangOpts) { |
| 7120 | if (isa<ObjCPropertyDecl>(D)) |
| 7121 | return AttributeInsertion::createInsertionAfter(D); |
| 7122 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7123 | if (MD->hasBody()) |
| 7124 | return None; |
| 7125 | return AttributeInsertion::createInsertionAfter(D); |
| 7126 | } |
| 7127 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
| 7128 | SourceLocation Loc = |
| 7129 | Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts); |
| 7130 | if (Loc.isInvalid()) |
| 7131 | return None; |
| 7132 | // Insert after the 'struct'/whatever keyword. |
| 7133 | return AttributeInsertion::createInsertionAfter(Loc); |
| 7134 | } |
| 7135 | return AttributeInsertion::createInsertionBefore(D); |
| 7136 | } |
| 7137 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7138 | /// Actually emit an availability diagnostic for a reference to an unavailable |
| 7139 | /// decl. |
| 7140 | /// |
| 7141 | /// \param Ctx The context that the reference occurred in |
| 7142 | /// \param ReferringDecl The exact declaration that was referenced. |
| 7143 | /// \param OffendingDecl A related decl to \c ReferringDecl that has an |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7144 | /// availability attribute corresponding to \c K attached to it. Note that this |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7145 | /// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and |
| 7146 | /// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl |
| 7147 | /// and OffendingDecl is the EnumDecl. |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7148 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7149 | Decl *Ctx, const NamedDecl *ReferringDecl, |
| 7150 | const NamedDecl *OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7151 | StringRef Message, |
| 7152 | ArrayRef<SourceLocation> Locs, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7153 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7154 | const ObjCPropertyDecl *ObjCProperty, |
| 7155 | bool ObjCPropertyAccess) { |
| 7156 | // Diagnostics for deprecated or unavailable. |
| 7157 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7158 | unsigned diag_available_here = diag::note_availability_specified_here; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7159 | SourceLocation NoteLocation = OffendingDecl->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7160 | |
| 7161 | // Matches 'diag::note_property_attribute' options. |
| 7162 | unsigned property_note_select; |
| 7163 | |
| 7164 | // Matches diag::note_availability_specified_here. |
| 7165 | unsigned available_here_select_kind; |
| 7166 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7167 | VersionTuple DeclVersion; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7168 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7169 | DeclVersion = AA->getIntroduced(); |
| 7170 | |
| 7171 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx)) |
| 7172 | return; |
| 7173 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7174 | SourceLocation Loc = Locs.front(); |
| 7175 | |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7176 | // The declaration can have multiple availability attributes, we are looking |
| 7177 | // at one of them. |
| 7178 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl); |
| 7179 | if (A && A->isInherited()) { |
| 7180 | for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl; |
| 7181 | Redecl = Redecl->getPreviousDecl()) { |
| 7182 | const AvailabilityAttr *AForRedecl = |
| 7183 | getAttrForPlatform(S.Context, Redecl); |
| 7184 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 7185 | // If D is a declaration with inherited attributes, the note should |
| 7186 | // point to the declaration with actual attributes. |
| 7187 | NoteLocation = Redecl->getLocation(); |
| 7188 | break; |
| 7189 | } |
| 7190 | } |
| 7191 | } |
| 7192 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7193 | switch (K) { |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7194 | case AR_NotYetIntroduced: { |
| 7195 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 7196 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 7197 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 7198 | // later. |
| 7199 | const AvailabilityAttr *AA = |
| 7200 | getAttrForPlatform(S.getASTContext(), OffendingDecl); |
| 7201 | VersionTuple Introduced = AA->getIntroduced(); |
| 7202 | |
| 7203 | bool UseNewWarning = shouldDiagnoseAvailabilityByDefault( |
| 7204 | S.Context, S.Context.getTargetInfo().getPlatformMinVersion(), |
| 7205 | Introduced); |
| 7206 | unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new |
| 7207 | : diag::warn_unguarded_availability; |
| 7208 | |
| 7209 | S.Diag(Loc, Warning) |
| 7210 | << OffendingDecl |
| 7211 | << AvailabilityAttr::getPrettyPlatformName( |
| 7212 | S.getASTContext().getTargetInfo().getPlatformName()) |
| 7213 | << Introduced.getAsString(); |
| 7214 | |
| 7215 | S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here) |
| 7216 | << OffendingDecl << /* partial */ 3; |
| 7217 | |
| 7218 | if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7219 | if (const auto *TD = dyn_cast<TagDecl>(Enclosing)) |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7220 | if (TD->getDeclName().isEmpty()) { |
| 7221 | S.Diag(TD->getLocation(), |
| 7222 | diag::note_decl_unguarded_availability_silence) |
| 7223 | << /*Anonymous*/ 1 << TD->getKindName(); |
| 7224 | return; |
| 7225 | } |
| 7226 | auto FixitNoteDiag = |
| 7227 | S.Diag(Enclosing->getLocation(), |
| 7228 | diag::note_decl_unguarded_availability_silence) |
| 7229 | << /*Named*/ 0 << Enclosing; |
| 7230 | // Don't offer a fixit for declarations with availability attributes. |
| 7231 | if (Enclosing->hasAttr<AvailabilityAttr>()) |
| 7232 | return; |
| 7233 | if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE")) |
| 7234 | return; |
| 7235 | Optional<AttributeInsertion> Insertion = createAttributeInsertion( |
| 7236 | Enclosing, S.getSourceManager(), S.getLangOpts()); |
| 7237 | if (!Insertion) |
| 7238 | return; |
| 7239 | std::string PlatformName = |
| 7240 | AvailabilityAttr::getPlatformNameSourceSpelling( |
| 7241 | S.getASTContext().getTargetInfo().getPlatformName()) |
| 7242 | .lower(); |
| 7243 | std::string Introduced = |
| 7244 | OffendingDecl->getVersionIntroduced().getAsString(); |
| 7245 | FixitNoteDiag << FixItHint::CreateInsertion( |
| 7246 | Insertion->Loc, |
| 7247 | (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName + |
| 7248 | "(" + Introduced + "))" + Insertion->Suffix) |
| 7249 | .str()); |
| 7250 | } |
| 7251 | return; |
| 7252 | } |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7253 | case AR_Deprecated: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7254 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 7255 | : diag::warn_property_method_deprecated; |
| 7256 | diag_message = diag::warn_deprecated_message; |
| 7257 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 7258 | property_note_select = /* deprecated */ 0; |
| 7259 | available_here_select_kind = /* deprecated */ 2; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7260 | if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 7261 | NoteLocation = AL->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7262 | break; |
| 7263 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7264 | case AR_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7265 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 7266 | : diag::err_property_method_unavailable; |
| 7267 | diag_message = diag::err_unavailable_message; |
| 7268 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 7269 | property_note_select = /* unavailable */ 1; |
| 7270 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7271 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7272 | if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) { |
| 7273 | if (AL->isImplicit() && AL->getImplicitReason()) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7274 | // Most of these failures are due to extra restrictions in ARC; |
| 7275 | // reflect that in the primary diagnostic when applicable. |
| 7276 | auto flagARCError = [&] { |
| 7277 | if (S.getLangOpts().ObjCAutoRefCount && |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7278 | S.getSourceManager().isInSystemHeader( |
| 7279 | OffendingDecl->getLocation())) |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7280 | diag = diag::err_unavailable_in_arc; |
| 7281 | }; |
| 7282 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7283 | switch (AL->getImplicitReason()) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7284 | case UnavailableAttr::IR_None: break; |
| 7285 | |
| 7286 | case UnavailableAttr::IR_ARCForbiddenType: |
| 7287 | flagARCError(); |
| 7288 | diag_available_here = diag::note_arc_forbidden_type; |
| 7289 | break; |
| 7290 | |
| 7291 | case UnavailableAttr::IR_ForbiddenWeak: |
| 7292 | if (S.getLangOpts().ObjCWeakRuntime) |
| 7293 | diag_available_here = diag::note_arc_weak_disabled; |
| 7294 | else |
| 7295 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 7296 | break; |
| 7297 | |
| 7298 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 7299 | flagARCError(); |
| 7300 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 7301 | break; |
| 7302 | |
| 7303 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 7304 | flagARCError(); |
| 7305 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 7306 | break; |
| 7307 | |
| 7308 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 7309 | flagARCError(); |
| 7310 | diag_available_here = diag::note_arc_field_with_ownership; |
| 7311 | break; |
| 7312 | } |
| 7313 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7314 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7315 | break; |
| 7316 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7317 | case AR_Available: |
| 7318 | llvm_unreachable("Warning for availability of available declaration?"); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7319 | } |
| 7320 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7321 | SmallVector<FixItHint, 12> FixIts; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7322 | if (K == AR_Deprecated) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7323 | StringRef Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7324 | if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 7325 | Replacement = AL->getReplacement(); |
| 7326 | if (auto AL = getAttrForPlatform(S.Context, OffendingDecl)) |
| 7327 | Replacement = AL->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7328 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7329 | CharSourceRange UseRange; |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7330 | if (!Replacement.empty()) |
| 7331 | UseRange = |
| 7332 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7333 | if (UseRange.isValid()) { |
| 7334 | if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) { |
| 7335 | Selector Sel = MethodDecl->getSelector(); |
| 7336 | SmallVector<StringRef, 12> SelectorSlotNames; |
| 7337 | Optional<unsigned> NumParams = tryParseObjCMethodName( |
| 7338 | Replacement, SelectorSlotNames, S.getLangOpts()); |
| 7339 | if (NumParams && NumParams.getValue() == Sel.getNumArgs()) { |
| 7340 | assert(SelectorSlotNames.size() == Locs.size()); |
| 7341 | for (unsigned I = 0; I < Locs.size(); ++I) { |
| 7342 | if (!Sel.getNameForSlot(I).empty()) { |
| 7343 | CharSourceRange NameRange = CharSourceRange::getCharRange( |
| 7344 | Locs[I], S.getLocForEndOfToken(Locs[I])); |
| 7345 | FixIts.push_back(FixItHint::CreateReplacement( |
| 7346 | NameRange, SelectorSlotNames[I])); |
| 7347 | } else |
| 7348 | FixIts.push_back( |
| 7349 | FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I])); |
| 7350 | } |
| 7351 | } else |
| 7352 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 7353 | } else |
| 7354 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 7355 | } |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7356 | } |
| 7357 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7358 | if (!Message.empty()) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7359 | S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7360 | if (ObjCProperty) |
| 7361 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7362 | << ObjCProperty->getDeclName() << property_note_select; |
| 7363 | } else if (!UnknownObjCClass) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7364 | S.Diag(Loc, diag) << ReferringDecl << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7365 | if (ObjCProperty) |
| 7366 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7367 | << ObjCProperty->getDeclName() << property_note_select; |
| 7368 | } else { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7369 | S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7370 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 7371 | } |
| 7372 | |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7373 | S.Diag(NoteLocation, diag_available_here) |
| 7374 | << OffendingDecl << available_here_select_kind; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
| 7377 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 7378 | Decl *Ctx) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7379 | assert(DD.Kind == DelayedDiagnostic::Availability && |
| 7380 | "Expected an availability diagnostic here"); |
| 7381 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7382 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 7383 | DoEmitAvailabilityWarning( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7384 | S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(), |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7385 | DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(), |
| 7386 | DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(), |
| 7387 | DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7390 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 7391 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7392 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7393 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7394 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7395 | // When delaying diagnostics to run in the context of a parsed |
| 7396 | // declaration, we only want to actually emit anything if parsing |
| 7397 | // succeeds. |
| 7398 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7399 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7400 | // We emit all the active diagnostics in this pool or any of its |
| 7401 | // parents. In general, we'll get one pool for the decl spec |
| 7402 | // and a child pool for each declarator; in a decl group like: |
| 7403 | // deprecated_typedef foo, *bar, baz(); |
| 7404 | // only the declarator pops will be passed decls. This is correct; |
| 7405 | // we really do need to consider delayed diagnostics from the decl spec |
| 7406 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7407 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7408 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7409 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7410 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 7411 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 7412 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7413 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7414 | continue; |
| 7415 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7416 | switch (diag.Kind) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7417 | case DelayedDiagnostic::Availability: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 7418 | // Don't bother giving deprecation/unavailable diagnostics if |
| 7419 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 7420 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7421 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7422 | break; |
| 7423 | |
| 7424 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7425 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7426 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7427 | |
| 7428 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7429 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7430 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7431 | } |
| 7432 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7433 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7434 | } |
| 7435 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7436 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 7437 | /// been delayed in the current context instead of in the given pool. |
| 7438 | /// Essentially, this just moves them to the current pool. |
| 7439 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 7440 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 7441 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 7442 | curPool->steal(pool); |
| 7443 | } |
| 7444 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7445 | static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR, |
| 7446 | const NamedDecl *ReferringDecl, |
| 7447 | const NamedDecl *OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7448 | StringRef Message, |
| 7449 | ArrayRef<SourceLocation> Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7450 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7451 | const ObjCPropertyDecl *ObjCProperty, |
| 7452 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7453 | // Delay if we're currently parsing a declaration. |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7454 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 7455 | S.DelayedDiagnostics.add( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7456 | DelayedDiagnostic::makeAvailability( |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7457 | AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7458 | ObjCProperty, Message, ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7459 | return; |
| 7460 | } |
| 7461 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7462 | Decl *Ctx = cast<Decl>(S.getCurLexicalContext()); |
| 7463 | DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7464 | Message, Locs, UnknownObjCClass, ObjCProperty, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7465 | ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7466 | } |
Erik Pilkington | 48c7cc9 | 2016-07-29 17:37:38 +0000 | [diff] [blame] | 7467 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7468 | namespace { |
| 7469 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7470 | /// Returns true if the given statement can be a body-like child of \p Parent. |
| 7471 | bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) { |
| 7472 | switch (Parent->getStmtClass()) { |
| 7473 | case Stmt::IfStmtClass: |
| 7474 | return cast<IfStmt>(Parent)->getThen() == S || |
| 7475 | cast<IfStmt>(Parent)->getElse() == S; |
| 7476 | case Stmt::WhileStmtClass: |
| 7477 | return cast<WhileStmt>(Parent)->getBody() == S; |
| 7478 | case Stmt::DoStmtClass: |
| 7479 | return cast<DoStmt>(Parent)->getBody() == S; |
| 7480 | case Stmt::ForStmtClass: |
| 7481 | return cast<ForStmt>(Parent)->getBody() == S; |
| 7482 | case Stmt::CXXForRangeStmtClass: |
| 7483 | return cast<CXXForRangeStmt>(Parent)->getBody() == S; |
| 7484 | case Stmt::ObjCForCollectionStmtClass: |
| 7485 | return cast<ObjCForCollectionStmt>(Parent)->getBody() == S; |
| 7486 | case Stmt::CaseStmtClass: |
| 7487 | case Stmt::DefaultStmtClass: |
| 7488 | return cast<SwitchCase>(Parent)->getSubStmt() == S; |
| 7489 | default: |
| 7490 | return false; |
| 7491 | } |
| 7492 | } |
| 7493 | |
| 7494 | class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> { |
| 7495 | const Stmt *Target; |
| 7496 | |
| 7497 | public: |
| 7498 | bool VisitStmt(Stmt *S) { return S != Target; } |
| 7499 | |
| 7500 | /// Returns true if the given statement is present in the given declaration. |
| 7501 | static bool isContained(const Stmt *Target, const Decl *D) { |
| 7502 | StmtUSEFinder Visitor; |
| 7503 | Visitor.Target = Target; |
| 7504 | return !Visitor.TraverseDecl(const_cast<Decl *>(D)); |
| 7505 | } |
| 7506 | }; |
| 7507 | |
| 7508 | /// Traverses the AST and finds the last statement that used a given |
| 7509 | /// declaration. |
| 7510 | class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> { |
| 7511 | const Decl *D; |
| 7512 | |
| 7513 | public: |
| 7514 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 7515 | if (DRE->getDecl() == D) |
| 7516 | return false; |
| 7517 | return true; |
| 7518 | } |
| 7519 | |
| 7520 | static const Stmt *findLastStmtThatUsesDecl(const Decl *D, |
| 7521 | const CompoundStmt *Scope) { |
| 7522 | LastDeclUSEFinder Visitor; |
| 7523 | Visitor.D = D; |
| 7524 | for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) { |
| 7525 | const Stmt *S = *I; |
| 7526 | if (!Visitor.TraverseStmt(const_cast<Stmt *>(S))) |
| 7527 | return S; |
| 7528 | } |
| 7529 | return nullptr; |
| 7530 | } |
| 7531 | }; |
| 7532 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7533 | /// This class implements -Wunguarded-availability. |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7534 | /// |
| 7535 | /// This is done with a traversal of the AST of a function that makes reference |
| 7536 | /// to a partially available declaration. Whenever we encounter an \c if of the |
| 7537 | /// form: \c if(@available(...)), we use the version from the condition to visit |
| 7538 | /// the then statement. |
| 7539 | class DiagnoseUnguardedAvailability |
| 7540 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 7541 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 7542 | |
| 7543 | Sema &SemaRef; |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7544 | Decl *Ctx; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7545 | |
| 7546 | /// Stack of potentially nested 'if (@available(...))'s. |
| 7547 | SmallVector<VersionTuple, 8> AvailabilityStack; |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7548 | SmallVector<const Stmt *, 16> StmtStack; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7549 | |
| 7550 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range); |
| 7551 | |
| 7552 | public: |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7553 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 7554 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 7555 | AvailabilityStack.push_back( |
| 7556 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7557 | } |
| 7558 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 7559 | bool TraverseDecl(Decl *D) { |
| 7560 | // Avoid visiting nested functions to prevent duplicate warnings. |
| 7561 | if (!D || isa<FunctionDecl>(D)) |
| 7562 | return true; |
| 7563 | return Base::TraverseDecl(D); |
| 7564 | } |
| 7565 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7566 | bool TraverseStmt(Stmt *S) { |
| 7567 | if (!S) |
| 7568 | return true; |
| 7569 | StmtStack.push_back(S); |
| 7570 | bool Result = Base::TraverseStmt(S); |
| 7571 | StmtStack.pop_back(); |
| 7572 | return Result; |
| 7573 | } |
| 7574 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7575 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 7576 | |
| 7577 | bool TraverseIfStmt(IfStmt *If); |
| 7578 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 7579 | bool TraverseLambdaExpr(LambdaExpr *E) { return true; } |
| 7580 | |
Erik Pilkington | ba87c62 | 2017-08-18 20:20:56 +0000 | [diff] [blame] | 7581 | // for 'case X:' statements, don't bother looking at the 'X'; it can't lead |
| 7582 | // to any useful diagnostics. |
| 7583 | bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); } |
| 7584 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 7585 | bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) { |
| 7586 | if (PRE->isClassReceiver()) |
| 7587 | DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation()); |
| 7588 | return true; |
| 7589 | } |
| 7590 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7591 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
| 7592 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) |
| 7593 | DiagnoseDeclAvailability( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7594 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc())); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7595 | return true; |
| 7596 | } |
| 7597 | |
| 7598 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 7599 | DiagnoseDeclAvailability(DRE->getDecl(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7600 | SourceRange(DRE->getBeginLoc(), DRE->getEndLoc())); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7601 | return true; |
| 7602 | } |
| 7603 | |
| 7604 | bool VisitMemberExpr(MemberExpr *ME) { |
| 7605 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7606 | SourceRange(ME->getBeginLoc(), ME->getEndLoc())); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7607 | return true; |
| 7608 | } |
| 7609 | |
Alex Lorenz | 0a484ba | 2017-05-24 15:15:29 +0000 | [diff] [blame] | 7610 | bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7611 | SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use) |
Alex Lorenz | 0a484ba | 2017-05-24 15:15:29 +0000 | [diff] [blame] | 7612 | << (!SemaRef.getLangOpts().ObjC1); |
| 7613 | return true; |
| 7614 | } |
| 7615 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7616 | bool VisitTypeLoc(TypeLoc Ty); |
| 7617 | }; |
| 7618 | |
| 7619 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
| 7620 | NamedDecl *D, SourceRange Range) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7621 | AvailabilityResult Result; |
| 7622 | const NamedDecl *OffendingDecl; |
| 7623 | std::tie(Result, OffendingDecl) = |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7624 | ShouldDiagnoseAvailabilityOfDecl(D, nullptr); |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7625 | if (Result != AR_Available) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7626 | // All other diagnostic kinds have already been handled in |
| 7627 | // DiagnoseAvailabilityOfDecl. |
| 7628 | if (Result != AR_NotYetIntroduced) |
| 7629 | return; |
| 7630 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7631 | const AvailabilityAttr *AA = |
| 7632 | getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7633 | VersionTuple Introduced = AA->getIntroduced(); |
| 7634 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7635 | if (AvailabilityStack.back() >= Introduced) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7636 | return; |
| 7637 | |
| 7638 | // If the context of this function is less available than D, we should not |
| 7639 | // emit a diagnostic. |
| 7640 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx)) |
| 7641 | return; |
| 7642 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7643 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 7644 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 7645 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 7646 | // later. |
| 7647 | unsigned DiagKind = |
| 7648 | shouldDiagnoseAvailabilityByDefault( |
| 7649 | SemaRef.Context, |
| 7650 | SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced) |
| 7651 | ? diag::warn_unguarded_availability_new |
| 7652 | : diag::warn_unguarded_availability; |
| 7653 | |
| 7654 | SemaRef.Diag(Range.getBegin(), DiagKind) |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7655 | << Range << D |
| 7656 | << AvailabilityAttr::getPrettyPlatformName( |
| 7657 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
| 7658 | << Introduced.getAsString(); |
| 7659 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7660 | SemaRef.Diag(OffendingDecl->getLocation(), |
| 7661 | diag::note_availability_specified_here) |
| 7662 | << OffendingDecl << /* partial */ 3; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7663 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7664 | auto FixitDiag = |
| 7665 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 7666 | << Range << D |
| 7667 | << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0 |
| 7668 | : /*__builtin_available*/ 1); |
| 7669 | |
| 7670 | // Find the statement which should be enclosed in the if @available check. |
| 7671 | if (StmtStack.empty()) |
| 7672 | return; |
| 7673 | const Stmt *StmtOfUse = StmtStack.back(); |
| 7674 | const CompoundStmt *Scope = nullptr; |
| 7675 | for (const Stmt *S : llvm::reverse(StmtStack)) { |
| 7676 | if (const auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 7677 | Scope = CS; |
| 7678 | break; |
| 7679 | } |
| 7680 | if (isBodyLikeChildStmt(StmtOfUse, S)) { |
| 7681 | // The declaration won't be seen outside of the statement, so we don't |
| 7682 | // have to wrap the uses of any declared variables in if (@available). |
| 7683 | // Therefore we can avoid setting Scope here. |
| 7684 | break; |
| 7685 | } |
| 7686 | StmtOfUse = S; |
| 7687 | } |
| 7688 | const Stmt *LastStmtOfUse = nullptr; |
| 7689 | if (isa<DeclStmt>(StmtOfUse) && Scope) { |
| 7690 | for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) { |
| 7691 | if (StmtUSEFinder::isContained(StmtStack.back(), D)) { |
| 7692 | LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope); |
| 7693 | break; |
| 7694 | } |
| 7695 | } |
| 7696 | } |
| 7697 | |
| 7698 | const SourceManager &SM = SemaRef.getSourceManager(); |
| 7699 | SourceLocation IfInsertionLoc = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7700 | SM.getExpansionLoc(StmtOfUse->getBeginLoc()); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7701 | SourceLocation StmtEndLoc = |
| 7702 | SM.getExpansionRange( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7703 | (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc()) |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7704 | .getEnd(); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7705 | if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc)) |
| 7706 | return; |
| 7707 | |
| 7708 | StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM); |
| 7709 | const char *ExtraIndentation = " "; |
| 7710 | std::string FixItString; |
| 7711 | llvm::raw_string_ostream FixItOS(FixItString); |
| 7712 | FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available" |
| 7713 | : "__builtin_available") |
Alex Lorenz | e1fb64e | 2017-05-09 15:34:46 +0000 | [diff] [blame] | 7714 | << "(" |
| 7715 | << AvailabilityAttr::getPlatformNameSourceSpelling( |
| 7716 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7717 | << " " << Introduced.getAsString() << ", *)) {\n" |
| 7718 | << Indentation << ExtraIndentation; |
| 7719 | FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str()); |
| 7720 | SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken( |
| 7721 | StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(), |
| 7722 | /*SkipTrailingWhitespaceAndNewLine=*/false); |
| 7723 | if (ElseInsertionLoc.isInvalid()) |
| 7724 | ElseInsertionLoc = |
| 7725 | Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts()); |
| 7726 | FixItOS.str().clear(); |
| 7727 | FixItOS << "\n" |
| 7728 | << Indentation << "} else {\n" |
| 7729 | << Indentation << ExtraIndentation |
| 7730 | << "// Fallback on earlier versions\n" |
| 7731 | << Indentation << "}"; |
| 7732 | FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7733 | } |
| 7734 | } |
| 7735 | |
| 7736 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 7737 | const Type *TyPtr = Ty.getTypePtr(); |
| 7738 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 7739 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 7740 | if (Range.isInvalid()) |
| 7741 | return true; |
| 7742 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7743 | if (const auto *TT = dyn_cast<TagType>(TyPtr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7744 | TagDecl *TD = TT->getDecl(); |
| 7745 | DiagnoseDeclAvailability(TD, Range); |
| 7746 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7747 | } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7748 | TypedefNameDecl *D = TD->getDecl(); |
| 7749 | DiagnoseDeclAvailability(D, Range); |
| 7750 | |
| 7751 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 7752 | if (NamedDecl *D = ObjCO->getInterface()) |
| 7753 | DiagnoseDeclAvailability(D, Range); |
| 7754 | } |
| 7755 | |
| 7756 | return true; |
| 7757 | } |
| 7758 | |
| 7759 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 7760 | VersionTuple CondVersion; |
| 7761 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 7762 | CondVersion = E->getVersion(); |
| 7763 | |
| 7764 | // If we're using the '*' case here or if this check is redundant, then we |
| 7765 | // use the enclosing version to check both branches. |
| 7766 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
Alex Lorenz | 98f9fcd | 2017-08-17 14:22:27 +0000 | [diff] [blame] | 7767 | return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7768 | } else { |
| 7769 | // This isn't an availability checking 'if', we can just continue. |
| 7770 | return Base::TraverseIfStmt(If); |
| 7771 | } |
| 7772 | |
| 7773 | AvailabilityStack.push_back(CondVersion); |
| 7774 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 7775 | AvailabilityStack.pop_back(); |
| 7776 | |
| 7777 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 7778 | } |
| 7779 | |
| 7780 | } // end anonymous namespace |
| 7781 | |
| 7782 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 7783 | Stmt *Body = nullptr; |
| 7784 | |
| 7785 | if (auto *FD = D->getAsFunction()) { |
| 7786 | // FIXME: We only examine the pattern decl for availability violations now, |
| 7787 | // but we should also examine instantiated templates. |
| 7788 | if (FD->isTemplateInstantiation()) |
| 7789 | return; |
| 7790 | |
| 7791 | Body = FD->getBody(); |
| 7792 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7793 | Body = MD->getBody(); |
Alex Lorenz | 28559ce | 2017-04-26 14:20:02 +0000 | [diff] [blame] | 7794 | else if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 7795 | Body = BD->getBody(); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7796 | |
| 7797 | assert(Body && "Need a body here!"); |
| 7798 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7799 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7800 | } |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7801 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7802 | void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D, |
| 7803 | ArrayRef<SourceLocation> Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7804 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7805 | bool ObjCPropertyAccess, |
| 7806 | bool AvoidPartialAvailabilityChecks) { |
| 7807 | std::string Message; |
| 7808 | AvailabilityResult Result; |
| 7809 | const NamedDecl* OffendingDecl; |
| 7810 | // See if this declaration is unavailable, deprecated, or partial. |
| 7811 | std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message); |
| 7812 | if (Result == AR_Available) |
| 7813 | return; |
| 7814 | |
| 7815 | if (Result == AR_NotYetIntroduced) { |
| 7816 | if (AvoidPartialAvailabilityChecks) |
| 7817 | return; |
| 7818 | |
| 7819 | // We need to know the @available context in the current function to |
| 7820 | // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that |
| 7821 | // when we're done parsing the current function. |
| 7822 | if (getCurFunctionOrMethodDecl()) { |
| 7823 | getEnclosingFunction()->HasPotentialAvailabilityViolations = true; |
| 7824 | return; |
| 7825 | } else if (getCurBlock() || getCurLambda()) { |
| 7826 | getCurFunction()->HasPotentialAvailabilityViolations = true; |
| 7827 | return; |
| 7828 | } |
| 7829 | } |
| 7830 | |
| 7831 | const ObjCPropertyDecl *ObjCPDecl = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7832 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7833 | if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { |
| 7834 | AvailabilityResult PDeclResult = PD->getAvailability(nullptr); |
| 7835 | if (PDeclResult == Result) |
| 7836 | ObjCPDecl = PD; |
| 7837 | } |
| 7838 | } |
| 7839 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7840 | EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7841 | UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess); |
| 7842 | } |