Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements decl-related attribute processing. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTMutationListener.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 24 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 29 | #include "clang/Sema/DelayedDiagnostic.h" |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Initialization.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Scope.h" |
Reid Kleckner | 04f9bca | 2018-03-07 22:48:35 +0000 | [diff] [blame] | 33 | #include "clang/Sema/ScopeInfo.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 34 | #include "clang/Sema/SemaInternal.h" |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 40 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 41 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 42 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 43 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 44 | C, |
| 45 | Cpp, |
| 46 | ObjC |
| 47 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 48 | } // end namespace AttributeLangSupport |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // Helper functions |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 54 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 55 | /// type (function or function-typed variable) or an Objective-C |
| 56 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 58 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 59 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 60 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 61 | /// Return true if the given decl has function type (function or |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 62 | /// function-typed variable) or an Objective-C method or a block. |
| 63 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 64 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 65 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 66 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 67 | /// Return true if the given decl has a declarator that should have |
| 68 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 69 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 70 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 71 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 72 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 75 | /// hasFunctionProto - Return true if the given decl has a argument |
| 76 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 77 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 79 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 80 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 81 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 84 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 85 | /// parameters. It is an error to call this on a K&R function (use |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 86 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 87 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 88 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 89 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 90 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 91 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 92 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 95 | static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D, |
| 96 | unsigned Idx) { |
| 97 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 98 | return FD->getParamDecl(Idx); |
| 99 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 100 | return MD->getParamDecl(Idx); |
| 101 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
| 102 | return BD->getParamDecl(Idx); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 106 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 107 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 108 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 109 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 110 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 111 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 112 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 115 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 116 | if (auto *PVD = getFunctionOrMethodParam(D, Idx)) |
| 117 | return PVD->getSourceRange(); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 118 | return SourceRange(); |
| 119 | } |
| 120 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 121 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 122 | if (const FunctionType *FnTy = D->getFunctionType()) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 123 | return FnTy->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 124 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 127 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 128 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 129 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 130 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 131 | return MD->getReturnTypeSourceRange(); |
| 132 | return SourceRange(); |
| 133 | } |
| 134 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 135 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 136 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 137 | return cast<FunctionProtoType>(FnTy)->isVariadic(); |
| 138 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 139 | return BD->isVariadic(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 140 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | static bool isInstanceMethod(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 144 | if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 145 | return MethodDecl->isInstance(); |
| 146 | return false; |
| 147 | } |
| 148 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 149 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 150 | const auto *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 151 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 152 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 153 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 154 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 155 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 156 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 157 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 158 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 160 | // FIXME: Should we walk the chain of classes? |
| 161 | return ClsName == &Ctx.Idents.get("NSString") || |
| 162 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 163 | } |
| 164 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 165 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 166 | const auto *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 167 | if (!PT) |
| 168 | return false; |
| 169 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 170 | const auto *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 171 | if (!RT) |
| 172 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 173 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 174 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 175 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 176 | return false; |
| 177 | |
| 178 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 179 | } |
| 180 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 181 | static unsigned getNumAttributeArgs(const ParsedAttr &AL) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 182 | // FIXME: Include the type in the argument list. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 183 | return AL.getNumArgs() + AL.hasParsedType(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 186 | template <typename Compare> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 187 | static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 188 | unsigned Num, unsigned Diag, |
| 189 | Compare Comp) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 190 | if (Comp(getNumAttributeArgs(AL), Num)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 191 | S.Diag(AL.getLoc(), Diag) << AL << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 198 | /// Check if the attribute has exactly as many args as Num. May |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 199 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 200 | static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 201 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 202 | diag::err_attribute_wrong_number_arguments, |
| 203 | std::not_equal_to<unsigned>()); |
| 204 | } |
| 205 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 206 | /// 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] | 207 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 208 | static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 209 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 210 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 211 | diag::err_attribute_too_few_arguments, |
| 212 | std::less<unsigned>()); |
| 213 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 214 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 215 | /// 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] | 216 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 217 | static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL, |
| 218 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 219 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 220 | diag::err_attribute_too_many_arguments, |
| 221 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 224 | /// A helper function to provide Attribute Location for the Attr types |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 225 | /// AND the ParsedAttr. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 226 | template <typename AttrInfo> |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 227 | static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 228 | SourceLocation>::type |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 229 | getAttrLoc(const AttrInfo &AL) { |
| 230 | return AL.getLocation(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 231 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 232 | static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); } |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 233 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 234 | /// 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] | 235 | /// expression and return success or failure. May output an error. |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 236 | /// |
| 237 | /// Negative argument is implicitly converted to unsigned, unless |
| 238 | /// \p StrictlyUnsigned is true. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 239 | template <typename AttrInfo> |
| 240 | static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 241 | uint32_t &Val, unsigned Idx = UINT_MAX, |
| 242 | bool StrictlyUnsigned = false) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 243 | llvm::APSInt I(32); |
| 244 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 245 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 246 | if (Idx != UINT_MAX) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 247 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 248 | << &AI << Idx << AANT_ArgumentIntegerConstant |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 249 | << Expr->getSourceRange(); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 250 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 251 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type) |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 252 | << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange(); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 255 | |
| 256 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 257 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 258 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 262 | if (StrictlyUnsigned && I.isSigned() && I.isNegative()) { |
Andrew Savonichev | 1a562348 | 2018-09-17 10:39:46 +0000 | [diff] [blame] | 263 | S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer) |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 264 | << &AI << /*non-negative*/ 1; |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 268 | Val = (uint32_t)I.getZExtValue(); |
| 269 | return true; |
| 270 | } |
| 271 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 272 | /// Wrapper around checkUInt32Argument, with an extra check to be sure |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 273 | /// that the result will fit into a regular (signed) int. All args have the same |
| 274 | /// purpose as they do in checkUInt32Argument. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 275 | template <typename AttrInfo> |
| 276 | static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 277 | int &Val, unsigned Idx = UINT_MAX) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 278 | uint32_t UVal; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 279 | if (!checkUInt32Argument(S, AI, Expr, UVal, Idx)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 280 | return false; |
| 281 | |
George Burgess IV | a804957 | 2016-12-22 19:00:31 +0000 | [diff] [blame] | 282 | if (UVal > (uint32_t)std::numeric_limits<int>::max()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 283 | llvm::APSInt I(32); // for toString |
| 284 | I = UVal; |
| 285 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 286 | << I.toString(10, false) << 32 << /* Unsigned */ 0; |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | Val = UVal; |
| 291 | return true; |
| 292 | } |
| 293 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 294 | /// Diagnose mutually exclusive attributes when present on a given |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 295 | /// declaration. Returns true if diagnosed. |
| 296 | template <typename AttrTy> |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 297 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 298 | if (const auto *A = D->getAttr<AttrTy>()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 299 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A; |
| 300 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
| 301 | return true; |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | template <typename AttrTy> |
| 307 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) { |
| 308 | if (const auto *A = D->getAttr<AttrTy>()) { |
| 309 | S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 310 | << A; |
| 311 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 317 | /// Check if IdxExpr is a valid parameter index for a function or |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 318 | /// instance method D. May output an error. |
| 319 | /// |
| 320 | /// \returns true if IdxExpr is a valid index. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 321 | template <typename AttrInfo> |
| 322 | static bool checkFunctionOrMethodParameterIndex( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 323 | Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 324 | const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 325 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 326 | |
| 327 | // In C++ the implicit 'this' function parameter also counts. |
| 328 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 329 | bool HP = hasFunctionProto(D); |
| 330 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 331 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 332 | unsigned NumParams = |
| 333 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 334 | |
| 335 | llvm::APSInt IdxInt; |
| 336 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 337 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 338 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 339 | << &AI << AttrArgNum << AANT_ArgumentIntegerConstant |
| 340 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 344 | unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX); |
| 345 | if (IdxSource < 1 || (!IV && IdxSource > NumParams)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 346 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 347 | << &AI << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 348 | return false; |
| 349 | } |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 350 | if (HasImplicitThisParam && !CanIndexImplicitThis) { |
| 351 | if (IdxSource == 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 352 | S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument) |
| 353 | << &AI << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 354 | return false; |
| 355 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 358 | Idx = ParamIdx(IdxSource, D); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 362 | /// 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] | 363 | /// If not emit an error and return false. If the argument is an identifier it |
| 364 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 365 | /// literal. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 366 | bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum, |
| 367 | StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 368 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 369 | // 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] | 370 | if (AL.isArgIdent(ArgNum)) { |
| 371 | IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 372 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 373 | << AL << AANT_ArgumentString |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 374 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 375 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 376 | Str = Loc->Ident->getName(); |
| 377 | if (ArgLocation) |
| 378 | *ArgLocation = Loc->Loc; |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | // Now check for an actual string literal. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 383 | Expr *ArgExpr = AL.getArgAsExpr(ArgNum); |
| 384 | const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 385 | if (ArgLocation) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 386 | *ArgLocation = ArgExpr->getBeginLoc(); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 387 | |
| 388 | if (!Literal || !Literal->isAscii()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 389 | Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 390 | << AL << AANT_ArgumentString; |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 391 | return false; |
| 392 | } |
| 393 | |
| 394 | Str = Literal->getString(); |
| 395 | return true; |
| 396 | } |
| 397 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 398 | /// Applies the given attribute to the Decl without performing any |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 399 | /// additional semantic checking. |
| 400 | template <typename AttrType> |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 401 | static void handleSimpleAttribute(Sema &S, Decl *D, SourceRange SR, |
| 402 | unsigned SpellingIndex) { |
| 403 | D->addAttr(::new (S.Context) AttrType(SR, S.Context, SpellingIndex)); |
| 404 | } |
| 405 | |
| 406 | template <typename AttrType> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 407 | static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) { |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 408 | handleSimpleAttribute<AttrType>(S, D, AL.getRange(), |
| 409 | AL.getAttributeSpellingListIndex()); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | template <typename... DiagnosticArgs> |
| 414 | static const Sema::SemaDiagnosticBuilder& |
| 415 | appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) { |
| 416 | return Bldr; |
| 417 | } |
| 418 | |
| 419 | template <typename T, typename... DiagnosticArgs> |
| 420 | static const Sema::SemaDiagnosticBuilder& |
| 421 | appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg, |
| 422 | DiagnosticArgs &&... ExtraArgs) { |
| 423 | return appendDiagnostics(Bldr << std::forward<T>(ExtraArg), |
| 424 | std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 425 | } |
| 426 | |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 427 | /// Add an attribute {@code AttrType} to declaration {@code D}, provided that |
| 428 | /// {@code PassesCheck} is true. |
| 429 | /// Otherwise, emit diagnostic {@code DiagID}, passing in all parameters |
| 430 | /// specified in {@code ExtraArgs}. |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 431 | template <typename AttrType, typename... DiagnosticArgs> |
| 432 | static void |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 433 | handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, SourceRange SR, |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 434 | unsigned SpellingIndex, |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 435 | bool PassesCheck, |
| 436 | unsigned DiagID, DiagnosticArgs&&... ExtraArgs) { |
| 437 | if (!PassesCheck) { |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 438 | Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID); |
| 439 | appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 440 | return; |
| 441 | } |
| 442 | handleSimpleAttribute<AttrType>(S, D, SR, SpellingIndex); |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 443 | } |
| 444 | |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 445 | template <typename AttrType, typename... DiagnosticArgs> |
| 446 | static void |
| 447 | handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, const ParsedAttr &AL, |
| 448 | bool PassesCheck, |
| 449 | unsigned DiagID, |
| 450 | DiagnosticArgs&&... ExtraArgs) { |
| 451 | return handleSimpleAttributeOrDiagnose<AttrType>( |
| 452 | S, D, AL.getRange(), AL.getAttributeSpellingListIndex(), PassesCheck, |
| 453 | DiagID, std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 454 | } |
| 455 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 456 | template <typename AttrType> |
| 457 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 458 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 459 | handleSimpleAttribute<AttrType>(S, D, AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 462 | /// 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] | 463 | /// already have one of the given incompatible attributes. |
| 464 | template <typename AttrType, typename IncompatibleAttrType, |
| 465 | typename... IncompatibleAttrTypes> |
| 466 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 467 | const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 468 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL)) |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 469 | return; |
| 470 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 471 | AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 474 | /// Check if the passed-in expression is of type int or bool. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | static bool isIntOrBool(Expr *Exp) { |
| 476 | QualType QT = Exp->getType(); |
| 477 | return QT->isBooleanType() || QT->isIntegerType(); |
| 478 | } |
| 479 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 480 | |
| 481 | // Check to see if the type is a smart pointer of some kind. We assume |
| 482 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 483 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 484 | auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record, |
| 485 | OverloadedOperatorKind Op) { |
| 486 | DeclContextLookupResult Result = |
| 487 | Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op)); |
| 488 | return !Result.empty(); |
| 489 | }; |
| 490 | |
| 491 | const RecordDecl *Record = RT->getDecl(); |
| 492 | bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star); |
| 493 | bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow); |
| 494 | if (foundStarOperator && foundArrowOperator) |
| 495 | return true; |
| 496 | |
| 497 | const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record); |
| 498 | if (!CXXRecord) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 499 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 500 | |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 501 | for (auto BaseSpecifier : CXXRecord->bases()) { |
| 502 | if (!foundStarOperator) |
| 503 | foundStarOperator = IsOverloadedOperatorPresent( |
| 504 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Star); |
| 505 | if (!foundArrowOperator) |
| 506 | foundArrowOperator = IsOverloadedOperatorPresent( |
| 507 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow); |
| 508 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 509 | |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 510 | if (foundStarOperator && foundArrowOperator) |
| 511 | return true; |
| 512 | |
| 513 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 516 | /// Check if passed in Decl is a pointer type. |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 517 | /// Note that this function may produce an error message. |
| 518 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 519 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 520 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 521 | const auto *VD = cast<ValueDecl>(D); |
| 522 | QualType QT = VD->getType(); |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 523 | if (QT->isAnyPointerType()) |
| 524 | return true; |
| 525 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 526 | if (const auto *RT = QT->getAs<RecordType>()) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 527 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 528 | // (We don't want to force template instantiation if we can avoid it, |
| 529 | // since that would alter the order in which templates are instantiated.) |
| 530 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 531 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 532 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 533 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 534 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 535 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 536 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 537 | 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] | 538 | return false; |
| 539 | } |
| 540 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 541 | /// Checks that the passed in QualType either is of RecordType or points |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 542 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 543 | static const RecordType *getRecordType(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 544 | if (const auto *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 545 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 546 | |
| 547 | // Now check if we point to record type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 548 | if (const auto *PT = QT->getAs<PointerType>()) |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 549 | return PT->getPointeeType()->getAs<RecordType>(); |
| 550 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 551 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 554 | template <typename AttrType> |
| 555 | static bool checkRecordDeclForAttr(const RecordDecl *RD) { |
| 556 | // Check if the record itself has the attribute. |
| 557 | if (RD->hasAttr<AttrType>()) |
| 558 | return true; |
| 559 | |
| 560 | // Else check if any base classes have the attribute. |
| 561 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 562 | CXXBasePaths BPaths(false, false); |
| 563 | if (CRD->lookupInBases( |
| 564 | [](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 565 | const auto &Ty = *BS->getType(); |
| 566 | // If it's type-dependent, we assume it could have the attribute. |
| 567 | if (Ty.isDependentType()) |
| 568 | return true; |
| 569 | return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>(); |
| 570 | }, |
| 571 | BPaths, true)) |
| 572 | return true; |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 577 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 578 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 579 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 580 | if (!RT) |
| 581 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 582 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 583 | // 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] | 584 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 585 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 586 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 587 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 588 | // FIXME -- Check the type that the smart pointer points to. |
| 589 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 590 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 591 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 592 | return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl()); |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 595 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 596 | const auto *TD = Ty->getAs<TypedefType>(); |
| 597 | if (!TD) |
| 598 | return false; |
| 599 | |
| 600 | TypedefNameDecl *TN = TD->getDecl(); |
| 601 | if (!TN) |
| 602 | return false; |
| 603 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 604 | return TN->hasAttr<CapabilityAttr>(); |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 607 | static bool typeHasCapability(Sema &S, QualType Ty) { |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 608 | if (checkTypedefTypeForCapability(Ty)) |
| 609 | return true; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 610 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 611 | if (checkRecordTypeForCapability(S, Ty)) |
| 612 | return true; |
| 613 | |
| 614 | return false; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 617 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 618 | // Capability expressions are simple expressions involving the boolean logic |
| 619 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 620 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 621 | // is a capability or not. |
| 622 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 623 | if (const auto *E = dyn_cast<CastExpr>(Ex)) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 624 | return isCapabilityExpr(S, E->getSubExpr()); |
| 625 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 626 | return isCapabilityExpr(S, E->getSubExpr()); |
| 627 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 628 | if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf || |
| 629 | E->getOpcode() == UO_Deref) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 630 | return isCapabilityExpr(S, E->getSubExpr()); |
| 631 | return false; |
| 632 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 633 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 634 | return isCapabilityExpr(S, E->getLHS()) && |
| 635 | isCapabilityExpr(S, E->getRHS()); |
| 636 | return false; |
| 637 | } |
| 638 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 639 | return typeHasCapability(S, Ex->getType()); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 642 | /// Checks that all attribute arguments, starting from Sidx, resolve to |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 643 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 644 | /// \param Sidx The attribute argument index to start checking with. |
| 645 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 646 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 647 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 648 | const ParsedAttr &AL, |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 649 | SmallVectorImpl<Expr *> &Args, |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 650 | unsigned Sidx = 0, |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 651 | bool ParamIdxOk = false) { |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 652 | if (Sidx == AL.getNumArgs()) { |
| 653 | // If we don't have any capability arguments, the attribute implicitly |
| 654 | // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're |
| 655 | // a non-static method, and that the class is a (scoped) capability. |
| 656 | const auto *MD = dyn_cast<const CXXMethodDecl>(D); |
| 657 | if (MD && !MD->isStatic()) { |
| 658 | const CXXRecordDecl *RD = MD->getParent(); |
| 659 | // FIXME -- need to check this again on template instantiation |
| 660 | if (!checkRecordDeclForAttr<CapabilityAttr>(RD) && |
| 661 | !checkRecordDeclForAttr<ScopedLockableAttr>(RD)) |
| 662 | S.Diag(AL.getLoc(), |
| 663 | diag::warn_thread_attribute_not_on_capability_member) |
| 664 | << AL << MD->getParent(); |
| 665 | } else { |
| 666 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member) |
| 667 | << AL; |
| 668 | } |
| 669 | } |
| 670 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 671 | for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) { |
| 672 | Expr *ArgExp = AL.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 673 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 674 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 675 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 676 | Args.push_back(ArgExp); |
| 677 | continue; |
| 678 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 679 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 680 | if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 681 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 682 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 683 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 684 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 685 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 686 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 687 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 688 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 689 | // We allow constant strings to be used as a placeholder for expressions |
| 690 | // that are not valid C++ syntax, but warn that they are ignored. |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 691 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 692 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 693 | continue; |
| 694 | } |
| 695 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 696 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 697 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 698 | // A pointer to member expression of the form &MyClass::mu is treated |
| 699 | // specially -- we need to look at the type of the member. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 700 | if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 701 | if (UOp->getOpcode() == UO_AddrOf) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 702 | if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 703 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 704 | ArgTy = DRE->getDecl()->getType(); |
| 705 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 706 | // 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] | 707 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 708 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 709 | // Now check if we index into a record type function param. |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 710 | if(!RT && ParamIdxOk) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 711 | const auto *FD = dyn_cast<FunctionDecl>(D); |
| 712 | const auto *IL = dyn_cast<IntegerLiteral>(ArgExp); |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 713 | if(FD && IL) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 714 | unsigned int NumParams = FD->getNumParams(); |
| 715 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 716 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 717 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 718 | if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Aaron Ballman | ce667f6 | 2019-02-12 13:19:02 +0000 | [diff] [blame] | 719 | S.Diag(AL.getLoc(), |
| 720 | diag::err_attribute_argument_out_of_bounds_extra_info) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 721 | << AL << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 722 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 723 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 724 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 728 | // If the type does not have a capability, see if the components of the |
| 729 | // expression have capabilities. This allows for writing C code where the |
| 730 | // capability may be on the type, and the expression is a capability |
| 731 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 732 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 733 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 734 | << AL << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 735 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 736 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 737 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 740 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 741 | // Attribute Implementations |
| 742 | //===----------------------------------------------------------------------===// |
| 743 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 744 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 745 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 746 | return; |
| 747 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 748 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 749 | PtGuardedVarAttr(AL.getRange(), S.Context, |
| 750 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 753 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 754 | Expr *&Arg) { |
| 755 | SmallVector<Expr *, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 756 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 757 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 758 | unsigned Size = Args.size(); |
| 759 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 760 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 761 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 762 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 763 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 764 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 767 | static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 768 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 769 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 770 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 771 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 772 | D->addAttr(::new (S.Context) GuardedByAttr( |
| 773 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 776 | static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 777 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 778 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 779 | return; |
| 780 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 781 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 782 | return; |
| 783 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 784 | D->addAttr(::new (S.Context) PtGuardedByAttr( |
| 785 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 788 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 789 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 790 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 791 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 792 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 793 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 794 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 795 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 796 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL; |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 797 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 798 | } |
| 799 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 800 | // Check that all arguments are lockable objects. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 801 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 802 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 803 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 804 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 805 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 808 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 809 | SmallVector<Expr *, 1> Args; |
| 810 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 811 | return; |
| 812 | |
| 813 | Expr **StartArg = &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 814 | D->addAttr(::new (S.Context) AcquiredAfterAttr( |
| 815 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 816 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 819 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 820 | SmallVector<Expr *, 1> Args; |
| 821 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 822 | return; |
| 823 | |
| 824 | Expr **StartArg = &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 825 | D->addAttr(::new (S.Context) AcquiredBeforeAttr( |
| 826 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 827 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 830 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 831 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 832 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 833 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 834 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 835 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 836 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 839 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 840 | SmallVector<Expr *, 1> Args; |
| 841 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 842 | return; |
| 843 | |
| 844 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 845 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 846 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 847 | AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size, |
| 848 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 852 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 853 | SmallVector<Expr *, 1> Args; |
| 854 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 855 | return; |
| 856 | |
| 857 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 858 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 859 | D->addAttr(::new (S.Context) AssertExclusiveLockAttr( |
| 860 | AL.getRange(), S.Context, StartArg, Size, |
| 861 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 864 | /// 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] | 865 | /// is an integral type. Will emit appropriate diagnostics if this returns |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 866 | /// false. |
| 867 | /// |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 868 | /// 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] | 869 | template <typename AttrInfo> |
| 870 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 871 | const AttrInfo &AI, unsigned AttrArgNo) { |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 872 | assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument"); |
| 873 | Expr *AttrArg = AI.getArgAsExpr(AttrArgNo); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 874 | ParamIdx Idx; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 875 | if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 876 | Idx)) |
| 877 | return false; |
| 878 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 879 | const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 880 | if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 881 | SourceLocation SrcLoc = AttrArg->getBeginLoc(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 882 | S.Diag(SrcLoc, diag::err_attribute_integers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 883 | << AI << Param->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 884 | return false; |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 889 | static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 890 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 891 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 892 | return; |
| 893 | |
| 894 | const auto *FD = cast<FunctionDecl>(D); |
| 895 | if (!FD->getReturnType()->isPointerType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 896 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 897 | return; |
| 898 | } |
| 899 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 900 | const Expr *SizeExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 901 | int SizeArgNoVal; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 902 | // Parameter indices are 1-indexed, hence Index=1 |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 903 | if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 904 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 905 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 906 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 907 | ParamIdx SizeArgNo(SizeArgNoVal, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 908 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 909 | ParamIdx NumberArgNo; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 910 | if (AL.getNumArgs() == 2) { |
| 911 | const Expr *NumberExpr = AL.getArgAsExpr(1); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 912 | int Val; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 913 | // Parameter indices are 1-based, hence Index=2 |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 914 | if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 915 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 916 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 917 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 918 | NumberArgNo = ParamIdx(Val, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 921 | D->addAttr(::new (S.Context) |
| 922 | AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo, |
| 923 | AL.getAttributeSpellingListIndex())); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 924 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 925 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 926 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 927 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 928 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 929 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 930 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 931 | if (!isIntOrBool(AL.getArgAsExpr(0))) { |
| 932 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 933 | << AL << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 934 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 938 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 939 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 940 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 943 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 944 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 945 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 946 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 947 | return; |
| 948 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 949 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr( |
| 950 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(), |
| 951 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 954 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 955 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 956 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 957 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 958 | return; |
| 959 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 960 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 961 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), |
| 962 | Args.size(), AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 965 | static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 966 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 967 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 968 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 969 | unsigned Size = Args.size(); |
| 970 | if (Size == 0) |
| 971 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 972 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 973 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 974 | LockReturnedAttr(AL.getRange(), S.Context, Args[0], |
| 975 | AL.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 978 | static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 979 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 980 | return; |
| 981 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 982 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 983 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 984 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 985 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 986 | if (Size == 0) |
| 987 | return; |
| 988 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 989 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 990 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 991 | LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size, |
| 992 | AL.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 995 | static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 996 | Expr *&Cond, StringRef &Msg) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 997 | Cond = AL.getArgAsExpr(0); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 998 | if (!Cond->isTypeDependent()) { |
| 999 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 1000 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1001 | return false; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1002 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1005 | if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1006 | return false; |
| 1007 | |
| 1008 | if (Msg.empty()) |
| 1009 | Msg = "<no message provided>"; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1010 | |
| 1011 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1012 | if (isa<FunctionDecl>(D) && !Cond->isValueDependent() && |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1013 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 1014 | Diags)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1015 | 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] | 1016 | for (const PartialDiagnosticAt &PDiag : Diags) |
| 1017 | S.Diag(PDiag.first, PDiag.second); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1018 | return false; |
| 1019 | } |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1023 | static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1024 | S.Diag(AL.getLoc(), diag::ext_clang_enable_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1025 | |
| 1026 | Expr *Cond; |
| 1027 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1028 | if (checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1029 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1030 | EnableIfAttr(AL.getRange(), S.Context, Cond, Msg, |
| 1031 | AL.getAttributeSpellingListIndex())); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | namespace { |
| 1035 | /// Determines if a given Expr references any of the given function's |
| 1036 | /// ParmVarDecls, or the function's implicit `this` parameter (if applicable). |
| 1037 | class ArgumentDependenceChecker |
| 1038 | : public RecursiveASTVisitor<ArgumentDependenceChecker> { |
| 1039 | #ifndef NDEBUG |
| 1040 | const CXXRecordDecl *ClassType; |
| 1041 | #endif |
| 1042 | llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms; |
| 1043 | bool Result; |
| 1044 | |
| 1045 | public: |
| 1046 | ArgumentDependenceChecker(const FunctionDecl *FD) { |
| 1047 | #ifndef NDEBUG |
| 1048 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1049 | ClassType = MD->getParent(); |
| 1050 | else |
| 1051 | ClassType = nullptr; |
| 1052 | #endif |
| 1053 | Parms.insert(FD->param_begin(), FD->param_end()); |
| 1054 | } |
| 1055 | |
| 1056 | bool referencesArgs(Expr *E) { |
| 1057 | Result = false; |
| 1058 | TraverseStmt(E); |
| 1059 | return Result; |
| 1060 | } |
| 1061 | |
| 1062 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 1063 | assert(E->getType()->getPointeeCXXRecordDecl() == ClassType && |
| 1064 | "`this` doesn't refer to the enclosing class?"); |
| 1065 | Result = true; |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 1070 | if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 1071 | if (Parms.count(PVD)) { |
| 1072 | Result = true; |
| 1073 | return false; |
| 1074 | } |
| 1075 | return true; |
| 1076 | } |
| 1077 | }; |
| 1078 | } |
| 1079 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1080 | static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1081 | S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1082 | |
| 1083 | Expr *Cond; |
| 1084 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1085 | if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1086 | return; |
| 1087 | |
| 1088 | StringRef DiagTypeStr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1089 | if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1090 | return; |
| 1091 | |
| 1092 | DiagnoseIfAttr::DiagnosticType DiagType; |
| 1093 | if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1094 | S.Diag(AL.getArgAsExpr(2)->getBeginLoc(), |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1095 | diag::err_diagnose_if_invalid_diagnostic_type); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1099 | bool ArgDependent = false; |
Argyrios Kyrtzidis | 5f0c0aa | 2017-05-24 18:35:01 +0000 | [diff] [blame] | 1100 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1101 | ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1102 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1103 | AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent, |
| 1104 | cast<NamedDecl>(D), AL.getAttributeSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1107 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1108 | if (D->hasAttr<PassObjectSizeAttr>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1109 | 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] | 1110 | return; |
| 1111 | } |
| 1112 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1113 | Expr *E = AL.getArgAsExpr(0); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1114 | uint32_t Type; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1115 | if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1)) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1116 | return; |
| 1117 | |
| 1118 | // pass_object_size's argument is passed in as the second argument of |
| 1119 | // __builtin_object_size. So, it has the same constraints as that second |
| 1120 | // argument; namely, it must be in the range [0, 3]. |
| 1121 | if (Type > 3) { |
Aaron Ballman | 52c9ad2 | 2019-02-12 13:04:11 +0000 | [diff] [blame] | 1122 | S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1123 | << AL << 0 << 3 << E->getSourceRange(); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | // pass_object_size is only supported on constant pointer parameters; as a |
| 1128 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 1129 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 1130 | // definition, so we defer the constness check until later. |
| 1131 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1132 | S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1136 | D->addAttr(::new (S.Context) PassObjectSizeAttr( |
| 1137 | AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex())); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1140 | static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1141 | ConsumableAttr::ConsumedState DefaultState; |
| 1142 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1143 | if (AL.isArgIdent(0)) { |
| 1144 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1145 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1146 | DefaultState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1147 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1148 | << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1149 | return; |
| 1150 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1151 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1152 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1153 | << AL << AANT_ArgumentIdentifier; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1154 | return; |
| 1155 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1156 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1157 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1158 | ConsumableAttr(AL.getRange(), S.Context, DefaultState, |
| 1159 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1163 | const ParsedAttr &AL) { |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 1164 | QualType ThisType = MD->getThisType()->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1165 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1166 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1167 | if (!RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1168 | S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1169 | RD->getNameAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1170 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
| 1173 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1174 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1175 | return true; |
| 1176 | } |
| 1177 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1178 | static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1179 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1180 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1181 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1182 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1183 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1184 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1185 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1186 | for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1187 | CallableWhenAttr::ConsumedState CallableState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1188 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1189 | StringRef StateString; |
| 1190 | SourceLocation Loc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1191 | if (AL.isArgIdent(ArgIndex)) { |
| 1192 | IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex); |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1193 | StateString = Ident->Ident->getName(); |
| 1194 | Loc = Ident->Loc; |
| 1195 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1196 | if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc)) |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1197 | return; |
| 1198 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1201 | CallableState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1202 | S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1205 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1206 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1207 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1208 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1209 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1210 | CallableWhenAttr(AL.getRange(), S.Context, States.data(), |
| 1211 | States.size(), AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1214 | static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1215 | ParamTypestateAttr::ConsumedState ParamState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1216 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1217 | if (AL.isArgIdent(0)) { |
| 1218 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1219 | StringRef StateString = Ident->Ident->getName(); |
| 1220 | |
| 1221 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1222 | ParamState)) { |
| 1223 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1224 | << AL << StateString; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1225 | return; |
| 1226 | } |
| 1227 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1228 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1229 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1230 | return; |
| 1231 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1232 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1233 | // FIXME: This check is currently being done in the analysis. It can be |
| 1234 | // enabled here only after the parser propagates attributes at |
| 1235 | // template specialization definition, not declaration. |
| 1236 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1237 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1238 | // |
| 1239 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1240 | // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1241 | // ReturnType.getAsString(); |
| 1242 | // return; |
| 1243 | //} |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1244 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1245 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1246 | ParamTypestateAttr(AL.getRange(), S.Context, ParamState, |
| 1247 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1250 | static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1251 | ReturnTypestateAttr::ConsumedState ReturnState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1252 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1253 | if (AL.isArgIdent(0)) { |
| 1254 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1255 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1256 | ReturnState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1257 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1258 | << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1259 | return; |
| 1260 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1261 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1262 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1263 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1264 | return; |
| 1265 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1266 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1267 | // FIXME: This check is currently being done in the analysis. It can be |
| 1268 | // enabled here only after the parser propagates attributes at |
| 1269 | // template specialization definition, not declaration. |
| 1270 | //QualType ReturnType; |
| 1271 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1272 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1273 | // ReturnType = Param->getType(); |
| 1274 | // |
| 1275 | //} else if (const CXXConstructorDecl *Constructor = |
| 1276 | // dyn_cast<CXXConstructorDecl>(D)) { |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 1277 | // ReturnType = Constructor->getThisType()->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1278 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1279 | //} else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1280 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1281 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1282 | //} |
| 1283 | // |
| 1284 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1285 | // |
| 1286 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1287 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1288 | // ReturnType.getAsString(); |
| 1289 | // return; |
| 1290 | //} |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1291 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1292 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1293 | ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState, |
| 1294 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1297 | static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1298 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1299 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1300 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1301 | SetTypestateAttr::ConsumedState NewState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1302 | if (AL.isArgIdent(0)) { |
| 1303 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1304 | StringRef Param = Ident->Ident->getName(); |
| 1305 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1306 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1307 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1308 | return; |
| 1309 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1310 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1311 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1312 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1315 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1316 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1317 | SetTypestateAttr(AL.getRange(), S.Context, NewState, |
| 1318 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1321 | static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1322 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1323 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1324 | |
| 1325 | TestTypestateAttr::ConsumedState TestState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1326 | if (AL.isArgIdent(0)) { |
| 1327 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1328 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1329 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1330 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1331 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1334 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1335 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1336 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1337 | return; |
| 1338 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1339 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1340 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1341 | TestTypestateAttr(AL.getRange(), S.Context, TestState, |
| 1342 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1345 | static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1346 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1347 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1350 | static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1351 | if (auto *TD = dyn_cast<TagDecl>(D)) |
| 1352 | TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context, |
| 1353 | AL.getAttributeSpellingListIndex())); |
| 1354 | else if (auto *FD = dyn_cast<FieldDecl>(D)) { |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1355 | bool BitfieldByteAligned = (!FD->getType()->isDependentType() && |
| 1356 | !FD->getType()->isIncompleteType() && |
| 1357 | FD->isBitField() && |
| 1358 | S.Context.getTypeAlign(FD->getType()) <= 8); |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1359 | |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1360 | if (S.getASTContext().getTargetInfo().getTriple().isPS4()) { |
| 1361 | if (BitfieldByteAligned) |
| 1362 | // The PS4 target needs to maintain ABI backwards compatibility. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1363 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1364 | << AL << FD->getType(); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1365 | else |
| 1366 | FD->addAttr(::new (S.Context) PackedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1367 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1368 | } else { |
| 1369 | // Report warning about changed offset in the newer compiler versions. |
| 1370 | if (BitfieldByteAligned) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1371 | S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1372 | |
| 1373 | FD->addAttr(::new (S.Context) PackedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1374 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1377 | } else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1378 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1381 | static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1382 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1383 | // variables or properties of Objective-C classes. The outlet must also |
| 1384 | // have an object reference type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1385 | if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1386 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1387 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1388 | << AL << VD->getType() << 0; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1389 | return false; |
| 1390 | } |
| 1391 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1392 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1393 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1394 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1395 | << AL << PD->getType() << 1; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1396 | return false; |
| 1397 | } |
| 1398 | } |
| 1399 | else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1400 | S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1401 | return false; |
| 1402 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1403 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1404 | return true; |
| 1405 | } |
| 1406 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1407 | static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1408 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1409 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1410 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1411 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1412 | IBOutletAttr(AL.getRange(), S.Context, |
| 1413 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1416 | static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1417 | |
| 1418 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1419 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1420 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1421 | return; |
| 1422 | } |
| 1423 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1424 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1425 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1426 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1427 | ParsedType PT; |
| 1428 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1429 | if (AL.hasParsedType()) |
| 1430 | PT = AL.getTypeArg(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1431 | else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1432 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1433 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1434 | if (!PT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1435 | S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1436 | return; |
| 1437 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1438 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1439 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1440 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1441 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1442 | if (!QTLoc) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1443 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1444 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1445 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1446 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1447 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1448 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1449 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1450 | S.Diag(AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1451 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1452 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1453 | return; |
| 1454 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1455 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1456 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1457 | IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc, |
| 1458 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1461 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1462 | if (RefOkay) { |
| 1463 | if (T->isReferenceType()) |
| 1464 | return true; |
| 1465 | } else { |
| 1466 | T = T.getNonReferenceType(); |
| 1467 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1468 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1469 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1470 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1471 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1472 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1473 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1474 | for (const auto *I : UD->fields()) { |
| 1475 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1476 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1477 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1478 | } |
| 1479 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1485 | static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1486 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1487 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1488 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1489 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1490 | if (isReturnValue) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1491 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1492 | << AL << AttrParmRange << TypeRange; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1493 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1494 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1495 | << AL << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
| 1498 | return true; |
| 1499 | } |
| 1500 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1501 | static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1502 | SmallVector<ParamIdx, 8> NonNullArgs; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1503 | for (unsigned I = 0; I < AL.getNumArgs(); ++I) { |
| 1504 | Expr *Ex = AL.getArgAsExpr(I); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1505 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1506 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1507 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1508 | |
| 1509 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1510 | if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) && |
| 1511 | !attrNonNullArgCheck( |
| 1512 | S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL, |
| 1513 | Ex->getSourceRange(), |
| 1514 | getFunctionOrMethodParamRange(D, Idx.getASTIndex()))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1515 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1516 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1517 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1518 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1519 | |
| 1520 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1521 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1522 | // check if the attribute came from a macro expansion or a template |
| 1523 | // instantiation. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1524 | if (NonNullArgs.empty() && AL.getLoc().isFileID() && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1525 | !S.inTemplateInstantiation()) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1526 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1527 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1528 | I != E && !AnyPointers; ++I) { |
| 1529 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1530 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1531 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1532 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1533 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1534 | if (!AnyPointers) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1535 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1536 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1537 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1538 | ParamIdx *Start = NonNullArgs.data(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1539 | unsigned Size = NonNullArgs.size(); |
| 1540 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1541 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1542 | NonNullAttr(AL.getRange(), S.Context, Start, Size, |
| 1543 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1546 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1547 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1548 | if (AL.getNumArgs() > 0) { |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1549 | if (D->getFunctionType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1550 | handleNonNullAttr(S, D, AL); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1551 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1552 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1553 | << D->getSourceRange(); |
| 1554 | } |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | // Is the argument a pointer type? |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1559 | if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(), |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1560 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1561 | return; |
| 1562 | |
| 1563 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1564 | NonNullAttr(AL.getRange(), S.Context, nullptr, 0, |
| 1565 | AL.getAttributeSpellingListIndex())); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1568 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1569 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1570 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1571 | if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1572 | /* isReturnValue */ true)) |
| 1573 | return; |
| 1574 | |
| 1575 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1576 | ReturnsNonNullAttr(AL.getRange(), S.Context, |
| 1577 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1580 | static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1581 | if (D->isInvalidDecl()) |
| 1582 | return; |
| 1583 | |
| 1584 | // noescape only applies to pointer types. |
| 1585 | QualType T = cast<ParmVarDecl>(D)->getType(); |
| 1586 | if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1587 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1588 | << AL << AL.getRange() << 0; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
| 1592 | D->addAttr(::new (S.Context) NoEscapeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1593 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1596 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1597 | Expr *E = AL.getArgAsExpr(0), |
| 1598 | *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr; |
| 1599 | S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE, |
| 1600 | AL.getAttributeSpellingListIndex()); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1603 | static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1604 | S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 1605 | AL.getAttributeSpellingListIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1608 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1609 | Expr *OE, unsigned SpellingListIndex) { |
| 1610 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1611 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1612 | |
| 1613 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1614 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1615 | |
| 1616 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1617 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1618 | << &TmpAttr << AttrRange << SR; |
| 1619 | return; |
| 1620 | } |
| 1621 | |
| 1622 | if (!E->isValueDependent()) { |
| 1623 | llvm::APSInt I(64); |
| 1624 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1625 | if (OE) |
| 1626 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1627 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1628 | << E->getSourceRange(); |
| 1629 | else |
| 1630 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1631 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1632 | << E->getSourceRange(); |
| 1633 | return; |
| 1634 | } |
| 1635 | |
| 1636 | if (!I.isPowerOf2()) { |
| 1637 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1638 | << E->getSourceRange(); |
| 1639 | return; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if (OE) { |
| 1644 | if (!OE->isValueDependent()) { |
| 1645 | llvm::APSInt I(64); |
| 1646 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1647 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1648 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1649 | << OE->getSourceRange(); |
| 1650 | return; |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | D->addAttr(::new (Context) |
| 1656 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1657 | } |
| 1658 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1659 | void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr, |
| 1660 | unsigned SpellingListIndex) { |
| 1661 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1662 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1663 | AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1664 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1665 | |
| 1666 | if (!ResultType->isDependentType() && |
| 1667 | !isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1668 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1669 | << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D); |
| 1670 | return; |
| 1671 | } |
| 1672 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1673 | ParamIdx Idx; |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1674 | const auto *FuncDecl = cast<FunctionDecl>(D); |
| 1675 | if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1676 | /*AttrArgNum=*/1, ParamExpr, Idx)) |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1677 | return; |
| 1678 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1679 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1680 | if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1681 | Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1682 | << &TmpAttr |
| 1683 | << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1684 | return; |
| 1685 | } |
| 1686 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1687 | D->addAttr(::new (Context) |
| 1688 | AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex)); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1691 | /// Normalize the attribute, __foo__ becomes foo. |
| 1692 | /// Returns true if normalization was applied. |
| 1693 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1694 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1695 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1696 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1697 | return true; |
| 1698 | } |
| 1699 | return false; |
| 1700 | } |
| 1701 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1702 | static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1703 | // This attribute must be applied to a function declaration. The first |
| 1704 | // argument to the attribute must be an identifier, the name of the resource, |
| 1705 | // for example: malloc. The following arguments must be argument indexes, the |
| 1706 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1707 | // 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] | 1708 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1709 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1710 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1711 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1712 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1713 | << AL << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1714 | return; |
| 1715 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1716 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1717 | // Figure out our Kind. |
| 1718 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1719 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1720 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1721 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1722 | // Check arguments. |
| 1723 | switch (K) { |
| 1724 | case OwnershipAttr::Takes: |
| 1725 | case OwnershipAttr::Holds: |
| 1726 | if (AL.getNumArgs() < 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1727 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1728 | return; |
| 1729 | } |
| 1730 | break; |
| 1731 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1732 | if (AL.getNumArgs() > 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1733 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1734 | return; |
| 1735 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1736 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1739 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1740 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1741 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1742 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1743 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1744 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1745 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1746 | SmallVector<ParamIdx, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1747 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1748 | Expr *Ex = AL.getArgAsExpr(i); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1749 | ParamIdx Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1750 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1751 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1752 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1753 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1754 | QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1755 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1756 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1757 | case OwnershipAttr::Takes: |
| 1758 | case OwnershipAttr::Holds: |
| 1759 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1760 | Err = 0; |
| 1761 | break; |
| 1762 | case OwnershipAttr::Returns: |
| 1763 | if (!T->isIntegerType()) |
| 1764 | Err = 1; |
| 1765 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1766 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1767 | if (-1 != Err) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1768 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err |
| 1769 | << Ex->getSourceRange(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1770 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1771 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1774 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1775 | // Cannot have two ownership attributes of different kinds for the same |
| 1776 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1777 | if (I->getOwnKind() != K && I->args_end() != |
| 1778 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1779 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1780 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1781 | } else if (K == OwnershipAttr::Returns && |
| 1782 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1783 | // A returns attribute conflicts with any other returns attribute using |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1784 | // a different index. |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1785 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1786 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1787 | << I->args_begin()->getSourceIndex(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1788 | if (I->args_size()) |
| 1789 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1790 | << Idx.getSourceIndex() << Ex->getSourceRange(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1791 | return; |
| 1792 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1795 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1798 | ParamIdx *Start = OwnershipArgs.data(); |
| 1799 | unsigned Size = OwnershipArgs.size(); |
| 1800 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1801 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1802 | OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size, |
| 1803 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1806 | static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1807 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1808 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1809 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1810 | return; |
| 1811 | } |
| 1812 | |
| 1813 | // gcc rejects |
| 1814 | // class c { |
| 1815 | // static int a __attribute__((weakref ("v2"))); |
| 1816 | // static int b() __attribute__((weakref ("f3"))); |
| 1817 | // }; |
| 1818 | // and ignores the attributes of |
| 1819 | // void f(void) { |
| 1820 | // static int a __attribute__((weakref ("v2"))); |
| 1821 | // } |
| 1822 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1823 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1824 | if (!Ctx->isFileContext()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1825 | S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1826 | << cast<NamedDecl>(D); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1827 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | // The GCC manual says |
| 1831 | // |
| 1832 | // At present, a declaration to which `weakref' is attached can only |
| 1833 | // be `static'. |
| 1834 | // |
| 1835 | // It also says |
| 1836 | // |
| 1837 | // Without a TARGET, |
| 1838 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1839 | // equivalent to `weak'. |
| 1840 | // |
| 1841 | // gcc 4.4.1 will accept |
| 1842 | // int a7 __attribute__((weakref)); |
| 1843 | // as |
| 1844 | // int a7 __attribute__((weak)); |
| 1845 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1846 | // it if this behaviour is actually used. |
| 1847 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1848 | // GCC rejects |
| 1849 | // static ((alias ("y"), weakref)). |
| 1850 | // Should we? How to check that weakref is before or after alias? |
| 1851 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1852 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1853 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1854 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1855 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1856 | if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1857 | // GCC will accept anything as the argument of weakref. Should we |
| 1858 | // check for an existing decl? |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1859 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1860 | AL.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1861 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1862 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1863 | WeakRefAttr(AL.getRange(), S.Context, |
| 1864 | AL.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1867 | static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1868 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1869 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1870 | return; |
| 1871 | |
| 1872 | // Aliases should be on declarations, not definitions. |
| 1873 | const auto *FD = cast<FunctionDecl>(D); |
| 1874 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1875 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1876 | return; |
| 1877 | } |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1878 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1879 | D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str, |
| 1880 | AL.getAttributeSpellingListIndex())); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1883 | static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1884 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1885 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1888 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1889 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin); |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1890 | return; |
| 1891 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1892 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1893 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx); |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1894 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1895 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1896 | // Aliases should be on declarations, not definitions. |
| 1897 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1898 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1899 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1900 | return; |
| 1901 | } |
| 1902 | } else { |
| 1903 | const auto *VD = cast<VarDecl>(D); |
| 1904 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1905 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1906 | return; |
| 1907 | } |
| 1908 | } |
| 1909 | |
Nick Desaulniers | 9b9fe41 | 2019-01-09 23:54:55 +0000 | [diff] [blame] | 1910 | // Mark target used to prevent unneeded-internal-declaration warnings. |
| 1911 | if (!S.LangOpts.CPlusPlus) { |
| 1912 | // FIXME: demangle Str for C++, as the attribute refers to the mangled |
| 1913 | // linkage name, not the pre-mangled identifier. |
| 1914 | const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc()); |
| 1915 | LookupResult LR(S, target, Sema::LookupOrdinaryName); |
| 1916 | if (S.LookupQualifiedName(LR, S.getCurLexicalContext())) |
| 1917 | for (NamedDecl *ND : LR) |
| 1918 | ND->markUsed(S.Context); |
| 1919 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1920 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1921 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1922 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1925 | static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1926 | StringRef Model; |
| 1927 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1928 | // Check that it is a string. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1929 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1930 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1931 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1932 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1933 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1934 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1935 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1936 | return; |
| 1937 | } |
| 1938 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1939 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1940 | TLSModelAttr(AL.getRange(), S.Context, Model, |
| 1941 | AL.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1944 | static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1945 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1946 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1947 | D->addAttr(::new (S.Context) RestrictAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1948 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1949 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1952 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1953 | << AL << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1956 | static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1957 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Erich Keane | 659c871 | 2018-09-10 14:31:56 +0000 | [diff] [blame] | 1958 | |
| 1959 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 1960 | if (MD->getParent()->isLambda()) { |
| 1961 | S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL; |
| 1962 | return; |
| 1963 | } |
| 1964 | } |
| 1965 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1966 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 1967 | return; |
| 1968 | |
| 1969 | SmallVector<IdentifierInfo *, 8> CPUs; |
| 1970 | for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) { |
| 1971 | if (!AL.isArgIdent(ArgNo)) { |
| 1972 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1973 | << AL << AANT_ArgumentIdentifier; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1974 | return; |
| 1975 | } |
| 1976 | |
| 1977 | IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo); |
| 1978 | StringRef CPUName = CPUArg->Ident->getName().trim(); |
| 1979 | |
| 1980 | if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) { |
| 1981 | S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value) |
| 1982 | << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch); |
| 1983 | return; |
| 1984 | } |
| 1985 | |
| 1986 | const TargetInfo &Target = S.Context.getTargetInfo(); |
| 1987 | if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) { |
| 1988 | return Target.CPUSpecificManglingCharacter(CPUName) == |
| 1989 | Target.CPUSpecificManglingCharacter(Cur->getName()); |
| 1990 | })) { |
| 1991 | S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries); |
| 1992 | return; |
| 1993 | } |
| 1994 | CPUs.push_back(CPUArg->Ident); |
| 1995 | } |
| 1996 | |
| 1997 | FD->setIsMultiVersion(true); |
| 1998 | if (AL.getKind() == ParsedAttr::AT_CPUSpecific) |
| 1999 | D->addAttr(::new (S.Context) CPUSpecificAttr( |
| 2000 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 2001 | AL.getAttributeSpellingListIndex())); |
| 2002 | else |
| 2003 | D->addAttr(::new (S.Context) CPUDispatchAttr( |
| 2004 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 2005 | AL.getAttributeSpellingListIndex())); |
| 2006 | } |
| 2007 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2008 | static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 2009 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2010 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2011 | << AL << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 2012 | return; |
| 2013 | } |
| 2014 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2015 | if (CommonAttr *CA = S.mergeCommonAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 2016 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2019 | static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2020 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL)) |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 2021 | return; |
| 2022 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2023 | if (AL.isDeclspecAttribute()) { |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 2024 | const auto &Triple = S.getASTContext().getTargetInfo().getTriple(); |
| 2025 | const auto &Arch = Triple.getArch(); |
| 2026 | if (Arch != llvm::Triple::x86 && |
| 2027 | (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2028 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2029 | << AL << Triple.getArchName(); |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 2030 | return; |
| 2031 | } |
| 2032 | } |
| 2033 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2034 | D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context, |
| 2035 | AL.getAttributeSpellingListIndex())); |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2038 | static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2039 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2040 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2041 | if (!isa<ObjCMethodDecl>(D)) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 2042 | S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2043 | << Attrs << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2044 | return; |
| 2045 | } |
| 2046 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2047 | D->addAttr(::new (S.Context) NoReturnAttr( |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 2048 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2051 | static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 2052 | if (!S.getLangOpts().CFProtectionBranch) |
| 2053 | S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored); |
| 2054 | else |
| 2055 | handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2058 | bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 2059 | if (!checkAttributeNumArgs(*this, Attrs, 0)) { |
| 2060 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2061 | return true; |
| 2062 | } |
| 2063 | |
| 2064 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2067 | bool Sema::CheckAttrTarget(const ParsedAttr &AL) { |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2068 | // Check whether the attribute is valid on the current target. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2069 | if (!AL.existsInTarget(Context.getTargetInfo())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2070 | Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2071 | AL.setInvalid(); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2072 | return true; |
| 2073 | } |
| 2074 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2075 | return false; |
| 2076 | } |
| 2077 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2078 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2079 | |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2080 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 2081 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 2082 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2083 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2084 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 2085 | !VD->getType()->isFunctionPointerType())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2086 | S.Diag(AL.getLoc(), AL.isCXX11Attribute() |
| 2087 | ? diag::err_attribute_wrong_decl_type |
| 2088 | : diag::warn_attribute_wrong_decl_type) |
| 2089 | << AL << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2090 | return; |
| 2091 | } |
| 2092 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2093 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2094 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2095 | AnalyzerNoReturnAttr(AL.getRange(), S.Context, |
| 2096 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2099 | // PS3 PPU-specific. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2100 | static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2101 | /* |
| 2102 | Returning a Vector Class in Registers |
| 2103 | |
| 2104 | According to the PPU ABI specifications, a class with a single member of |
| 2105 | vector type is returned in memory when used as the return value of a |
| 2106 | function. |
| 2107 | This results in inefficient code when implementing vector classes. To return |
| 2108 | the value in a single vector register, add the vecreturn attribute to the |
| 2109 | class definition. This attribute is also applicable to struct types. |
| 2110 | |
| 2111 | Example: |
| 2112 | |
| 2113 | struct Vector |
| 2114 | { |
| 2115 | __vector float xyzw; |
| 2116 | } __attribute__((vecreturn)); |
| 2117 | |
| 2118 | Vector Add(Vector lhs, Vector rhs) |
| 2119 | { |
| 2120 | Vector result; |
| 2121 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 2122 | return result; // This will be returned in a register |
| 2123 | } |
| 2124 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 2125 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2126 | S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2127 | return; |
| 2128 | } |
| 2129 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2130 | const auto *R = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2131 | int count = 0; |
| 2132 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2133 | if (!isa<CXXRecordDecl>(R)) { |
| 2134 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2135 | return; |
| 2136 | } |
| 2137 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2138 | if (!cast<CXXRecordDecl>(R)->isPOD()) { |
| 2139 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2140 | return; |
| 2141 | } |
| 2142 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2143 | for (const auto *I : R->fields()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2144 | if ((count == 1) || !I->getType()->isVectorType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2145 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2146 | return; |
| 2147 | } |
| 2148 | count++; |
| 2149 | } |
| 2150 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2151 | D->addAttr(::new (S.Context) VecReturnAttr( |
| 2152 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2155 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2156 | const ParsedAttr &AL) { |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2157 | if (isa<ParmVarDecl>(D)) { |
| 2158 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 2159 | // parameter of a function declaration or lambda. |
| 2160 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2161 | S.Diag(AL.getLoc(), |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2162 | diag::err_carries_dependency_param_not_function_decl); |
| 2163 | return; |
| 2164 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2165 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2166 | |
| 2167 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2168 | AL.getRange(), S.Context, |
| 2169 | AL.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2172 | static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2173 | bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName(); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2174 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2175 | // 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] | 2176 | // about using it as an extension. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2177 | if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2178 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2179 | |
| 2180 | D->addAttr(::new (S.Context) UnusedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2181 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2184 | static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2185 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2186 | if (AL.getNumArgs() && |
| 2187 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2188 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2189 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2190 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2191 | ConstructorAttr(AL.getRange(), S.Context, priority, |
| 2192 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2195 | static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 2196 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2197 | if (AL.getNumArgs() && |
| 2198 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2199 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2200 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2201 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2202 | DestructorAttr(AL.getRange(), S.Context, priority, |
| 2203 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2206 | template <typename AttrTy> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2207 | static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2208 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2209 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2210 | if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2211 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2212 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2213 | D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str, |
| 2214 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2217 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2218 | const ParsedAttr &AL) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2219 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2220 | S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2221 | << AL << AL.getRange(); |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 2222 | return; |
| 2223 | } |
| 2224 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2225 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2226 | ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context, |
| 2227 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2230 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2231 | IdentifierInfo *Platform, |
| 2232 | VersionTuple Introduced, |
| 2233 | VersionTuple Deprecated, |
| 2234 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2235 | StringRef PlatformName |
| 2236 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2237 | if (PlatformName.empty()) |
| 2238 | PlatformName = Platform->getName(); |
| 2239 | |
| 2240 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2241 | // of these steps are needed). |
| 2242 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2243 | !(Introduced <= Deprecated)) { |
| 2244 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2245 | << 1 << PlatformName << Deprecated.getAsString() |
| 2246 | << 0 << Introduced.getAsString(); |
| 2247 | return true; |
| 2248 | } |
| 2249 | |
| 2250 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2251 | !(Introduced <= Obsoleted)) { |
| 2252 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2253 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2254 | << 0 << Introduced.getAsString(); |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
| 2258 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2259 | !(Deprecated <= Obsoleted)) { |
| 2260 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2261 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2262 | << 1 << Deprecated.getAsString(); |
| 2263 | return true; |
| 2264 | } |
| 2265 | |
| 2266 | return false; |
| 2267 | } |
| 2268 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2269 | /// Check whether the two versions match. |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2270 | /// |
| 2271 | /// If either version tuple is empty, then they are assumed to match. If |
| 2272 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2273 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2274 | bool BeforeIsOkay) { |
| 2275 | if (X.empty() || Y.empty()) |
| 2276 | return true; |
| 2277 | |
| 2278 | if (X == Y) |
| 2279 | return true; |
| 2280 | |
| 2281 | if (BeforeIsOkay && X < Y) |
| 2282 | return true; |
| 2283 | |
| 2284 | return false; |
| 2285 | } |
| 2286 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2287 | AvailabilityAttr *Sema::mergeAvailabilityAttr( |
| 2288 | NamedDecl *D, SourceRange Range, IdentifierInfo *Platform, bool Implicit, |
| 2289 | VersionTuple Introduced, VersionTuple Deprecated, VersionTuple Obsoleted, |
| 2290 | bool IsUnavailable, StringRef Message, bool IsStrict, StringRef Replacement, |
| 2291 | AvailabilityMergeKind AMK, int Priority, unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2292 | VersionTuple MergedIntroduced = Introduced; |
| 2293 | VersionTuple MergedDeprecated = Deprecated; |
| 2294 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2295 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2296 | bool OverrideOrImpl = false; |
| 2297 | switch (AMK) { |
| 2298 | case AMK_None: |
| 2299 | case AMK_Redeclaration: |
| 2300 | OverrideOrImpl = false; |
| 2301 | break; |
| 2302 | |
| 2303 | case AMK_Override: |
| 2304 | case AMK_ProtocolImplementation: |
| 2305 | OverrideOrImpl = true; |
| 2306 | break; |
| 2307 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2308 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2309 | if (D->hasAttrs()) { |
| 2310 | AttrVec &Attrs = D->getAttrs(); |
| 2311 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2312 | const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2313 | if (!OldAA) { |
| 2314 | ++i; |
| 2315 | continue; |
| 2316 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2317 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2318 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2319 | if (OldPlatform != Platform) { |
| 2320 | ++i; |
| 2321 | continue; |
| 2322 | } |
| 2323 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2324 | // If there is an existing availability attribute for this platform that |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2325 | // has a lower priority use the existing one and discard the new |
| 2326 | // attribute. |
| 2327 | if (OldAA->getPriority() < Priority) |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2328 | return nullptr; |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2329 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2330 | // If there is an existing attribute for this platform that has a higher |
| 2331 | // priority than the new attribute then erase the old one and continue |
| 2332 | // processing the attributes. |
| 2333 | if (OldAA->getPriority() > Priority) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2334 | Attrs.erase(Attrs.begin() + i); |
| 2335 | --e; |
| 2336 | continue; |
| 2337 | } |
| 2338 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2339 | FoundAny = true; |
| 2340 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2341 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2342 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2343 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2344 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2345 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2346 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2347 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2348 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2349 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2350 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2351 | int Which = -1; |
| 2352 | VersionTuple FirstVersion; |
| 2353 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2354 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2355 | Which = 0; |
| 2356 | FirstVersion = OldIntroduced; |
| 2357 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2358 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2359 | Which = 1; |
| 2360 | FirstVersion = Deprecated; |
| 2361 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2362 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2363 | Which = 2; |
| 2364 | FirstVersion = Obsoleted; |
| 2365 | SecondVersion = OldObsoleted; |
| 2366 | } |
| 2367 | |
| 2368 | if (Which == -1) { |
| 2369 | Diag(OldAA->getLocation(), |
| 2370 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2371 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2372 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2373 | } else { |
| 2374 | Diag(OldAA->getLocation(), |
| 2375 | diag::warn_mismatched_availability_override) |
| 2376 | << Which |
| 2377 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2378 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2379 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2380 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2381 | if (AMK == AMK_Override) |
| 2382 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2383 | else |
| 2384 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2385 | } else { |
| 2386 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2387 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2388 | } |
| 2389 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2390 | Attrs.erase(Attrs.begin() + i); |
| 2391 | --e; |
| 2392 | continue; |
| 2393 | } |
| 2394 | |
| 2395 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2396 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2397 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2398 | |
| 2399 | if (MergedIntroduced2.empty()) |
| 2400 | MergedIntroduced2 = OldIntroduced; |
| 2401 | if (MergedDeprecated2.empty()) |
| 2402 | MergedDeprecated2 = OldDeprecated; |
| 2403 | if (MergedObsoleted2.empty()) |
| 2404 | MergedObsoleted2 = OldObsoleted; |
| 2405 | |
| 2406 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2407 | MergedIntroduced2, MergedDeprecated2, |
| 2408 | MergedObsoleted2)) { |
| 2409 | Attrs.erase(Attrs.begin() + i); |
| 2410 | --e; |
| 2411 | continue; |
| 2412 | } |
| 2413 | |
| 2414 | MergedIntroduced = MergedIntroduced2; |
| 2415 | MergedDeprecated = MergedDeprecated2; |
| 2416 | MergedObsoleted = MergedObsoleted2; |
| 2417 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2418 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | if (FoundAny && |
| 2422 | MergedIntroduced == Introduced && |
| 2423 | MergedDeprecated == Deprecated && |
| 2424 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2425 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2426 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2427 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2428 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2429 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2430 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2431 | !OverrideOrImpl) { |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2432 | auto *Avail = ::new (Context) |
| 2433 | AvailabilityAttr(Range, Context, Platform, Introduced, Deprecated, |
| 2434 | Obsoleted, IsUnavailable, Message, IsStrict, |
| 2435 | Replacement, Priority, AttrSpellingListIndex); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2436 | Avail->setImplicit(Implicit); |
| 2437 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2438 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2439 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2442 | static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2443 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2444 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2445 | IdentifierLoc *Platform = AL.getArgAsIdent(0); |
| 2446 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2447 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2448 | IdentifierInfo *II = Platform->Ident; |
| 2449 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2450 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2451 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2452 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2453 | auto *ND = dyn_cast<NamedDecl>(D); |
Alex Lorenz | 472cc79 | 2017-04-20 09:35:02 +0000 | [diff] [blame] | 2454 | if (!ND) // We warned about this already, so just return. |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2455 | return; |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2456 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2457 | AvailabilityChange Introduced = AL.getAvailabilityIntroduced(); |
| 2458 | AvailabilityChange Deprecated = AL.getAvailabilityDeprecated(); |
| 2459 | AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted(); |
| 2460 | bool IsUnavailable = AL.getUnavailableLoc().isValid(); |
| 2461 | bool IsStrict = AL.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2462 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2463 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2464 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2465 | StringRef Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2466 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr())) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2467 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2468 | |
Michael Wu | 260e962 | 2018-11-12 02:44:33 +0000 | [diff] [blame] | 2469 | if (II->isStr("swift")) { |
| 2470 | if (Introduced.isValid() || Obsoleted.isValid() || |
| 2471 | (!IsUnavailable && !Deprecated.isValid())) { |
| 2472 | S.Diag(AL.getLoc(), |
| 2473 | diag::warn_availability_swift_unavailable_deprecated_only); |
| 2474 | return; |
| 2475 | } |
| 2476 | } |
| 2477 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2478 | int PriorityModifier = AL.isPragmaClangAttribute() |
| 2479 | ? Sema::AP_PragmaClangAttribute |
| 2480 | : Sema::AP_Explicit; |
| 2481 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
| 2482 | ND, AL.getRange(), II, false /*Implicit*/, Introduced.Version, |
| 2483 | Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict, |
| 2484 | Replacement, Sema::AMK_None, PriorityModifier, Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2485 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2486 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2487 | |
| 2488 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2489 | // matches before the start of the watchOS platform. |
| 2490 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2491 | IdentifierInfo *NewII = nullptr; |
| 2492 | if (II->getName() == "ios") |
| 2493 | NewII = &S.Context.Idents.get("watchos"); |
| 2494 | else if (II->getName() == "ios_app_extension") |
| 2495 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2496 | |
| 2497 | if (NewII) { |
| 2498 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2499 | if (Version.empty()) |
| 2500 | return Version; |
| 2501 | auto Major = Version.getMajor(); |
| 2502 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2503 | if (NewMajor >= 2) { |
| 2504 | if (Version.getMinor().hasValue()) { |
| 2505 | if (Version.getSubminor().hasValue()) |
| 2506 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2507 | Version.getSubminor().getValue()); |
| 2508 | else |
| 2509 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2510 | } |
Alex Lorenz | 0b43648 | 2019-03-20 20:02:00 +0000 | [diff] [blame] | 2511 | return VersionTuple(NewMajor); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | return VersionTuple(2, 0); |
| 2515 | }; |
| 2516 | |
| 2517 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2518 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2519 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2520 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2521 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
| 2522 | ND, AL.getRange(), NewII, true /*Implicit*/, NewIntroduced, |
| 2523 | NewDeprecated, NewObsoleted, IsUnavailable, Str, IsStrict, |
| 2524 | Replacement, Sema::AMK_None, |
| 2525 | PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2526 | if (NewAttr) |
| 2527 | D->addAttr(NewAttr); |
| 2528 | } |
| 2529 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2530 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2531 | // matches before the start of the tvOS platform. |
| 2532 | IdentifierInfo *NewII = nullptr; |
| 2533 | if (II->getName() == "ios") |
| 2534 | NewII = &S.Context.Idents.get("tvos"); |
| 2535 | else if (II->getName() == "ios_app_extension") |
| 2536 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2537 | |
| 2538 | if (NewII) { |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2539 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
| 2540 | ND, AL.getRange(), NewII, true /*Implicit*/, Introduced.Version, |
| 2541 | Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict, |
| 2542 | Replacement, Sema::AMK_None, |
| 2543 | PriorityModifier + Sema::AP_InferredFromOtherPlatform, Index); |
| 2544 | if (NewAttr) |
| 2545 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2546 | } |
| 2547 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2550 | static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2551 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2552 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2553 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2554 | assert(checkAttributeAtMostNumArgs(S, AL, 3) && |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2555 | "Invalid number of arguments in an external_source_symbol attribute"); |
| 2556 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2557 | StringRef Language; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2558 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2559 | Language = SE->getString(); |
| 2560 | StringRef DefinedIn; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2561 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2562 | DefinedIn = SE->getString(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2563 | bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2564 | |
| 2565 | D->addAttr(::new (S.Context) ExternalSourceSymbolAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2566 | AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration, |
| 2567 | AL.getAttributeSpellingListIndex())); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2570 | template <class T> |
| 2571 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2572 | typename T::VisibilityType value, |
| 2573 | unsigned attrSpellingListIndex) { |
| 2574 | T *existingAttr = D->getAttr<T>(); |
| 2575 | if (existingAttr) { |
| 2576 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2577 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2578 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2579 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2580 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2581 | D->dropAttr<T>(); |
| 2582 | } |
| 2583 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2584 | } |
| 2585 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2586 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2587 | VisibilityAttr::VisibilityType Vis, |
| 2588 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2589 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2590 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2593 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2594 | TypeVisibilityAttr::VisibilityType Vis, |
| 2595 | unsigned AttrSpellingListIndex) { |
| 2596 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2597 | AttrSpellingListIndex); |
| 2598 | } |
| 2599 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2600 | static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2601 | bool isTypeVisibility) { |
| 2602 | // Visibility attributes don't mean anything on a typedef. |
| 2603 | if (isa<TypedefNameDecl>(D)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2604 | S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2605 | return; |
| 2606 | } |
| 2607 | |
| 2608 | // 'type_visibility' can only go on a type or namespace. |
| 2609 | if (isTypeVisibility && |
| 2610 | !(isa<TagDecl>(D) || |
| 2611 | isa<ObjCInterfaceDecl>(D) || |
| 2612 | isa<NamespaceDecl>(D))) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2613 | S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2614 | << AL << ExpectedTypeOrNamespace; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2615 | return; |
| 2616 | } |
| 2617 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2618 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2619 | StringRef TypeStr; |
| 2620 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2621 | if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2622 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2623 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2624 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2625 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2626 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL |
| 2627 | << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2628 | return; |
| 2629 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2630 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2631 | // Complain about attempts to use protected visibility on targets |
| 2632 | // (like Darwin) that don't support it. |
| 2633 | if (type == VisibilityAttr::Protected && |
| 2634 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2635 | S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2636 | type = VisibilityAttr::Default; |
| 2637 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2638 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2639 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 2640 | Attr *newAttr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2641 | if (isTypeVisibility) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2642 | newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(), |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2643 | (TypeVisibilityAttr::VisibilityType) type, |
| 2644 | Index); |
| 2645 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2646 | newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2647 | } |
| 2648 | if (newAttr) |
| 2649 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2652 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2653 | const auto *M = cast<ObjCMethodDecl>(D); |
| 2654 | if (!AL.isArgIdent(0)) { |
| 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 << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2657 | return; |
| 2658 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2659 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2660 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2661 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2662 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2663 | 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] | 2664 | return; |
| 2665 | } |
| 2666 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2667 | if (F == ObjCMethodFamilyAttr::OMF_init && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2668 | !M->getReturnType()->isObjCObjectPointerType()) { |
| 2669 | S.Diag(M->getLocation(), diag::err_init_method_bad_return_type) |
| 2670 | << M->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2671 | // Ignore the attribute. |
| 2672 | return; |
| 2673 | } |
| 2674 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2675 | D->addAttr(new (S.Context) ObjCMethodFamilyAttr( |
| 2676 | AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2679 | static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2680 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2681 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2682 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2683 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2684 | return; |
| 2685 | } |
| 2686 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2687 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2688 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2689 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2690 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2691 | return; |
| 2692 | } |
| 2693 | } |
| 2694 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2695 | // It is okay to include this attribute on properties, e.g.: |
| 2696 | // |
| 2697 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2698 | // |
| 2699 | // 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] | 2700 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2701 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2702 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2703 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2704 | ObjCNSObjectAttr(AL.getRange(), S.Context, |
| 2705 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2708 | static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2709 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2710 | QualType T = TD->getUnderlyingType(); |
| 2711 | if (!T->isObjCObjectPointerType()) { |
| 2712 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2713 | return; |
| 2714 | } |
| 2715 | } else { |
| 2716 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2717 | return; |
| 2718 | } |
| 2719 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2720 | ObjCIndependentClassAttr(AL.getRange(), S.Context, |
| 2721 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2724 | static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2725 | if (!AL.isArgIdent(0)) { |
| 2726 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2727 | << AL << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2728 | return; |
| 2729 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2730 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2731 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2732 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2733 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2734 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2735 | return; |
| 2736 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2737 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2738 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2739 | BlocksAttr(AL.getRange(), S.Context, type, |
| 2740 | AL.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2743 | static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2744 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2745 | if (AL.getNumArgs() > 0) { |
| 2746 | Expr *E = AL.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2747 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2748 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2749 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2750 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2751 | << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2752 | return; |
| 2753 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2754 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2755 | if (Idx.isSigned() && Idx.isNegative()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2756 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2757 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2758 | return; |
| 2759 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2760 | |
| 2761 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2764 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2765 | if (AL.getNumArgs() > 1) { |
| 2766 | Expr *E = AL.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2767 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2768 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2769 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2770 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2771 | << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2772 | return; |
| 2773 | } |
| 2774 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2775 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2776 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2777 | // FIXME: This error message could be improved, it would be nice |
| 2778 | // to say what the bounds actually are. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2779 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2780 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2781 | return; |
| 2782 | } |
| 2783 | } |
| 2784 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2785 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2786 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2787 | if (isa<FunctionNoProtoType>(FT)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2788 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2789 | return; |
| 2790 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2791 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2792 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2793 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2794 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2795 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2796 | } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2797 | if (!MD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2798 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2799 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2800 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2801 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2802 | if (!BD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2803 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2804 | return; |
| 2805 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2806 | } else if (const auto *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2807 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2808 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2809 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2810 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2811 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2812 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2813 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2814 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2815 | return; |
| 2816 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2817 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2818 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2819 | << AL << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2820 | return; |
| 2821 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2822 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2823 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2824 | << AL << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2825 | return; |
| 2826 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2827 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2828 | SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos, |
| 2829 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2832 | static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2833 | if (D->getFunctionType() && |
| 2834 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2835 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2836 | return; |
| 2837 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2838 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2839 | if (MD->getReturnType()->isVoidType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2840 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1; |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2841 | return; |
| 2842 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2843 | |
Aaron Ballman | 3bef014 | 2019-07-20 07:56:34 +0000 | [diff] [blame^] | 2844 | StringRef Str; |
| 2845 | if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) { |
| 2846 | // If this is spelled as the standard C++17 attribute, but not in C++17, |
| 2847 | // warn about using it as an extension. If there are attribute arguments, |
| 2848 | // then claim it's a C++2a extension instead. |
| 2849 | // FIXME: If WG14 does not seem likely to adopt the same feature, add an |
| 2850 | // extension warning for C2x mode. |
| 2851 | const LangOptions &LO = S.getLangOpts(); |
| 2852 | if (AL.getNumArgs() == 1) { |
| 2853 | if (LO.CPlusPlus && !LO.CPlusPlus2a) |
| 2854 | S.Diag(AL.getLoc(), diag::ext_cxx2a_attr) << AL; |
| 2855 | |
| 2856 | // Since this this is spelled [[nodiscard]], get the optional string |
| 2857 | // literal. If in C++ mode, but not in C++2a mode, diagnose as an |
| 2858 | // extension. |
| 2859 | // FIXME: C2x should support this feature as well, even as an extension. |
| 2860 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr)) |
| 2861 | return; |
| 2862 | } else if (LO.CPlusPlus && !LO.CPlusPlus17) |
| 2863 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
| 2864 | } |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2865 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2866 | D->addAttr(::new (S.Context) |
Aaron Ballman | 3bef014 | 2019-07-20 07:56:34 +0000 | [diff] [blame^] | 2867 | WarnUnusedResultAttr(AL.getRange(), S.Context, Str, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2868 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2871 | static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2872 | // weak_import only applies to variable & function declarations. |
| 2873 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2874 | if (!D->canBeWeakImported(isDef)) { |
| 2875 | if (isDef) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2876 | S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2877 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2878 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2879 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2880 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2881 | // Nothing to warn about here. |
| 2882 | } else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2883 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2884 | << AL << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2885 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2886 | return; |
| 2887 | } |
| 2888 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2889 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2890 | WeakImportAttr(AL.getRange(), S.Context, |
| 2891 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2894 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2895 | template <typename WorkGroupAttr> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2896 | static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2897 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2898 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2899 | const Expr *E = AL.getArgAsExpr(i); |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 2900 | if (!checkUInt32Argument(S, AL, E, WGSize[i], i, |
| 2901 | /*StrictlyUnsigned=*/true)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2902 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2903 | if (WGSize[i] == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2904 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2905 | << AL << E->getSourceRange(); |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2906 | return; |
| 2907 | } |
| 2908 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2909 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2910 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2911 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2912 | Existing->getYDim() == WGSize[1] && |
| 2913 | Existing->getZDim() == WGSize[2])) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2914 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2915 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2916 | D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context, |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2917 | WGSize[0], WGSize[1], WGSize[2], |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2918 | AL.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2921 | // Handles intel_reqd_sub_group_size. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2922 | static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2923 | uint32_t SGSize; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2924 | const Expr *E = AL.getArgAsExpr(0); |
| 2925 | if (!checkUInt32Argument(S, AL, E, SGSize)) |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2926 | return; |
| 2927 | if (SGSize == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2928 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2929 | << AL << E->getSourceRange(); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2930 | return; |
| 2931 | } |
| 2932 | |
| 2933 | OpenCLIntelReqdSubGroupSizeAttr *Existing = |
| 2934 | D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>(); |
| 2935 | if (Existing && Existing->getSubGroupSize() != SGSize) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2936 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2937 | |
| 2938 | D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2939 | AL.getRange(), S.Context, SGSize, |
| 2940 | AL.getAttributeSpellingListIndex())); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2943 | static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2944 | if (!AL.hasParsedType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2945 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2946 | return; |
| 2947 | } |
| 2948 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2949 | TypeSourceInfo *ParmTSI = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2950 | QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2951 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2952 | |
| 2953 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2954 | (ParmType->isBooleanType() || |
| 2955 | !ParmType->isIntegralType(S.getASTContext()))) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2956 | S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint) |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2957 | << ParmType; |
| 2958 | return; |
| 2959 | } |
| 2960 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2961 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2962 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2963 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2964 | return; |
| 2965 | } |
| 2966 | } |
| 2967 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2968 | D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2969 | ParmTSI, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2970 | AL.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2973 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2974 | StringRef Name, |
| 2975 | unsigned AttrSpellingListIndex) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2976 | // Explicit or partial specializations do not inherit |
| 2977 | // the section attribute from the primary template. |
| 2978 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 2979 | if (AttrSpellingListIndex == SectionAttr::Declspec_allocate && |
| 2980 | FD->isFunctionTemplateSpecialization()) |
| 2981 | return nullptr; |
| 2982 | } |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2983 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2984 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2985 | return nullptr; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2986 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 2987 | << 1 /*section*/; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2988 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2989 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2990 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2991 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2992 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2995 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2996 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2997 | if (!Error.empty()) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2998 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2999 | << 1 /*'section'*/; |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 3000 | return false; |
| 3001 | } |
| 3002 | return true; |
| 3003 | } |
| 3004 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3005 | static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 3006 | // Make sure that there is a string literal as the sections's single |
| 3007 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3008 | StringRef Str; |
| 3009 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3010 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 3011 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 3013 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 3014 | return; |
| 3015 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3016 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3017 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 3018 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3019 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 3020 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3021 | return; |
| 3022 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3024 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 3025 | SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3026 | if (NewAttr) |
| 3027 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame] | 3030 | // This is used for `__declspec(code_seg("segname"))` on a decl. |
| 3031 | // `#pragma code_seg("segname")` uses checkSectionName() instead. |
| 3032 | static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc, |
| 3033 | StringRef CodeSegName) { |
| 3034 | std::string Error = |
| 3035 | S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName); |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 3036 | if (!Error.empty()) { |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame] | 3037 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
| 3038 | << Error << 0 /*'code-seg'*/; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 3039 | return false; |
| 3040 | } |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame] | 3041 | |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 3042 | return true; |
| 3043 | } |
| 3044 | |
| 3045 | CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range, |
| 3046 | StringRef Name, |
| 3047 | unsigned AttrSpellingListIndex) { |
| 3048 | // Explicit or partial specializations do not inherit |
| 3049 | // the code_seg attribute from the primary template. |
| 3050 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 3051 | if (FD->isFunctionTemplateSpecialization()) |
| 3052 | return nullptr; |
| 3053 | } |
| 3054 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 3055 | if (ExistingAttr->getName() == Name) |
| 3056 | return nullptr; |
| 3057 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 3058 | << 0 /*codeseg*/; |
| 3059 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 3060 | return nullptr; |
| 3061 | } |
| 3062 | return ::new (Context) CodeSegAttr(Range, Context, Name, |
| 3063 | AttrSpellingListIndex); |
| 3064 | } |
| 3065 | |
| 3066 | static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3067 | StringRef Str; |
| 3068 | SourceLocation LiteralLoc; |
| 3069 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
| 3070 | return; |
| 3071 | if (!checkCodeSegName(S, LiteralLoc, Str)) |
| 3072 | return; |
| 3073 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 3074 | if (!ExistingAttr->isImplicit()) { |
| 3075 | S.Diag(AL.getLoc(), |
| 3076 | ExistingAttr->getName() == Str |
| 3077 | ? diag::warn_duplicate_codeseg_attribute |
| 3078 | : diag::err_conflicting_codeseg_attribute); |
| 3079 | return; |
| 3080 | } |
| 3081 | D->dropAttr<CodeSegAttr>(); |
| 3082 | } |
| 3083 | if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str, |
| 3084 | AL.getAttributeSpellingListIndex())) |
| 3085 | D->addAttr(CSA); |
| 3086 | } |
| 3087 | |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3088 | // Check for things we'd like to warn about. Multiversioning issues are |
| 3089 | // handled later in the process, once we know how many exist. |
| 3090 | bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 3091 | enum FirstParam { Unsupported, Duplicate }; |
| 3092 | enum SecondParam { None, Architecture }; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3093 | for (auto Str : {"tune=", "fpmath="}) |
| 3094 | if (AttrStr.find(Str) != StringRef::npos) |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3095 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3096 | << Unsupported << None << Str; |
| 3097 | |
| 3098 | TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr); |
| 3099 | |
| 3100 | if (!ParsedAttrs.Architecture.empty() && |
| 3101 | !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture)) |
| 3102 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3103 | << Unsupported << Architecture << ParsedAttrs.Architecture; |
| 3104 | |
| 3105 | if (ParsedAttrs.DuplicateArchitecture) |
| 3106 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3107 | << Duplicate << None << "arch="; |
| 3108 | |
| 3109 | for (const auto &Feature : ParsedAttrs.Features) { |
| 3110 | auto CurFeature = StringRef(Feature).drop_front(); // remove + or -. |
| 3111 | if (!Context.getTargetInfo().isValidFeatureName(CurFeature)) |
| 3112 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3113 | << Unsupported << None << CurFeature; |
| 3114 | } |
| 3115 | |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3116 | return false; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3119 | static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3120 | StringRef Str; |
| 3121 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3122 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) || |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3123 | S.checkTargetAttr(LiteralLoc, Str)) |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3124 | return; |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3125 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3126 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3127 | TargetAttr *NewAttr = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3128 | ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3129 | D->addAttr(NewAttr); |
| 3130 | } |
| 3131 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3132 | static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3133 | Expr *E = AL.getArgAsExpr(0); |
| 3134 | uint32_t VecWidth; |
| 3135 | if (!checkUInt32Argument(S, AL, E, VecWidth)) { |
| 3136 | AL.setInvalid(); |
| 3137 | return; |
| 3138 | } |
| 3139 | |
| 3140 | MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>(); |
| 3141 | if (Existing && Existing->getVectorWidth() != VecWidth) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3142 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3143 | return; |
| 3144 | } |
| 3145 | |
| 3146 | D->addAttr(::new (S.Context) |
| 3147 | MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth, |
| 3148 | AL.getAttributeSpellingListIndex())); |
| 3149 | } |
| 3150 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3151 | static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3152 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3153 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3154 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3155 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3156 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3157 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 3158 | // will warn the user. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3159 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3160 | if (DRE->hasQualifier()) |
| 3161 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3162 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3163 | NI = DRE->getNameInfo(); |
| 3164 | if (!FD) { |
| 3165 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 3166 | << NI.getName(); |
| 3167 | return; |
| 3168 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3169 | } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3170 | if (ULE->hasExplicitTemplateArgs()) |
| 3171 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3172 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 3173 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 3174 | if (!FD) { |
| 3175 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 3176 | << NI.getName(); |
| 3177 | if (ULE->getType() == S.Context.OverloadTy) |
| 3178 | S.NoteAllOverloadCandidates(ULE); |
| 3179 | return; |
| 3180 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3181 | } else { |
| 3182 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3183 | return; |
| 3184 | } |
| 3185 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3186 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3187 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3188 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3189 | return; |
| 3190 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3191 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3192 | // We're currently more strict than GCC about what function types we accept. |
| 3193 | // 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] | 3194 | QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType()); |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3195 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3196 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3197 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3198 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 3199 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3200 | return; |
| 3201 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3202 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3203 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3204 | CleanupAttr(AL.getRange(), S.Context, FD, |
| 3205 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3208 | static void handleEnumExtensibilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3209 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3210 | if (!AL.isArgIdent(0)) { |
| 3211 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3212 | << AL << 0 << AANT_ArgumentIdentifier; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3213 | return; |
| 3214 | } |
| 3215 | |
| 3216 | EnumExtensibilityAttr::Kind ExtensibilityKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3217 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3218 | if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(), |
| 3219 | ExtensibilityKind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3220 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3221 | return; |
| 3222 | } |
| 3223 | |
| 3224 | D->addAttr(::new (S.Context) EnumExtensibilityAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3225 | AL.getRange(), S.Context, ExtensibilityKind, |
| 3226 | AL.getAttributeSpellingListIndex())); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3229 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3230 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3231 | static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3232 | Expr *IdxExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3233 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3234 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3235 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3236 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3237 | // Make sure the format string is really a string. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3238 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3239 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3240 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 3241 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3242 | !isCFStringType(Ty, S.Context) && |
| 3243 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3244 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3245 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3246 | << "a string type" << IdxExpr->getSourceRange() |
| 3247 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3248 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3249 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3250 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3251 | if (!isNSStringType(Ty, S.Context) && |
| 3252 | !isCFStringType(Ty, S.Context) && |
| 3253 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3254 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3255 | S.Diag(AL.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3256 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 3257 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3258 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3261 | D->addAttr(::new (S.Context) FormatArgAttr( |
| 3262 | AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3265 | enum FormatAttrKind { |
| 3266 | CFStringFormat, |
| 3267 | NSStringFormat, |
| 3268 | StrftimeFormat, |
| 3269 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3270 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3271 | InvalidFormat |
| 3272 | }; |
| 3273 | |
| 3274 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3275 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3276 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3277 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3278 | // Check for formats that get handled specially. |
| 3279 | .Case("NSString", NSStringFormat) |
| 3280 | .Case("CFString", CFStringFormat) |
| 3281 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3282 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3283 | // Otherwise, check for supported formats. |
| 3284 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3285 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3286 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 3287 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 3288 | .Case("os_trace", SupportedFormat) |
| 3289 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3290 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3291 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3292 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3295 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3296 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3297 | static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3298 | if (!S.getLangOpts().CPlusPlus) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3299 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3302 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3303 | if (S.getCurFunctionOrMethodDecl()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3304 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3305 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3306 | return; |
| 3307 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3308 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3309 | if (S.Context.getAsArrayType(T)) |
| 3310 | T = S.Context.getBaseElementType(T); |
| 3311 | if (!T->getAs<RecordType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3312 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3313 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3314 | return; |
| 3315 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3316 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3317 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3318 | uint32_t prioritynum; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3319 | if (!checkUInt32Argument(S, AL, E, prioritynum)) { |
| 3320 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3321 | return; |
| 3322 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3323 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3324 | if (prioritynum < 101 || prioritynum > 65535) { |
Aaron Ballman | 52c9ad2 | 2019-02-12 13:04:11 +0000 | [diff] [blame] | 3325 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3326 | << E->getSourceRange() << AL << 101 << 65535; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3327 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3328 | return; |
| 3329 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3330 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3331 | InitPriorityAttr(AL.getRange(), S.Context, prioritynum, |
| 3332 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3335 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3336 | IdentifierInfo *Format, int FormatIdx, |
| 3337 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3338 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3339 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3340 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3341 | if (F->getType() == Format && |
| 3342 | F->getFormatIdx() == FormatIdx && |
| 3343 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3344 | // If we don't have a valid location for this attribute, adopt the |
| 3345 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3346 | if (F->getLocation().isInvalid()) |
| 3347 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3348 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3352 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3353 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3356 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3357 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3358 | static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3359 | if (!AL.isArgIdent(0)) { |
| 3360 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3361 | << AL << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3362 | return; |
| 3363 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3364 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3365 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3366 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3367 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3368 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3369 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3370 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3371 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3372 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3373 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3374 | // If we've modified the string name, we need a new identifier for it. |
| 3375 | II = &S.Context.Idents.get(Format); |
| 3376 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3377 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3378 | // Check for supported formats. |
| 3379 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3380 | |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3381 | if (Kind == IgnoredFormat) |
| 3382 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3383 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3384 | if (Kind == InvalidFormat) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3385 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3386 | << AL << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3387 | return; |
| 3388 | } |
| 3389 | |
| 3390 | // checks for the 2nd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3391 | Expr *IdxExpr = AL.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3392 | uint32_t Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3393 | if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3394 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3395 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3396 | if (Idx < 1 || Idx > NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3397 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3398 | << AL << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3399 | return; |
| 3400 | } |
| 3401 | |
| 3402 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3403 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3404 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3405 | if (HasImplicitThisParam) { |
| 3406 | if (ArgIdx == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3407 | S.Diag(AL.getLoc(), |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3408 | diag::err_format_attribute_implicit_this_format_string) |
| 3409 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3410 | return; |
| 3411 | } |
| 3412 | ArgIdx--; |
| 3413 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3414 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3415 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3416 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3417 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3418 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3419 | if (!isCFStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3420 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3421 | << "a CFString" << IdxExpr->getSourceRange() |
| 3422 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3423 | return; |
| 3424 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3425 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3426 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3427 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3428 | if (!isNSStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3429 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3430 | << "an NSString" << IdxExpr->getSourceRange() |
| 3431 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3432 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3433 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3434 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3435 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3436 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3437 | << "a string type" << IdxExpr->getSourceRange() |
| 3438 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3439 | return; |
| 3440 | } |
| 3441 | |
| 3442 | // check the 3rd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3443 | Expr *FirstArgExpr = AL.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3444 | uint32_t FirstArg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3445 | if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3446 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3447 | |
| 3448 | // check if the function is variadic if the 3rd argument non-zero |
| 3449 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3450 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3451 | ++NumArgs; // +1 for ... |
| 3452 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3453 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3454 | return; |
| 3455 | } |
| 3456 | } |
| 3457 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3458 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3459 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3460 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3461 | if (FirstArg != 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3462 | S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3463 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3464 | return; |
| 3465 | } |
| 3466 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3467 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3468 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3469 | << AL << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3470 | return; |
| 3471 | } |
| 3472 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3473 | FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3474 | Idx, FirstArg, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3475 | AL.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3476 | if (NewAttr) |
| 3477 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 3480 | /// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes. |
| 3481 | static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3482 | // The index that identifies the callback callee is mandatory. |
| 3483 | if (AL.getNumArgs() == 0) { |
| 3484 | S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee) |
| 3485 | << AL.getRange(); |
| 3486 | return; |
| 3487 | } |
| 3488 | |
| 3489 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3490 | int32_t NumArgs = getFunctionOrMethodNumParams(D); |
| 3491 | |
| 3492 | FunctionDecl *FD = D->getAsFunction(); |
| 3493 | assert(FD && "Expected a function declaration!"); |
| 3494 | |
| 3495 | llvm::StringMap<int> NameIdxMapping; |
| 3496 | NameIdxMapping["__"] = -1; |
| 3497 | |
| 3498 | NameIdxMapping["this"] = 0; |
| 3499 | |
| 3500 | int Idx = 1; |
| 3501 | for (const ParmVarDecl *PVD : FD->parameters()) |
| 3502 | NameIdxMapping[PVD->getName()] = Idx++; |
| 3503 | |
| 3504 | auto UnknownName = NameIdxMapping.end(); |
| 3505 | |
| 3506 | SmallVector<int, 8> EncodingIndices; |
| 3507 | for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) { |
| 3508 | SourceRange SR; |
| 3509 | int32_t ArgIdx; |
| 3510 | |
| 3511 | if (AL.isArgIdent(I)) { |
| 3512 | IdentifierLoc *IdLoc = AL.getArgAsIdent(I); |
| 3513 | auto It = NameIdxMapping.find(IdLoc->Ident->getName()); |
| 3514 | if (It == UnknownName) { |
| 3515 | S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown) |
| 3516 | << IdLoc->Ident << IdLoc->Loc; |
| 3517 | return; |
| 3518 | } |
| 3519 | |
| 3520 | SR = SourceRange(IdLoc->Loc); |
| 3521 | ArgIdx = It->second; |
| 3522 | } else if (AL.isArgExpr(I)) { |
| 3523 | Expr *IdxExpr = AL.getArgAsExpr(I); |
| 3524 | |
| 3525 | // If the expression is not parseable as an int32_t we have a problem. |
| 3526 | if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1, |
| 3527 | false)) { |
| 3528 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3529 | << AL << (I + 1) << IdxExpr->getSourceRange(); |
| 3530 | return; |
| 3531 | } |
| 3532 | |
| 3533 | // Check oob, excluding the special values, 0 and -1. |
| 3534 | if (ArgIdx < -1 || ArgIdx > NumArgs) { |
| 3535 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3536 | << AL << (I + 1) << IdxExpr->getSourceRange(); |
| 3537 | return; |
| 3538 | } |
| 3539 | |
| 3540 | SR = IdxExpr->getSourceRange(); |
| 3541 | } else { |
| 3542 | llvm_unreachable("Unexpected ParsedAttr argument type!"); |
| 3543 | } |
| 3544 | |
| 3545 | if (ArgIdx == 0 && !HasImplicitThisParam) { |
| 3546 | S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available) |
| 3547 | << (I + 1) << SR; |
| 3548 | return; |
| 3549 | } |
| 3550 | |
| 3551 | // Adjust for the case we do not have an implicit "this" parameter. In this |
| 3552 | // case we decrease all positive values by 1 to get LLVM argument indices. |
| 3553 | if (!HasImplicitThisParam && ArgIdx > 0) |
| 3554 | ArgIdx -= 1; |
| 3555 | |
| 3556 | EncodingIndices.push_back(ArgIdx); |
| 3557 | } |
| 3558 | |
| 3559 | int CalleeIdx = EncodingIndices.front(); |
| 3560 | // Check if the callee index is proper, thus not "this" and not "unknown". |
Johannes Doerfert | e068d05 | 2019-01-21 14:23:46 +0000 | [diff] [blame] | 3561 | // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam" |
| 3562 | // is false and positive if "HasImplicitThisParam" is true. |
| 3563 | if (CalleeIdx < (int)HasImplicitThisParam) { |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 3564 | S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee) |
| 3565 | << AL.getRange(); |
| 3566 | return; |
| 3567 | } |
| 3568 | |
| 3569 | // Get the callee type, note the index adjustment as the AST doesn't contain |
| 3570 | // the this type (which the callee cannot reference anyway!). |
| 3571 | const Type *CalleeType = |
| 3572 | getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam) |
| 3573 | .getTypePtr(); |
| 3574 | if (!CalleeType || !CalleeType->isFunctionPointerType()) { |
| 3575 | S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type) |
| 3576 | << AL.getRange(); |
| 3577 | return; |
| 3578 | } |
| 3579 | |
| 3580 | const Type *CalleeFnType = |
| 3581 | CalleeType->getPointeeType()->getUnqualifiedDesugaredType(); |
| 3582 | |
| 3583 | // TODO: Check the type of the callee arguments. |
| 3584 | |
| 3585 | const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType); |
| 3586 | if (!CalleeFnProtoType) { |
| 3587 | S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type) |
| 3588 | << AL.getRange(); |
| 3589 | return; |
| 3590 | } |
| 3591 | |
| 3592 | if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) { |
| 3593 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3594 | << AL << (unsigned)(EncodingIndices.size() - 1); |
| 3595 | return; |
| 3596 | } |
| 3597 | |
| 3598 | if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) { |
| 3599 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3600 | << AL << (unsigned)(EncodingIndices.size() - 1); |
| 3601 | return; |
| 3602 | } |
| 3603 | |
| 3604 | if (CalleeFnProtoType->isVariadic()) { |
| 3605 | S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange(); |
| 3606 | return; |
| 3607 | } |
| 3608 | |
| 3609 | // Do not allow multiple callback attributes. |
| 3610 | if (D->hasAttr<CallbackAttr>()) { |
| 3611 | S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange(); |
| 3612 | return; |
| 3613 | } |
| 3614 | |
| 3615 | D->addAttr(::new (S.Context) CallbackAttr( |
| 3616 | AL.getRange(), S.Context, EncodingIndices.data(), EncodingIndices.size(), |
| 3617 | AL.getAttributeSpellingListIndex())); |
| 3618 | } |
| 3619 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3620 | static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3621 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3622 | RecordDecl *RD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3623 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3624 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3625 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3626 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3627 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3628 | |
| 3629 | if (!RD || !RD->isUnion()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3630 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL |
| 3631 | << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3632 | return; |
| 3633 | } |
| 3634 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3635 | if (!RD->isCompleteDefinition()) { |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3636 | if (!RD->isBeingDefined()) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3637 | S.Diag(AL.getLoc(), |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3638 | diag::warn_transparent_union_attribute_not_definition); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3639 | return; |
| 3640 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3641 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3642 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3643 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3644 | if (Field == FieldEnd) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3645 | S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3646 | return; |
| 3647 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3648 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3649 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3650 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3651 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3652 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3653 | diag::warn_transparent_union_attribute_floating) |
| 3654 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3655 | return; |
| 3656 | } |
| 3657 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3658 | if (FirstType->isIncompleteType()) |
| 3659 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3660 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3661 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3662 | for (; Field != FieldEnd; ++Field) { |
| 3663 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3664 | if (FieldType->isIncompleteType()) |
| 3665 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3666 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3667 | // members of the union would all have the same calling convention as the |
| 3668 | // first member of the union. Checking just the size and alignment isn't |
| 3669 | // sufficient (consider structs passed on the stack instead of in registers |
| 3670 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3671 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3672 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3673 | // Warn if we drop the attribute. |
| 3674 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3675 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3676 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3677 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3678 | diag::warn_transparent_union_attribute_field_size_align) |
| 3679 | << isSize << Field->getDeclName() << FieldBits; |
| 3680 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3681 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3682 | diag::note_transparent_union_first_field_size_align) |
| 3683 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3684 | return; |
| 3685 | } |
| 3686 | } |
| 3687 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3688 | RD->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3689 | TransparentUnionAttr(AL.getRange(), S.Context, |
| 3690 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3693 | static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3694 | // Make sure that there is a string literal as the annotation's single |
| 3695 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3696 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3697 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3698 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3699 | |
| 3700 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3701 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3702 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3703 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3704 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3705 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3706 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3707 | AnnotateAttr(AL.getRange(), S.Context, Str, |
| 3708 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3711 | static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3712 | S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 3713 | AL.getAttributeSpellingListIndex()); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3717 | unsigned SpellingListIndex) { |
| 3718 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3719 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3720 | |
| 3721 | QualType T; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3722 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3723 | T = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3724 | else if (const auto *VD = dyn_cast<ValueDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3725 | T = VD->getType(); |
| 3726 | else |
| 3727 | llvm_unreachable("Unknown decl type for align_value"); |
| 3728 | |
| 3729 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3730 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3731 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3732 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3737 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3738 | ExprResult ICE |
| 3739 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3740 | diag::err_align_value_attribute_argument_not_int, |
| 3741 | /*AllowFold*/ false); |
| 3742 | if (ICE.isInvalid()) |
| 3743 | return; |
| 3744 | |
| 3745 | if (!Alignment.isPowerOf2()) { |
| 3746 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3747 | << E->getSourceRange(); |
| 3748 | return; |
| 3749 | } |
| 3750 | |
| 3751 | D->addAttr(::new (Context) |
| 3752 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3753 | SpellingListIndex)); |
| 3754 | return; |
| 3755 | } |
| 3756 | |
| 3757 | // Save dependent expressions in the AST to be instantiated. |
| 3758 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3761 | static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3762 | // check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3763 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3764 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3765 | return; |
| 3766 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3767 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3768 | if (AL.getNumArgs() == 0) { |
| 3769 | D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context, |
| 3770 | true, nullptr, AL.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3771 | return; |
| 3772 | } |
| 3773 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3774 | Expr *E = AL.getArgAsExpr(0); |
| 3775 | if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3776 | S.Diag(AL.getEllipsisLoc(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3777 | diag::err_pack_expansion_without_parameter_packs); |
| 3778 | return; |
| 3779 | } |
| 3780 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3781 | if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3782 | return; |
| 3783 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3784 | S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(), |
| 3785 | AL.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3789 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3790 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3791 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3792 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3793 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3794 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3795 | // C++11 [dcl.align]p1: |
| 3796 | // An alignment-specifier may be applied to a variable or to a class |
| 3797 | // data member, but it shall not be applied to a bit-field, a function |
| 3798 | // parameter, the formal parameter of a catch clause, or a variable |
| 3799 | // declared with the register storage class specifier. An |
| 3800 | // alignment-specifier may also be applied to the declaration of a class |
| 3801 | // or enumeration type. |
| 3802 | // C11 6.7.5/2: |
| 3803 | // An alignment attribute shall not be specified in a declaration of |
| 3804 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3805 | // object declared with the register storage-class specifier. |
| 3806 | int DiagKind = -1; |
| 3807 | if (isa<ParmVarDecl>(D)) { |
| 3808 | DiagKind = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3809 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3810 | if (VD->getStorageClass() == SC_Register) |
| 3811 | DiagKind = 1; |
| 3812 | if (VD->isExceptionVariable()) |
| 3813 | DiagKind = 2; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3814 | } else if (const auto *FD = dyn_cast<FieldDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3815 | if (FD->isBitField()) |
| 3816 | DiagKind = 3; |
| 3817 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3818 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3819 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3820 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3821 | return; |
| 3822 | } |
| 3823 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3824 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3825 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3826 | return; |
| 3827 | } |
| 3828 | } |
| 3829 | |
Richard Smith | 90ae967 | 2018-06-20 23:36:55 +0000 | [diff] [blame] | 3830 | if (E->isValueDependent()) { |
| 3831 | // We can't support a dependent alignment on a non-dependent type, |
| 3832 | // because we have no way to model that a type is "alignment-dependent" |
| 3833 | // but not dependent in any other way. |
| 3834 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3835 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3836 | Diag(AttrLoc, diag::err_alignment_dependent_typedef_name) |
| 3837 | << E->getSourceRange(); |
| 3838 | return; |
| 3839 | } |
| 3840 | } |
| 3841 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3842 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3843 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3844 | AA->setPackExpansion(IsPackExpansion); |
| 3845 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3846 | return; |
| 3847 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3848 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3849 | // FIXME: Cache the number on the AL object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3850 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3851 | ExprResult ICE |
| 3852 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3853 | diag::err_aligned_attribute_argument_not_int, |
| 3854 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3855 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3856 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3857 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3858 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3859 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3860 | // C++11 [dcl.align]p2: |
| 3861 | // -- if the constant expression evaluates to zero, the alignment |
| 3862 | // specifier shall have no effect |
| 3863 | // C11 6.7.5p6: |
| 3864 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3865 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3866 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3867 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3868 | << E->getSourceRange(); |
| 3869 | return; |
| 3870 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3871 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3872 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3873 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3874 | unsigned MaxValidAlignment = |
| 3875 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3876 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3877 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3878 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3879 | << E->getSourceRange(); |
| 3880 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3881 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3882 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3883 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3884 | unsigned MaxTLSAlign = |
| 3885 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3886 | .getQuantity(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3887 | const auto *VD = dyn_cast<VarDecl>(D); |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3888 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3889 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3890 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3891 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3892 | return; |
| 3893 | } |
| 3894 | } |
| 3895 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3896 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3897 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3898 | AA->setPackExpansion(IsPackExpansion); |
| 3899 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3902 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3903 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3904 | // FIXME: Cache the number on the AL object if non-dependent? |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3905 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3906 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3907 | SpellingListIndex); |
| 3908 | AA->setPackExpansion(IsPackExpansion); |
| 3909 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3910 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3911 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3912 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3913 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3914 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3915 | QualType UnderlyingTy, DiagTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3916 | if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3917 | UnderlyingTy = DiagTy = VD->getType(); |
| 3918 | } else { |
| 3919 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3920 | if (const auto *ED = dyn_cast<EnumDecl>(D)) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3921 | UnderlyingTy = ED->getIntegerType(); |
| 3922 | } |
| 3923 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3924 | return; |
| 3925 | |
| 3926 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3927 | // The combined effect of all alignment attributes in a declaration shall |
| 3928 | // not specify an alignment that is less strict than the alignment that |
| 3929 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3930 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3931 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3932 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3933 | if (I->isAlignmentDependent()) |
| 3934 | return; |
| 3935 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3936 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3937 | Align = std::max(Align, I->getAlignment(Context)); |
| 3938 | } |
| 3939 | |
| 3940 | if (AlignasAttr && Align) { |
| 3941 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3942 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3943 | if (NaturalAlign > RequestedAlign) |
| 3944 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3945 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3946 | } |
| 3947 | } |
| 3948 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3949 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3950 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3951 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3952 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3953 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3954 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3955 | // have to wait until the record is defined to catch any mismatches. |
| 3956 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3957 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3958 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3959 | // The unspecified model never matches what a definition could need. |
| 3960 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3961 | return false; |
| 3962 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3963 | if (BestCase) { |
| 3964 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3965 | return false; |
| 3966 | } else { |
| 3967 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3968 | return false; |
| 3969 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3970 | |
| 3971 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3972 | << 0 /*definition*/; |
| 3973 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3974 | << RD->getNameAsString(); |
| 3975 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3978 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3979 | /// attribute. |
| 3980 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3981 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3982 | IntegerMode = true; |
| 3983 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3984 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3985 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3986 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3987 | case 'Q': |
| 3988 | DestWidth = 8; |
| 3989 | break; |
| 3990 | case 'H': |
| 3991 | DestWidth = 16; |
| 3992 | break; |
| 3993 | case 'S': |
| 3994 | DestWidth = 32; |
| 3995 | break; |
| 3996 | case 'D': |
| 3997 | DestWidth = 64; |
| 3998 | break; |
| 3999 | case 'X': |
| 4000 | DestWidth = 96; |
| 4001 | break; |
| 4002 | case 'T': |
| 4003 | DestWidth = 128; |
| 4004 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4005 | } |
| 4006 | if (Str[1] == 'F') { |
| 4007 | IntegerMode = false; |
| 4008 | } else if (Str[1] == 'C') { |
| 4009 | IntegerMode = false; |
| 4010 | ComplexMode = true; |
| 4011 | } else if (Str[1] != 'I') { |
| 4012 | DestWidth = 0; |
| 4013 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4014 | break; |
| 4015 | case 4: |
| 4016 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 4017 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 4018 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 4019 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 4020 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4021 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4022 | break; |
| 4023 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 4024 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4025 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4026 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 4027 | case 11: |
| 4028 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 4029 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 4030 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4031 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 4035 | /// type. |
| 4036 | /// |
| 4037 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 4038 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 4039 | /// HImode, not an intermediate pointer. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4040 | static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4041 | // This attribute isn't documented, but glibc uses it. It changes |
| 4042 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4043 | if (!AL.isArgIdent(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4044 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 4045 | << AL << AANT_ArgumentIdentifier; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4046 | return; |
| 4047 | } |
| 4048 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4049 | IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4050 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4051 | S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex()); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 4055 | unsigned SpellingListIndex, bool InInstantiation) { |
| 4056 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4057 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4058 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4059 | |
| 4060 | unsigned DestWidth = 0; |
| 4061 | bool IntegerMode = true; |
| 4062 | bool ComplexMode = false; |
| 4063 | llvm::APInt VectorSize(64, 0); |
| 4064 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 4065 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 4066 | size_t StrSize = Str.size(); |
| 4067 | size_t VectorStringLength = 0; |
| 4068 | while ((VectorStringLength + 1) < StrSize && |
| 4069 | isdigit(Str[VectorStringLength + 1])) |
| 4070 | ++VectorStringLength; |
| 4071 | if (VectorStringLength && |
| 4072 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 4073 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4074 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4075 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4076 | // Avoid duplicate warning from template instantiation. |
| 4077 | if (!InInstantiation) |
| 4078 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4079 | } else { |
| 4080 | VectorSize = 0; |
| 4081 | } |
| 4082 | } |
| 4083 | |
| 4084 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4085 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 4086 | |
| 4087 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 4088 | // and friends, at least with glibc. |
| 4089 | // FIXME: Make sure floating-point mappings are accurate |
| 4090 | // FIXME: Support XF and TF types |
| 4091 | if (!DestWidth) { |
| 4092 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 4093 | return; |
| 4094 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4095 | |
| 4096 | QualType OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4097 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4098 | OldTy = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4099 | else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4100 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 4101 | // Try to get type from enum declaration, default to int. |
| 4102 | OldTy = ED->getIntegerType(); |
| 4103 | if (OldTy.isNull()) |
| 4104 | OldTy = Context.IntTy; |
| 4105 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 4106 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4107 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4108 | if (OldTy->isDependentType()) { |
| 4109 | D->addAttr(::new (Context) |
| 4110 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 4111 | return; |
| 4112 | } |
| 4113 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4114 | // Base type can also be a vector type (see PR17453). |
| 4115 | // Distinguish between base type and base element type. |
| 4116 | QualType OldElemTy = OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4117 | if (const auto *VT = OldTy->getAs<VectorType>()) |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4118 | OldElemTy = VT->getElementType(); |
| 4119 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4120 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 4121 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 4122 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 4123 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 4124 | VectorSize.getBoolValue()) { |
| 4125 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 4126 | return; |
| 4127 | } |
| 4128 | bool IntegralOrAnyEnumType = |
| 4129 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 4130 | |
| 4131 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 4132 | !IntegralOrAnyEnumType) |
| 4133 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4134 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4135 | if (!IntegralOrAnyEnumType) |
| 4136 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4137 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4138 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4139 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4140 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4141 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4142 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4145 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4146 | |
| 4147 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4148 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 4149 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4150 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4151 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4152 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4153 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4154 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4155 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4158 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4159 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4163 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4164 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 4165 | VectorType::GenericVector); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4166 | } else if (const auto *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4167 | // Complex machine mode does not support base vector types. |
| 4168 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4169 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4170 | return; |
| 4171 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4172 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4173 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4174 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4175 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4176 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4180 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4181 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | // Install the new type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4185 | if (auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 4186 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4187 | else if (auto *ED = dyn_cast<EnumDecl>(D)) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4188 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 4189 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 4190 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 4191 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4192 | D->addAttr(::new (Context) |
| 4193 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4194 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4195 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4196 | static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4197 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4198 | NoDebugAttr(AL.getRange(), S.Context, |
| 4199 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4202 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4203 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4204 | unsigned AttrSpellingListIndex) { |
| 4205 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4206 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4207 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4208 | return nullptr; |
| 4209 | } |
| 4210 | |
| 4211 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 4212 | return nullptr; |
| 4213 | |
| 4214 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 4215 | AttrSpellingListIndex); |
| 4216 | } |
| 4217 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4218 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) { |
| 4219 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4220 | return nullptr; |
| 4221 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4222 | return ::new (Context) |
| 4223 | CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4226 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) { |
| 4227 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
| 4228 | return nullptr; |
| 4229 | |
| 4230 | return ::new (Context) |
| 4231 | CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4232 | } |
| 4233 | |
| 4234 | InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D, |
| 4235 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4236 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4237 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 4238 | // ImplicitParm or VarTemplateSpecialization). |
| 4239 | if (VD->getKind() != Decl::Var) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4240 | Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4241 | << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4242 | : ExpectedVariableOrFunction); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4243 | return nullptr; |
| 4244 | } |
| 4245 | // Attribute does not apply to non-static local variables. |
| 4246 | if (VD->hasLocalStorage()) { |
| 4247 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4248 | return nullptr; |
| 4249 | } |
| 4250 | } |
| 4251 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4252 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
| 4253 | return nullptr; |
| 4254 | |
| 4255 | return ::new (Context) InternalLinkageAttr( |
| 4256 | AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
| 4257 | } |
| 4258 | InternalLinkageAttr * |
| 4259 | Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) { |
| 4260 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 4261 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 4262 | // ImplicitParm or VarTemplateSpecialization). |
| 4263 | if (VD->getKind() != Decl::Var) { |
| 4264 | Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type) |
| 4265 | << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4266 | : ExpectedVariableOrFunction); |
| 4267 | return nullptr; |
| 4268 | } |
| 4269 | // Attribute does not apply to non-static local variables. |
| 4270 | if (VD->hasLocalStorage()) { |
| 4271 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4272 | return nullptr; |
| 4273 | } |
| 4274 | } |
| 4275 | |
| 4276 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4277 | return nullptr; |
| 4278 | |
| 4279 | return ::new (Context) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4280 | InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4283 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 4284 | unsigned AttrSpellingListIndex) { |
| 4285 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 4286 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 4287 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4288 | return nullptr; |
| 4289 | } |
| 4290 | |
| 4291 | if (D->hasAttr<MinSizeAttr>()) |
| 4292 | return nullptr; |
| 4293 | |
| 4294 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 4295 | } |
| 4296 | |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 4297 | NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr( |
| 4298 | Decl *D, const NoSpeculativeLoadHardeningAttr &AL) { |
| 4299 | if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL)) |
| 4300 | return nullptr; |
| 4301 | |
| 4302 | return ::new (Context) NoSpeculativeLoadHardeningAttr( |
| 4303 | AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4304 | } |
| 4305 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4306 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 4307 | unsigned AttrSpellingListIndex) { |
| 4308 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 4309 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 4310 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4311 | D->dropAttr<AlwaysInlineAttr>(); |
| 4312 | } |
| 4313 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 4314 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 4315 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4316 | D->dropAttr<MinSizeAttr>(); |
| 4317 | } |
| 4318 | |
| 4319 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 4320 | return nullptr; |
| 4321 | |
| 4322 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 4323 | AttrSpellingListIndex); |
| 4324 | } |
| 4325 | |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 4326 | SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr( |
| 4327 | Decl *D, const SpeculativeLoadHardeningAttr &AL) { |
| 4328 | if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL)) |
| 4329 | return nullptr; |
| 4330 | |
| 4331 | return ::new (Context) SpeculativeLoadHardeningAttr( |
| 4332 | AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4333 | } |
| 4334 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4335 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4336 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL)) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 4337 | return; |
| 4338 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4339 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4340 | D, AL.getRange(), AL.getName(), |
| 4341 | AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4342 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4345 | static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4346 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4347 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4348 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4351 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4352 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4353 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4354 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4357 | static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4358 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL)) |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4359 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4360 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4361 | if (!VD->hasGlobalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4362 | S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4363 | return; |
| 4364 | } |
| 4365 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4366 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4369 | static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4370 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL)) |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4371 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4372 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 4373 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 4374 | // "int x[]"). |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 4375 | if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() && |
Jonas Hahnfeld | ee47d8c | 2018-02-14 16:04:03 +0000 | [diff] [blame] | 4376 | !isa<IncompleteArrayType>(VD->getType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4377 | S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4378 | return; |
| 4379 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4380 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4381 | S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared) |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4382 | << S.CurrentCUDATarget()) |
| 4383 | return; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4384 | D->addAttr(::new (S.Context) CUDASharedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4385 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4388 | static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4389 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) || |
| 4390 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 4391 | return; |
| 4392 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4393 | const auto *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4394 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4395 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 4396 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4397 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4398 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 4399 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4400 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4401 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4402 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 4403 | if (Method->isInstance()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4404 | S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method) |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4405 | << Method; |
| 4406 | return; |
| 4407 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4408 | S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method; |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4409 | } |
| 4410 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 4411 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4412 | S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4413 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4414 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4415 | CUDAGlobalAttr(AL.getRange(), S.Context, |
| 4416 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4419 | static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4420 | const auto *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 4421 | if (!Fn->isInlineSpecified()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4422 | S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 4423 | return; |
| 4424 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4425 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4426 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4427 | GNUInlineAttr(AL.getRange(), S.Context, |
| 4428 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4431 | static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4432 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4433 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4434 | // Diagnostic is emitted elsewhere: here we store the (valid) AL |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4435 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 4436 | CallingConv CC; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4437 | if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4438 | return; |
| 4439 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4440 | if (!isa<ObjCMethodDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4441 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4442 | << AL << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4443 | return; |
| 4444 | } |
| 4445 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4446 | switch (AL.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4447 | case ParsedAttr::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4448 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4449 | FastCallAttr(AL.getRange(), S.Context, |
| 4450 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4451 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4452 | case ParsedAttr::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4453 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4454 | StdCallAttr(AL.getRange(), S.Context, |
| 4455 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4456 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4457 | case ParsedAttr::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4458 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4459 | ThisCallAttr(AL.getRange(), S.Context, |
| 4460 | AL.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 4461 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4462 | case ParsedAttr::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4463 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4464 | CDeclAttr(AL.getRange(), S.Context, |
| 4465 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4466 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4467 | case ParsedAttr::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4468 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4469 | PascalAttr(AL.getRange(), S.Context, |
| 4470 | AL.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4471 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4472 | case ParsedAttr::AT_SwiftCall: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4473 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4474 | SwiftCallAttr(AL.getRange(), S.Context, |
| 4475 | AL.getAttributeSpellingListIndex())); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4476 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4477 | case ParsedAttr::AT_VectorCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4478 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4479 | VectorCallAttr(AL.getRange(), S.Context, |
| 4480 | AL.getAttributeSpellingListIndex())); |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4481 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4482 | case ParsedAttr::AT_MSABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4483 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4484 | MSABIAttr(AL.getRange(), S.Context, |
| 4485 | AL.getAttributeSpellingListIndex())); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4486 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4487 | case ParsedAttr::AT_SysVABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4488 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4489 | SysVABIAttr(AL.getRange(), S.Context, |
| 4490 | AL.getAttributeSpellingListIndex())); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4491 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4492 | case ParsedAttr::AT_RegCall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4493 | D->addAttr(::new (S.Context) RegCallAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4494 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4495 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4496 | case ParsedAttr::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4497 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4498 | switch (CC) { |
| 4499 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4500 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4501 | break; |
| 4502 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4503 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4504 | break; |
| 4505 | default: |
| 4506 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4509 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4510 | PcsAttr(AL.getRange(), S.Context, PCS, |
| 4511 | AL.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4512 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4513 | } |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 4514 | case ParsedAttr::AT_AArch64VectorPcs: |
| 4515 | D->addAttr(::new(S.Context) |
| 4516 | AArch64VectorPcsAttr(AL.getRange(), S.Context, |
| 4517 | AL.getAttributeSpellingListIndex())); |
| 4518 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4519 | case ParsedAttr::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4520 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4521 | IntelOclBiccAttr(AL.getRange(), S.Context, |
| 4522 | AL.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4523 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4524 | case ParsedAttr::AT_PreserveMost: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4525 | D->addAttr(::new (S.Context) PreserveMostAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4526 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4527 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4528 | case ParsedAttr::AT_PreserveAll: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4529 | D->addAttr(::new (S.Context) PreserveAllAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4530 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4531 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4532 | default: |
| 4533 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4537 | static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4538 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4539 | return; |
| 4540 | |
| 4541 | std::vector<StringRef> DiagnosticIdentifiers; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4542 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4543 | StringRef RuleName; |
| 4544 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4545 | if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4546 | return; |
| 4547 | |
| 4548 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 4549 | // clang-tidy knows about available rules. |
| 4550 | DiagnosticIdentifiers.push_back(RuleName); |
| 4551 | } |
| 4552 | D->addAttr(::new (S.Context) SuppressAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4553 | AL.getRange(), S.Context, DiagnosticIdentifiers.data(), |
| 4554 | DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex())); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4557 | bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC, |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4558 | const FunctionDecl *FD) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4559 | if (Attrs.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4560 | return true; |
| 4561 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4562 | if (Attrs.hasProcessingCache()) { |
| 4563 | CC = (CallingConv) Attrs.getProcessingCache(); |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4564 | return false; |
| 4565 | } |
| 4566 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4567 | unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4568 | if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) { |
| 4569 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4570 | return true; |
| 4571 | } |
| 4572 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4573 | // TODO: diagnose uses of these conventions on the wrong target. |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4574 | switch (Attrs.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4575 | case ParsedAttr::AT_CDecl: |
| 4576 | CC = CC_C; |
| 4577 | break; |
| 4578 | case ParsedAttr::AT_FastCall: |
| 4579 | CC = CC_X86FastCall; |
| 4580 | break; |
| 4581 | case ParsedAttr::AT_StdCall: |
| 4582 | CC = CC_X86StdCall; |
| 4583 | break; |
| 4584 | case ParsedAttr::AT_ThisCall: |
| 4585 | CC = CC_X86ThisCall; |
| 4586 | break; |
| 4587 | case ParsedAttr::AT_Pascal: |
| 4588 | CC = CC_X86Pascal; |
| 4589 | break; |
| 4590 | case ParsedAttr::AT_SwiftCall: |
| 4591 | CC = CC_Swift; |
| 4592 | break; |
| 4593 | case ParsedAttr::AT_VectorCall: |
| 4594 | CC = CC_X86VectorCall; |
| 4595 | break; |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 4596 | case ParsedAttr::AT_AArch64VectorPcs: |
| 4597 | CC = CC_AArch64VectorCall; |
| 4598 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4599 | case ParsedAttr::AT_RegCall: |
| 4600 | CC = CC_X86RegCall; |
| 4601 | break; |
| 4602 | case ParsedAttr::AT_MSABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4603 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 4604 | CC_Win64; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4605 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4606 | case ParsedAttr::AT_SysVABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4607 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4608 | CC_C; |
| 4609 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4610 | case ParsedAttr::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 4611 | StringRef StrRef; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4612 | if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) { |
| 4613 | Attrs.setInvalid(); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4614 | return true; |
| 4615 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4616 | if (StrRef == "aapcs") { |
| 4617 | CC = CC_AAPCS; |
| 4618 | break; |
| 4619 | } else if (StrRef == "aapcs-vfp") { |
| 4620 | CC = CC_AAPCS_VFP; |
| 4621 | break; |
| 4622 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4623 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4624 | Attrs.setInvalid(); |
| 4625 | Diag(Attrs.getLoc(), diag::err_invalid_pcs); |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4626 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4627 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4628 | case ParsedAttr::AT_IntelOclBicc: |
| 4629 | CC = CC_IntelOclBicc; |
| 4630 | break; |
| 4631 | case ParsedAttr::AT_PreserveMost: |
| 4632 | CC = CC_PreserveMost; |
| 4633 | break; |
| 4634 | case ParsedAttr::AT_PreserveAll: |
| 4635 | CC = CC_PreserveAll; |
| 4636 | break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4637 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
Yaxun Liu | fa49c3a | 2019-02-26 22:24:49 +0000 | [diff] [blame] | 4640 | TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK; |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4641 | const TargetInfo &TI = Context.getTargetInfo(); |
Yaxun Liu | 785cbd8 | 2019-02-27 15:46:29 +0000 | [diff] [blame] | 4642 | // CUDA functions may have host and/or device attributes which indicate |
| 4643 | // their targeted execution environment, therefore the calling convention |
| 4644 | // of functions in CUDA should be checked against the target deduced based |
| 4645 | // on their host/device attributes. |
Yaxun Liu | fa49c3a | 2019-02-26 22:24:49 +0000 | [diff] [blame] | 4646 | if (LangOpts.CUDA) { |
Yaxun Liu | 785cbd8 | 2019-02-27 15:46:29 +0000 | [diff] [blame] | 4647 | auto *Aux = Context.getAuxTargetInfo(); |
Yaxun Liu | fa49c3a | 2019-02-26 22:24:49 +0000 | [diff] [blame] | 4648 | auto CudaTarget = IdentifyCUDATarget(FD); |
| 4649 | bool CheckHost = false, CheckDevice = false; |
| 4650 | switch (CudaTarget) { |
| 4651 | case CFT_HostDevice: |
| 4652 | CheckHost = true; |
| 4653 | CheckDevice = true; |
| 4654 | break; |
| 4655 | case CFT_Host: |
| 4656 | CheckHost = true; |
| 4657 | break; |
| 4658 | case CFT_Device: |
| 4659 | case CFT_Global: |
| 4660 | CheckDevice = true; |
| 4661 | break; |
| 4662 | case CFT_InvalidTarget: |
| 4663 | llvm_unreachable("unexpected cuda target"); |
| 4664 | } |
| 4665 | auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI; |
| 4666 | auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux; |
| 4667 | if (CheckHost && HostTI) |
| 4668 | A = HostTI->checkCallingConvention(CC); |
| 4669 | if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI) |
| 4670 | A = DeviceTI->checkCallingConvention(CC); |
| 4671 | } else { |
| 4672 | A = TI.checkCallingConvention(CC); |
| 4673 | } |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4674 | |
| 4675 | switch (A) { |
| 4676 | case TargetInfo::CCCR_OK: |
| 4677 | break; |
| 4678 | |
| 4679 | case TargetInfo::CCCR_Ignore: |
| 4680 | // Treat an ignored convention as if it was an explicit C calling convention |
| 4681 | // attribute. For example, __stdcall on Win x64 functions as __cdecl, so |
| 4682 | // that command line flags that change the default convention to |
| 4683 | // __vectorcall don't affect declarations marked __stdcall. |
| 4684 | CC = CC_C; |
| 4685 | break; |
| 4686 | |
Sunil Srivastava | f4038e7 | 2019-07-19 21:38:34 +0000 | [diff] [blame] | 4687 | case TargetInfo::CCCR_Error: |
| 4688 | Diag(Attrs.getLoc(), diag::error_cconv_unsupported) |
| 4689 | << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget; |
| 4690 | break; |
| 4691 | |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4692 | case TargetInfo::CCCR_Warning: { |
Sunil Srivastava | 85d667f | 2019-07-17 20:41:26 +0000 | [diff] [blame] | 4693 | Diag(Attrs.getLoc(), diag::warn_cconv_unsupported) |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4694 | << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4695 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4696 | // This convention is not valid for the target. Use the default function or |
| 4697 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 4698 | bool IsCXXMethod = false, IsVariadic = false; |
| 4699 | if (FD) { |
| 4700 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4701 | IsVariadic = FD->isVariadic(); |
| 4702 | } |
| 4703 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4704 | break; |
| 4705 | } |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4708 | Attrs.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4709 | return false; |
| 4710 | } |
| 4711 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4712 | /// Pointer-like types in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4713 | static bool isValidSwiftContextType(QualType Ty) { |
| 4714 | if (!Ty->hasPointerRepresentation()) |
| 4715 | return Ty->isDependentType(); |
| 4716 | return Ty->getPointeeType().getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | /// Pointers and references in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4720 | static bool isValidSwiftIndirectResultType(QualType Ty) { |
| 4721 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4722 | Ty = PtrType->getPointeeType(); |
| 4723 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4724 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4725 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4726 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4727 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4728 | return Ty.getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | /// Pointers and references to pointers in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4732 | static bool isValidSwiftErrorResultType(QualType Ty) { |
| 4733 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4734 | Ty = PtrType->getPointeeType(); |
| 4735 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4736 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4737 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4738 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4739 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4740 | if (!Ty.getQualifiers().empty()) |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4741 | return false; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4742 | return isValidSwiftContextType(Ty); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4745 | static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs, |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4746 | ParameterABI Abi) { |
| 4747 | S.AddParameterABIAttr(Attrs.getRange(), D, Abi, |
| 4748 | Attrs.getAttributeSpellingListIndex()); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 4752 | unsigned spellingIndex) { |
| 4753 | |
| 4754 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4755 | |
| 4756 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4757 | if (existingAttr->getABI() != abi) { |
| 4758 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4759 | << getParameterABISpelling(abi) << existingAttr; |
| 4760 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4761 | return; |
| 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | switch (abi) { |
| 4766 | case ParameterABI::Ordinary: |
| 4767 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4768 | |
| 4769 | case ParameterABI::SwiftContext: |
| 4770 | if (!isValidSwiftContextType(type)) { |
| 4771 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4772 | << getParameterABISpelling(abi) |
| 4773 | << /*pointer to pointer */ 0 << type; |
| 4774 | } |
| 4775 | D->addAttr(::new (Context) |
| 4776 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4777 | return; |
| 4778 | |
| 4779 | case ParameterABI::SwiftErrorResult: |
| 4780 | if (!isValidSwiftErrorResultType(type)) { |
| 4781 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4782 | << getParameterABISpelling(abi) |
| 4783 | << /*pointer to pointer */ 1 << type; |
| 4784 | } |
| 4785 | D->addAttr(::new (Context) |
| 4786 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4787 | return; |
| 4788 | |
| 4789 | case ParameterABI::SwiftIndirectResult: |
| 4790 | if (!isValidSwiftIndirectResultType(type)) { |
| 4791 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4792 | << getParameterABISpelling(abi) |
| 4793 | << /*pointer*/ 0 << type; |
| 4794 | } |
| 4795 | D->addAttr(::new (Context) |
| 4796 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4797 | return; |
| 4798 | } |
| 4799 | llvm_unreachable("bad parameter ABI attribute"); |
| 4800 | } |
| 4801 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4802 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4803 | /// otherwise setting numParams to the appropriate value. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4804 | bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4805 | if (AL.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4806 | return true; |
| 4807 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4808 | if (!checkAttributeNumArgs(*this, AL, 1)) { |
| 4809 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4810 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4811 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4812 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4813 | uint32_t NP; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4814 | Expr *NumParamsExpr = AL.getArgAsExpr(0); |
| 4815 | if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) { |
| 4816 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4817 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4820 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4821 | Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4822 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4823 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4824 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4827 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4828 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4829 | Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4830 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4831 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4832 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4835 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4838 | // Checks whether an argument of launch_bounds attribute is |
| 4839 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4840 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4841 | // and may output an error. |
| 4842 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4843 | const CUDALaunchBoundsAttr &AL, |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4844 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4845 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4846 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4847 | |
| 4848 | // Accept template arguments for now as they depend on something else. |
| 4849 | // We'll get to check them when they eventually get instantiated. |
| 4850 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4851 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4852 | |
| 4853 | llvm::APSInt I(64); |
| 4854 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4855 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4856 | << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4857 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4858 | } |
| 4859 | // Make sure we can fit it in 32 bits. |
| 4860 | if (!I.isIntN(32)) { |
| 4861 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4862 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4863 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4864 | } |
| 4865 | if (I < 0) |
| 4866 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4867 | << &AL << Idx << E->getSourceRange(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4868 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4869 | // We may need to perform implicit conversion of the argument. |
| 4870 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4871 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4872 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4873 | assert(!ValArg.isInvalid() && |
| 4874 | "Unexpected PerformCopyInitialization() failure."); |
| 4875 | |
| 4876 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4880 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4881 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4882 | SpellingListIndex); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4883 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4884 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4885 | return; |
| 4886 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4887 | if (MinBlocks) { |
| 4888 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4889 | if (MinBlocks == nullptr) |
| 4890 | return; |
| 4891 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4892 | |
| 4893 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4894 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4895 | } |
| 4896 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4897 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4898 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 4899 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4900 | return; |
| 4901 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4902 | S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 4903 | AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr, |
| 4904 | AL.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4907 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4908 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4909 | if (!AL.isArgIdent(0)) { |
| 4910 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4911 | << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4912 | return; |
| 4913 | } |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4914 | |
| 4915 | ParamIdx ArgumentIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4916 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4917 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4918 | return; |
| 4919 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4920 | ParamIdx TypeTagIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4921 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4922 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4923 | return; |
| 4924 | |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 4925 | bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag"; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4926 | if (IsPointer) { |
| 4927 | // Ensure that buffer has a pointer type. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4928 | unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex(); |
| 4929 | if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) || |
| 4930 | !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType()) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4931 | S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 4934 | D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr( |
| 4935 | AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx, |
| 4936 | TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4940 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4941 | if (!AL.isArgIdent(0)) { |
| 4942 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4943 | << AL << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4944 | return; |
| 4945 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4946 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4947 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4948 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4949 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4950 | if (!isa<VarDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4951 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4952 | << AL << ExpectedVariable; |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4953 | return; |
| 4954 | } |
| 4955 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4956 | IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4957 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4958 | S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4959 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4960 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4961 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4962 | TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4963 | MatchingCTypeLoc, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4964 | AL.getLayoutCompatible(), |
| 4965 | AL.getMustBeNull(), |
| 4966 | AL.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4969 | static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4970 | ParamIdx ArgCount; |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4971 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4972 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0), |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4973 | ArgCount, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4974 | true /* CanIndexImplicitThis */)) |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4975 | return; |
| 4976 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4977 | // ArgCount isn't a parameter index [0;n), it's a count [1;n] |
| 4978 | D->addAttr(::new (S.Context) XRayLogArgsAttr( |
| 4979 | AL.getRange(), S.Context, ArgCount.getSourceIndex(), |
| 4980 | AL.getAttributeSpellingListIndex())); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4983 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4984 | // Checker-specific attribute handlers. |
| 4985 | //===----------------------------------------------------------------------===// |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4986 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) { |
| 4987 | return QT->isDependentType() || QT->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 4990 | static bool isValidSubjectOfNSAttribute(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4991 | return QT->isDependentType() || QT->isObjCObjectPointerType() || |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 4992 | QT->isObjCNSObjectType(); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4993 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4994 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 4995 | static bool isValidSubjectOfCFAttribute(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4996 | return QT->isDependentType() || QT->isPointerType() || |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 4997 | isValidSubjectOfNSAttribute(QT); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5000 | static bool isValidSubjectOfOSAttribute(QualType QT) { |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5001 | if (QT->isDependentType()) |
| 5002 | return true; |
| 5003 | QualType PT = QT->getPointeeType(); |
| 5004 | return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr; |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 5005 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5006 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5007 | void Sema::AddXConsumedAttr(Decl *D, SourceRange SR, unsigned SpellingIndex, |
| 5008 | RetainOwnershipKind K, |
| 5009 | bool IsTemplateInstantiation) { |
| 5010 | ValueDecl *VD = cast<ValueDecl>(D); |
| 5011 | switch (K) { |
| 5012 | case RetainOwnershipKind::OS: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5013 | handleSimpleAttributeOrDiagnose<OSConsumedAttr>( |
| 5014 | *this, VD, SR, SpellingIndex, isValidSubjectOfOSAttribute(VD->getType()), |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5015 | diag::warn_ns_attribute_wrong_parameter_type, |
| 5016 | /*ExtraArgs=*/SR, "os_consumed", /*pointers*/ 1); |
| 5017 | return; |
| 5018 | case RetainOwnershipKind::NS: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5019 | handleSimpleAttributeOrDiagnose<NSConsumedAttr>( |
| 5020 | *this, VD, SR, SpellingIndex, isValidSubjectOfNSAttribute(VD->getType()), |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 5021 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5022 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 5023 | // is significant. Allow non-dependent code to contain inappropriate |
| 5024 | // attributes even in ARC, but require template instantiations to be |
| 5025 | // set up correctly. |
| 5026 | ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount) |
| 5027 | ? diag::err_ns_attribute_wrong_parameter_type |
| 5028 | : diag::warn_ns_attribute_wrong_parameter_type), |
| 5029 | /*ExtraArgs=*/SR, "ns_consumed", /*objc pointers*/ 0); |
| 5030 | return; |
| 5031 | case RetainOwnershipKind::CF: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5032 | handleSimpleAttributeOrDiagnose<CFConsumedAttr>( |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5033 | *this, VD, SR, SpellingIndex, |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5034 | isValidSubjectOfCFAttribute(VD->getType()), |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5035 | diag::warn_ns_attribute_wrong_parameter_type, |
| 5036 | /*ExtraArgs=*/SR, "cf_consumed", /*pointers*/1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5037 | return; |
| 5038 | } |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5039 | } |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5040 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5041 | static Sema::RetainOwnershipKind |
| 5042 | parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) { |
| 5043 | switch (AL.getKind()) { |
| 5044 | case ParsedAttr::AT_CFConsumed: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5045 | case ParsedAttr::AT_CFReturnsRetained: |
| 5046 | case ParsedAttr::AT_CFReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5047 | return Sema::RetainOwnershipKind::CF; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5048 | case ParsedAttr::AT_OSConsumesThis: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5049 | case ParsedAttr::AT_OSConsumed: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5050 | case ParsedAttr::AT_OSReturnsRetained: |
| 5051 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5052 | case ParsedAttr::AT_OSReturnsRetainedOnZero: |
| 5053 | case ParsedAttr::AT_OSReturnsRetainedOnNonZero: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5054 | return Sema::RetainOwnershipKind::OS; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5055 | case ParsedAttr::AT_NSConsumesSelf: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5056 | case ParsedAttr::AT_NSConsumed: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5057 | case ParsedAttr::AT_NSReturnsRetained: |
| 5058 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 5059 | case ParsedAttr::AT_NSReturnsAutoreleased: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5060 | return Sema::RetainOwnershipKind::NS; |
| 5061 | default: |
| 5062 | llvm_unreachable("Wrong argument supplied"); |
| 5063 | } |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5066 | bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) { |
| 5067 | if (isValidSubjectOfNSReturnsRetainedAttribute(QT)) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5068 | return false; |
| 5069 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5070 | Diag(Loc, diag::warn_ns_attribute_wrong_return_type) |
| 5071 | << "'ns_returns_retained'" << 0 << 0; |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5072 | return true; |
| 5073 | } |
| 5074 | |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5075 | /// \return whether the parameter is a pointer to OSObject pointer. |
| 5076 | static bool isValidOSObjectOutParameter(const Decl *D) { |
| 5077 | const auto *PVD = dyn_cast<ParmVarDecl>(D); |
| 5078 | if (!PVD) |
| 5079 | return false; |
| 5080 | QualType QT = PVD->getType(); |
| 5081 | QualType PT = QT->getPointeeType(); |
| 5082 | return !PT.isNull() && isValidSubjectOfOSAttribute(PT); |
| 5083 | } |
| 5084 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5085 | static void handleXReturnsXRetainedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5086 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5087 | QualType ReturnType; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5088 | Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5089 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5090 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5091 | ReturnType = MD->getReturnType(); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5092 | } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
| 5093 | (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5094 | return; // ignore: was handled as a type attribute |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5095 | } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5096 | ReturnType = PD->getType(); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5097 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5098 | ReturnType = FD->getReturnType(); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5099 | } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 5100 | // Attributes on parameters are used for out-parameters, |
| 5101 | // passed as pointers-to-pointers. |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5102 | unsigned DiagID = K == Sema::RetainOwnershipKind::CF |
| 5103 | ? /*pointer-to-CF-pointer*/2 |
| 5104 | : /*pointer-to-OSObject-pointer*/3; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5105 | ReturnType = Param->getType()->getPointeeType(); |
| 5106 | if (ReturnType.isNull()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5107 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5108 | << AL << DiagID << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5109 | return; |
| 5110 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5111 | } else if (AL.isUsedAsTypeAttr()) { |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5112 | return; |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5113 | } else { |
| 5114 | AttributeDeclKind ExpectedDeclKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5115 | switch (AL.getKind()) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5116 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5117 | case ParsedAttr::AT_NSReturnsRetained: |
| 5118 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5119 | case ParsedAttr::AT_NSReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5120 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 5121 | break; |
| 5122 | |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5123 | case ParsedAttr::AT_OSReturnsRetained: |
| 5124 | case ParsedAttr::AT_OSReturnsNotRetained: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5125 | case ParsedAttr::AT_CFReturnsRetained: |
| 5126 | case ParsedAttr::AT_CFReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5127 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 5128 | break; |
| 5129 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5130 | S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5131 | << AL.getRange() << AL << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5132 | return; |
| 5133 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5134 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5135 | bool TypeOK; |
| 5136 | bool Cf; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5137 | unsigned ParmDiagID = 2; // Pointer-to-CF-pointer |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5138 | switch (AL.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5139 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5140 | case ParsedAttr::AT_NSReturnsRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5141 | TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType); |
| 5142 | Cf = false; |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 5143 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5144 | |
| 5145 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5146 | case ParsedAttr::AT_NSReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5147 | TypeOK = isValidSubjectOfNSAttribute(ReturnType); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5148 | Cf = false; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5149 | break; |
| 5150 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5151 | case ParsedAttr::AT_CFReturnsRetained: |
| 5152 | case ParsedAttr::AT_CFReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5153 | TypeOK = isValidSubjectOfCFAttribute(ReturnType); |
| 5154 | Cf = true; |
| 5155 | break; |
| 5156 | |
| 5157 | case ParsedAttr::AT_OSReturnsRetained: |
| 5158 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5159 | TypeOK = isValidSubjectOfOSAttribute(ReturnType); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5160 | Cf = true; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5161 | ParmDiagID = 3; // Pointer-to-OSObject-pointer |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5162 | break; |
| 5163 | } |
| 5164 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5165 | if (!TypeOK) { |
| 5166 | if (AL.isUsedAsTypeAttr()) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5167 | return; |
| 5168 | |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5169 | if (isa<ParmVarDecl>(D)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5170 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5171 | << AL << ParmDiagID << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5172 | } else { |
| 5173 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 5174 | enum : unsigned { |
| 5175 | Function, |
| 5176 | Method, |
| 5177 | Property |
| 5178 | } SubjectKind = Function; |
| 5179 | if (isa<ObjCMethodDecl>(D)) |
| 5180 | SubjectKind = Method; |
| 5181 | else if (isa<ObjCPropertyDecl>(D)) |
| 5182 | SubjectKind = Property; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5183 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5184 | << AL << SubjectKind << Cf << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5185 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5186 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 5187 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5188 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5189 | switch (AL.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5190 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5191 | llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5192 | case ParsedAttr::AT_NSReturnsAutoreleased: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5193 | handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5194 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5195 | case ParsedAttr::AT_CFReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5196 | handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 5197 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5198 | case ParsedAttr::AT_NSReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5199 | handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 5200 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5201 | case ParsedAttr::AT_CFReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5202 | handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5203 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5204 | case ParsedAttr::AT_NSReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5205 | handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL); |
| 5206 | return; |
| 5207 | case ParsedAttr::AT_OSReturnsRetained: |
| 5208 | handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL); |
| 5209 | return; |
| 5210 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5211 | handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5212 | return; |
| 5213 | }; |
| 5214 | } |
| 5215 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5216 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5217 | const ParsedAttr &Attrs) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 5218 | const int EP_ObjCMethod = 1; |
| 5219 | const int EP_ObjCProperty = 2; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5220 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 5221 | SourceLocation loc = Attrs.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 5222 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5223 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5224 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 5225 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5226 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5227 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 5228 | if (!resultType->isReferenceType() && |
| 5229 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5230 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5231 | << SourceRange(loc) << Attrs |
| 5232 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
| 5233 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5234 | |
| 5235 | // Drop the attribute. |
| 5236 | return; |
| 5237 | } |
| 5238 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 5239 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 5240 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5243 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5244 | const ParsedAttr &Attrs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5245 | const auto *Method = cast<ObjCMethodDecl>(D); |
| 5246 | |
| 5247 | const DeclContext *DC = Method->getDeclContext(); |
| 5248 | if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5249 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5250 | << 0; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5251 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 5252 | return; |
| 5253 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5254 | if (Method->getMethodFamily() == OMF_dealloc) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5255 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5256 | << 1; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5257 | return; |
| 5258 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5259 | |
| 5260 | D->addAttr(::new (S.Context) ObjCRequiresSuperAttr( |
| 5261 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5264 | static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5265 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 5266 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5267 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5268 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5269 | return; |
| 5270 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5271 | |
| 5272 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5273 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5274 | if (!Parm->Ident->isStr("id")) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5275 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL; |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5276 | return; |
| 5277 | } |
| 5278 | |
| 5279 | // Only allow 'cv void *'. |
| 5280 | QualType T = TD->getUnderlyingType(); |
| 5281 | if (!T->isVoidPointerType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5282 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5283 | return; |
| 5284 | } |
| 5285 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5286 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5287 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5288 | ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident, |
| 5289 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5292 | static void handleObjCBridgeMutableAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5293 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5294 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5295 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5296 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5297 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5298 | return; |
| 5299 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5300 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5301 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5302 | ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident, |
| 5303 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5306 | static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5307 | const ParsedAttr &AL) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5308 | IdentifierInfo *RelatedClass = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5309 | AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5310 | if (!RelatedClass) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5311 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5312 | return; |
| 5313 | } |
| 5314 | IdentifierInfo *ClassMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5315 | AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5316 | IdentifierInfo *InstanceMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5317 | AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5318 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5319 | ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5320 | ClassMethod, InstanceMethod, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5321 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5324 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5325 | const ParsedAttr &AL) { |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 5326 | DeclContext *Ctx = D->getDeclContext(); |
| 5327 | |
| 5328 | // This attribute can only be applied to methods in interfaces or class |
| 5329 | // extensions. |
| 5330 | if (!isa<ObjCInterfaceDecl>(Ctx) && |
| 5331 | !(isa<ObjCCategoryDecl>(Ctx) && |
| 5332 | cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) { |
| 5333 | S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init); |
| 5334 | return; |
| 5335 | } |
| 5336 | |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 5337 | ObjCInterfaceDecl *IFace; |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 5338 | if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx)) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 5339 | IFace = CatDecl->getClassInterface(); |
| 5340 | else |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 5341 | IFace = cast<ObjCInterfaceDecl>(Ctx); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5342 | |
| 5343 | if (!IFace) |
| 5344 | return; |
| 5345 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 5346 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 5347 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5348 | ObjCDesignatedInitializerAttr(AL.getRange(), S.Context, |
| 5349 | AL.getAttributeSpellingListIndex())); |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5352 | static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5353 | StringRef MetaDataName; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5354 | if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName)) |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5355 | return; |
| 5356 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5357 | ObjCRuntimeNameAttr(AL.getRange(), S.Context, |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5358 | MetaDataName, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5359 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 5362 | // When a user wants to use objc_boxable with a union or struct |
| 5363 | // but they don't have access to the declaration (legacy/third-party code) |
| 5364 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5365 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5366 | static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5367 | bool notify = false; |
| 5368 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5369 | auto *RD = dyn_cast<RecordDecl>(D); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5370 | if (RD && RD->getDefinition()) { |
| 5371 | RD = RD->getDefinition(); |
| 5372 | notify = true; |
| 5373 | } |
| 5374 | |
| 5375 | if (RD) { |
| 5376 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5377 | ObjCBoxableAttr(AL.getRange(), S.Context, |
| 5378 | AL.getAttributeSpellingListIndex()); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5379 | RD->addAttr(BoxableAttr); |
| 5380 | if (notify) { |
| 5381 | // we need to notify ASTReader/ASTWriter about |
| 5382 | // modification of existing declaration |
| 5383 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 5384 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 5385 | } |
| 5386 | } |
| 5387 | } |
| 5388 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5389 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5390 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5391 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5392 | S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5393 | << AL.getRange() << AL << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5396 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5397 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5398 | const auto *VD = cast<ValueDecl>(D); |
| 5399 | QualType QT = VD->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5400 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5401 | if (!QT->isDependentType() && |
| 5402 | !QT->isObjCLifetimeType()) { |
| 5403 | S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
| 5404 | << QT; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5405 | return; |
| 5406 | } |
| 5407 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5408 | Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5409 | |
| 5410 | // If we have no lifetime yet, check the lifetime we're presumably |
| 5411 | // going to infer. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5412 | if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType()) |
| 5413 | Lifetime = QT->getObjCARCImplicitLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5414 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5415 | switch (Lifetime) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5416 | case Qualifiers::OCL_None: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5417 | assert(QT->isDependentType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5418 | "didn't infer lifetime for non-dependent type?"); |
| 5419 | break; |
| 5420 | |
| 5421 | case Qualifiers::OCL_Weak: // meaningful |
| 5422 | case Qualifiers::OCL_Strong: // meaningful |
| 5423 | break; |
| 5424 | |
| 5425 | case Qualifiers::OCL_ExplicitNone: |
| 5426 | case Qualifiers::OCL_Autoreleasing: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5427 | S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
| 5428 | << (Lifetime == Qualifiers::OCL_Autoreleasing); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5429 | break; |
| 5430 | } |
| 5431 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5432 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5433 | ObjCPreciseLifetimeAttr(AL.getRange(), S.Context, |
| 5434 | AL.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5437 | //===----------------------------------------------------------------------===// |
| 5438 | // Microsoft specific attribute handlers. |
| 5439 | //===----------------------------------------------------------------------===// |
| 5440 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5441 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 5442 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 5443 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 5444 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5445 | return nullptr; |
| 5446 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 5447 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 5448 | D->dropAttr<UuidAttr>(); |
| 5449 | } |
| 5450 | |
| 5451 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 5452 | } |
| 5453 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5454 | static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5455 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5456 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5457 | << AL << AttributeLangSupport::C; |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5458 | return; |
| 5459 | } |
| 5460 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5461 | StringRef StrRef; |
| 5462 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5463 | if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5464 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5465 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5466 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 5467 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5468 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 5469 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5470 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5471 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5472 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5473 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5474 | return; |
| 5475 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 5476 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5477 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5478 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5479 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5480 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5481 | return; |
| 5482 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5483 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5484 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5485 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5486 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5487 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5488 | |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5489 | // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's |
| 5490 | // the only thing in the [] list, the [] too), and add an insertion of |
| 5491 | // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas |
| 5492 | // separating attributes nor of the [ and the ] are in the AST. |
Nico Weber | 0a23404 | 2017-05-05 17:15:08 +0000 | [diff] [blame] | 5493 | // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc" |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5494 | // on cfe-dev. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5495 | if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling. |
| 5496 | S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated); |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5497 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5498 | UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(), |
| 5499 | AL.getAttributeSpellingListIndex(), StrRef); |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5500 | if (UA) |
| 5501 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5504 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5505 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5506 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5507 | << AL << AttributeLangSupport::C; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5508 | return; |
| 5509 | } |
| 5510 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5511 | D, AL.getRange(), /*BestCase=*/true, |
| 5512 | AL.getAttributeSpellingListIndex(), |
| 5513 | (MSInheritanceAttr::Spelling)AL.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5514 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5515 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5516 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 5517 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5520 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5521 | const auto *VD = cast<VarDecl>(D); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5522 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5523 | S.Diag(AL.getLoc(), diag::err_thread_unsupported); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5524 | return; |
| 5525 | } |
| 5526 | if (VD->getTSCSpec() != TSCS_unspecified) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5527 | S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5528 | return; |
| 5529 | } |
| 5530 | if (VD->hasLocalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5531 | S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5532 | return; |
| 5533 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5534 | D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context, |
| 5535 | AL.getAttributeSpellingListIndex())); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5538 | static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5539 | SmallVector<StringRef, 4> Tags; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5540 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5541 | StringRef Tag; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5542 | if (!S.checkStringLiteralArgumentAttr(AL, I, Tag)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5543 | return; |
| 5544 | Tags.push_back(Tag); |
| 5545 | } |
| 5546 | |
| 5547 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 5548 | if (!NS->isInline()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5549 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5550 | return; |
| 5551 | } |
| 5552 | if (NS->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5553 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5554 | return; |
| 5555 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5556 | if (AL.getNumArgs() == 0) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5557 | Tags.push_back(NS->getName()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5558 | } else if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5559 | return; |
| 5560 | |
| 5561 | // Store tags sorted and without duplicates. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 5562 | llvm::sort(Tags); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5563 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 5564 | |
| 5565 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5566 | AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(), |
| 5567 | AL.getAttributeSpellingListIndex())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5570 | static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5571 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5572 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5573 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5574 | return; |
| 5575 | } |
| 5576 | |
| 5577 | StringRef Str; |
| 5578 | SourceLocation ArgLoc; |
| 5579 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5580 | if (AL.getNumArgs() == 0) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5581 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5582 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5583 | return; |
| 5584 | |
| 5585 | ARMInterruptAttr::InterruptType Kind; |
| 5586 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5587 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5588 | << ArgLoc; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5589 | return; |
| 5590 | } |
| 5591 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5592 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5593 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5594 | ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5597 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5598 | // MSP430 'interrupt' attribute is applied to |
| 5599 | // a function with no parameters and void return type. |
| 5600 | if (!isFunctionOrMethod(D)) { |
| 5601 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5602 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5603 | return; |
| 5604 | } |
| 5605 | |
| 5606 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5607 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5608 | << /*MSP430*/ 1 << 0; |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5609 | return; |
| 5610 | } |
| 5611 | |
| 5612 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5613 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5614 | << /*MSP430*/ 1 << 1; |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5615 | return; |
| 5616 | } |
| 5617 | |
| 5618 | // The attribute takes one integer argument. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5619 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5620 | return; |
| 5621 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5622 | if (!AL.isArgExpr(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5623 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 5624 | << AL << AANT_ArgumentIntegerConstant; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5625 | return; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5628 | Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5629 | llvm::APSInt NumParams(32); |
| 5630 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5631 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5632 | << AL << AANT_ArgumentIntegerConstant |
| 5633 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5634 | return; |
| 5635 | } |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5636 | // The argument should be in range 0..63. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5637 | unsigned Num = NumParams.getLimitedValue(255); |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5638 | if (Num > 63) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5639 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5640 | << AL << (int)NumParams.getSExtValue() |
| 5641 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5642 | return; |
| 5643 | } |
| 5644 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5645 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5646 | MSP430InterruptAttr(AL.getLoc(), S.Context, Num, |
| 5647 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5648 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5651 | static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5652 | // Only one optional argument permitted. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5653 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5654 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5655 | return; |
| 5656 | } |
| 5657 | |
| 5658 | StringRef Str; |
| 5659 | SourceLocation ArgLoc; |
| 5660 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5661 | if (AL.getNumArgs() == 0) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5662 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5663 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5664 | return; |
| 5665 | |
| 5666 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 5667 | // a) Must be a function. |
| 5668 | // b) Must have no parameters. |
| 5669 | // c) Must have the 'void' return type. |
| 5670 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 5671 | // lacks the 'eret' instruction. |
| 5672 | // e) The attribute itself must either have no argument or one of the |
| 5673 | // valid interrupt types, see [MipsInterruptDocs]. |
| 5674 | |
| 5675 | if (!isFunctionOrMethod(D)) { |
| 5676 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5677 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5678 | return; |
| 5679 | } |
| 5680 | |
| 5681 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5682 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5683 | << /*MIPS*/ 0 << 0; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5684 | return; |
| 5685 | } |
| 5686 | |
| 5687 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5688 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5689 | << /*MIPS*/ 0 << 1; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5690 | return; |
| 5691 | } |
| 5692 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5693 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5694 | return; |
| 5695 | |
| 5696 | MipsInterruptAttr::InterruptType Kind; |
| 5697 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5698 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5699 | << AL << "'" + std::string(Str) + "'"; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5700 | return; |
| 5701 | } |
| 5702 | |
| 5703 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5704 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5707 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5708 | // Semantic checks for a function with the 'interrupt' attribute. |
| 5709 | // a) Must be a function. |
| 5710 | // b) Must have the 'void' return type. |
| 5711 | // c) Must take 1 or 2 arguments. |
| 5712 | // d) The 1st argument must be a pointer. |
| 5713 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 5714 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5715 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5716 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5717 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5718 | << AL << ExpectedFunctionWithProtoType; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5719 | return; |
| 5720 | } |
| 5721 | // Interrupt handler must have void return type. |
| 5722 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5723 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5724 | diag::err_anyx86_interrupt_attribute) |
| 5725 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5726 | ? 0 |
| 5727 | : 1) |
| 5728 | << 0; |
| 5729 | return; |
| 5730 | } |
| 5731 | // Interrupt handler must have 1 or 2 parameters. |
| 5732 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5733 | if (NumParams < 1 || NumParams > 2) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5734 | S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute) |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5735 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5736 | ? 0 |
| 5737 | : 1) |
| 5738 | << 1; |
| 5739 | return; |
| 5740 | } |
| 5741 | // The first argument must be a pointer. |
| 5742 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5743 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5744 | diag::err_anyx86_interrupt_attribute) |
| 5745 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5746 | ? 0 |
| 5747 | : 1) |
| 5748 | << 2; |
| 5749 | return; |
| 5750 | } |
| 5751 | // The second argument, if present, must be an unsigned integer. |
| 5752 | unsigned TypeSize = |
| 5753 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5754 | ? 64 |
| 5755 | : 32; |
| 5756 | if (NumParams == 2 && |
| 5757 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5758 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5759 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5760 | diag::err_anyx86_interrupt_attribute) |
| 5761 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5762 | ? 0 |
| 5763 | : 1) |
| 5764 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 5765 | return; |
| 5766 | } |
| 5767 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5768 | AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex())); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5769 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5770 | } |
| 5771 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5772 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5773 | if (!isFunctionOrMethod(D)) { |
| 5774 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5775 | << "'interrupt'" << ExpectedFunction; |
| 5776 | return; |
| 5777 | } |
| 5778 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5779 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5780 | return; |
| 5781 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5782 | handleSimpleAttribute<AVRInterruptAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5785 | static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5786 | if (!isFunctionOrMethod(D)) { |
| 5787 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5788 | << "'signal'" << ExpectedFunction; |
| 5789 | return; |
| 5790 | } |
| 5791 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5792 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5793 | return; |
| 5794 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5795 | handleSimpleAttribute<AVRSignalAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 5798 | static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5799 | if (!isFunctionOrMethod(D)) { |
| 5800 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5801 | << "'import_module'" << ExpectedFunction; |
| 5802 | return; |
| 5803 | } |
| 5804 | |
| 5805 | auto *FD = cast<FunctionDecl>(D); |
| 5806 | if (FD->isThisDeclarationADefinition()) { |
| 5807 | S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; |
| 5808 | return; |
| 5809 | } |
| 5810 | |
| 5811 | StringRef Str; |
| 5812 | SourceLocation ArgLoc; |
| 5813 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5814 | return; |
| 5815 | |
| 5816 | FD->addAttr(::new (S.Context) WebAssemblyImportModuleAttr( |
| 5817 | AL.getRange(), S.Context, Str, |
| 5818 | AL.getAttributeSpellingListIndex())); |
| 5819 | } |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5820 | |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 5821 | static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5822 | if (!isFunctionOrMethod(D)) { |
| 5823 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5824 | << "'import_name'" << ExpectedFunction; |
| 5825 | return; |
| 5826 | } |
| 5827 | |
| 5828 | auto *FD = cast<FunctionDecl>(D); |
| 5829 | if (FD->isThisDeclarationADefinition()) { |
| 5830 | S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; |
| 5831 | return; |
| 5832 | } |
| 5833 | |
| 5834 | StringRef Str; |
| 5835 | SourceLocation ArgLoc; |
| 5836 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5837 | return; |
| 5838 | |
| 5839 | FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr( |
| 5840 | AL.getRange(), S.Context, Str, |
| 5841 | AL.getAttributeSpellingListIndex())); |
| 5842 | } |
| 5843 | |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5844 | static void handleRISCVInterruptAttr(Sema &S, Decl *D, |
| 5845 | const ParsedAttr &AL) { |
| 5846 | // Warn about repeated attributes. |
| 5847 | if (const auto *A = D->getAttr<RISCVInterruptAttr>()) { |
| 5848 | S.Diag(AL.getRange().getBegin(), |
| 5849 | diag::warn_riscv_repeated_interrupt_attribute); |
| 5850 | S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute); |
| 5851 | return; |
| 5852 | } |
| 5853 | |
| 5854 | // Check the attribute argument. Argument is optional. |
| 5855 | if (!checkAttributeAtMostNumArgs(S, AL, 1)) |
| 5856 | return; |
| 5857 | |
| 5858 | StringRef Str; |
| 5859 | SourceLocation ArgLoc; |
| 5860 | |
| 5861 | // 'machine'is the default interrupt mode. |
| 5862 | if (AL.getNumArgs() == 0) |
| 5863 | Str = "machine"; |
| 5864 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5865 | return; |
| 5866 | |
| 5867 | // Semantic checks for a function with the 'interrupt' attribute: |
| 5868 | // - Must be a function. |
| 5869 | // - Must have no parameters. |
| 5870 | // - Must have the 'void' return type. |
| 5871 | // - The attribute itself must either have no argument or one of the |
| 5872 | // valid interrupt types, see [RISCVInterruptDocs]. |
| 5873 | |
| 5874 | if (D->getFunctionType() == nullptr) { |
| 5875 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5876 | << "'interrupt'" << ExpectedFunction; |
| 5877 | return; |
| 5878 | } |
| 5879 | |
| 5880 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5881 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5882 | << /*RISC-V*/ 2 << 0; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5883 | return; |
| 5884 | } |
| 5885 | |
| 5886 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5887 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5888 | << /*RISC-V*/ 2 << 1; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5889 | return; |
| 5890 | } |
| 5891 | |
| 5892 | RISCVInterruptAttr::InterruptType Kind; |
| 5893 | if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5894 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5895 | << ArgLoc; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5896 | return; |
| 5897 | } |
| 5898 | |
| 5899 | D->addAttr(::new (S.Context) RISCVInterruptAttr( |
| 5900 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
| 5901 | } |
| 5902 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5903 | static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5904 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5905 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5906 | case llvm::Triple::msp430: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5907 | handleMSP430InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5908 | break; |
| 5909 | case llvm::Triple::mipsel: |
| 5910 | case llvm::Triple::mips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5911 | handleMipsInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5912 | break; |
| 5913 | case llvm::Triple::x86: |
| 5914 | case llvm::Triple::x86_64: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5915 | handleAnyX86InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5916 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5917 | case llvm::Triple::avr: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5918 | handleAVRInterruptAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5919 | break; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5920 | case llvm::Triple::riscv32: |
| 5921 | case llvm::Triple::riscv64: |
| 5922 | handleRISCVInterruptAttr(S, D, AL); |
| 5923 | break; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5924 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5925 | handleARMInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5926 | break; |
| 5927 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5928 | } |
| 5929 | |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5930 | static bool |
| 5931 | checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr, |
| 5932 | const AMDGPUFlatWorkGroupSizeAttr &Attr) { |
| 5933 | // Accept template arguments for now as they depend on something else. |
| 5934 | // We'll get to check them when they eventually get instantiated. |
| 5935 | if (MinExpr->isValueDependent() || MaxExpr->isValueDependent()) |
| 5936 | return false; |
| 5937 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5938 | uint32_t Min = 0; |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5939 | if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0)) |
| 5940 | return true; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5941 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5942 | uint32_t Max = 0; |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5943 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1)) |
| 5944 | return true; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5945 | |
| 5946 | if (Min == 0 && Max != 0) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5947 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5948 | << &Attr << 0; |
| 5949 | return true; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5950 | } |
| 5951 | if (Min > Max) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5952 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5953 | << &Attr << 1; |
| 5954 | return true; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5955 | } |
| 5956 | |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5957 | return false; |
| 5958 | } |
| 5959 | |
| 5960 | void Sema::addAMDGPUFlatWorkGroupSizeAttr(SourceRange AttrRange, Decl *D, |
| 5961 | Expr *MinExpr, Expr *MaxExpr, |
| 5962 | unsigned SpellingListIndex) { |
| 5963 | AMDGPUFlatWorkGroupSizeAttr TmpAttr(AttrRange, Context, MinExpr, MaxExpr, |
| 5964 | SpellingListIndex); |
| 5965 | |
| 5966 | if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr)) |
| 5967 | return; |
| 5968 | |
| 5969 | D->addAttr(::new (Context) AMDGPUFlatWorkGroupSizeAttr( |
| 5970 | AttrRange, Context, MinExpr, MaxExpr, SpellingListIndex)); |
| 5971 | } |
| 5972 | |
| 5973 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
| 5974 | const ParsedAttr &AL) { |
| 5975 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 5976 | Expr *MaxExpr = AL.getArgAsExpr(1); |
| 5977 | |
| 5978 | S.addAMDGPUFlatWorkGroupSizeAttr(AL.getRange(), D, MinExpr, MaxExpr, |
| 5979 | AL.getAttributeSpellingListIndex()); |
| 5980 | } |
| 5981 | |
| 5982 | static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr, |
| 5983 | Expr *MaxExpr, |
| 5984 | const AMDGPUWavesPerEUAttr &Attr) { |
| 5985 | if (S.DiagnoseUnexpandedParameterPack(MinExpr) || |
| 5986 | (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr))) |
| 5987 | return true; |
| 5988 | |
| 5989 | // Accept template arguments for now as they depend on something else. |
| 5990 | // We'll get to check them when they eventually get instantiated. |
| 5991 | if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent())) |
| 5992 | return false; |
| 5993 | |
| 5994 | uint32_t Min = 0; |
| 5995 | if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0)) |
| 5996 | return true; |
| 5997 | |
| 5998 | uint32_t Max = 0; |
| 5999 | if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1)) |
| 6000 | return true; |
| 6001 | |
| 6002 | if (Min == 0 && Max != 0) { |
| 6003 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 6004 | << &Attr << 0; |
| 6005 | return true; |
| 6006 | } |
| 6007 | if (Max != 0 && Min > Max) { |
| 6008 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 6009 | << &Attr << 1; |
| 6010 | return true; |
| 6011 | } |
| 6012 | |
| 6013 | return false; |
| 6014 | } |
| 6015 | |
| 6016 | void Sema::addAMDGPUWavesPerEUAttr(SourceRange AttrRange, Decl *D, |
| 6017 | Expr *MinExpr, Expr *MaxExpr, |
| 6018 | unsigned SpellingListIndex) { |
| 6019 | AMDGPUWavesPerEUAttr TmpAttr(AttrRange, Context, MinExpr, MaxExpr, |
| 6020 | SpellingListIndex); |
| 6021 | |
| 6022 | if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr)) |
| 6023 | return; |
| 6024 | |
| 6025 | D->addAttr(::new (Context) AMDGPUWavesPerEUAttr(AttrRange, Context, MinExpr, |
| 6026 | MaxExpr, SpellingListIndex)); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6029 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6030 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 6031 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
| 6032 | return; |
| 6033 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6034 | Expr *MinExpr = AL.getArgAsExpr(0); |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6035 | Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6036 | |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6037 | S.addAMDGPUWavesPerEUAttr(AL.getRange(), D, MinExpr, MaxExpr, |
| 6038 | AL.getAttributeSpellingListIndex()); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6039 | } |
| 6040 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6041 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6042 | uint32_t NumSGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6043 | Expr *NumSGPRExpr = AL.getArgAsExpr(0); |
| 6044 | if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6045 | return; |
| 6046 | |
| 6047 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6048 | AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR, |
| 6049 | AL.getAttributeSpellingListIndex())); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6052 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6053 | uint32_t NumVGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6054 | Expr *NumVGPRExpr = AL.getArgAsExpr(0); |
| 6055 | if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6056 | return; |
| 6057 | |
| 6058 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6059 | AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR, |
| 6060 | AL.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6061 | } |
| 6062 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6063 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6064 | const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6065 | // If we try to apply it to a function pointer, don't warn, but don't |
| 6066 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 6067 | // special about calling a force_align_arg_pointer function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6068 | const auto *VD = dyn_cast<ValueDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6069 | if (VD && VD->getType()->isFunctionPointerType()) |
| 6070 | return; |
| 6071 | // Also don't warn on function pointer typedefs. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6072 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6073 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 6074 | TD->getUnderlyingType()->isFunctionType())) |
| 6075 | return; |
| 6076 | // Attribute can only be applied to function types. |
| 6077 | if (!isa<FunctionDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6078 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6079 | << AL << ExpectedFunction; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6080 | return; |
| 6081 | } |
| 6082 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6083 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6084 | X86ForceAlignArgPointerAttr(AL.getRange(), S.Context, |
| 6085 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6086 | } |
| 6087 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6088 | static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6089 | uint32_t Version; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6090 | Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
| 6091 | if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version)) |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6092 | return; |
| 6093 | |
| 6094 | // TODO: Investigate what happens with the next major version of MSVC. |
Reid Kleckner | 1a94d87 | 2018-12-17 23:16:43 +0000 | [diff] [blame] | 6095 | if (Version != LangOptions::MSVC2015 / 100) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6096 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6097 | << AL << Version << VersionExpr->getSourceRange(); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6098 | return; |
| 6099 | } |
| 6100 | |
Reid Kleckner | 1a94d87 | 2018-12-17 23:16:43 +0000 | [diff] [blame] | 6101 | // The attribute expects a "major" version number like 19, but new versions of |
| 6102 | // MSVC have moved to updating the "minor", or less significant numbers, so we |
| 6103 | // have to multiply by 100 now. |
| 6104 | Version *= 100; |
| 6105 | |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6106 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6107 | LayoutVersionAttr(AL.getRange(), S.Context, Version, |
| 6108 | AL.getAttributeSpellingListIndex())); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6111 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 6112 | unsigned AttrSpellingListIndex) { |
| 6113 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 6114 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6115 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6116 | } |
| 6117 | |
| 6118 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6119 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6120 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 6121 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6124 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 6125 | unsigned AttrSpellingListIndex) { |
| 6126 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 6127 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6128 | D->dropAttr<DLLImportAttr>(); |
| 6129 | } |
| 6130 | |
| 6131 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6132 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6133 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 6134 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6135 | } |
| 6136 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6137 | static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 6138 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 6139 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6140 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A; |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 6141 | return; |
| 6142 | } |
| 6143 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6144 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6145 | if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport && |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 6146 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 6147 | // MinGW doesn't allow dllimport on inline functions. |
| 6148 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6149 | << A; |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 6150 | return; |
| 6151 | } |
| 6152 | } |
| 6153 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6154 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 6155 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 6156 | MD->getParent()->isLambda()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6157 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A; |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 6158 | return; |
| 6159 | } |
| 6160 | } |
| 6161 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 6162 | unsigned Index = A.getAttributeSpellingListIndex(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6163 | Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 6164 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 6165 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6166 | if (NewAttr) |
| 6167 | D->addAttr(NewAttr); |
| 6168 | } |
| 6169 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6170 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 6171 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6172 | unsigned AttrSpellingListIndex, |
| 6173 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 6174 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 6175 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6176 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6177 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 6178 | << 1 /*previous declaration*/; |
| 6179 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 6180 | D->dropAttr<MSInheritanceAttr>(); |
| 6181 | } |
| 6182 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6183 | auto *RD = cast<CXXRecordDecl>(D); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6184 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 6185 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 6186 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6187 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6188 | } |
| 6189 | } else { |
| 6190 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 6191 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 6192 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6193 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6194 | } |
| 6195 | if (RD->getDescribedClassTemplate()) { |
| 6196 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 6197 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6198 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6199 | } |
| 6200 | } |
| 6201 | |
| 6202 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 6203 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6206 | static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6207 | // The capability attributes take a single string parameter for the name of |
| 6208 | // the capability they represent. The lockable attribute does not take any |
| 6209 | // parameters. However, semantically, both attributes represent the same |
| 6210 | // concept, and so they use the same semantic attribute. Eventually, the |
| 6211 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 6212 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 6213 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 6214 | // literal will be considered a "mutex." |
| 6215 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6216 | SourceLocation LiteralLoc; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6217 | if (AL.getKind() == ParsedAttr::AT_Capability && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6218 | !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6219 | return; |
| 6220 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 6221 | // Currently, there are only two names allowed for a capability: role and |
| 6222 | // mutex (case insensitive). Diagnose other capability names. |
| 6223 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 6224 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 6225 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6226 | D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N, |
| 6227 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6230 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 6231 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6232 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 6233 | return; |
| 6234 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6235 | D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context, |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 6236 | Args.data(), Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6237 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
| 6240 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6241 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6242 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6243 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6244 | return; |
| 6245 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6246 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6247 | S.Context, |
| 6248 | Args.data(), Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6249 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6250 | } |
| 6251 | |
| 6252 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6253 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6254 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6255 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6256 | return; |
| 6257 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6258 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6259 | S.Context, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6260 | AL.getArgAsExpr(0), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6261 | Args.data(), |
| 6262 | Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6263 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6264 | } |
| 6265 | |
| 6266 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6267 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6268 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 6269 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6270 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6271 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 6272 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6273 | AL.getRange(), S.Context, Args.data(), Args.size(), |
| 6274 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6277 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6278 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6279 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6280 | return; |
| 6281 | |
| 6282 | // check that all arguments are lockable objects |
| 6283 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6284 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6285 | if (Args.empty()) |
| 6286 | return; |
| 6287 | |
| 6288 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6289 | RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(), |
| 6290 | Args.size(), AL.getAttributeSpellingListIndex()); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6291 | |
| 6292 | D->addAttr(RCA); |
| 6293 | } |
| 6294 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6295 | static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6296 | if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6297 | if (NSD->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6298 | S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace); |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6299 | // Do not want to attach the attribute to the namespace because that will |
| 6300 | // cause confusing diagnostic reports for uses of declarations within the |
| 6301 | // namespace. |
| 6302 | return; |
| 6303 | } |
| 6304 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 6305 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6306 | // Handle the cases where the attribute has a text message. |
| 6307 | StringRef Str, Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6308 | if (AL.isArgExpr(0) && AL.getArgAsExpr(0) && |
| 6309 | !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6310 | return; |
| 6311 | |
| 6312 | // Only support a single optional message for Declspec and CXX11. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6313 | if (AL.isDeclspecAttribute() || AL.isCXX11Attribute()) |
| 6314 | checkAttributeAtMostNumArgs(S, AL, 1); |
| 6315 | else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) && |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 6316 | !S.checkStringLiteralArgumentAttr(AL, 1, Replacement)) |
| 6317 | return; |
| 6318 | |
| 6319 | if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope()) |
| 6320 | S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL; |
| 6321 | |
| 6322 | D->addAttr(::new (S.Context) |
| 6323 | DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6324 | AL.getAttributeSpellingListIndex())); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | static bool isGlobalVar(const Decl *D) { |
| 6328 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 6329 | return S->hasGlobalStorage(); |
| 6330 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6333 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6334 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6335 | return; |
| 6336 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 6337 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6338 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6339 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6340 | StringRef SanitizerName; |
| 6341 | SourceLocation LiteralLoc; |
| 6342 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6343 | if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6344 | return; |
| 6345 | |
Pierre Gousseau | ae5303d | 2019-03-01 10:05:15 +0000 | [diff] [blame] | 6346 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == |
| 6347 | SanitizerMask()) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6348 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 6349 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 6350 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6351 | << AL << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6352 | Sanitizers.push_back(SanitizerName); |
| 6353 | } |
| 6354 | |
| 6355 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6356 | AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 6357 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
| 6360 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6361 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6362 | StringRef AttrName = AL.getName()->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 6363 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 6364 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 6365 | .Case("no_address_safety_analysis", "address") |
| 6366 | .Case("no_sanitize_address", "address") |
| 6367 | .Case("no_sanitize_thread", "thread") |
| 6368 | .Case("no_sanitize_memory", "memory"); |
| 6369 | if (isGlobalVar(D) && SanitizerName != "address") |
| 6370 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6371 | << AL << ExpectedFunction; |
Aaron Ballman | 31ca49b | 2019-05-21 17:24:49 +0000 | [diff] [blame] | 6372 | |
| 6373 | // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a |
| 6374 | // NoSanitizeAttr object; but we need to calculate the correct spelling list |
| 6375 | // index rather than incorrectly assume the index for NoSanitizeSpecificAttr |
| 6376 | // has the same spellings as the index for NoSanitizeAttr. We don't have a |
| 6377 | // general way to "translate" between the two, so this hack attempts to work |
| 6378 | // around the issue with hard-coded indicies. This is critical for calling |
| 6379 | // getSpelling() or prettyPrint() on the resulting semantic attribute object |
| 6380 | // without failing assertions. |
| 6381 | unsigned TranslatedSpellingIndex = 0; |
| 6382 | if (AL.isC2xAttribute() || AL.isCXX11Attribute()) |
| 6383 | TranslatedSpellingIndex = 1; |
| 6384 | |
| 6385 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 6386 | AL.getRange(), S.Context, &SanitizerName, 1, TranslatedSpellingIndex)); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6389 | static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6390 | if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 6391 | D->addAttr(Internal); |
| 6392 | } |
| 6393 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6394 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 6395 | if (S.LangOpts.OpenCLVersion != 200) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6396 | S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6397 | << AL << "2.0" << 0; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 6398 | else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6399 | S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL |
| 6400 | << "2.0"; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6403 | /// Handles semantic checking for features that are common to all attributes, |
| 6404 | /// such as checking whether a parameter was properly specified, or the correct |
| 6405 | /// number of arguments were passed, etc. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6406 | static bool handleCommonAttributeFeatures(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6407 | const ParsedAttr &AL) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6408 | // Several attributes carry different semantics than the parsing requires, so |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6409 | // those are opted out of the common argument checks. |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6410 | // |
| 6411 | // We also bail on unknown and ignored attributes because those are handled |
| 6412 | // as part of the target-specific handling logic. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6413 | if (AL.getKind() == ParsedAttr::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6414 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6415 | // Check whether the attribute requires specific language extensions to be |
| 6416 | // enabled. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6417 | if (!AL.diagnoseLangOpts(S)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6418 | return true; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6419 | // Check whether the attribute appertains to the given subject. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6420 | if (!AL.diagnoseAppertainsTo(S, D)) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6421 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6422 | if (AL.hasCustomParsing()) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6423 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6424 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6425 | if (AL.getMinArgs() == AL.getMaxArgs()) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6426 | // If there are no optional arguments, then checking for the argument count |
| 6427 | // is trivial. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6428 | if (!checkAttributeNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6429 | return true; |
| 6430 | } else { |
| 6431 | // There are optional arguments, so checking is slightly more involved. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6432 | if (AL.getMinArgs() && |
| 6433 | !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6434 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6435 | else if (!AL.hasVariadicArg() && AL.getMaxArgs() && |
| 6436 | !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6437 | return true; |
| 6438 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 6439 | |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 6440 | if (S.CheckAttrTarget(AL)) |
| 6441 | return true; |
| 6442 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6443 | return false; |
| 6444 | } |
| 6445 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6446 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6447 | if (D->isInvalidDecl()) |
| 6448 | return; |
| 6449 | |
| 6450 | // Check if there is only one access qualifier. |
| 6451 | if (D->hasAttr<OpenCLAccessAttr>()) { |
Andrew Savonichev | 05a15af | 2018-09-06 15:10:26 +0000 | [diff] [blame] | 6452 | if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() == |
| 6453 | AL.getSemanticSpelling()) { |
| 6454 | S.Diag(AL.getLoc(), diag::warn_duplicate_declspec) |
| 6455 | << AL.getName()->getName() << AL.getRange(); |
| 6456 | } else { |
| 6457 | S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 6458 | << D->getSourceRange(); |
| 6459 | D->setInvalidDecl(true); |
| 6460 | return; |
| 6461 | } |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
| 6464 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 6465 | // image object can be read and written. |
| 6466 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 6467 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 6468 | // qualifier is a compilation error. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6469 | if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6470 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6471 | if (AL.getName()->getName().find("read_write") != StringRef::npos) { |
Anastasia Stulova | 2c4730d | 2019-02-15 12:07:57 +0000 | [diff] [blame] | 6472 | if ((!S.getLangOpts().OpenCLCPlusPlus && |
| 6473 | S.getLangOpts().OpenCLVersion < 200) || |
| 6474 | DeclTy->isPipeType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6475 | S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6476 | << AL << PDecl->getType() << DeclTy->isImageType(); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6477 | D->setInvalidDecl(true); |
| 6478 | return; |
| 6479 | } |
| 6480 | } |
| 6481 | } |
| 6482 | |
| 6483 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6484 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6485 | } |
| 6486 | |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6487 | static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6488 | if (!cast<VarDecl>(D)->hasGlobalStorage()) { |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6489 | S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var) |
| 6490 | << (A.getKind() == ParsedAttr::AT_AlwaysDestroy); |
| 6491 | return; |
| 6492 | } |
| 6493 | |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6494 | if (A.getKind() == ParsedAttr::AT_AlwaysDestroy) |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6495 | handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A); |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6496 | else |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6497 | handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A); |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 6500 | static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6501 | assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic && |
| 6502 | "uninitialized is only valid on automatic duration variables"); |
| 6503 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 6504 | D->addAttr(::new (S.Context) |
| 6505 | UninitializedAttr(AL.getLoc(), S.Context, Index)); |
| 6506 | } |
| 6507 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 6508 | static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD, |
| 6509 | bool DiagnoseFailure) { |
| 6510 | QualType Ty = VD->getType(); |
| 6511 | if (!Ty->isObjCRetainableType()) { |
| 6512 | if (DiagnoseFailure) { |
| 6513 | S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6514 | << 0; |
| 6515 | } |
| 6516 | return false; |
| 6517 | } |
| 6518 | |
| 6519 | Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime(); |
| 6520 | |
| 6521 | // Sema::inferObjCARCLifetime must run after processing decl attributes |
| 6522 | // (because __block lowers to an attribute), so if the lifetime hasn't been |
| 6523 | // explicitly specified, infer it locally now. |
| 6524 | if (LifetimeQual == Qualifiers::OCL_None) |
| 6525 | LifetimeQual = Ty->getObjCARCImplicitLifetime(); |
| 6526 | |
| 6527 | // The attributes only really makes sense for __strong variables; ignore any |
| 6528 | // attempts to annotate a parameter with any other lifetime qualifier. |
| 6529 | if (LifetimeQual != Qualifiers::OCL_Strong) { |
| 6530 | if (DiagnoseFailure) { |
| 6531 | S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6532 | << 1; |
| 6533 | } |
| 6534 | return false; |
| 6535 | } |
| 6536 | |
| 6537 | // Tampering with the type of a VarDecl here is a bit of a hack, but we need |
| 6538 | // to ensure that the variable is 'const' so that we can error on |
| 6539 | // modification, which can otherwise over-release. |
| 6540 | VD->setType(Ty.withConst()); |
| 6541 | VD->setARCPseudoStrong(true); |
| 6542 | return true; |
| 6543 | } |
| 6544 | |
| 6545 | static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D, |
| 6546 | const ParsedAttr &AL) { |
| 6547 | if (auto *VD = dyn_cast<VarDecl>(D)) { |
| 6548 | assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically"); |
| 6549 | if (!VD->hasLocalStorage()) { |
| 6550 | S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6551 | << 0; |
| 6552 | return; |
| 6553 | } |
| 6554 | |
| 6555 | if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true)) |
| 6556 | return; |
| 6557 | |
| 6558 | handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL); |
| 6559 | return; |
| 6560 | } |
| 6561 | |
| 6562 | // If D is a function-like declaration (method, block, or function), then we |
| 6563 | // make every parameter psuedo-strong. |
| 6564 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); I != E; ++I) { |
| 6565 | auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I)); |
| 6566 | QualType Ty = PVD->getType(); |
| 6567 | |
| 6568 | // If a user wrote a parameter with __strong explicitly, then assume they |
| 6569 | // want "real" strong semantics for that parameter. This works because if |
| 6570 | // the parameter was written with __strong, then the strong qualifier will |
| 6571 | // be non-local. |
| 6572 | if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() == |
| 6573 | Qualifiers::OCL_Strong) |
| 6574 | continue; |
| 6575 | |
| 6576 | tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false); |
| 6577 | } |
| 6578 | handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL); |
| 6579 | } |
| 6580 | |
Artem Dergachev | c333d77 | 2019-02-21 00:01:02 +0000 | [diff] [blame] | 6581 | static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6582 | // Check that the return type is a `typedef int kern_return_t` or a typedef |
| 6583 | // around it, because otherwise MIG convention checks make no sense. |
| 6584 | // BlockDecl doesn't store a return type, so it's annoying to check, |
| 6585 | // so let's skip it for now. |
| 6586 | if (!isa<BlockDecl>(D)) { |
| 6587 | QualType T = getFunctionOrMethodResultType(D); |
| 6588 | bool IsKernReturnT = false; |
| 6589 | while (const auto *TT = T->getAs<TypedefType>()) { |
| 6590 | IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t"); |
| 6591 | T = TT->desugar(); |
| 6592 | } |
| 6593 | if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) { |
| 6594 | S.Diag(D->getBeginLoc(), |
| 6595 | diag::warn_mig_server_routine_does_not_return_kern_return_t); |
| 6596 | return; |
| 6597 | } |
| 6598 | } |
| 6599 | |
| 6600 | handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL); |
| 6601 | } |
| 6602 | |
Reid Kleckner | 1181c9f | 2019-03-25 23:20:18 +0000 | [diff] [blame] | 6603 | static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6604 | // Warn if the return type is not a pointer or reference type. |
| 6605 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 6606 | QualType RetTy = FD->getReturnType(); |
| 6607 | if (!RetTy->isPointerType() && !RetTy->isReferenceType()) { |
| 6608 | S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer) |
| 6609 | << AL.getRange() << RetTy; |
| 6610 | return; |
| 6611 | } |
| 6612 | } |
| 6613 | |
| 6614 | handleSimpleAttribute<MSAllocatorAttr>(S, D, AL); |
| 6615 | } |
| 6616 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 6617 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6618 | // Top Level Sema Entry Points |
| 6619 | //===----------------------------------------------------------------------===// |
| 6620 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6621 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 6622 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 6623 | /// silently ignore it if a GNU attribute. |
| 6624 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6625 | const ParsedAttr &AL, |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6626 | bool IncludeCXX11Attributes) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6627 | if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6628 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 6629 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6630 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 6631 | // instead. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6632 | if (AL.isCXX11Attribute() && !IncludeCXX11Attributes) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6633 | return; |
| 6634 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6635 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 6636 | // which do not apply to the current target architecture are treated as |
| 6637 | // though they were unknown attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6638 | if (AL.getKind() == ParsedAttr::UnknownAttribute || |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6639 | !AL.existsInTarget(S.Context.getTargetInfo())) { |
Simon Pilgrim | fc0ff61 | 2018-12-17 12:17:37 +0000 | [diff] [blame] | 6640 | S.Diag(AL.getLoc(), |
| 6641 | AL.isDeclspecAttribute() |
| 6642 | ? (unsigned)diag::warn_unhandled_ms_attribute_ignored |
| 6643 | : (unsigned)diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6644 | << AL; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6645 | return; |
| 6646 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6647 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6648 | if (handleCommonAttributeFeatures(S, D, AL)) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6649 | return; |
| 6650 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6651 | switch (AL.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6652 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6653 | if (!AL.isStmtAttr()) { |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 6654 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6655 | assert(AL.isTypeAttr() && "Non-type attribute not handled"); |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 6656 | break; |
| 6657 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6658 | S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6659 | << AL << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6660 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6661 | case ParsedAttr::AT_Interrupt: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6662 | handleInterruptAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6663 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6664 | case ParsedAttr::AT_X86ForceAlignArgPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6665 | handleX86ForceAlignArgPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6666 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6667 | case ParsedAttr::AT_DLLExport: |
| 6668 | case ParsedAttr::AT_DLLImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6669 | handleDLLAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6670 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6671 | case ParsedAttr::AT_Mips16: |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6672 | handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6673 | MipsInterruptAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6674 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6675 | case ParsedAttr::AT_NoMips16: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6676 | handleSimpleAttribute<NoMips16Attr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6677 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6678 | case ParsedAttr::AT_MicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6679 | handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6680 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6681 | case ParsedAttr::AT_NoMicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6682 | handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6683 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6684 | case ParsedAttr::AT_MipsLongCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6685 | handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6686 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6687 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6688 | case ParsedAttr::AT_MipsShortCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6689 | handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6690 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6691 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6692 | case ParsedAttr::AT_AMDGPUFlatWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6693 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6694 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6695 | case ParsedAttr::AT_AMDGPUWavesPerEU: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6696 | handleAMDGPUWavesPerEUAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6697 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6698 | case ParsedAttr::AT_AMDGPUNumSGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6699 | handleAMDGPUNumSGPRAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6700 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6701 | case ParsedAttr::AT_AMDGPUNumVGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6702 | handleAMDGPUNumVGPRAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6703 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6704 | case ParsedAttr::AT_AVRSignal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6705 | handleAVRSignalAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 6706 | break; |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 6707 | case ParsedAttr::AT_WebAssemblyImportModule: |
| 6708 | handleWebAssemblyImportModuleAttr(S, D, AL); |
| 6709 | break; |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 6710 | case ParsedAttr::AT_WebAssemblyImportName: |
| 6711 | handleWebAssemblyImportNameAttr(S, D, AL); |
| 6712 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6713 | case ParsedAttr::AT_IBAction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6714 | handleSimpleAttribute<IBActionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6715 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6716 | case ParsedAttr::AT_IBOutlet: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6717 | handleIBOutlet(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6718 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6719 | case ParsedAttr::AT_IBOutletCollection: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6720 | handleIBOutletCollection(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6721 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6722 | case ParsedAttr::AT_IFunc: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6723 | handleIFuncAttr(S, D, AL); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 6724 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6725 | case ParsedAttr::AT_Alias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6726 | handleAliasAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6727 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6728 | case ParsedAttr::AT_Aligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6729 | handleAlignedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6730 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6731 | case ParsedAttr::AT_AlignValue: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6732 | handleAlignValueAttr(S, D, AL); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 6733 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6734 | case ParsedAttr::AT_AllocSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6735 | handleAllocSizeAttr(S, D, AL); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6736 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6737 | case ParsedAttr::AT_AlwaysInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6738 | handleAlwaysInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6739 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6740 | case ParsedAttr::AT_Artificial: |
Aaron Ballman | 736c09b | 2018-02-15 16:28:10 +0000 | [diff] [blame] | 6741 | handleSimpleAttribute<ArtificialAttr>(S, D, AL); |
Erich Keane | 293a055 | 2018-02-14 00:14:07 +0000 | [diff] [blame] | 6742 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6743 | case ParsedAttr::AT_AnalyzerNoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6744 | handleAnalyzerNoReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6745 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6746 | case ParsedAttr::AT_TLSModel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6747 | handleTLSModelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6748 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6749 | case ParsedAttr::AT_Annotate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6750 | handleAnnotateAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6751 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6752 | case ParsedAttr::AT_Availability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6753 | handleAvailabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6754 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6755 | case ParsedAttr::AT_CarriesDependency: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6756 | handleDependencyAttr(S, scope, D, AL); |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6757 | break; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 6758 | case ParsedAttr::AT_CPUDispatch: |
| 6759 | case ParsedAttr::AT_CPUSpecific: |
| 6760 | handleCPUSpecificAttr(S, D, AL); |
| 6761 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6762 | case ParsedAttr::AT_Common: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6763 | handleCommonAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6764 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6765 | case ParsedAttr::AT_CUDAConstant: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6766 | handleConstantAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6767 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6768 | case ParsedAttr::AT_PassObjectSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6769 | handlePassObjectSizeAttr(S, D, AL); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6770 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6771 | case ParsedAttr::AT_Constructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6772 | handleConstructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6773 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6774 | case ParsedAttr::AT_CXX11NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6775 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6776 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6777 | case ParsedAttr::AT_Deprecated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6778 | handleDeprecatedAttr(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6779 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6780 | case ParsedAttr::AT_Destructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6781 | handleDestructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6782 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6783 | case ParsedAttr::AT_EnableIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6784 | handleEnableIfAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6785 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6786 | case ParsedAttr::AT_DiagnoseIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6787 | handleDiagnoseIfAttr(S, D, AL); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6788 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6789 | case ParsedAttr::AT_ExtVectorType: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6790 | handleExtVectorTypeAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6791 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6792 | case ParsedAttr::AT_ExternalSourceSymbol: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6793 | handleExternalSourceSymbolAttr(S, D, AL); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 6794 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6795 | case ParsedAttr::AT_MinSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6796 | handleMinSizeAttr(S, D, AL); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6797 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6798 | case ParsedAttr::AT_OptimizeNone: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6799 | handleOptimizeNoneAttr(S, D, AL); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 6800 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6801 | case ParsedAttr::AT_FlagEnum: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6802 | handleSimpleAttribute<FlagEnumAttr>(S, D, AL); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 6803 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6804 | case ParsedAttr::AT_EnumExtensibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6805 | handleEnumExtensibilityAttr(S, D, AL); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 6806 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6807 | case ParsedAttr::AT_Flatten: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6808 | handleSimpleAttribute<FlattenAttr>(S, D, AL); |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 6809 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6810 | case ParsedAttr::AT_Format: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6811 | handleFormatAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6812 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6813 | case ParsedAttr::AT_FormatArg: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6814 | handleFormatArgAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6815 | break; |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 6816 | case ParsedAttr::AT_Callback: |
| 6817 | handleCallbackAttr(S, D, AL); |
| 6818 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6819 | case ParsedAttr::AT_CUDAGlobal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6820 | handleGlobalAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6821 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6822 | case ParsedAttr::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6823 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6824 | AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6825 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6826 | case ParsedAttr::AT_CUDAHost: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6827 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6828 | break; |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 6829 | case ParsedAttr::AT_HIPPinnedShadow: |
| 6830 | handleSimpleAttributeWithExclusions<HIPPinnedShadowAttr, CUDADeviceAttr, |
| 6831 | CUDAConstantAttr>(S, D, AL); |
| 6832 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6833 | case ParsedAttr::AT_GNUInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6834 | handleGNUInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6835 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6836 | case ParsedAttr::AT_CUDALaunchBounds: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6837 | handleLaunchBoundsAttr(S, D, AL); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 6838 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6839 | case ParsedAttr::AT_Restrict: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6840 | handleRestrictAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6841 | break; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6842 | case ParsedAttr::AT_LifetimeBound: |
| 6843 | handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL); |
| 6844 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6845 | case ParsedAttr::AT_MayAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6846 | handleSimpleAttribute<MayAliasAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6847 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6848 | case ParsedAttr::AT_Mode: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6849 | handleModeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6850 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6851 | case ParsedAttr::AT_NoAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6852 | handleSimpleAttribute<NoAliasAttr>(S, D, AL); |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 6853 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6854 | case ParsedAttr::AT_NoCommon: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6855 | handleSimpleAttribute<NoCommonAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6856 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6857 | case ParsedAttr::AT_NoSplitStack: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6858 | handleSimpleAttribute<NoSplitStackAttr>(S, D, AL); |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 6859 | break; |
Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 6860 | case ParsedAttr::AT_NoUniqueAddress: |
| 6861 | handleSimpleAttribute<NoUniqueAddressAttr>(S, D, AL); |
| 6862 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6863 | case ParsedAttr::AT_NonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6864 | if (auto *PVD = dyn_cast<ParmVarDecl>(D)) |
| 6865 | handleNonNullAttrParameter(S, PVD, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6866 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6867 | handleNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6868 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6869 | case ParsedAttr::AT_ReturnsNonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6870 | handleReturnsNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6871 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6872 | case ParsedAttr::AT_NoEscape: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6873 | handleNoEscapeAttr(S, D, AL); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 6874 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6875 | case ParsedAttr::AT_AssumeAligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6876 | handleAssumeAlignedAttr(S, D, AL); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 6877 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6878 | case ParsedAttr::AT_AllocAlign: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6879 | handleAllocAlignAttr(S, D, AL); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 6880 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6881 | case ParsedAttr::AT_Overloadable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6882 | handleSimpleAttribute<OverloadableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6883 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6884 | case ParsedAttr::AT_Ownership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6885 | handleOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6886 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6887 | case ParsedAttr::AT_Cold: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6888 | handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6889 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6890 | case ParsedAttr::AT_Hot: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6891 | handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6892 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6893 | case ParsedAttr::AT_Naked: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6894 | handleNakedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6895 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6896 | case ParsedAttr::AT_NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6897 | handleNoReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6898 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6899 | case ParsedAttr::AT_AnyX86NoCfCheck: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 6900 | handleNoCfCheckAttr(S, D, AL); |
| 6901 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6902 | case ParsedAttr::AT_NoThrow: |
Erich Keane | d02f4a1 | 2019-05-30 17:31:54 +0000 | [diff] [blame] | 6903 | if (!AL.isUsedAsTypeAttr()) |
| 6904 | handleSimpleAttribute<NoThrowAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6905 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6906 | case ParsedAttr::AT_CUDAShared: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6907 | handleSharedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6908 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6909 | case ParsedAttr::AT_VecReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6910 | handleVecReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6911 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6912 | case ParsedAttr::AT_ObjCOwnership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6913 | handleObjCOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6914 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6915 | case ParsedAttr::AT_ObjCPreciseLifetime: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6916 | handleObjCPreciseLifetimeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6917 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6918 | case ParsedAttr::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6919 | handleObjCReturnsInnerPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6920 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6921 | case ParsedAttr::AT_ObjCRequiresSuper: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6922 | handleObjCRequiresSuperAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6923 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6924 | case ParsedAttr::AT_ObjCBridge: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6925 | handleObjCBridgeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6926 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6927 | case ParsedAttr::AT_ObjCBridgeMutable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6928 | handleObjCBridgeMutableAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6929 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6930 | case ParsedAttr::AT_ObjCBridgeRelated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6931 | handleObjCBridgeRelatedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6932 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6933 | case ParsedAttr::AT_ObjCDesignatedInitializer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6934 | handleObjCDesignatedInitializer(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6935 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6936 | case ParsedAttr::AT_ObjCRuntimeName: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6937 | handleObjCRuntimeName(S, D, AL); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6938 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6939 | case ParsedAttr::AT_ObjCRuntimeVisible: |
| 6940 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL); |
| 6941 | break; |
| 6942 | case ParsedAttr::AT_ObjCBoxable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6943 | handleObjCBoxable(S, D, AL); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 6944 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6945 | case ParsedAttr::AT_CFAuditedTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6946 | handleSimpleAttributeWithExclusions<CFAuditedTransferAttr, |
| 6947 | CFUnknownTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6948 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6949 | case ParsedAttr::AT_CFUnknownTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6950 | handleSimpleAttributeWithExclusions<CFUnknownTransferAttr, |
| 6951 | CFAuditedTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6952 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6953 | case ParsedAttr::AT_CFConsumed: |
| 6954 | case ParsedAttr::AT_NSConsumed: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 6955 | case ParsedAttr::AT_OSConsumed: |
| 6956 | S.AddXConsumedAttr(D, AL.getRange(), AL.getAttributeSpellingListIndex(), |
| 6957 | parsedAttrToRetainOwnershipKind(AL), |
| 6958 | /*IsTemplateInstantiation=*/false); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6959 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6960 | case ParsedAttr::AT_NSConsumesSelf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6961 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6962 | break; |
George Karpenkov | da2c77f | 2018-12-06 22:06:59 +0000 | [diff] [blame] | 6963 | case ParsedAttr::AT_OSConsumesThis: |
| 6964 | handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL); |
| 6965 | break; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 6966 | case ParsedAttr::AT_OSReturnsRetainedOnZero: |
| 6967 | handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>( |
| 6968 | S, D, AL, isValidOSObjectOutParameter(D), |
| 6969 | diag::warn_ns_attribute_wrong_parameter_type, |
| 6970 | /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange()); |
| 6971 | break; |
| 6972 | case ParsedAttr::AT_OSReturnsRetainedOnNonZero: |
| 6973 | handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>( |
| 6974 | S, D, AL, isValidOSObjectOutParameter(D), |
| 6975 | diag::warn_ns_attribute_wrong_parameter_type, |
| 6976 | /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange()); |
| 6977 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6978 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 6979 | case ParsedAttr::AT_NSReturnsNotRetained: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6980 | case ParsedAttr::AT_NSReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 6981 | case ParsedAttr::AT_CFReturnsNotRetained: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6982 | case ParsedAttr::AT_CFReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 6983 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 6984 | case ParsedAttr::AT_OSReturnsRetained: |
| 6985 | handleXReturnsXRetainedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6986 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6987 | case ParsedAttr::AT_WorkGroupSizeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6988 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6989 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6990 | case ParsedAttr::AT_ReqdWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6991 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6992 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6993 | case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6994 | handleSubGroupSize(S, D, AL); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 6995 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6996 | case ParsedAttr::AT_VecTypeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6997 | handleVecTypeHint(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6998 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6999 | case ParsedAttr::AT_RequireConstantInit: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7000 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL); |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 7001 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7002 | case ParsedAttr::AT_InitPriority: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7003 | handleInitPriorityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7004 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7005 | case ParsedAttr::AT_Packed: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7006 | handlePackedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7007 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7008 | case ParsedAttr::AT_Section: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7009 | handleSectionAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7010 | break; |
Zola Bridges | cbac3ad | 2018-11-27 19:56:46 +0000 | [diff] [blame] | 7011 | case ParsedAttr::AT_SpeculativeLoadHardening: |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 7012 | handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr, |
| 7013 | NoSpeculativeLoadHardeningAttr>(S, D, |
| 7014 | AL); |
| 7015 | break; |
| 7016 | case ParsedAttr::AT_NoSpeculativeLoadHardening: |
| 7017 | handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr, |
| 7018 | SpeculativeLoadHardeningAttr>(S, D, AL); |
Zola Bridges | cbac3ad | 2018-11-27 19:56:46 +0000 | [diff] [blame] | 7019 | break; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 7020 | case ParsedAttr::AT_CodeSeg: |
| 7021 | handleCodeSegAttr(S, D, AL); |
| 7022 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7023 | case ParsedAttr::AT_Target: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7024 | handleTargetAttr(S, D, AL); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 7025 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7026 | case ParsedAttr::AT_MinVectorWidth: |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 7027 | handleMinVectorWidthAttr(S, D, AL); |
| 7028 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7029 | case ParsedAttr::AT_Unavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7030 | handleAttrWithMessage<UnavailableAttr>(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 7031 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7032 | case ParsedAttr::AT_ArcWeakrefUnavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7033 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7034 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7035 | case ParsedAttr::AT_ObjCRootClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7036 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7037 | break; |
Joe Daniels | f7393d2 | 2019-02-04 23:32:55 +0000 | [diff] [blame] | 7038 | case ParsedAttr::AT_ObjCNonLazyClass: |
| 7039 | handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL); |
| 7040 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7041 | case ParsedAttr::AT_ObjCSubclassingRestricted: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7042 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL); |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 7043 | break; |
John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 7044 | case ParsedAttr::AT_ObjCClassStub: |
| 7045 | handleSimpleAttribute<ObjCClassStubAttr>(S, D, AL); |
| 7046 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7047 | case ParsedAttr::AT_ObjCExplicitProtocolImpl: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7048 | handleObjCSuppresProtocolAttr(S, D, AL); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 7049 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7050 | case ParsedAttr::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7051 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7052 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7053 | case ParsedAttr::AT_Unused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7054 | handleUnusedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7055 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7056 | case ParsedAttr::AT_ReturnsTwice: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7057 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7058 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7059 | case ParsedAttr::AT_NotTailCalled: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7060 | handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>( |
| 7061 | S, D, AL); |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 7062 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7063 | case ParsedAttr::AT_DisableTailCalls: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7064 | handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D, |
| 7065 | AL); |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 7066 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7067 | case ParsedAttr::AT_Used: |
Aaron Ballman | 1a3901c | 2018-03-03 21:02:09 +0000 | [diff] [blame] | 7068 | handleSimpleAttribute<UsedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7069 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7070 | case ParsedAttr::AT_Visibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7071 | handleVisibilityAttr(S, D, AL, false); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 7072 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7073 | case ParsedAttr::AT_TypeVisibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7074 | handleVisibilityAttr(S, D, AL, true); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 7075 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7076 | case ParsedAttr::AT_WarnUnused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7077 | handleSimpleAttribute<WarnUnusedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7078 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7079 | case ParsedAttr::AT_WarnUnusedResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7080 | handleWarnUnusedResult(S, D, AL); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 7081 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7082 | case ParsedAttr::AT_Weak: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7083 | handleSimpleAttribute<WeakAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7084 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7085 | case ParsedAttr::AT_WeakRef: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7086 | handleWeakRefAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7087 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7088 | case ParsedAttr::AT_WeakImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7089 | handleWeakImportAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7090 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7091 | case ParsedAttr::AT_TransparentUnion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7092 | handleTransparentUnionAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7093 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7094 | case ParsedAttr::AT_ObjCException: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7095 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7096 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7097 | case ParsedAttr::AT_ObjCMethodFamily: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7098 | handleObjCMethodFamilyAttr(S, D, AL); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 7099 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7100 | case ParsedAttr::AT_ObjCNSObject: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7101 | handleObjCNSObject(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7102 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7103 | case ParsedAttr::AT_ObjCIndependentClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7104 | handleObjCIndependentClass(S, D, AL); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 7105 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7106 | case ParsedAttr::AT_Blocks: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7107 | handleBlocksAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7108 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7109 | case ParsedAttr::AT_Sentinel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7110 | handleSentinelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7111 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7112 | case ParsedAttr::AT_Const: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7113 | handleSimpleAttribute<ConstAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7114 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7115 | case ParsedAttr::AT_Pure: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7116 | handleSimpleAttribute<PureAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7117 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7118 | case ParsedAttr::AT_Cleanup: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7119 | handleCleanupAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7120 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7121 | case ParsedAttr::AT_NoDebug: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7122 | handleNoDebugAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7123 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7124 | case ParsedAttr::AT_NoDuplicate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7125 | handleSimpleAttribute<NoDuplicateAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7126 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7127 | case ParsedAttr::AT_Convergent: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7128 | handleSimpleAttribute<ConvergentAttr>(S, D, AL); |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 7129 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7130 | case ParsedAttr::AT_NoInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7131 | handleSimpleAttribute<NoInlineAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7132 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7133 | case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7134 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7135 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7136 | case ParsedAttr::AT_NoStackProtector: |
Manoj Gupta | 4fbf84c | 2018-05-09 21:41:18 +0000 | [diff] [blame] | 7137 | // Interacts with -fstack-protector options. |
| 7138 | handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL); |
| 7139 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7140 | case ParsedAttr::AT_StdCall: |
| 7141 | case ParsedAttr::AT_CDecl: |
| 7142 | case ParsedAttr::AT_FastCall: |
| 7143 | case ParsedAttr::AT_ThisCall: |
| 7144 | case ParsedAttr::AT_Pascal: |
| 7145 | case ParsedAttr::AT_RegCall: |
| 7146 | case ParsedAttr::AT_SwiftCall: |
| 7147 | case ParsedAttr::AT_VectorCall: |
| 7148 | case ParsedAttr::AT_MSABI: |
| 7149 | case ParsedAttr::AT_SysVABI: |
| 7150 | case ParsedAttr::AT_Pcs: |
| 7151 | case ParsedAttr::AT_IntelOclBicc: |
| 7152 | case ParsedAttr::AT_PreserveMost: |
| 7153 | case ParsedAttr::AT_PreserveAll: |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 7154 | case ParsedAttr::AT_AArch64VectorPcs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7155 | handleCallConvAttr(S, D, AL); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 7156 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7157 | case ParsedAttr::AT_Suppress: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7158 | handleSuppressAttr(S, D, AL); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 7159 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7160 | case ParsedAttr::AT_OpenCLKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7161 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7162 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7163 | case ParsedAttr::AT_OpenCLAccess: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7164 | handleOpenCLAccessAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7165 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7166 | case ParsedAttr::AT_OpenCLNoSVM: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7167 | handleOpenCLNoSVMAttr(S, D, AL); |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 7168 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7169 | case ParsedAttr::AT_SwiftContext: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7170 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 7171 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7172 | case ParsedAttr::AT_SwiftErrorResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7173 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 7174 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7175 | case ParsedAttr::AT_SwiftIndirectResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7176 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 7177 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7178 | case ParsedAttr::AT_InternalLinkage: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7179 | handleInternalLinkageAttr(S, D, AL); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 7180 | break; |
Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 7181 | case ParsedAttr::AT_ExcludeFromExplicitInstantiation: |
| 7182 | handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL); |
| 7183 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7184 | case ParsedAttr::AT_LTOVisibilityPublic: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7185 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL); |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 7186 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 7187 | |
| 7188 | // Microsoft attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7189 | case ParsedAttr::AT_EmptyBases: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7190 | handleSimpleAttribute<EmptyBasesAttr>(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 7191 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7192 | case ParsedAttr::AT_LayoutVersion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7193 | handleLayoutVersion(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 7194 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7195 | case ParsedAttr::AT_TrivialABI: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7196 | handleSimpleAttribute<TrivialABIAttr>(S, D, AL); |
Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 7197 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7198 | case ParsedAttr::AT_MSNoVTable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7199 | handleSimpleAttribute<MSNoVTableAttr>(S, D, AL); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 7200 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7201 | case ParsedAttr::AT_MSStruct: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7202 | handleSimpleAttribute<MSStructAttr>(S, D, AL); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 7203 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7204 | case ParsedAttr::AT_Uuid: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7205 | handleUuidAttr(S, D, AL); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 7206 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7207 | case ParsedAttr::AT_MSInheritance: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7208 | handleMSInheritanceAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7209 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7210 | case ParsedAttr::AT_SelectAny: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7211 | handleSimpleAttribute<SelectAnyAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7212 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7213 | case ParsedAttr::AT_Thread: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7214 | handleDeclspecThreadAttr(S, D, AL); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 7215 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 7216 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7217 | case ParsedAttr::AT_AbiTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7218 | handleAbiTagAttr(S, D, AL); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 7219 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 7220 | |
| 7221 | // Thread safety attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7222 | case ParsedAttr::AT_AssertExclusiveLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7223 | handleAssertExclusiveLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 7224 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7225 | case ParsedAttr::AT_AssertSharedLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7226 | handleAssertSharedLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 7227 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7228 | case ParsedAttr::AT_GuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7229 | handleSimpleAttribute<GuardedVarAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7230 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7231 | case ParsedAttr::AT_PtGuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7232 | handlePtGuardedVarAttr(S, D, AL); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 7233 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7234 | case ParsedAttr::AT_ScopedLockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7235 | handleSimpleAttribute<ScopedLockableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7236 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7237 | case ParsedAttr::AT_NoSanitize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7238 | handleNoSanitizeAttr(S, D, AL); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 7239 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7240 | case ParsedAttr::AT_NoSanitizeSpecific: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7241 | handleNoSanitizeSpecificAttr(S, D, AL); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 7242 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7243 | case ParsedAttr::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7244 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 7245 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7246 | case ParsedAttr::AT_GuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7247 | handleGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7248 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7249 | case ParsedAttr::AT_PtGuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7250 | handlePtGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7251 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7252 | case ParsedAttr::AT_ExclusiveTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7253 | handleExclusiveTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7254 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7255 | case ParsedAttr::AT_LockReturned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7256 | handleLockReturnedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7257 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7258 | case ParsedAttr::AT_LocksExcluded: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7259 | handleLocksExcludedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7260 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7261 | case ParsedAttr::AT_SharedTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7262 | handleSharedTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7263 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7264 | case ParsedAttr::AT_AcquiredBefore: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7265 | handleAcquiredBeforeAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7266 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7267 | case ParsedAttr::AT_AcquiredAfter: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7268 | handleAcquiredAfterAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7269 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 7270 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 7271 | // Capability analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7272 | case ParsedAttr::AT_Capability: |
| 7273 | case ParsedAttr::AT_Lockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7274 | handleCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7275 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7276 | case ParsedAttr::AT_RequiresCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7277 | handleRequiresCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7278 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 7279 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7280 | case ParsedAttr::AT_AssertCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7281 | handleAssertCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7282 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7283 | case ParsedAttr::AT_AcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7284 | handleAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7285 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7286 | case ParsedAttr::AT_ReleaseCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7287 | handleReleaseCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7288 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7289 | case ParsedAttr::AT_TryAcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7290 | handleTryAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7291 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 7292 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 7293 | // Consumed analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7294 | case ParsedAttr::AT_Consumable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7295 | handleConsumableAttr(S, D, AL); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 7296 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7297 | case ParsedAttr::AT_ConsumableAutoCast: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7298 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7299 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7300 | case ParsedAttr::AT_ConsumableSetOnRead: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7301 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7302 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7303 | case ParsedAttr::AT_CallableWhen: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7304 | handleCallableWhenAttr(S, D, AL); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 7305 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7306 | case ParsedAttr::AT_ParamTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7307 | handleParamTypestateAttr(S, D, AL); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 7308 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7309 | case ParsedAttr::AT_ReturnTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7310 | handleReturnTypestateAttr(S, D, AL); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 7311 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7312 | case ParsedAttr::AT_SetTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7313 | handleSetTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 7314 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7315 | case ParsedAttr::AT_TestTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7316 | handleTestTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 7317 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 7318 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7319 | // Type safety attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7320 | case ParsedAttr::AT_ArgumentWithTypeTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7321 | handleArgumentWithTypeTagAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7322 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7323 | case ParsedAttr::AT_TypeTagForDatatype: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7324 | handleTypeTagForDatatypeAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7325 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7326 | case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 7327 | handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 7328 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7329 | case ParsedAttr::AT_RenderScriptKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7330 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 7331 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 7332 | // XRay attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7333 | case ParsedAttr::AT_XRayInstrument: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7334 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL); |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 7335 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7336 | case ParsedAttr::AT_XRayLogArgs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7337 | handleXRayLogArgsAttr(S, D, AL); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 7338 | break; |
Martin Bohme | 4e1293b | 2018-08-13 14:11:03 +0000 | [diff] [blame] | 7339 | |
| 7340 | // Move semantics attribute. |
| 7341 | case ParsedAttr::AT_Reinitializes: |
| 7342 | handleSimpleAttribute<ReinitializesAttr>(S, D, AL); |
| 7343 | break; |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 7344 | |
| 7345 | case ParsedAttr::AT_AlwaysDestroy: |
| 7346 | case ParsedAttr::AT_NoDestroy: |
| 7347 | handleDestroyAttr(S, D, AL); |
| 7348 | break; |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 7349 | |
| 7350 | case ParsedAttr::AT_Uninitialized: |
| 7351 | handleUninitializedAttr(S, D, AL); |
| 7352 | break; |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 7353 | |
| 7354 | case ParsedAttr::AT_ObjCExternallyRetained: |
| 7355 | handleObjCExternallyRetainedAttr(S, D, AL); |
| 7356 | break; |
Erik Pilkington | e3cd735 | 2019-02-11 23:21:39 +0000 | [diff] [blame] | 7357 | |
Artem Dergachev | c333d77 | 2019-02-21 00:01:02 +0000 | [diff] [blame] | 7358 | case ParsedAttr::AT_MIGServerRoutine: |
| 7359 | handleMIGServerRoutineAttr(S, D, AL); |
| 7360 | break; |
Reid Kleckner | 1181c9f | 2019-03-25 23:20:18 +0000 | [diff] [blame] | 7361 | |
| 7362 | case ParsedAttr::AT_MSAllocator: |
| 7363 | handleMSAllocatorAttr(S, D, AL); |
| 7364 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 7369 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 7370 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7371 | const ParsedAttributesView &AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 7372 | bool IncludeCXX11Attributes) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7373 | if (AttrList.empty()) |
| 7374 | return; |
| 7375 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7376 | for (const ParsedAttr &AL : AttrList) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7377 | ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 7378 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7379 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 7380 | // GCC accepts |
| 7381 | // static int a9 __attribute__((weakref)); |
| 7382 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 7383 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7384 | Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias) |
| 7385 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 7386 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 7387 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7388 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7389 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 7390 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 7391 | // good to have a way to specify "these attributes must appear as a group", |
| 7392 | // for these. Additionally, it would be good to have a way to specify "these |
| 7393 | // 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] | 7394 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 7395 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7396 | if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 7397 | // FIXME: This emits a different error message than |
| 7398 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 7399 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7400 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7401 | } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 7402 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7403 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7404 | } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 7405 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7406 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7407 | } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 7408 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 7409 | D->setInvalidDecl(); |
Yaxun Liu | aa24601 | 2018-06-12 23:58:59 +0000 | [diff] [blame] | 7410 | } else if (!D->hasAttr<CUDAGlobalAttr>()) { |
| 7411 | if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 7412 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7413 | << A << ExpectedKernelFunction; |
| 7414 | D->setInvalidDecl(); |
| 7415 | } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7416 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7417 | << A << ExpectedKernelFunction; |
| 7418 | D->setInvalidDecl(); |
| 7419 | } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 7420 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7421 | << A << ExpectedKernelFunction; |
| 7422 | D->setInvalidDecl(); |
| 7423 | } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7424 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7425 | << A << ExpectedKernelFunction; |
| 7426 | D->setInvalidDecl(); |
| 7427 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7428 | } |
| 7429 | } |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 7430 | |
| 7431 | // Do this check after processing D's attributes because the attribute |
| 7432 | // objc_method_family can change whether the given method is in the init |
| 7433 | // family, and it can be applied after objc_designated_initializer. This is a |
| 7434 | // bit of a hack, but we need it to be compatible with versions of clang that |
| 7435 | // processed the attribute list in the wrong order. |
| 7436 | if (D->hasAttr<ObjCDesignatedInitializerAttr>() && |
| 7437 | cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) { |
| 7438 | Diag(D->getLocation(), diag::err_designated_init_attr_non_init); |
| 7439 | D->dropAttr<ObjCDesignatedInitializerAttr>(); |
| 7440 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
Hiroshi Inoue | 939d932 | 2017-06-30 05:40:31 +0000 | [diff] [blame] | 7443 | // Helper for delayed processing TransparentUnion attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7444 | void Sema::ProcessDeclAttributeDelayed(Decl *D, |
| 7445 | const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7446 | for (const ParsedAttr &AL : AttrList) |
| 7447 | if (AL.getKind() == ParsedAttr::AT_TransparentUnion) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7448 | handleTransparentUnionAttr(*this, D, AL); |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 7449 | break; |
| 7450 | } |
| 7451 | } |
| 7452 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7453 | // Annotation attributes are the only attributes allowed after an access |
| 7454 | // specifier. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7455 | bool Sema::ProcessAccessDeclAttributeList( |
| 7456 | AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7457 | for (const ParsedAttr &AL : AttrList) { |
| 7458 | if (AL.getKind() == ParsedAttr::AT_Annotate) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7459 | ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7460 | } else { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7461 | Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7462 | return true; |
| 7463 | } |
| 7464 | } |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7465 | return false; |
| 7466 | } |
| 7467 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7468 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 7469 | /// contains any decl attributes that we should warn about. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7470 | static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7471 | for (const ParsedAttr &AL : A) { |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7472 | // Only warn if the attribute is an unignored, non-type attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7473 | if (AL.isUsedAsTypeAttr() || AL.isInvalid()) |
| 7474 | continue; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7475 | if (AL.getKind() == ParsedAttr::IgnoredAttribute) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7476 | continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7477 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7478 | if (AL.getKind() == ParsedAttr::UnknownAttribute) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7479 | S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 7480 | << AL << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7481 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 7482 | S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL |
| 7483 | << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7484 | } |
| 7485 | } |
| 7486 | } |
| 7487 | |
| 7488 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 7489 | /// used to build a declaration, complain about any decl attributes |
| 7490 | /// which might be lying around on it. |
| 7491 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7492 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes()); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7493 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 7494 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 7495 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 7496 | } |
| 7497 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7498 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7499 | /// \#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] | 7500 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 7501 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 7502 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7503 | NamedDecl *NewD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7504 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 7505 | FunctionDecl *NewFD; |
| 7506 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7507 | // FIXME: Mangling? |
| 7508 | // FIXME: Is the qualifier info correct? |
| 7509 | // FIXME: Is the DeclContext correct? |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 7510 | NewFD = FunctionDecl::Create( |
| 7511 | FD->getASTContext(), FD->getDeclContext(), Loc, Loc, |
| 7512 | DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None, |
| 7513 | false /*isInlineSpecified*/, FD->hasPrototype(), CSK_unspecified); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7514 | NewD = NewFD; |
| 7515 | |
| 7516 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 7517 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7518 | |
| 7519 | // Fake up parameter variables; they are declared as if this were |
| 7520 | // a typedef. |
| 7521 | QualType FDTy = FD->getType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7522 | if (const auto *FT = FDTy->getAs<FunctionProtoType>()) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7523 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 7524 | for (const auto &AI : FT->param_types()) { |
| 7525 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7526 | Param->setScopeInfo(0, Params.size()); |
| 7527 | Params.push_back(Param); |
| 7528 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7529 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7530 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7531 | } else if (auto *VD = dyn_cast<VarDecl>(ND)) { |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7532 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7533 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 7534 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7535 | VD->getStorageClass()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7536 | if (VD->getQualifier()) |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame] | 7537 | cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc()); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7538 | } |
| 7539 | return NewD; |
| 7540 | } |
| 7541 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7542 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7543 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 7544 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 7545 | if (W.getUsed()) return; // only do this once |
| 7546 | W.setUsed(true); |
| 7547 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 7548 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7549 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7550 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 7551 | W.getLocation())); |
| 7552 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 7553 | WeakTopLevelDecl.push_back(NewD); |
| 7554 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 7555 | // to insert Decl at TU scope, sorry. |
| 7556 | DeclContext *SavedContext = CurContext; |
| 7557 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 7558 | NewD->setDeclContext(CurContext); |
| 7559 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 7560 | PushOnScopeChains(NewD, S); |
| 7561 | CurContext = SavedContext; |
| 7562 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 7563 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7564 | } |
| 7565 | } |
| 7566 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7567 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 7568 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 7569 | // have to do this. |
| 7570 | LoadExternalWeakUndeclaredIdentifiers(); |
| 7571 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7572 | NamedDecl *ND = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7573 | if (auto *VD = dyn_cast<VarDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7574 | if (VD->isExternC()) |
| 7575 | ND = VD; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7576 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7577 | if (FD->isExternC()) |
| 7578 | ND = FD; |
| 7579 | if (ND) { |
| 7580 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 7581 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7582 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 7583 | WeakInfo W = I->second; |
| 7584 | DeclApplyPragmaWeak(S, ND, W); |
| 7585 | WeakUndeclaredIdentifiers[Id] = W; |
| 7586 | } |
| 7587 | } |
| 7588 | } |
| 7589 | } |
| 7590 | } |
| 7591 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7592 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 7593 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 7594 | /// 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] | 7595 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7596 | // Apply decl attributes from the DeclSpec if present. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7597 | if (!PD.getDeclSpec().getAttributes().empty()) |
| 7598 | ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 7599 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7600 | // Walk the declarator structure, applying decl attributes that were in a type |
| 7601 | // position to the decl itself. This handles cases like: |
| 7602 | // int *__attr__(x)** D; |
| 7603 | // when X is a decl attribute. |
| 7604 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7605 | ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(), |
| 7606 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 7607 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7608 | // Finally, apply any attributes on the decl itself. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7609 | ProcessDeclAttributeList(S, D, PD.getAttributes()); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 7610 | |
| 7611 | // Apply additional attributes specified by '#pragma clang attribute'. |
| 7612 | AddPragmaAttributes(S, D); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7613 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7614 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7615 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7616 | /// If so, it'll still be annotated with an attribute that makes it |
| 7617 | /// illegal to actually use. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7618 | static bool isForbiddenTypeAllowed(Sema &S, Decl *D, |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7619 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7620 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7621 | // Private ivars are always okay. Unfortunately, people don't |
| 7622 | // always properly make their ivars private, even in system headers. |
| 7623 | // Plus we need to make fields okay, too. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7624 | if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) && |
| 7625 | !isa<FunctionDecl>(D)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7626 | return false; |
| 7627 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7628 | // Silently accept unsupported uses of __weak in both user and system |
| 7629 | // declarations when it's been disabled, for ease of integration with |
| 7630 | // -fno-objc-arc files. We do have to take some care against attempts |
| 7631 | // to define such things; for now, we've only done that for ivars |
| 7632 | // and properties. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7633 | if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7634 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 7635 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 7636 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 7637 | return true; |
| 7638 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7641 | // Allow all sorts of things in system headers. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7642 | if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7643 | // Currently, all the failures dealt with this way are due to ARC |
| 7644 | // restrictions. |
| 7645 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 7646 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7650 | } |
| 7651 | |
| 7652 | /// Handle a delayed forbidden-type diagnostic. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7653 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD, |
| 7654 | Decl *D) { |
| 7655 | auto Reason = UnavailableAttr::IR_None; |
| 7656 | if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) { |
| 7657 | assert(Reason && "didn't set reason?"); |
| 7658 | D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7659 | return; |
| 7660 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7661 | if (S.getLangOpts().ObjCAutoRefCount) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7662 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 7663 | // FIXME: we may want to suppress diagnostics for all |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7664 | // kind of forbidden type messages on unavailable functions. |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 7665 | if (FD->hasAttr<UnavailableAttr>() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7666 | DD.getForbiddenTypeDiagnostic() == |
| 7667 | diag::err_arc_array_param_no_ownership) { |
| 7668 | DD.Triggered = true; |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 7669 | return; |
| 7670 | } |
| 7671 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7672 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7673 | S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic()) |
| 7674 | << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument(); |
| 7675 | DD.Triggered = true; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7676 | } |
| 7677 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7678 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 7679 | const Decl *D) { |
| 7680 | // Check each AvailabilityAttr to find the one for this platform. |
| 7681 | for (const auto *A : D->attrs()) { |
| 7682 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 7683 | // FIXME: this is copied from CheckAvailability. We should try to |
| 7684 | // de-duplicate. |
| 7685 | |
| 7686 | // Check if this is an App Extension "platform", and if so chop off |
| 7687 | // the suffix for matching with the actual platform. |
| 7688 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 7689 | StringRef RealizedPlatform = ActualPlatform; |
| 7690 | if (Context.getLangOpts().AppExt) { |
| 7691 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 7692 | if (suffix != StringRef::npos) |
| 7693 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 7694 | } |
| 7695 | |
| 7696 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 7697 | |
| 7698 | // Match the platform name. |
| 7699 | if (RealizedPlatform == TargetPlatform) |
| 7700 | return Avail; |
| 7701 | } |
| 7702 | } |
| 7703 | return nullptr; |
| 7704 | } |
| 7705 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7706 | /// The diagnostic we should emit for \c D, and the declaration that |
| 7707 | /// originated it, or \c AR_Available. |
| 7708 | /// |
| 7709 | /// \param D The declaration to check. |
| 7710 | /// \param Message If non-null, this will be populated with the message from |
| 7711 | /// the availability attribute that is selected. |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7712 | /// \param ClassReceiver If we're checking the the method of a class message |
| 7713 | /// send, the class. Otherwise nullptr. |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7714 | static std::pair<AvailabilityResult, const NamedDecl *> |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7715 | ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D, |
| 7716 | std::string *Message, |
| 7717 | ObjCInterfaceDecl *ClassReceiver) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7718 | AvailabilityResult Result = D->getAvailability(Message); |
| 7719 | |
| 7720 | // For typedefs, if the typedef declaration appears available look |
| 7721 | // to the underlying type to see if it is more restrictive. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7722 | while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7723 | if (Result == AR_Available) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7724 | if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7725 | D = TT->getDecl(); |
| 7726 | Result = D->getAvailability(Message); |
| 7727 | continue; |
| 7728 | } |
| 7729 | } |
| 7730 | break; |
| 7731 | } |
| 7732 | |
| 7733 | // Forward class declarations get their attributes from their definition. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7734 | if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7735 | if (IDecl->getDefinition()) { |
| 7736 | D = IDecl->getDefinition(); |
| 7737 | Result = D->getAvailability(Message); |
| 7738 | } |
| 7739 | } |
| 7740 | |
| 7741 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 7742 | if (Result == AR_Available) { |
| 7743 | const DeclContext *DC = ECD->getDeclContext(); |
| 7744 | if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) { |
| 7745 | Result = TheEnumDecl->getAvailability(Message); |
| 7746 | D = TheEnumDecl; |
| 7747 | } |
| 7748 | } |
| 7749 | |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7750 | // For +new, infer availability from -init. |
| 7751 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7752 | if (S.NSAPIObj && ClassReceiver) { |
| 7753 | ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod( |
| 7754 | S.NSAPIObj->getInitSelector()); |
| 7755 | if (Init && Result == AR_Available && MD->isClassMethod() && |
| 7756 | MD->getSelector() == S.NSAPIObj->getNewSelector() && |
| 7757 | MD->definedInNSObject(S.getASTContext())) { |
| 7758 | Result = Init->getAvailability(Message); |
| 7759 | D = Init; |
| 7760 | } |
| 7761 | } |
| 7762 | } |
| 7763 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7764 | return {Result, D}; |
| 7765 | } |
| 7766 | |
| 7767 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7768 | /// 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] | 7769 | /// the context of \c Ctx. For example, we should emit an unavailable diagnostic |
| 7770 | /// in a deprecated context, but not the other way around. |
Alex Lorenz | 4e3c0bd | 2019-01-09 22:31:37 +0000 | [diff] [blame] | 7771 | static bool |
| 7772 | ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 7773 | VersionTuple DeclVersion, Decl *Ctx, |
| 7774 | const NamedDecl *OffendingDecl) { |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7775 | assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 7776 | |
| 7777 | // Checks if we should emit the availability diagnostic in the context of C. |
| 7778 | auto CheckContext = [&](const Decl *C) { |
| 7779 | if (K == AR_NotYetIntroduced) { |
| 7780 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 7781 | if (AA->getIntroduced() >= DeclVersion) |
| 7782 | return true; |
Alex Lorenz | 4e3c0bd | 2019-01-09 22:31:37 +0000 | [diff] [blame] | 7783 | } else if (K == AR_Deprecated) { |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7784 | if (C->isDeprecated()) |
| 7785 | return true; |
Alex Lorenz | 4e3c0bd | 2019-01-09 22:31:37 +0000 | [diff] [blame] | 7786 | } else if (K == AR_Unavailable) { |
| 7787 | // It is perfectly fine to refer to an 'unavailable' Objective-C method |
Alex Lorenz | 194d00e | 2019-01-17 18:12:45 +0000 | [diff] [blame] | 7788 | // when it is referenced from within the @implementation itself. In this |
| 7789 | // context, we interpret unavailable as a form of access control. |
Alex Lorenz | 4e3c0bd | 2019-01-09 22:31:37 +0000 | [diff] [blame] | 7790 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) { |
| 7791 | if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) { |
Alex Lorenz | 194d00e | 2019-01-17 18:12:45 +0000 | [diff] [blame] | 7792 | if (MD->getClassInterface() == Impl->getClassInterface()) |
Alex Lorenz | 4e3c0bd | 2019-01-09 22:31:37 +0000 | [diff] [blame] | 7793 | return true; |
| 7794 | } |
| 7795 | } |
| 7796 | } |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7797 | |
| 7798 | if (C->isUnavailable()) |
| 7799 | return true; |
| 7800 | return false; |
| 7801 | }; |
| 7802 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7803 | do { |
| 7804 | if (CheckContext(Ctx)) |
| 7805 | return false; |
| 7806 | |
| 7807 | // An implementation implicitly has the availability of the interface. |
Steven Wu | 3bb4aa5 | 2018-04-16 23:34:18 +0000 | [diff] [blame] | 7808 | // Unless it is "+load" method. |
| 7809 | if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx)) |
| 7810 | if (MethodD->isClassMethod() && |
| 7811 | MethodD->getSelector().getAsString() == "load") |
| 7812 | return true; |
| 7813 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7814 | if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7815 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 7816 | if (CheckContext(Interface)) |
| 7817 | return false; |
| 7818 | } |
| 7819 | // A category implicitly has the availability of the interface. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7820 | else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7821 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 7822 | if (CheckContext(Interface)) |
| 7823 | return false; |
| 7824 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 7825 | |
| 7826 | return true; |
| 7827 | } |
| 7828 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7829 | static bool |
| 7830 | shouldDiagnoseAvailabilityByDefault(const ASTContext &Context, |
| 7831 | const VersionTuple &DeploymentVersion, |
| 7832 | const VersionTuple &DeclVersion) { |
| 7833 | const auto &Triple = Context.getTargetInfo().getTriple(); |
| 7834 | VersionTuple ForceAvailabilityFromVersion; |
| 7835 | switch (Triple.getOS()) { |
| 7836 | case llvm::Triple::IOS: |
| 7837 | case llvm::Triple::TvOS: |
| 7838 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11); |
| 7839 | break; |
| 7840 | case llvm::Triple::WatchOS: |
| 7841 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4); |
| 7842 | break; |
| 7843 | case llvm::Triple::Darwin: |
| 7844 | case llvm::Triple::MacOSX: |
| 7845 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13); |
| 7846 | break; |
| 7847 | default: |
| 7848 | // New targets should always warn about availability. |
| 7849 | return Triple.getVendor() == llvm::Triple::Apple; |
| 7850 | } |
| 7851 | return DeploymentVersion >= ForceAvailabilityFromVersion || |
| 7852 | DeclVersion >= ForceAvailabilityFromVersion; |
| 7853 | } |
| 7854 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7855 | static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) { |
| 7856 | for (Decl *Ctx = OrigCtx; Ctx; |
| 7857 | Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) { |
| 7858 | if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx)) |
| 7859 | return cast<NamedDecl>(Ctx); |
| 7860 | if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) { |
| 7861 | if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx)) |
| 7862 | return Imp->getClassInterface(); |
| 7863 | return CD; |
| 7864 | } |
| 7865 | } |
| 7866 | |
| 7867 | return dyn_cast<NamedDecl>(OrigCtx); |
| 7868 | } |
| 7869 | |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7870 | namespace { |
| 7871 | |
| 7872 | struct AttributeInsertion { |
| 7873 | StringRef Prefix; |
| 7874 | SourceLocation Loc; |
| 7875 | StringRef Suffix; |
| 7876 | |
| 7877 | static AttributeInsertion createInsertionAfter(const NamedDecl *D) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7878 | return {" ", D->getEndLoc(), ""}; |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7879 | } |
| 7880 | static AttributeInsertion createInsertionAfter(SourceLocation Loc) { |
| 7881 | return {" ", Loc, ""}; |
| 7882 | } |
| 7883 | static AttributeInsertion createInsertionBefore(const NamedDecl *D) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7884 | return {"", D->getBeginLoc(), "\n"}; |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7885 | } |
| 7886 | }; |
| 7887 | |
| 7888 | } // end anonymous namespace |
| 7889 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7890 | /// Tries to parse a string as ObjC method name. |
| 7891 | /// |
| 7892 | /// \param Name The string to parse. Expected to originate from availability |
| 7893 | /// attribute argument. |
| 7894 | /// \param SlotNames The vector that will be populated with slot names. In case |
| 7895 | /// of unsuccessful parsing can contain invalid data. |
| 7896 | /// \returns A number of method parameters if parsing was successful, None |
| 7897 | /// otherwise. |
| 7898 | static Optional<unsigned> |
| 7899 | tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames, |
| 7900 | const LangOptions &LangOpts) { |
| 7901 | // Accept replacements starting with - or + as valid ObjC method names. |
| 7902 | if (!Name.empty() && (Name.front() == '-' || Name.front() == '+')) |
| 7903 | Name = Name.drop_front(1); |
| 7904 | if (Name.empty()) |
| 7905 | return None; |
| 7906 | Name.split(SlotNames, ':'); |
| 7907 | unsigned NumParams; |
| 7908 | if (Name.back() == ':') { |
| 7909 | // Remove an empty string at the end that doesn't represent any slot. |
| 7910 | SlotNames.pop_back(); |
| 7911 | NumParams = SlotNames.size(); |
| 7912 | } else { |
| 7913 | if (SlotNames.size() != 1) |
| 7914 | // Not a valid method name, just a colon-separated string. |
| 7915 | return None; |
| 7916 | NumParams = 0; |
| 7917 | } |
| 7918 | // Verify all slot names are valid. |
| 7919 | bool AllowDollar = LangOpts.DollarIdents; |
| 7920 | for (StringRef S : SlotNames) { |
| 7921 | if (S.empty()) |
| 7922 | continue; |
| 7923 | if (!isValidIdentifier(S, AllowDollar)) |
| 7924 | return None; |
| 7925 | } |
| 7926 | return NumParams; |
| 7927 | } |
| 7928 | |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7929 | /// Returns a source location in which it's appropriate to insert a new |
| 7930 | /// attribute for the given declaration \D. |
| 7931 | static Optional<AttributeInsertion> |
| 7932 | createAttributeInsertion(const NamedDecl *D, const SourceManager &SM, |
| 7933 | const LangOptions &LangOpts) { |
| 7934 | if (isa<ObjCPropertyDecl>(D)) |
| 7935 | return AttributeInsertion::createInsertionAfter(D); |
| 7936 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7937 | if (MD->hasBody()) |
| 7938 | return None; |
| 7939 | return AttributeInsertion::createInsertionAfter(D); |
| 7940 | } |
| 7941 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
| 7942 | SourceLocation Loc = |
| 7943 | Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts); |
| 7944 | if (Loc.isInvalid()) |
| 7945 | return None; |
| 7946 | // Insert after the 'struct'/whatever keyword. |
| 7947 | return AttributeInsertion::createInsertionAfter(Loc); |
| 7948 | } |
| 7949 | return AttributeInsertion::createInsertionBefore(D); |
| 7950 | } |
| 7951 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7952 | /// Actually emit an availability diagnostic for a reference to an unavailable |
| 7953 | /// decl. |
| 7954 | /// |
| 7955 | /// \param Ctx The context that the reference occurred in |
| 7956 | /// \param ReferringDecl The exact declaration that was referenced. |
| 7957 | /// \param OffendingDecl A related decl to \c ReferringDecl that has an |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7958 | /// availability attribute corresponding to \c K attached to it. Note that this |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7959 | /// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and |
| 7960 | /// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl |
| 7961 | /// and OffendingDecl is the EnumDecl. |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7962 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7963 | Decl *Ctx, const NamedDecl *ReferringDecl, |
| 7964 | const NamedDecl *OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7965 | StringRef Message, |
| 7966 | ArrayRef<SourceLocation> Locs, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7967 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7968 | const ObjCPropertyDecl *ObjCProperty, |
| 7969 | bool ObjCPropertyAccess) { |
| 7970 | // Diagnostics for deprecated or unavailable. |
| 7971 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7972 | unsigned diag_available_here = diag::note_availability_specified_here; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7973 | SourceLocation NoteLocation = OffendingDecl->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7974 | |
| 7975 | // Matches 'diag::note_property_attribute' options. |
| 7976 | unsigned property_note_select; |
| 7977 | |
| 7978 | // Matches diag::note_availability_specified_here. |
| 7979 | unsigned available_here_select_kind; |
| 7980 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7981 | VersionTuple DeclVersion; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7982 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7983 | DeclVersion = AA->getIntroduced(); |
| 7984 | |
Alex Lorenz | 4e3c0bd | 2019-01-09 22:31:37 +0000 | [diff] [blame] | 7985 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx, |
| 7986 | OffendingDecl)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7987 | return; |
| 7988 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7989 | SourceLocation Loc = Locs.front(); |
| 7990 | |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7991 | // The declaration can have multiple availability attributes, we are looking |
| 7992 | // at one of them. |
| 7993 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl); |
| 7994 | if (A && A->isInherited()) { |
| 7995 | for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl; |
| 7996 | Redecl = Redecl->getPreviousDecl()) { |
| 7997 | const AvailabilityAttr *AForRedecl = |
| 7998 | getAttrForPlatform(S.Context, Redecl); |
| 7999 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 8000 | // If D is a declaration with inherited attributes, the note should |
| 8001 | // point to the declaration with actual attributes. |
| 8002 | NoteLocation = Redecl->getLocation(); |
| 8003 | break; |
| 8004 | } |
| 8005 | } |
| 8006 | } |
| 8007 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8008 | switch (K) { |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 8009 | case AR_NotYetIntroduced: { |
| 8010 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 8011 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 8012 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 8013 | // later. |
| 8014 | const AvailabilityAttr *AA = |
| 8015 | getAttrForPlatform(S.getASTContext(), OffendingDecl); |
| 8016 | VersionTuple Introduced = AA->getIntroduced(); |
| 8017 | |
| 8018 | bool UseNewWarning = shouldDiagnoseAvailabilityByDefault( |
| 8019 | S.Context, S.Context.getTargetInfo().getPlatformMinVersion(), |
| 8020 | Introduced); |
| 8021 | unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new |
| 8022 | : diag::warn_unguarded_availability; |
| 8023 | |
Erik Pilkington | 0535b0f | 2019-01-14 19:17:31 +0000 | [diff] [blame] | 8024 | std::string PlatformName = AvailabilityAttr::getPrettyPlatformName( |
| 8025 | S.getASTContext().getTargetInfo().getPlatformName()); |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 8026 | |
Erik Pilkington | 0535b0f | 2019-01-14 19:17:31 +0000 | [diff] [blame] | 8027 | S.Diag(Loc, Warning) << OffendingDecl << PlatformName |
| 8028 | << Introduced.getAsString(); |
| 8029 | |
| 8030 | S.Diag(OffendingDecl->getLocation(), |
| 8031 | diag::note_partial_availability_specified_here) |
| 8032 | << OffendingDecl << PlatformName << Introduced.getAsString() |
| 8033 | << S.Context.getTargetInfo().getPlatformMinVersion().getAsString(); |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 8034 | |
| 8035 | if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8036 | if (const auto *TD = dyn_cast<TagDecl>(Enclosing)) |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 8037 | if (TD->getDeclName().isEmpty()) { |
| 8038 | S.Diag(TD->getLocation(), |
| 8039 | diag::note_decl_unguarded_availability_silence) |
| 8040 | << /*Anonymous*/ 1 << TD->getKindName(); |
| 8041 | return; |
| 8042 | } |
| 8043 | auto FixitNoteDiag = |
| 8044 | S.Diag(Enclosing->getLocation(), |
| 8045 | diag::note_decl_unguarded_availability_silence) |
| 8046 | << /*Named*/ 0 << Enclosing; |
| 8047 | // Don't offer a fixit for declarations with availability attributes. |
| 8048 | if (Enclosing->hasAttr<AvailabilityAttr>()) |
| 8049 | return; |
| 8050 | if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE")) |
| 8051 | return; |
| 8052 | Optional<AttributeInsertion> Insertion = createAttributeInsertion( |
| 8053 | Enclosing, S.getSourceManager(), S.getLangOpts()); |
| 8054 | if (!Insertion) |
| 8055 | return; |
| 8056 | std::string PlatformName = |
| 8057 | AvailabilityAttr::getPlatformNameSourceSpelling( |
| 8058 | S.getASTContext().getTargetInfo().getPlatformName()) |
| 8059 | .lower(); |
| 8060 | std::string Introduced = |
| 8061 | OffendingDecl->getVersionIntroduced().getAsString(); |
| 8062 | FixitNoteDiag << FixItHint::CreateInsertion( |
| 8063 | Insertion->Loc, |
| 8064 | (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName + |
| 8065 | "(" + Introduced + "))" + Insertion->Suffix) |
| 8066 | .str()); |
| 8067 | } |
| 8068 | return; |
| 8069 | } |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 8070 | case AR_Deprecated: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8071 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 8072 | : diag::warn_property_method_deprecated; |
| 8073 | diag_message = diag::warn_deprecated_message; |
| 8074 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 8075 | property_note_select = /* deprecated */ 0; |
| 8076 | available_here_select_kind = /* deprecated */ 2; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8077 | if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 8078 | NoteLocation = AL->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8079 | break; |
| 8080 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 8081 | case AR_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8082 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 8083 | : diag::err_property_method_unavailable; |
| 8084 | diag_message = diag::err_unavailable_message; |
| 8085 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 8086 | property_note_select = /* unavailable */ 1; |
| 8087 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 8088 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8089 | if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) { |
| 8090 | if (AL->isImplicit() && AL->getImplicitReason()) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 8091 | // Most of these failures are due to extra restrictions in ARC; |
| 8092 | // reflect that in the primary diagnostic when applicable. |
| 8093 | auto flagARCError = [&] { |
| 8094 | if (S.getLangOpts().ObjCAutoRefCount && |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8095 | S.getSourceManager().isInSystemHeader( |
| 8096 | OffendingDecl->getLocation())) |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 8097 | diag = diag::err_unavailable_in_arc; |
| 8098 | }; |
| 8099 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8100 | switch (AL->getImplicitReason()) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 8101 | case UnavailableAttr::IR_None: break; |
| 8102 | |
| 8103 | case UnavailableAttr::IR_ARCForbiddenType: |
| 8104 | flagARCError(); |
| 8105 | diag_available_here = diag::note_arc_forbidden_type; |
| 8106 | break; |
| 8107 | |
| 8108 | case UnavailableAttr::IR_ForbiddenWeak: |
| 8109 | if (S.getLangOpts().ObjCWeakRuntime) |
| 8110 | diag_available_here = diag::note_arc_weak_disabled; |
| 8111 | else |
| 8112 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 8113 | break; |
| 8114 | |
| 8115 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 8116 | flagARCError(); |
| 8117 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 8118 | break; |
| 8119 | |
| 8120 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 8121 | flagARCError(); |
| 8122 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 8123 | break; |
| 8124 | |
| 8125 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 8126 | flagARCError(); |
| 8127 | diag_available_here = diag::note_arc_field_with_ownership; |
| 8128 | break; |
| 8129 | } |
| 8130 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 8131 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8132 | break; |
| 8133 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 8134 | case AR_Available: |
| 8135 | llvm_unreachable("Warning for availability of available declaration?"); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8138 | SmallVector<FixItHint, 12> FixIts; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 8139 | if (K == AR_Deprecated) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8140 | StringRef Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8141 | if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 8142 | Replacement = AL->getReplacement(); |
| 8143 | if (auto AL = getAttrForPlatform(S.Context, OffendingDecl)) |
| 8144 | Replacement = AL->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 8145 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8146 | CharSourceRange UseRange; |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 8147 | if (!Replacement.empty()) |
| 8148 | UseRange = |
| 8149 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8150 | if (UseRange.isValid()) { |
| 8151 | if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) { |
| 8152 | Selector Sel = MethodDecl->getSelector(); |
| 8153 | SmallVector<StringRef, 12> SelectorSlotNames; |
| 8154 | Optional<unsigned> NumParams = tryParseObjCMethodName( |
| 8155 | Replacement, SelectorSlotNames, S.getLangOpts()); |
| 8156 | if (NumParams && NumParams.getValue() == Sel.getNumArgs()) { |
| 8157 | assert(SelectorSlotNames.size() == Locs.size()); |
| 8158 | for (unsigned I = 0; I < Locs.size(); ++I) { |
| 8159 | if (!Sel.getNameForSlot(I).empty()) { |
| 8160 | CharSourceRange NameRange = CharSourceRange::getCharRange( |
| 8161 | Locs[I], S.getLocForEndOfToken(Locs[I])); |
| 8162 | FixIts.push_back(FixItHint::CreateReplacement( |
| 8163 | NameRange, SelectorSlotNames[I])); |
| 8164 | } else |
| 8165 | FixIts.push_back( |
| 8166 | FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I])); |
| 8167 | } |
| 8168 | } else |
| 8169 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 8170 | } else |
| 8171 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 8172 | } |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 8173 | } |
| 8174 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8175 | if (!Message.empty()) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8176 | S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8177 | if (ObjCProperty) |
| 8178 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 8179 | << ObjCProperty->getDeclName() << property_note_select; |
| 8180 | } else if (!UnknownObjCClass) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8181 | S.Diag(Loc, diag) << ReferringDecl << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8182 | if (ObjCProperty) |
| 8183 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 8184 | << ObjCProperty->getDeclName() << property_note_select; |
| 8185 | } else { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8186 | S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8187 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 8188 | } |
| 8189 | |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 8190 | S.Diag(NoteLocation, diag_available_here) |
| 8191 | << OffendingDecl << available_here_select_kind; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 8195 | Decl *Ctx) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 8196 | assert(DD.Kind == DelayedDiagnostic::Availability && |
| 8197 | "Expected an availability diagnostic here"); |
| 8198 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8199 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 8200 | DoEmitAvailabilityWarning( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8201 | S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(), |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8202 | DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(), |
| 8203 | DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(), |
| 8204 | DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8205 | } |
| 8206 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8207 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 8208 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 8209 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8210 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 8211 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8212 | // When delaying diagnostics to run in the context of a parsed |
| 8213 | // declaration, we only want to actually emit anything if parsing |
| 8214 | // succeeds. |
| 8215 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 8216 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8217 | // We emit all the active diagnostics in this pool or any of its |
| 8218 | // parents. In general, we'll get one pool for the decl spec |
| 8219 | // and a child pool for each declarator; in a decl group like: |
| 8220 | // deprecated_typedef foo, *bar, baz(); |
| 8221 | // only the declarator pops will be passed decls. This is correct; |
| 8222 | // we really do need to consider delayed diagnostics from the decl spec |
| 8223 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 8224 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8225 | do { |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 8226 | bool AnyAccessFailures = false; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 8227 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8228 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 8229 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 8230 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 8231 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 8232 | continue; |
| 8233 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 8234 | switch (diag.Kind) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 8235 | case DelayedDiagnostic::Availability: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 8236 | // Don't bother giving deprecation/unavailable diagnostics if |
| 8237 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 8238 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 8239 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 8240 | break; |
| 8241 | |
| 8242 | case DelayedDiagnostic::Access: |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 8243 | // Only produce one access control diagnostic for a structured binding |
| 8244 | // declaration: we don't need to tell the user that all the fields are |
| 8245 | // inaccessible one at a time. |
| 8246 | if (AnyAccessFailures && isa<DecompositionDecl>(decl)) |
| 8247 | continue; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8248 | HandleDelayedAccessCheck(diag, decl); |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 8249 | if (diag.Triggered) |
| 8250 | AnyAccessFailures = true; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 8251 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8252 | |
| 8253 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8254 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8255 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 8256 | } |
| 8257 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 8258 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 8259 | } |
| 8260 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 8261 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 8262 | /// been delayed in the current context instead of in the given pool. |
| 8263 | /// Essentially, this just moves them to the current pool. |
| 8264 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 8265 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 8266 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 8267 | curPool->steal(pool); |
| 8268 | } |
| 8269 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8270 | static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR, |
| 8271 | const NamedDecl *ReferringDecl, |
| 8272 | const NamedDecl *OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8273 | StringRef Message, |
| 8274 | ArrayRef<SourceLocation> Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8275 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 8276 | const ObjCPropertyDecl *ObjCProperty, |
| 8277 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 8278 | // Delay if we're currently parsing a declaration. |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8279 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 8280 | S.DelayedDiagnostics.add( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8281 | DelayedDiagnostic::makeAvailability( |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8282 | AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8283 | ObjCProperty, Message, ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 8284 | return; |
| 8285 | } |
| 8286 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8287 | Decl *Ctx = cast<Decl>(S.getCurLexicalContext()); |
| 8288 | DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8289 | Message, Locs, UnknownObjCClass, ObjCProperty, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8290 | ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 8291 | } |
Erik Pilkington | 48c7cc9 | 2016-07-29 17:37:38 +0000 | [diff] [blame] | 8292 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8293 | namespace { |
| 8294 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8295 | /// Returns true if the given statement can be a body-like child of \p Parent. |
| 8296 | bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) { |
| 8297 | switch (Parent->getStmtClass()) { |
| 8298 | case Stmt::IfStmtClass: |
| 8299 | return cast<IfStmt>(Parent)->getThen() == S || |
| 8300 | cast<IfStmt>(Parent)->getElse() == S; |
| 8301 | case Stmt::WhileStmtClass: |
| 8302 | return cast<WhileStmt>(Parent)->getBody() == S; |
| 8303 | case Stmt::DoStmtClass: |
| 8304 | return cast<DoStmt>(Parent)->getBody() == S; |
| 8305 | case Stmt::ForStmtClass: |
| 8306 | return cast<ForStmt>(Parent)->getBody() == S; |
| 8307 | case Stmt::CXXForRangeStmtClass: |
| 8308 | return cast<CXXForRangeStmt>(Parent)->getBody() == S; |
| 8309 | case Stmt::ObjCForCollectionStmtClass: |
| 8310 | return cast<ObjCForCollectionStmt>(Parent)->getBody() == S; |
| 8311 | case Stmt::CaseStmtClass: |
| 8312 | case Stmt::DefaultStmtClass: |
| 8313 | return cast<SwitchCase>(Parent)->getSubStmt() == S; |
| 8314 | default: |
| 8315 | return false; |
| 8316 | } |
| 8317 | } |
| 8318 | |
| 8319 | class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> { |
| 8320 | const Stmt *Target; |
| 8321 | |
| 8322 | public: |
| 8323 | bool VisitStmt(Stmt *S) { return S != Target; } |
| 8324 | |
| 8325 | /// Returns true if the given statement is present in the given declaration. |
| 8326 | static bool isContained(const Stmt *Target, const Decl *D) { |
| 8327 | StmtUSEFinder Visitor; |
| 8328 | Visitor.Target = Target; |
| 8329 | return !Visitor.TraverseDecl(const_cast<Decl *>(D)); |
| 8330 | } |
| 8331 | }; |
| 8332 | |
| 8333 | /// Traverses the AST and finds the last statement that used a given |
| 8334 | /// declaration. |
| 8335 | class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> { |
| 8336 | const Decl *D; |
| 8337 | |
| 8338 | public: |
| 8339 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 8340 | if (DRE->getDecl() == D) |
| 8341 | return false; |
| 8342 | return true; |
| 8343 | } |
| 8344 | |
| 8345 | static const Stmt *findLastStmtThatUsesDecl(const Decl *D, |
| 8346 | const CompoundStmt *Scope) { |
| 8347 | LastDeclUSEFinder Visitor; |
| 8348 | Visitor.D = D; |
| 8349 | for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) { |
| 8350 | const Stmt *S = *I; |
| 8351 | if (!Visitor.TraverseStmt(const_cast<Stmt *>(S))) |
| 8352 | return S; |
| 8353 | } |
| 8354 | return nullptr; |
| 8355 | } |
| 8356 | }; |
| 8357 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8358 | /// This class implements -Wunguarded-availability. |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8359 | /// |
| 8360 | /// This is done with a traversal of the AST of a function that makes reference |
| 8361 | /// to a partially available declaration. Whenever we encounter an \c if of the |
| 8362 | /// form: \c if(@available(...)), we use the version from the condition to visit |
| 8363 | /// the then statement. |
| 8364 | class DiagnoseUnguardedAvailability |
| 8365 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 8366 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 8367 | |
| 8368 | Sema &SemaRef; |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 8369 | Decl *Ctx; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8370 | |
| 8371 | /// Stack of potentially nested 'if (@available(...))'s. |
| 8372 | SmallVector<VersionTuple, 8> AvailabilityStack; |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8373 | SmallVector<const Stmt *, 16> StmtStack; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8374 | |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 8375 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range, |
| 8376 | ObjCInterfaceDecl *ClassReceiver = nullptr); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8377 | |
| 8378 | public: |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 8379 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 8380 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 8381 | AvailabilityStack.push_back( |
| 8382 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 8385 | bool TraverseDecl(Decl *D) { |
| 8386 | // Avoid visiting nested functions to prevent duplicate warnings. |
| 8387 | if (!D || isa<FunctionDecl>(D)) |
| 8388 | return true; |
| 8389 | return Base::TraverseDecl(D); |
| 8390 | } |
| 8391 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8392 | bool TraverseStmt(Stmt *S) { |
| 8393 | if (!S) |
| 8394 | return true; |
| 8395 | StmtStack.push_back(S); |
| 8396 | bool Result = Base::TraverseStmt(S); |
| 8397 | StmtStack.pop_back(); |
| 8398 | return Result; |
| 8399 | } |
| 8400 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8401 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 8402 | |
| 8403 | bool TraverseIfStmt(IfStmt *If); |
| 8404 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 8405 | bool TraverseLambdaExpr(LambdaExpr *E) { return true; } |
| 8406 | |
Erik Pilkington | ba87c62 | 2017-08-18 20:20:56 +0000 | [diff] [blame] | 8407 | // for 'case X:' statements, don't bother looking at the 'X'; it can't lead |
| 8408 | // to any useful diagnostics. |
| 8409 | bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); } |
| 8410 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 8411 | bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) { |
| 8412 | if (PRE->isClassReceiver()) |
| 8413 | DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation()); |
| 8414 | return true; |
| 8415 | } |
| 8416 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8417 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 8418 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) { |
| 8419 | ObjCInterfaceDecl *ID = nullptr; |
| 8420 | QualType ReceiverTy = Msg->getClassReceiver(); |
| 8421 | if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType()) |
| 8422 | ID = ReceiverTy->getAsObjCInterfaceType()->getInterface(); |
| 8423 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8424 | DiagnoseDeclAvailability( |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 8425 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID); |
| 8426 | } |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8427 | return true; |
| 8428 | } |
| 8429 | |
| 8430 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 8431 | DiagnoseDeclAvailability(DRE->getDecl(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8432 | SourceRange(DRE->getBeginLoc(), DRE->getEndLoc())); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8433 | return true; |
| 8434 | } |
| 8435 | |
| 8436 | bool VisitMemberExpr(MemberExpr *ME) { |
| 8437 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8438 | SourceRange(ME->getBeginLoc(), ME->getEndLoc())); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8439 | return true; |
| 8440 | } |
| 8441 | |
Alex Lorenz | 0a484ba | 2017-05-24 15:15:29 +0000 | [diff] [blame] | 8442 | bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8443 | SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use) |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 8444 | << (!SemaRef.getLangOpts().ObjC); |
Alex Lorenz | 0a484ba | 2017-05-24 15:15:29 +0000 | [diff] [blame] | 8445 | return true; |
| 8446 | } |
| 8447 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8448 | bool VisitTypeLoc(TypeLoc Ty); |
| 8449 | }; |
| 8450 | |
| 8451 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 8452 | NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8453 | AvailabilityResult Result; |
| 8454 | const NamedDecl *OffendingDecl; |
| 8455 | std::tie(Result, OffendingDecl) = |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 8456 | ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass); |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8457 | if (Result != AR_Available) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8458 | // All other diagnostic kinds have already been handled in |
| 8459 | // DiagnoseAvailabilityOfDecl. |
| 8460 | if (Result != AR_NotYetIntroduced) |
| 8461 | return; |
| 8462 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8463 | const AvailabilityAttr *AA = |
| 8464 | getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8465 | VersionTuple Introduced = AA->getIntroduced(); |
| 8466 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8467 | if (AvailabilityStack.back() >= Introduced) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 8468 | return; |
| 8469 | |
| 8470 | // If the context of this function is less available than D, we should not |
| 8471 | // emit a diagnostic. |
Alex Lorenz | 4e3c0bd | 2019-01-09 22:31:37 +0000 | [diff] [blame] | 8472 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx, |
| 8473 | OffendingDecl)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 8474 | return; |
| 8475 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 8476 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 8477 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 8478 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 8479 | // later. |
| 8480 | unsigned DiagKind = |
| 8481 | shouldDiagnoseAvailabilityByDefault( |
| 8482 | SemaRef.Context, |
| 8483 | SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced) |
| 8484 | ? diag::warn_unguarded_availability_new |
| 8485 | : diag::warn_unguarded_availability; |
| 8486 | |
Erik Pilkington | 0535b0f | 2019-01-14 19:17:31 +0000 | [diff] [blame] | 8487 | std::string PlatformName = AvailabilityAttr::getPrettyPlatformName( |
| 8488 | SemaRef.getASTContext().getTargetInfo().getPlatformName()); |
| 8489 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 8490 | SemaRef.Diag(Range.getBegin(), DiagKind) |
Erik Pilkington | 0535b0f | 2019-01-14 19:17:31 +0000 | [diff] [blame] | 8491 | << Range << D << PlatformName << Introduced.getAsString(); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8492 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 8493 | SemaRef.Diag(OffendingDecl->getLocation(), |
Erik Pilkington | 0535b0f | 2019-01-14 19:17:31 +0000 | [diff] [blame] | 8494 | diag::note_partial_availability_specified_here) |
| 8495 | << OffendingDecl << PlatformName << Introduced.getAsString() |
| 8496 | << SemaRef.Context.getTargetInfo() |
| 8497 | .getPlatformMinVersion() |
| 8498 | .getAsString(); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8499 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8500 | auto FixitDiag = |
| 8501 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 8502 | << Range << D |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 8503 | << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0 |
| 8504 | : /*__builtin_available*/ 1); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8505 | |
| 8506 | // Find the statement which should be enclosed in the if @available check. |
| 8507 | if (StmtStack.empty()) |
| 8508 | return; |
| 8509 | const Stmt *StmtOfUse = StmtStack.back(); |
| 8510 | const CompoundStmt *Scope = nullptr; |
| 8511 | for (const Stmt *S : llvm::reverse(StmtStack)) { |
| 8512 | if (const auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 8513 | Scope = CS; |
| 8514 | break; |
| 8515 | } |
| 8516 | if (isBodyLikeChildStmt(StmtOfUse, S)) { |
| 8517 | // The declaration won't be seen outside of the statement, so we don't |
| 8518 | // have to wrap the uses of any declared variables in if (@available). |
| 8519 | // Therefore we can avoid setting Scope here. |
| 8520 | break; |
| 8521 | } |
| 8522 | StmtOfUse = S; |
| 8523 | } |
| 8524 | const Stmt *LastStmtOfUse = nullptr; |
| 8525 | if (isa<DeclStmt>(StmtOfUse) && Scope) { |
| 8526 | for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) { |
| 8527 | if (StmtUSEFinder::isContained(StmtStack.back(), D)) { |
| 8528 | LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope); |
| 8529 | break; |
| 8530 | } |
| 8531 | } |
| 8532 | } |
| 8533 | |
| 8534 | const SourceManager &SM = SemaRef.getSourceManager(); |
| 8535 | SourceLocation IfInsertionLoc = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8536 | SM.getExpansionLoc(StmtOfUse->getBeginLoc()); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8537 | SourceLocation StmtEndLoc = |
| 8538 | SM.getExpansionRange( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 8539 | (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc()) |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 8540 | .getEnd(); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8541 | if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc)) |
| 8542 | return; |
| 8543 | |
| 8544 | StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM); |
| 8545 | const char *ExtraIndentation = " "; |
| 8546 | std::string FixItString; |
| 8547 | llvm::raw_string_ostream FixItOS(FixItString); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 8548 | FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available" |
| 8549 | : "__builtin_available") |
Alex Lorenz | e1fb64e | 2017-05-09 15:34:46 +0000 | [diff] [blame] | 8550 | << "(" |
| 8551 | << AvailabilityAttr::getPlatformNameSourceSpelling( |
| 8552 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 8553 | << " " << Introduced.getAsString() << ", *)) {\n" |
| 8554 | << Indentation << ExtraIndentation; |
| 8555 | FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str()); |
| 8556 | SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken( |
| 8557 | StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(), |
| 8558 | /*SkipTrailingWhitespaceAndNewLine=*/false); |
| 8559 | if (ElseInsertionLoc.isInvalid()) |
| 8560 | ElseInsertionLoc = |
| 8561 | Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts()); |
| 8562 | FixItOS.str().clear(); |
| 8563 | FixItOS << "\n" |
| 8564 | << Indentation << "} else {\n" |
| 8565 | << Indentation << ExtraIndentation |
| 8566 | << "// Fallback on earlier versions\n" |
| 8567 | << Indentation << "}"; |
| 8568 | FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8569 | } |
| 8570 | } |
| 8571 | |
| 8572 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 8573 | const Type *TyPtr = Ty.getTypePtr(); |
| 8574 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 8575 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 8576 | if (Range.isInvalid()) |
| 8577 | return true; |
| 8578 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8579 | if (const auto *TT = dyn_cast<TagType>(TyPtr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8580 | TagDecl *TD = TT->getDecl(); |
| 8581 | DiagnoseDeclAvailability(TD, Range); |
| 8582 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8583 | } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8584 | TypedefNameDecl *D = TD->getDecl(); |
| 8585 | DiagnoseDeclAvailability(D, Range); |
| 8586 | |
| 8587 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 8588 | if (NamedDecl *D = ObjCO->getInterface()) |
| 8589 | DiagnoseDeclAvailability(D, Range); |
| 8590 | } |
| 8591 | |
| 8592 | return true; |
| 8593 | } |
| 8594 | |
| 8595 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 8596 | VersionTuple CondVersion; |
| 8597 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 8598 | CondVersion = E->getVersion(); |
| 8599 | |
| 8600 | // If we're using the '*' case here or if this check is redundant, then we |
| 8601 | // use the enclosing version to check both branches. |
| 8602 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
Alex Lorenz | 98f9fcd | 2017-08-17 14:22:27 +0000 | [diff] [blame] | 8603 | return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8604 | } else { |
| 8605 | // This isn't an availability checking 'if', we can just continue. |
| 8606 | return Base::TraverseIfStmt(If); |
| 8607 | } |
| 8608 | |
| 8609 | AvailabilityStack.push_back(CondVersion); |
| 8610 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 8611 | AvailabilityStack.pop_back(); |
| 8612 | |
| 8613 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 8614 | } |
| 8615 | |
| 8616 | } // end anonymous namespace |
| 8617 | |
| 8618 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 8619 | Stmt *Body = nullptr; |
| 8620 | |
| 8621 | if (auto *FD = D->getAsFunction()) { |
| 8622 | // FIXME: We only examine the pattern decl for availability violations now, |
| 8623 | // but we should also examine instantiated templates. |
| 8624 | if (FD->isTemplateInstantiation()) |
| 8625 | return; |
| 8626 | |
| 8627 | Body = FD->getBody(); |
| 8628 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8629 | Body = MD->getBody(); |
Alex Lorenz | 28559ce | 2017-04-26 14:20:02 +0000 | [diff] [blame] | 8630 | else if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 8631 | Body = BD->getBody(); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8632 | |
| 8633 | assert(Body && "Need a body here!"); |
| 8634 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 8635 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 8636 | } |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8637 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8638 | void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D, |
| 8639 | ArrayRef<SourceLocation> Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8640 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 8641 | bool ObjCPropertyAccess, |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 8642 | bool AvoidPartialAvailabilityChecks, |
| 8643 | ObjCInterfaceDecl *ClassReceiver) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8644 | std::string Message; |
| 8645 | AvailabilityResult Result; |
| 8646 | const NamedDecl* OffendingDecl; |
| 8647 | // See if this declaration is unavailable, deprecated, or partial. |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 8648 | std::tie(Result, OffendingDecl) = |
| 8649 | ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver); |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8650 | if (Result == AR_Available) |
| 8651 | return; |
| 8652 | |
| 8653 | if (Result == AR_NotYetIntroduced) { |
| 8654 | if (AvoidPartialAvailabilityChecks) |
| 8655 | return; |
| 8656 | |
| 8657 | // We need to know the @available context in the current function to |
| 8658 | // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that |
| 8659 | // when we're done parsing the current function. |
| 8660 | if (getCurFunctionOrMethodDecl()) { |
| 8661 | getEnclosingFunction()->HasPotentialAvailabilityViolations = true; |
| 8662 | return; |
| 8663 | } else if (getCurBlock() || getCurLambda()) { |
| 8664 | getCurFunction()->HasPotentialAvailabilityViolations = true; |
| 8665 | return; |
| 8666 | } |
| 8667 | } |
| 8668 | |
| 8669 | const ObjCPropertyDecl *ObjCPDecl = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 8670 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8671 | if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { |
| 8672 | AvailabilityResult PDeclResult = PD->getAvailability(nullptr); |
| 8673 | if (PDeclResult == Result) |
| 8674 | ObjCPDecl = PD; |
| 8675 | } |
| 8676 | } |
| 8677 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 8678 | EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 8679 | UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess); |
| 8680 | } |