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" |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 26 | #include "clang/Basic/TargetBuiltins.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 29 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 30 | #include "clang/Sema/DelayedDiagnostic.h" |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Initialization.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 33 | #include "clang/Sema/Scope.h" |
Reid Kleckner | 04f9bca | 2018-03-07 22:48:35 +0000 | [diff] [blame] | 34 | #include "clang/Sema/ScopeInfo.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 35 | #include "clang/Sema/SemaInternal.h" |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 40 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 41 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 42 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 43 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 44 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 45 | C, |
| 46 | Cpp, |
| 47 | ObjC |
| 48 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 49 | } // end namespace AttributeLangSupport |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Helper functions |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 55 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 56 | /// type (function or function-typed variable) or an Objective-C |
| 57 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 58 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 59 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 60 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 61 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 62 | /// Return true if the given decl has function type (function or |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 63 | /// function-typed variable) or an Objective-C method or a block. |
| 64 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 65 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 66 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 67 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 68 | /// Return true if the given decl has a declarator that should have |
| 69 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 71 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 73 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 76 | /// hasFunctionProto - Return true if the given decl has a argument |
| 77 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 78 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 80 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 81 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 82 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 85 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 86 | /// parameters. It is an error to call this on a K&R function (use |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 87 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 88 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 89 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 90 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 91 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 92 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 96 | static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D, |
| 97 | unsigned Idx) { |
| 98 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 99 | return FD->getParamDecl(Idx); |
| 100 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 101 | return MD->getParamDecl(Idx); |
| 102 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
| 103 | return BD->getParamDecl(Idx); |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 107 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 108 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 109 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 110 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 111 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 112 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 113 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 116 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 117 | if (auto *PVD = getFunctionOrMethodParam(D, Idx)) |
| 118 | return PVD->getSourceRange(); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 119 | return SourceRange(); |
| 120 | } |
| 121 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 122 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 123 | if (const FunctionType *FnTy = D->getFunctionType()) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 124 | return FnTy->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 125 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 128 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 129 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 130 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 131 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 132 | return MD->getReturnTypeSourceRange(); |
| 133 | return SourceRange(); |
| 134 | } |
| 135 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 136 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 137 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 138 | return cast<FunctionProtoType>(FnTy)->isVariadic(); |
| 139 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 140 | return BD->isVariadic(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 141 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | static bool isInstanceMethod(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 145 | if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 146 | return MethodDecl->isInstance(); |
| 147 | return false; |
| 148 | } |
| 149 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 150 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 151 | const auto *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 152 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 153 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 154 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 155 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 156 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 157 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 158 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 159 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 161 | // FIXME: Should we walk the chain of classes? |
| 162 | return ClsName == &Ctx.Idents.get("NSString") || |
| 163 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 164 | } |
| 165 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 166 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 167 | const auto *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 168 | if (!PT) |
| 169 | return false; |
| 170 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 171 | const auto *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 172 | if (!RT) |
| 173 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 174 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 175 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 176 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 177 | return false; |
| 178 | |
| 179 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 180 | } |
| 181 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 182 | static unsigned getNumAttributeArgs(const ParsedAttr &AL) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 183 | // FIXME: Include the type in the argument list. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 184 | return AL.getNumArgs() + AL.hasParsedType(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 187 | template <typename Compare> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 188 | static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 189 | unsigned Num, unsigned Diag, |
| 190 | Compare Comp) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 191 | if (Comp(getNumAttributeArgs(AL), Num)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 192 | S.Diag(AL.getLoc(), Diag) << AL << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 199 | /// Check if the attribute has exactly as many args as Num. May |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 200 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 201 | static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 202 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 203 | diag::err_attribute_wrong_number_arguments, |
| 204 | std::not_equal_to<unsigned>()); |
| 205 | } |
| 206 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 207 | /// 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] | 208 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 209 | static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 210 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 211 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 212 | diag::err_attribute_too_few_arguments, |
| 213 | std::less<unsigned>()); |
| 214 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 215 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 216 | /// 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] | 217 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 218 | static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL, |
| 219 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 220 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 221 | diag::err_attribute_too_many_arguments, |
| 222 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 225 | /// A helper function to provide Attribute Location for the Attr types |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 226 | /// AND the ParsedAttr. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 227 | template <typename AttrInfo> |
Justin Lebar | 027eb71 | 2020-02-10 23:23:44 -0800 | [diff] [blame] | 228 | static std::enable_if_t<std::is_base_of<Attr, AttrInfo>::value, SourceLocation> |
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> |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 401 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 402 | const AttributeCommonInfo &CI) { |
| 403 | D->addAttr(::new (S.Context) AttrType(S.Context, CI)); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 404 | } |
| 405 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 406 | template <typename... DiagnosticArgs> |
| 407 | static const Sema::SemaDiagnosticBuilder& |
| 408 | appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) { |
| 409 | return Bldr; |
| 410 | } |
| 411 | |
| 412 | template <typename T, typename... DiagnosticArgs> |
| 413 | static const Sema::SemaDiagnosticBuilder& |
| 414 | appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg, |
| 415 | DiagnosticArgs &&... ExtraArgs) { |
| 416 | return appendDiagnostics(Bldr << std::forward<T>(ExtraArg), |
| 417 | std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 418 | } |
| 419 | |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 420 | /// Add an attribute {@code AttrType} to declaration {@code D}, provided that |
| 421 | /// {@code PassesCheck} is true. |
| 422 | /// Otherwise, emit diagnostic {@code DiagID}, passing in all parameters |
| 423 | /// specified in {@code ExtraArgs}. |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 424 | template <typename AttrType, typename... DiagnosticArgs> |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 425 | static void handleSimpleAttributeOrDiagnose(Sema &S, Decl *D, |
| 426 | const AttributeCommonInfo &CI, |
| 427 | bool PassesCheck, unsigned DiagID, |
| 428 | DiagnosticArgs &&... ExtraArgs) { |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 429 | if (!PassesCheck) { |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 430 | Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID); |
| 431 | appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...); |
| 432 | return; |
| 433 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 434 | handleSimpleAttribute<AttrType>(S, D, CI); |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 437 | template <typename AttrType> |
| 438 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 439 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 440 | handleSimpleAttribute<AttrType>(S, D, AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 443 | /// 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] | 444 | /// already have one of the given incompatible attributes. |
| 445 | template <typename AttrType, typename IncompatibleAttrType, |
| 446 | typename... IncompatibleAttrTypes> |
| 447 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 448 | const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 449 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL)) |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 450 | return; |
| 451 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 452 | AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 455 | /// Check if the passed-in expression is of type int or bool. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 456 | static bool isIntOrBool(Expr *Exp) { |
| 457 | QualType QT = Exp->getType(); |
| 458 | return QT->isBooleanType() || QT->isIntegerType(); |
| 459 | } |
| 460 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 461 | |
| 462 | // Check to see if the type is a smart pointer of some kind. We assume |
| 463 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 464 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 465 | auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record, |
| 466 | OverloadedOperatorKind Op) { |
| 467 | DeclContextLookupResult Result = |
| 468 | Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op)); |
| 469 | return !Result.empty(); |
| 470 | }; |
| 471 | |
| 472 | const RecordDecl *Record = RT->getDecl(); |
| 473 | bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star); |
| 474 | bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow); |
| 475 | if (foundStarOperator && foundArrowOperator) |
| 476 | return true; |
| 477 | |
| 478 | const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record); |
| 479 | if (!CXXRecord) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 480 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 481 | |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 482 | for (auto BaseSpecifier : CXXRecord->bases()) { |
| 483 | if (!foundStarOperator) |
| 484 | foundStarOperator = IsOverloadedOperatorPresent( |
| 485 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Star); |
| 486 | if (!foundArrowOperator) |
| 487 | foundArrowOperator = IsOverloadedOperatorPresent( |
| 488 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow); |
| 489 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 490 | |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 491 | if (foundStarOperator && foundArrowOperator) |
| 492 | return true; |
| 493 | |
| 494 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 497 | /// Check if passed in Decl is a pointer type. |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 498 | /// Note that this function may produce an error message. |
| 499 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 500 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 501 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 502 | const auto *VD = cast<ValueDecl>(D); |
| 503 | QualType QT = VD->getType(); |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 504 | if (QT->isAnyPointerType()) |
| 505 | return true; |
| 506 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 507 | if (const auto *RT = QT->getAs<RecordType>()) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 508 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 509 | // (We don't want to force template instantiation if we can avoid it, |
| 510 | // since that would alter the order in which templates are instantiated.) |
| 511 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 512 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 513 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 514 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 515 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 516 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 517 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 518 | 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] | 519 | return false; |
| 520 | } |
| 521 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 522 | /// Checks that the passed in QualType either is of RecordType or points |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 523 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 524 | static const RecordType *getRecordType(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 525 | if (const auto *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 526 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 527 | |
| 528 | // Now check if we point to record type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 529 | if (const auto *PT = QT->getAs<PointerType>()) |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 530 | return PT->getPointeeType()->getAs<RecordType>(); |
| 531 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 532 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 535 | template <typename AttrType> |
| 536 | static bool checkRecordDeclForAttr(const RecordDecl *RD) { |
| 537 | // Check if the record itself has the attribute. |
| 538 | if (RD->hasAttr<AttrType>()) |
| 539 | return true; |
| 540 | |
| 541 | // Else check if any base classes have the attribute. |
| 542 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 543 | CXXBasePaths BPaths(false, false); |
| 544 | if (CRD->lookupInBases( |
| 545 | [](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 546 | const auto &Ty = *BS->getType(); |
| 547 | // If it's type-dependent, we assume it could have the attribute. |
| 548 | if (Ty.isDependentType()) |
| 549 | return true; |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 550 | return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>(); |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 551 | }, |
| 552 | BPaths, true)) |
| 553 | return true; |
| 554 | } |
| 555 | return false; |
| 556 | } |
| 557 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 558 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 559 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 560 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 561 | if (!RT) |
| 562 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 563 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 564 | // 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] | 565 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 566 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 567 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 568 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 569 | // FIXME -- Check the type that the smart pointer points to. |
| 570 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 571 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 572 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 573 | return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl()); |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 576 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 577 | const auto *TD = Ty->getAs<TypedefType>(); |
| 578 | if (!TD) |
| 579 | return false; |
| 580 | |
| 581 | TypedefNameDecl *TN = TD->getDecl(); |
| 582 | if (!TN) |
| 583 | return false; |
| 584 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 585 | return TN->hasAttr<CapabilityAttr>(); |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 588 | static bool typeHasCapability(Sema &S, QualType Ty) { |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 589 | if (checkTypedefTypeForCapability(Ty)) |
| 590 | return true; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 591 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 592 | if (checkRecordTypeForCapability(S, Ty)) |
| 593 | return true; |
| 594 | |
| 595 | return false; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 598 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 599 | // Capability expressions are simple expressions involving the boolean logic |
| 600 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 601 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 602 | // is a capability or not. |
| 603 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 604 | if (const auto *E = dyn_cast<CastExpr>(Ex)) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 605 | return isCapabilityExpr(S, E->getSubExpr()); |
| 606 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 607 | return isCapabilityExpr(S, E->getSubExpr()); |
| 608 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 609 | if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf || |
| 610 | E->getOpcode() == UO_Deref) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 611 | return isCapabilityExpr(S, E->getSubExpr()); |
| 612 | return false; |
| 613 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 614 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 615 | return isCapabilityExpr(S, E->getLHS()) && |
| 616 | isCapabilityExpr(S, E->getRHS()); |
| 617 | return false; |
| 618 | } |
| 619 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 620 | return typeHasCapability(S, Ex->getType()); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 623 | /// Checks that all attribute arguments, starting from Sidx, resolve to |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 624 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 625 | /// \param Sidx The attribute argument index to start checking with. |
| 626 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 627 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 628 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 629 | const ParsedAttr &AL, |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 630 | SmallVectorImpl<Expr *> &Args, |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 631 | unsigned Sidx = 0, |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 632 | bool ParamIdxOk = false) { |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 633 | if (Sidx == AL.getNumArgs()) { |
| 634 | // If we don't have any capability arguments, the attribute implicitly |
| 635 | // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're |
| 636 | // a non-static method, and that the class is a (scoped) capability. |
| 637 | const auto *MD = dyn_cast<const CXXMethodDecl>(D); |
| 638 | if (MD && !MD->isStatic()) { |
| 639 | const CXXRecordDecl *RD = MD->getParent(); |
| 640 | // FIXME -- need to check this again on template instantiation |
| 641 | if (!checkRecordDeclForAttr<CapabilityAttr>(RD) && |
| 642 | !checkRecordDeclForAttr<ScopedLockableAttr>(RD)) |
| 643 | S.Diag(AL.getLoc(), |
| 644 | diag::warn_thread_attribute_not_on_capability_member) |
| 645 | << AL << MD->getParent(); |
| 646 | } else { |
| 647 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member) |
| 648 | << AL; |
| 649 | } |
| 650 | } |
| 651 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 652 | for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) { |
| 653 | Expr *ArgExp = AL.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 654 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 655 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 656 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 657 | Args.push_back(ArgExp); |
| 658 | continue; |
| 659 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 661 | if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 662 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 663 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 664 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 665 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 666 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 667 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 668 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 669 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 670 | // We allow constant strings to be used as a placeholder for expressions |
| 671 | // that are not valid C++ syntax, but warn that they are ignored. |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 672 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 673 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 674 | continue; |
| 675 | } |
| 676 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 677 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 678 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 679 | // A pointer to member expression of the form &MyClass::mu is treated |
| 680 | // specially -- we need to look at the type of the member. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 681 | if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 682 | if (UOp->getOpcode() == UO_AddrOf) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 683 | if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 684 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 685 | ArgTy = DRE->getDecl()->getType(); |
| 686 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 687 | // 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] | 688 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 689 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 690 | // Now check if we index into a record type function param. |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 691 | if(!RT && ParamIdxOk) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 692 | const auto *FD = dyn_cast<FunctionDecl>(D); |
| 693 | const auto *IL = dyn_cast<IntegerLiteral>(ArgExp); |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 694 | if(FD && IL) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 695 | unsigned int NumParams = FD->getNumParams(); |
| 696 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 697 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 698 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 699 | if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Aaron Ballman | ce667f6 | 2019-02-12 13:19:02 +0000 | [diff] [blame] | 700 | S.Diag(AL.getLoc(), |
| 701 | diag::err_attribute_argument_out_of_bounds_extra_info) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 702 | << AL << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 703 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 704 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 705 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 709 | // If the type does not have a capability, see if the components of the |
| 710 | // expression have capabilities. This allows for writing C code where the |
| 711 | // capability may be on the type, and the expression is a capability |
| 712 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 713 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 714 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 715 | << AL << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 716 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 717 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 718 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 721 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 722 | // Attribute Implementations |
| 723 | //===----------------------------------------------------------------------===// |
| 724 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 725 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 726 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | return; |
| 728 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 729 | D->addAttr(::new (S.Context) PtGuardedVarAttr(S.Context, AL)); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 732 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 733 | Expr *&Arg) { |
| 734 | SmallVector<Expr *, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 735 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 736 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 737 | unsigned Size = Args.size(); |
| 738 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 739 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 740 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 742 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 743 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 746 | static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 747 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 748 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 749 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 750 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 751 | D->addAttr(::new (S.Context) GuardedByAttr(S.Context, AL, Arg)); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 754 | static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 755 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 756 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 757 | return; |
| 758 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 759 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 760 | return; |
| 761 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 762 | D->addAttr(::new (S.Context) PtGuardedByAttr(S.Context, AL, Arg)); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 765 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 766 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 767 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 768 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 769 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 770 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 771 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 772 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 773 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL; |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 774 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 775 | } |
| 776 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 777 | // Check that all arguments are lockable objects. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 778 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 779 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 780 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 781 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 782 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 785 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 786 | SmallVector<Expr *, 1> Args; |
| 787 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 788 | return; |
| 789 | |
| 790 | Expr **StartArg = &Args[0]; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 791 | D->addAttr(::new (S.Context) |
| 792 | AcquiredAfterAttr(S.Context, AL, StartArg, Args.size())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 795 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 796 | SmallVector<Expr *, 1> Args; |
| 797 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 798 | return; |
| 799 | |
| 800 | Expr **StartArg = &Args[0]; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 801 | D->addAttr(::new (S.Context) |
| 802 | AcquiredBeforeAttr(S.Context, AL, StartArg, Args.size())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 805 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 806 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 807 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 808 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 809 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 810 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 811 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 814 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 815 | SmallVector<Expr *, 1> Args; |
| 816 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 817 | return; |
| 818 | |
| 819 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 820 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 821 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 822 | AssertSharedLockAttr(S.Context, AL, StartArg, Size)); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 826 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 827 | SmallVector<Expr *, 1> Args; |
| 828 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 829 | return; |
| 830 | |
| 831 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 832 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 833 | D->addAttr(::new (S.Context) |
| 834 | AssertExclusiveLockAttr(S.Context, AL, StartArg, Size)); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 837 | /// 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] | 838 | /// is an integral type. Will emit appropriate diagnostics if this returns |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 839 | /// false. |
| 840 | /// |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 841 | /// 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] | 842 | template <typename AttrInfo> |
| 843 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 844 | const AttrInfo &AI, unsigned AttrArgNo) { |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 845 | assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument"); |
| 846 | Expr *AttrArg = AI.getArgAsExpr(AttrArgNo); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 847 | ParamIdx Idx; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 848 | if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 849 | Idx)) |
| 850 | return false; |
| 851 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 852 | const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 853 | if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 854 | SourceLocation SrcLoc = AttrArg->getBeginLoc(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 855 | S.Diag(SrcLoc, diag::err_attribute_integers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 856 | << AI << Param->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 857 | return false; |
| 858 | } |
| 859 | return true; |
| 860 | } |
| 861 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 862 | static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 863 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 864 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 865 | return; |
| 866 | |
| 867 | const auto *FD = cast<FunctionDecl>(D); |
| 868 | if (!FD->getReturnType()->isPointerType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 869 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 873 | const Expr *SizeExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 874 | int SizeArgNoVal; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 875 | // Parameter indices are 1-indexed, hence Index=1 |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 876 | if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 877 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 878 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 879 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 880 | ParamIdx SizeArgNo(SizeArgNoVal, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 881 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 882 | ParamIdx NumberArgNo; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 883 | if (AL.getNumArgs() == 2) { |
| 884 | const Expr *NumberExpr = AL.getArgAsExpr(1); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 885 | int Val; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 886 | // Parameter indices are 1-based, hence Index=2 |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 887 | if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 888 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 889 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 890 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 891 | NumberArgNo = ParamIdx(Val, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 894 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 895 | AllocSizeAttr(S.Context, AL, SizeArgNo, NumberArgNo)); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 896 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 897 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 898 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 899 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 900 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 901 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 902 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 903 | if (!isIntOrBool(AL.getArgAsExpr(0))) { |
| 904 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 905 | << AL << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 906 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 910 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 911 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 912 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 915 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 916 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 917 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 918 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 919 | return; |
| 920 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 921 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 922 | S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 925 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 926 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 927 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 928 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 929 | return; |
| 930 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 931 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 932 | S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 935 | static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 936 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 937 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 938 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 939 | unsigned Size = Args.size(); |
| 940 | if (Size == 0) |
| 941 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 942 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 943 | D->addAttr(::new (S.Context) LockReturnedAttr(S.Context, AL, Args[0])); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 946 | static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 947 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 948 | return; |
| 949 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 950 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 951 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 952 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 953 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 954 | if (Size == 0) |
| 955 | return; |
| 956 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 957 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 958 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 959 | LocksExcludedAttr(S.Context, AL, StartArg, Size)); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 962 | static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 963 | Expr *&Cond, StringRef &Msg) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 964 | Cond = AL.getArgAsExpr(0); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 965 | if (!Cond->isTypeDependent()) { |
| 966 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 967 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 968 | return false; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 969 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 972 | if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 973 | return false; |
| 974 | |
| 975 | if (Msg.empty()) |
| 976 | Msg = "<no message provided>"; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 977 | |
| 978 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 979 | if (isa<FunctionDecl>(D) && !Cond->isValueDependent() && |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 980 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 981 | Diags)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 982 | 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] | 983 | for (const PartialDiagnosticAt &PDiag : Diags) |
| 984 | S.Diag(PDiag.first, PDiag.second); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 985 | return false; |
| 986 | } |
| 987 | return true; |
| 988 | } |
| 989 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 990 | static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 991 | S.Diag(AL.getLoc(), diag::ext_clang_enable_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 992 | |
| 993 | Expr *Cond; |
| 994 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 995 | if (checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 996 | D->addAttr(::new (S.Context) EnableIfAttr(S.Context, AL, Cond, Msg)); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | namespace { |
| 1000 | /// Determines if a given Expr references any of the given function's |
| 1001 | /// ParmVarDecls, or the function's implicit `this` parameter (if applicable). |
| 1002 | class ArgumentDependenceChecker |
| 1003 | : public RecursiveASTVisitor<ArgumentDependenceChecker> { |
| 1004 | #ifndef NDEBUG |
| 1005 | const CXXRecordDecl *ClassType; |
| 1006 | #endif |
| 1007 | llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms; |
| 1008 | bool Result; |
| 1009 | |
| 1010 | public: |
| 1011 | ArgumentDependenceChecker(const FunctionDecl *FD) { |
| 1012 | #ifndef NDEBUG |
| 1013 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1014 | ClassType = MD->getParent(); |
| 1015 | else |
| 1016 | ClassType = nullptr; |
| 1017 | #endif |
| 1018 | Parms.insert(FD->param_begin(), FD->param_end()); |
| 1019 | } |
| 1020 | |
| 1021 | bool referencesArgs(Expr *E) { |
| 1022 | Result = false; |
| 1023 | TraverseStmt(E); |
| 1024 | return Result; |
| 1025 | } |
| 1026 | |
| 1027 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 1028 | assert(E->getType()->getPointeeCXXRecordDecl() == ClassType && |
| 1029 | "`this` doesn't refer to the enclosing class?"); |
| 1030 | Result = true; |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 1035 | if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 1036 | if (Parms.count(PVD)) { |
| 1037 | Result = true; |
| 1038 | return false; |
| 1039 | } |
| 1040 | return true; |
| 1041 | } |
| 1042 | }; |
| 1043 | } |
| 1044 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1045 | static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1046 | S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1047 | |
| 1048 | Expr *Cond; |
| 1049 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1050 | if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | |
| 1053 | StringRef DiagTypeStr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1054 | if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1055 | return; |
| 1056 | |
| 1057 | DiagnoseIfAttr::DiagnosticType DiagType; |
| 1058 | if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1059 | S.Diag(AL.getArgAsExpr(2)->getBeginLoc(), |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1060 | diag::err_diagnose_if_invalid_diagnostic_type); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1061 | return; |
| 1062 | } |
| 1063 | |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1064 | bool ArgDependent = false; |
Argyrios Kyrtzidis | 5f0c0aa | 2017-05-24 18:35:01 +0000 | [diff] [blame] | 1065 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1066 | ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1067 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1068 | S.Context, AL, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D))); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Guillaume Chatelet | 98f3151 | 2019-09-25 11:31:28 +0200 | [diff] [blame] | 1071 | static void handleNoBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1072 | static constexpr const StringRef kWildcard = "*"; |
| 1073 | |
| 1074 | llvm::SmallVector<StringRef, 16> Names; |
| 1075 | bool HasWildcard = false; |
| 1076 | |
| 1077 | const auto AddBuiltinName = [&Names, &HasWildcard](StringRef Name) { |
| 1078 | if (Name == kWildcard) |
| 1079 | HasWildcard = true; |
| 1080 | Names.push_back(Name); |
| 1081 | }; |
| 1082 | |
| 1083 | // Add previously defined attributes. |
| 1084 | if (const auto *NBA = D->getAttr<NoBuiltinAttr>()) |
| 1085 | for (StringRef BuiltinName : NBA->builtinNames()) |
| 1086 | AddBuiltinName(BuiltinName); |
| 1087 | |
| 1088 | // Add current attributes. |
| 1089 | if (AL.getNumArgs() == 0) |
| 1090 | AddBuiltinName(kWildcard); |
| 1091 | else |
| 1092 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
| 1093 | StringRef BuiltinName; |
| 1094 | SourceLocation LiteralLoc; |
| 1095 | if (!S.checkStringLiteralArgumentAttr(AL, I, BuiltinName, &LiteralLoc)) |
| 1096 | return; |
| 1097 | |
Guillaume Chatelet | 1c85a2e | 2019-10-29 17:28:34 +0100 | [diff] [blame] | 1098 | if (Builtin::Context::isBuiltinFunc(BuiltinName)) |
Guillaume Chatelet | 98f3151 | 2019-09-25 11:31:28 +0200 | [diff] [blame] | 1099 | AddBuiltinName(BuiltinName); |
| 1100 | else |
| 1101 | S.Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name) |
Aaron Ballman | dab43c8 | 2020-03-14 17:00:45 -0400 | [diff] [blame] | 1102 | << BuiltinName << AL; |
Guillaume Chatelet | 98f3151 | 2019-09-25 11:31:28 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | // Repeating the same attribute is fine. |
| 1106 | llvm::sort(Names); |
| 1107 | Names.erase(std::unique(Names.begin(), Names.end()), Names.end()); |
| 1108 | |
| 1109 | // Empty no_builtin must be on its own. |
| 1110 | if (HasWildcard && Names.size() > 1) |
| 1111 | S.Diag(D->getLocation(), |
| 1112 | diag::err_attribute_no_builtin_wildcard_or_builtin_name) |
Aaron Ballman | dab43c8 | 2020-03-14 17:00:45 -0400 | [diff] [blame] | 1113 | << AL; |
Guillaume Chatelet | 98f3151 | 2019-09-25 11:31:28 +0200 | [diff] [blame] | 1114 | |
| 1115 | if (D->hasAttr<NoBuiltinAttr>()) |
| 1116 | D->dropAttr<NoBuiltinAttr>(); |
| 1117 | D->addAttr(::new (S.Context) |
| 1118 | NoBuiltinAttr(S.Context, AL, Names.data(), Names.size())); |
| 1119 | } |
| 1120 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1121 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1122 | if (D->hasAttr<PassObjectSizeAttr>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1123 | 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] | 1124 | return; |
| 1125 | } |
| 1126 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1127 | Expr *E = AL.getArgAsExpr(0); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1128 | uint32_t Type; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1129 | if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1)) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | |
| 1132 | // pass_object_size's argument is passed in as the second argument of |
| 1133 | // __builtin_object_size. So, it has the same constraints as that second |
| 1134 | // argument; namely, it must be in the range [0, 3]. |
| 1135 | if (Type > 3) { |
Aaron Ballman | 52c9ad2 | 2019-02-12 13:04:11 +0000 | [diff] [blame] | 1136 | S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1137 | << AL << 0 << 3 << E->getSourceRange(); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | // pass_object_size is only supported on constant pointer parameters; as a |
| 1142 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 1143 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 1144 | // definition, so we defer the constness check until later. |
| 1145 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1146 | S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1147 | return; |
| 1148 | } |
| 1149 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1150 | D->addAttr(::new (S.Context) PassObjectSizeAttr(S.Context, AL, (int)Type)); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1153 | static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1154 | ConsumableAttr::ConsumedState DefaultState; |
| 1155 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1156 | if (AL.isArgIdent(0)) { |
| 1157 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1158 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1159 | DefaultState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1160 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1161 | << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1164 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1165 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1166 | << AL << AANT_ArgumentIdentifier; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1167 | return; |
| 1168 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1169 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1170 | D->addAttr(::new (S.Context) ConsumableAttr(S.Context, AL, DefaultState)); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1174 | const ParsedAttr &AL) { |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 1175 | QualType ThisType = MD->getThisType()->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1176 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1177 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1178 | if (!RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | dab43c8 | 2020-03-14 17:00:45 -0400 | [diff] [blame] | 1179 | S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << RD; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1180 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1181 | return false; |
| 1182 | } |
| 1183 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1184 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1185 | return true; |
| 1186 | } |
| 1187 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1188 | static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1189 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1190 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1191 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1192 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1193 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1194 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1195 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1196 | for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1197 | CallableWhenAttr::ConsumedState CallableState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1198 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1199 | StringRef StateString; |
| 1200 | SourceLocation Loc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1201 | if (AL.isArgIdent(ArgIndex)) { |
| 1202 | IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex); |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1203 | StateString = Ident->Ident->getName(); |
| 1204 | Loc = Ident->Loc; |
| 1205 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1206 | if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc)) |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1207 | return; |
| 1208 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1209 | |
| 1210 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1211 | CallableState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1212 | S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1213 | return; |
| 1214 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1215 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1216 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1217 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1218 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1219 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1220 | CallableWhenAttr(S.Context, AL, States.data(), States.size())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1223 | static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1224 | ParamTypestateAttr::ConsumedState ParamState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1225 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1226 | if (AL.isArgIdent(0)) { |
| 1227 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1228 | StringRef StateString = Ident->Ident->getName(); |
| 1229 | |
| 1230 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1231 | ParamState)) { |
| 1232 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1233 | << AL << StateString; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1234 | return; |
| 1235 | } |
| 1236 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1237 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1238 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1239 | return; |
| 1240 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1241 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1242 | // FIXME: This check is currently being done in the analysis. It can be |
| 1243 | // enabled here only after the parser propagates attributes at |
| 1244 | // template specialization definition, not declaration. |
| 1245 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1246 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1247 | // |
| 1248 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1249 | // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1250 | // ReturnType.getAsString(); |
| 1251 | // return; |
| 1252 | //} |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1253 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1254 | D->addAttr(::new (S.Context) ParamTypestateAttr(S.Context, AL, ParamState)); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1257 | static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1258 | ReturnTypestateAttr::ConsumedState ReturnState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1259 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1260 | if (AL.isArgIdent(0)) { |
| 1261 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1262 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1263 | ReturnState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1264 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1265 | << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1266 | return; |
| 1267 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1268 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1269 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1270 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1271 | return; |
| 1272 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1273 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1274 | // FIXME: This check is currently being done in the analysis. It can be |
| 1275 | // enabled here only after the parser propagates attributes at |
| 1276 | // template specialization definition, not declaration. |
| 1277 | //QualType ReturnType; |
| 1278 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1279 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1280 | // ReturnType = Param->getType(); |
| 1281 | // |
| 1282 | //} else if (const CXXConstructorDecl *Constructor = |
| 1283 | // dyn_cast<CXXConstructorDecl>(D)) { |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 1284 | // ReturnType = Constructor->getThisType()->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1285 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1286 | //} else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1287 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1288 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1289 | //} |
| 1290 | // |
| 1291 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1292 | // |
| 1293 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1294 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1295 | // ReturnType.getAsString(); |
| 1296 | // return; |
| 1297 | //} |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1298 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1299 | D->addAttr(::new (S.Context) ReturnTypestateAttr(S.Context, AL, ReturnState)); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1302 | static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1303 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1304 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1305 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1306 | SetTypestateAttr::ConsumedState NewState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1307 | if (AL.isArgIdent(0)) { |
| 1308 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1309 | StringRef Param = Ident->Ident->getName(); |
| 1310 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1311 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1312 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1315 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1316 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1317 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1318 | return; |
| 1319 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1320 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1321 | D->addAttr(::new (S.Context) SetTypestateAttr(S.Context, AL, NewState)); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1324 | static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1325 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1326 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1327 | |
| 1328 | TestTypestateAttr::ConsumedState TestState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1329 | if (AL.isArgIdent(0)) { |
| 1330 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1331 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1332 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1333 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1334 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1335 | return; |
| 1336 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1337 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1338 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1339 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1340 | return; |
| 1341 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1342 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1343 | D->addAttr(::new (S.Context) TestTypestateAttr(S.Context, AL, TestState)); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1346 | static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1347 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1348 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1351 | static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1352 | if (auto *TD = dyn_cast<TagDecl>(D)) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1353 | TD->addAttr(::new (S.Context) PackedAttr(S.Context, AL)); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 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 |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1366 | FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL)); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1367 | } else { |
| 1368 | // Report warning about changed offset in the newer compiler versions. |
| 1369 | if (BitfieldByteAligned) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1370 | S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1371 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1372 | FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL)); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1375 | } else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1376 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1379 | static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1380 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1381 | // variables or properties of Objective-C classes. The outlet must also |
| 1382 | // have an object reference type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1383 | if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1384 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1385 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1386 | << AL << VD->getType() << 0; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
| 1389 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1390 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1391 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1392 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1393 | << AL << PD->getType() << 1; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1394 | return false; |
| 1395 | } |
| 1396 | } |
| 1397 | else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1398 | S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1399 | return false; |
| 1400 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1401 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1402 | return true; |
| 1403 | } |
| 1404 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1405 | static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1406 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1407 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1408 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1409 | D->addAttr(::new (S.Context) IBOutletAttr(S.Context, AL)); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1412 | static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1413 | |
| 1414 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1415 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1416 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1417 | return; |
| 1418 | } |
| 1419 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1420 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1421 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1422 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1423 | ParsedType PT; |
| 1424 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1425 | if (AL.hasParsedType()) |
| 1426 | PT = AL.getTypeArg(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1427 | else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1428 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1429 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1430 | if (!PT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1431 | S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1432 | return; |
| 1433 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1434 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1435 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1436 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1437 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1438 | if (!QTLoc) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1439 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1440 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1441 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1442 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1443 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1444 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1445 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1446 | S.Diag(AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1447 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1448 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1449 | return; |
| 1450 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1451 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1452 | D->addAttr(::new (S.Context) IBOutletCollectionAttr(S.Context, AL, QTLoc)); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1455 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1456 | if (RefOkay) { |
| 1457 | if (T->isReferenceType()) |
| 1458 | return true; |
| 1459 | } else { |
| 1460 | T = T.getNonReferenceType(); |
| 1461 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1462 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1463 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1464 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1465 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1466 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1467 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1468 | for (const auto *I : UD->fields()) { |
| 1469 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1470 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1471 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1472 | } |
| 1473 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1479 | static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1480 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1481 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1482 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1483 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1484 | if (isReturnValue) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1485 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1486 | << AL << AttrParmRange << TypeRange; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1487 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1488 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1489 | << AL << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1490 | return false; |
| 1491 | } |
| 1492 | return true; |
| 1493 | } |
| 1494 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1495 | static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1496 | SmallVector<ParamIdx, 8> NonNullArgs; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1497 | for (unsigned I = 0; I < AL.getNumArgs(); ++I) { |
| 1498 | Expr *Ex = AL.getArgAsExpr(I); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1499 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1500 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1501 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1504 | if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) && |
| 1505 | !attrNonNullArgCheck( |
| 1506 | S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL, |
| 1507 | Ex->getSourceRange(), |
| 1508 | getFunctionOrMethodParamRange(D, Idx.getASTIndex()))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1509 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1510 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1511 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1512 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1513 | |
| 1514 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1515 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1516 | // check if the attribute came from a macro expansion or a template |
| 1517 | // instantiation. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1518 | if (NonNullArgs.empty() && AL.getLoc().isFileID() && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1519 | !S.inTemplateInstantiation()) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1520 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1521 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1522 | I != E && !AnyPointers; ++I) { |
| 1523 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1524 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1525 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1526 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1527 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1528 | if (!AnyPointers) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1529 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1530 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1531 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1532 | ParamIdx *Start = NonNullArgs.data(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1533 | unsigned Size = NonNullArgs.size(); |
| 1534 | llvm::array_pod_sort(Start, Start + Size); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1535 | D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, Start, Size)); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1538 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1539 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1540 | if (AL.getNumArgs() > 0) { |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1541 | if (D->getFunctionType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1542 | handleNonNullAttr(S, D, AL); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1543 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1544 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1545 | << D->getSourceRange(); |
| 1546 | } |
| 1547 | return; |
| 1548 | } |
| 1549 | |
| 1550 | // Is the argument a pointer type? |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1551 | if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(), |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1552 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1553 | return; |
| 1554 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1555 | D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, nullptr, 0)); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1558 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1559 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1560 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1561 | if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1562 | /* isReturnValue */ true)) |
| 1563 | return; |
| 1564 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1565 | D->addAttr(::new (S.Context) ReturnsNonNullAttr(S.Context, AL)); |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1568 | static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1569 | if (D->isInvalidDecl()) |
| 1570 | return; |
| 1571 | |
| 1572 | // noescape only applies to pointer types. |
| 1573 | QualType T = cast<ParmVarDecl>(D)->getType(); |
| 1574 | if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1575 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1576 | << AL << AL.getRange() << 0; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1577 | return; |
| 1578 | } |
| 1579 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1580 | D->addAttr(::new (S.Context) NoEscapeAttr(S.Context, AL)); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1583 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1584 | Expr *E = AL.getArgAsExpr(0), |
| 1585 | *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1586 | S.AddAssumeAlignedAttr(D, AL, E, OE); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1589 | static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1590 | S.AddAllocAlignAttr(D, AL, AL.getArgAsExpr(0)); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1593 | void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, |
| 1594 | Expr *OE) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1595 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1596 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1597 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1598 | AssumeAlignedAttr TmpAttr(Context, CI, E, OE); |
| 1599 | SourceLocation AttrLoc = TmpAttr.getLocation(); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1600 | |
| 1601 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1602 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1603 | << &TmpAttr << TmpAttr.getRange() << SR; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1604 | return; |
| 1605 | } |
| 1606 | |
| 1607 | if (!E->isValueDependent()) { |
| 1608 | llvm::APSInt I(64); |
| 1609 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1610 | if (OE) |
| 1611 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1612 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1613 | << E->getSourceRange(); |
| 1614 | else |
| 1615 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1616 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1617 | << E->getSourceRange(); |
| 1618 | return; |
| 1619 | } |
| 1620 | |
| 1621 | if (!I.isPowerOf2()) { |
| 1622 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1623 | << E->getSourceRange(); |
| 1624 | return; |
| 1625 | } |
Roman Lebedev | 0a002f6 | 2020-01-23 22:48:57 +0300 | [diff] [blame] | 1626 | |
Roman Lebedev | 1d0972f | 2020-01-24 17:01:27 +0300 | [diff] [blame] | 1627 | if (I > Sema::MaximumAlignment) |
Roman Lebedev | 0a002f6 | 2020-01-23 22:48:57 +0300 | [diff] [blame] | 1628 | Diag(CI.getLoc(), diag::warn_assume_aligned_too_great) |
Roman Lebedev | 1d0972f | 2020-01-24 17:01:27 +0300 | [diff] [blame] | 1629 | << CI.getRange() << Sema::MaximumAlignment; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | if (OE) { |
| 1633 | if (!OE->isValueDependent()) { |
| 1634 | llvm::APSInt I(64); |
| 1635 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1636 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1637 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1638 | << OE->getSourceRange(); |
| 1639 | return; |
| 1640 | } |
| 1641 | } |
| 1642 | } |
| 1643 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1644 | D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE)); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1647 | void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI, |
| 1648 | Expr *ParamExpr) { |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1649 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1650 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1651 | AllocAlignAttr TmpAttr(Context, CI, ParamIdx()); |
| 1652 | SourceLocation AttrLoc = CI.getLoc(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1653 | |
| 1654 | if (!ResultType->isDependentType() && |
| 1655 | !isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1656 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1657 | << &TmpAttr << CI.getRange() << getFunctionOrMethodResultSourceRange(D); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | } |
| 1660 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1661 | ParamIdx Idx; |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1662 | const auto *FuncDecl = cast<FunctionDecl>(D); |
| 1663 | if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1664 | /*AttrArgNum=*/1, ParamExpr, Idx)) |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1665 | return; |
| 1666 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1667 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Roman Lebedev | b749af6 | 2020-01-23 22:50:34 +0300 | [diff] [blame] | 1668 | if (!Ty->isDependentType() && !Ty->isIntegralType(Context) && |
| 1669 | !Ty->isAlignValT()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1670 | Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1671 | << &TmpAttr |
| 1672 | << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1673 | return; |
| 1674 | } |
| 1675 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1676 | D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx)); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1679 | /// Normalize the attribute, __foo__ becomes foo. |
| 1680 | /// Returns true if normalization was applied. |
| 1681 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1682 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1683 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1684 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1685 | return true; |
| 1686 | } |
| 1687 | return false; |
| 1688 | } |
| 1689 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1690 | static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1691 | // This attribute must be applied to a function declaration. The first |
| 1692 | // argument to the attribute must be an identifier, the name of the resource, |
| 1693 | // for example: malloc. The following arguments must be argument indexes, the |
| 1694 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1695 | // 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] | 1696 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1697 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1698 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1699 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1700 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1701 | << AL << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1702 | return; |
| 1703 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1704 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1705 | // Figure out our Kind. |
| 1706 | OwnershipAttr::OwnershipKind K = |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1707 | OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1708 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1709 | // Check arguments. |
| 1710 | switch (K) { |
| 1711 | case OwnershipAttr::Takes: |
| 1712 | case OwnershipAttr::Holds: |
| 1713 | if (AL.getNumArgs() < 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1714 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1715 | return; |
| 1716 | } |
| 1717 | break; |
| 1718 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1719 | if (AL.getNumArgs() > 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1720 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1721 | return; |
| 1722 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1723 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1726 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1727 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1728 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1729 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1730 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1731 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1732 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1733 | SmallVector<ParamIdx, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1734 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1735 | Expr *Ex = AL.getArgAsExpr(i); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1736 | ParamIdx Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1737 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1738 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1739 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1740 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1741 | QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1742 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1743 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1744 | case OwnershipAttr::Takes: |
| 1745 | case OwnershipAttr::Holds: |
| 1746 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1747 | Err = 0; |
| 1748 | break; |
| 1749 | case OwnershipAttr::Returns: |
| 1750 | if (!T->isIntegerType()) |
| 1751 | Err = 1; |
| 1752 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1753 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1754 | if (-1 != Err) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1755 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err |
| 1756 | << Ex->getSourceRange(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1757 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1758 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1761 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1762 | // Cannot have two ownership attributes of different kinds for the same |
| 1763 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1764 | if (I->getOwnKind() != K && I->args_end() != |
| 1765 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1766 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1767 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1768 | } else if (K == OwnershipAttr::Returns && |
| 1769 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1770 | // A returns attribute conflicts with any other returns attribute using |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1771 | // a different index. |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1772 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1773 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1774 | << I->args_begin()->getSourceIndex(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1775 | if (I->args_size()) |
| 1776 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1777 | << Idx.getSourceIndex() << Ex->getSourceRange(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1778 | return; |
| 1779 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1782 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1785 | ParamIdx *Start = OwnershipArgs.data(); |
| 1786 | unsigned Size = OwnershipArgs.size(); |
| 1787 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1788 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1789 | OwnershipAttr(S.Context, AL, Module, Start, Size)); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1792 | static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1793 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1794 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1795 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1796 | return; |
| 1797 | } |
| 1798 | |
| 1799 | // gcc rejects |
| 1800 | // class c { |
| 1801 | // static int a __attribute__((weakref ("v2"))); |
| 1802 | // static int b() __attribute__((weakref ("f3"))); |
| 1803 | // }; |
| 1804 | // and ignores the attributes of |
| 1805 | // void f(void) { |
| 1806 | // static int a __attribute__((weakref ("v2"))); |
| 1807 | // } |
| 1808 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1809 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1810 | if (!Ctx->isFileContext()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1811 | S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1812 | << cast<NamedDecl>(D); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1813 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | // The GCC manual says |
| 1817 | // |
| 1818 | // At present, a declaration to which `weakref' is attached can only |
| 1819 | // be `static'. |
| 1820 | // |
| 1821 | // It also says |
| 1822 | // |
| 1823 | // Without a TARGET, |
| 1824 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1825 | // equivalent to `weak'. |
| 1826 | // |
| 1827 | // gcc 4.4.1 will accept |
| 1828 | // int a7 __attribute__((weakref)); |
| 1829 | // as |
| 1830 | // int a7 __attribute__((weak)); |
| 1831 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1832 | // it if this behaviour is actually used. |
| 1833 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1834 | // GCC rejects |
| 1835 | // static ((alias ("y"), weakref)). |
| 1836 | // Should we? How to check that weakref is before or after alias? |
| 1837 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1838 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1839 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1840 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1841 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1842 | if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1843 | // GCC will accept anything as the argument of weakref. Should we |
| 1844 | // check for an existing decl? |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1845 | D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str)); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1846 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1847 | D->addAttr(::new (S.Context) WeakRefAttr(S.Context, AL)); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1850 | static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1851 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1852 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1853 | return; |
| 1854 | |
| 1855 | // Aliases should be on declarations, not definitions. |
| 1856 | const auto *FD = cast<FunctionDecl>(D); |
| 1857 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1858 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1861 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1862 | D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str)); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1865 | static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1866 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1867 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1868 | return; |
| 1869 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1870 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1871 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin); |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1872 | return; |
| 1873 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1874 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1875 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx); |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1876 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1877 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1878 | // Aliases should be on declarations, not definitions. |
| 1879 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1880 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1881 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1882 | return; |
| 1883 | } |
| 1884 | } else { |
| 1885 | const auto *VD = cast<VarDecl>(D); |
| 1886 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1887 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1888 | return; |
| 1889 | } |
| 1890 | } |
| 1891 | |
Nick Desaulniers | 9b9fe41 | 2019-01-09 23:54:55 +0000 | [diff] [blame] | 1892 | // Mark target used to prevent unneeded-internal-declaration warnings. |
| 1893 | if (!S.LangOpts.CPlusPlus) { |
| 1894 | // FIXME: demangle Str for C++, as the attribute refers to the mangled |
| 1895 | // linkage name, not the pre-mangled identifier. |
| 1896 | const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc()); |
| 1897 | LookupResult LR(S, target, Sema::LookupOrdinaryName); |
| 1898 | if (S.LookupQualifiedName(LR, S.getCurLexicalContext())) |
| 1899 | for (NamedDecl *ND : LR) |
| 1900 | ND->markUsed(S.Context); |
| 1901 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1902 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1903 | D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1906 | static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1907 | StringRef Model; |
| 1908 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1909 | // Check that it is a string. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1910 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1911 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1912 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1913 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1914 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1915 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1916 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1917 | return; |
| 1918 | } |
| 1919 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1920 | D->addAttr(::new (S.Context) TLSModelAttr(S.Context, AL, Model)); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1923 | static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1924 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1925 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1926 | D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL)); |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1927 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1930 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1931 | << AL << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1934 | static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1935 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Erich Keane | 659c871 | 2018-09-10 14:31:56 +0000 | [diff] [blame] | 1936 | |
| 1937 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 1938 | if (MD->getParent()->isLambda()) { |
| 1939 | S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL; |
| 1940 | return; |
| 1941 | } |
| 1942 | } |
| 1943 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1944 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 1945 | return; |
| 1946 | |
| 1947 | SmallVector<IdentifierInfo *, 8> CPUs; |
| 1948 | for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) { |
| 1949 | if (!AL.isArgIdent(ArgNo)) { |
| 1950 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1951 | << AL << AANT_ArgumentIdentifier; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1952 | return; |
| 1953 | } |
| 1954 | |
| 1955 | IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo); |
| 1956 | StringRef CPUName = CPUArg->Ident->getName().trim(); |
| 1957 | |
| 1958 | if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) { |
| 1959 | S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value) |
| 1960 | << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch); |
| 1961 | return; |
| 1962 | } |
| 1963 | |
| 1964 | const TargetInfo &Target = S.Context.getTargetInfo(); |
| 1965 | if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) { |
| 1966 | return Target.CPUSpecificManglingCharacter(CPUName) == |
| 1967 | Target.CPUSpecificManglingCharacter(Cur->getName()); |
| 1968 | })) { |
| 1969 | S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries); |
| 1970 | return; |
| 1971 | } |
| 1972 | CPUs.push_back(CPUArg->Ident); |
| 1973 | } |
| 1974 | |
| 1975 | FD->setIsMultiVersion(true); |
| 1976 | if (AL.getKind() == ParsedAttr::AT_CPUSpecific) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1977 | D->addAttr(::new (S.Context) |
| 1978 | CPUSpecificAttr(S.Context, AL, CPUs.data(), CPUs.size())); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1979 | else |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1980 | D->addAttr(::new (S.Context) |
| 1981 | CPUDispatchAttr(S.Context, AL, CPUs.data(), CPUs.size())); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1984 | static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1985 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1986 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1987 | << AL << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1988 | return; |
| 1989 | } |
| 1990 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1991 | if (CommonAttr *CA = S.mergeCommonAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1992 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1995 | static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1996 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL)) |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1997 | return; |
| 1998 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1999 | if (AL.isDeclspecAttribute()) { |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 2000 | const auto &Triple = S.getASTContext().getTargetInfo().getTriple(); |
| 2001 | const auto &Arch = Triple.getArch(); |
| 2002 | if (Arch != llvm::Triple::x86 && |
| 2003 | (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2004 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2005 | << AL << Triple.getArchName(); |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 2006 | return; |
| 2007 | } |
| 2008 | } |
| 2009 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2010 | D->addAttr(::new (S.Context) NakedAttr(S.Context, AL)); |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2013 | static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2014 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2015 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2016 | if (!isa<ObjCMethodDecl>(D)) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 2017 | S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2018 | << Attrs << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2022 | D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs)); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2025 | static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 2026 | if (!S.getLangOpts().CFProtectionBranch) |
| 2027 | S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored); |
| 2028 | else |
| 2029 | handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2032 | bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 2033 | if (!checkAttributeNumArgs(*this, Attrs, 0)) { |
| 2034 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2041 | bool Sema::CheckAttrTarget(const ParsedAttr &AL) { |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2042 | // Check whether the attribute is valid on the current target. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2043 | if (!AL.existsInTarget(Context.getTargetInfo())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2044 | Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2045 | AL.setInvalid(); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2046 | return true; |
| 2047 | } |
| 2048 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2049 | return false; |
| 2050 | } |
| 2051 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2052 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2053 | |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2054 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 2055 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 2056 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2057 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2058 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 2059 | !VD->getType()->isFunctionPointerType())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2060 | S.Diag(AL.getLoc(), AL.isCXX11Attribute() |
| 2061 | ? diag::err_attribute_wrong_decl_type |
| 2062 | : diag::warn_attribute_wrong_decl_type) |
| 2063 | << AL << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2064 | return; |
| 2065 | } |
| 2066 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2067 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2068 | D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2071 | // PS3 PPU-specific. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2072 | static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2073 | /* |
| 2074 | Returning a Vector Class in Registers |
| 2075 | |
| 2076 | According to the PPU ABI specifications, a class with a single member of |
| 2077 | vector type is returned in memory when used as the return value of a |
| 2078 | function. |
| 2079 | This results in inefficient code when implementing vector classes. To return |
| 2080 | the value in a single vector register, add the vecreturn attribute to the |
| 2081 | class definition. This attribute is also applicable to struct types. |
| 2082 | |
| 2083 | Example: |
| 2084 | |
| 2085 | struct Vector |
| 2086 | { |
| 2087 | __vector float xyzw; |
| 2088 | } __attribute__((vecreturn)); |
| 2089 | |
| 2090 | Vector Add(Vector lhs, Vector rhs) |
| 2091 | { |
| 2092 | Vector result; |
| 2093 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 2094 | return result; // This will be returned in a register |
| 2095 | } |
| 2096 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 2097 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2098 | S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2099 | return; |
| 2100 | } |
| 2101 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2102 | const auto *R = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2103 | int count = 0; |
| 2104 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2105 | if (!isa<CXXRecordDecl>(R)) { |
| 2106 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2107 | return; |
| 2108 | } |
| 2109 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2110 | if (!cast<CXXRecordDecl>(R)->isPOD()) { |
| 2111 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2112 | return; |
| 2113 | } |
| 2114 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2115 | for (const auto *I : R->fields()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2116 | if ((count == 1) || !I->getType()->isVectorType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2117 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2118 | return; |
| 2119 | } |
| 2120 | count++; |
| 2121 | } |
| 2122 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2123 | D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL)); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2126 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2127 | const ParsedAttr &AL) { |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2128 | if (isa<ParmVarDecl>(D)) { |
| 2129 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 2130 | // parameter of a function declaration or lambda. |
| 2131 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2132 | S.Diag(AL.getLoc(), |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2133 | diag::err_carries_dependency_param_not_function_decl); |
| 2134 | return; |
| 2135 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2136 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2137 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2138 | D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL)); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2141 | static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2142 | bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName(); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2143 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2144 | // 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] | 2145 | // about using it as an extension. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2146 | if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2147 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2148 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2149 | D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL)); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2152 | static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2153 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2154 | if (AL.getNumArgs() && |
| 2155 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2156 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2157 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2158 | D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2161 | static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 2162 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2163 | if (AL.getNumArgs() && |
| 2164 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2165 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2166 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2167 | D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2170 | template <typename AttrTy> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2171 | static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2172 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2173 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2174 | if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2175 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2176 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2177 | D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str)); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2180 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2181 | const ParsedAttr &AL) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2182 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2183 | S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2184 | << AL << AL.getRange(); |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 2185 | return; |
| 2186 | } |
| 2187 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2188 | D->addAttr(::new (S.Context) ObjCExplicitProtocolImplAttr(S.Context, AL)); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2191 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2192 | IdentifierInfo *Platform, |
| 2193 | VersionTuple Introduced, |
| 2194 | VersionTuple Deprecated, |
| 2195 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2196 | StringRef PlatformName |
| 2197 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2198 | if (PlatformName.empty()) |
| 2199 | PlatformName = Platform->getName(); |
| 2200 | |
| 2201 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2202 | // of these steps are needed). |
| 2203 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2204 | !(Introduced <= Deprecated)) { |
| 2205 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2206 | << 1 << PlatformName << Deprecated.getAsString() |
| 2207 | << 0 << Introduced.getAsString(); |
| 2208 | return true; |
| 2209 | } |
| 2210 | |
| 2211 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2212 | !(Introduced <= Obsoleted)) { |
| 2213 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2214 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2215 | << 0 << Introduced.getAsString(); |
| 2216 | return true; |
| 2217 | } |
| 2218 | |
| 2219 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2220 | !(Deprecated <= Obsoleted)) { |
| 2221 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2222 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2223 | << 1 << Deprecated.getAsString(); |
| 2224 | return true; |
| 2225 | } |
| 2226 | |
| 2227 | return false; |
| 2228 | } |
| 2229 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2230 | /// Check whether the two versions match. |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2231 | /// |
| 2232 | /// If either version tuple is empty, then they are assumed to match. If |
| 2233 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2234 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2235 | bool BeforeIsOkay) { |
| 2236 | if (X.empty() || Y.empty()) |
| 2237 | return true; |
| 2238 | |
| 2239 | if (X == Y) |
| 2240 | return true; |
| 2241 | |
| 2242 | if (BeforeIsOkay && X < Y) |
| 2243 | return true; |
| 2244 | |
| 2245 | return false; |
| 2246 | } |
| 2247 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2248 | AvailabilityAttr *Sema::mergeAvailabilityAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2249 | NamedDecl *D, const AttributeCommonInfo &CI, IdentifierInfo *Platform, |
| 2250 | bool Implicit, VersionTuple Introduced, VersionTuple Deprecated, |
| 2251 | VersionTuple Obsoleted, bool IsUnavailable, StringRef Message, |
| 2252 | bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK, |
| 2253 | int Priority) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2254 | VersionTuple MergedIntroduced = Introduced; |
| 2255 | VersionTuple MergedDeprecated = Deprecated; |
| 2256 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2257 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2258 | bool OverrideOrImpl = false; |
| 2259 | switch (AMK) { |
| 2260 | case AMK_None: |
| 2261 | case AMK_Redeclaration: |
| 2262 | OverrideOrImpl = false; |
| 2263 | break; |
| 2264 | |
| 2265 | case AMK_Override: |
| 2266 | case AMK_ProtocolImplementation: |
| 2267 | OverrideOrImpl = true; |
| 2268 | break; |
| 2269 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2270 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2271 | if (D->hasAttrs()) { |
| 2272 | AttrVec &Attrs = D->getAttrs(); |
| 2273 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2274 | const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2275 | if (!OldAA) { |
| 2276 | ++i; |
| 2277 | continue; |
| 2278 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2279 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2280 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2281 | if (OldPlatform != Platform) { |
| 2282 | ++i; |
| 2283 | continue; |
| 2284 | } |
| 2285 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2286 | // If there is an existing availability attribute for this platform that |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2287 | // has a lower priority use the existing one and discard the new |
| 2288 | // attribute. |
| 2289 | if (OldAA->getPriority() < Priority) |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2290 | return nullptr; |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2291 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2292 | // If there is an existing attribute for this platform that has a higher |
| 2293 | // priority than the new attribute then erase the old one and continue |
| 2294 | // processing the attributes. |
| 2295 | if (OldAA->getPriority() > Priority) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2296 | Attrs.erase(Attrs.begin() + i); |
| 2297 | --e; |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2301 | FoundAny = true; |
| 2302 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2303 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2304 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2305 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2306 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2307 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2308 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2309 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2310 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2311 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2312 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2313 | int Which = -1; |
| 2314 | VersionTuple FirstVersion; |
| 2315 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2316 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2317 | Which = 0; |
| 2318 | FirstVersion = OldIntroduced; |
| 2319 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2320 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2321 | Which = 1; |
| 2322 | FirstVersion = Deprecated; |
| 2323 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2324 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2325 | Which = 2; |
| 2326 | FirstVersion = Obsoleted; |
| 2327 | SecondVersion = OldObsoleted; |
| 2328 | } |
| 2329 | |
| 2330 | if (Which == -1) { |
| 2331 | Diag(OldAA->getLocation(), |
| 2332 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2333 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2334 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2335 | } else { |
| 2336 | Diag(OldAA->getLocation(), |
| 2337 | diag::warn_mismatched_availability_override) |
| 2338 | << Which |
| 2339 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2340 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2341 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2342 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2343 | if (AMK == AMK_Override) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2344 | Diag(CI.getLoc(), diag::note_overridden_method); |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2345 | else |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2346 | Diag(CI.getLoc(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2347 | } else { |
| 2348 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2349 | Diag(CI.getLoc(), diag::note_previous_attribute); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2352 | Attrs.erase(Attrs.begin() + i); |
| 2353 | --e; |
| 2354 | continue; |
| 2355 | } |
| 2356 | |
| 2357 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2358 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2359 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2360 | |
| 2361 | if (MergedIntroduced2.empty()) |
| 2362 | MergedIntroduced2 = OldIntroduced; |
| 2363 | if (MergedDeprecated2.empty()) |
| 2364 | MergedDeprecated2 = OldDeprecated; |
| 2365 | if (MergedObsoleted2.empty()) |
| 2366 | MergedObsoleted2 = OldObsoleted; |
| 2367 | |
| 2368 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2369 | MergedIntroduced2, MergedDeprecated2, |
| 2370 | MergedObsoleted2)) { |
| 2371 | Attrs.erase(Attrs.begin() + i); |
| 2372 | --e; |
| 2373 | continue; |
| 2374 | } |
| 2375 | |
| 2376 | MergedIntroduced = MergedIntroduced2; |
| 2377 | MergedDeprecated = MergedDeprecated2; |
| 2378 | MergedObsoleted = MergedObsoleted2; |
| 2379 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2380 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | if (FoundAny && |
| 2384 | MergedIntroduced == Introduced && |
| 2385 | MergedDeprecated == Deprecated && |
| 2386 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2387 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2389 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2390 | // the checking. |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2391 | if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2392 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2393 | !OverrideOrImpl) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2394 | auto *Avail = ::new (Context) AvailabilityAttr( |
| 2395 | Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable, |
| 2396 | Message, IsStrict, Replacement, Priority); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2397 | Avail->setImplicit(Implicit); |
| 2398 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2399 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2400 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2403 | static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2404 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2405 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2406 | IdentifierLoc *Platform = AL.getArgAsIdent(0); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2407 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2408 | IdentifierInfo *II = Platform->Ident; |
| 2409 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2410 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2411 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2412 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2413 | auto *ND = dyn_cast<NamedDecl>(D); |
Alex Lorenz | 472cc79 | 2017-04-20 09:35:02 +0000 | [diff] [blame] | 2414 | if (!ND) // We warned about this already, so just return. |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2415 | return; |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2416 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2417 | AvailabilityChange Introduced = AL.getAvailabilityIntroduced(); |
| 2418 | AvailabilityChange Deprecated = AL.getAvailabilityDeprecated(); |
| 2419 | AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted(); |
| 2420 | bool IsUnavailable = AL.getUnavailableLoc().isValid(); |
| 2421 | bool IsStrict = AL.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2422 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2423 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2424 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2425 | StringRef Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2426 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr())) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2427 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2428 | |
Michael Wu | 260e962 | 2018-11-12 02:44:33 +0000 | [diff] [blame] | 2429 | if (II->isStr("swift")) { |
| 2430 | if (Introduced.isValid() || Obsoleted.isValid() || |
| 2431 | (!IsUnavailable && !Deprecated.isValid())) { |
| 2432 | S.Diag(AL.getLoc(), |
| 2433 | diag::warn_availability_swift_unavailable_deprecated_only); |
| 2434 | return; |
| 2435 | } |
| 2436 | } |
| 2437 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2438 | int PriorityModifier = AL.isPragmaClangAttribute() |
| 2439 | ? Sema::AP_PragmaClangAttribute |
| 2440 | : Sema::AP_Explicit; |
| 2441 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2442 | ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version, |
| 2443 | Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement, |
| 2444 | Sema::AMK_None, PriorityModifier); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2445 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2446 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2447 | |
| 2448 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2449 | // matches before the start of the watchOS platform. |
| 2450 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2451 | IdentifierInfo *NewII = nullptr; |
| 2452 | if (II->getName() == "ios") |
| 2453 | NewII = &S.Context.Idents.get("watchos"); |
| 2454 | else if (II->getName() == "ios_app_extension") |
| 2455 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2456 | |
| 2457 | if (NewII) { |
| 2458 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2459 | if (Version.empty()) |
| 2460 | return Version; |
| 2461 | auto Major = Version.getMajor(); |
| 2462 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2463 | if (NewMajor >= 2) { |
| 2464 | if (Version.getMinor().hasValue()) { |
| 2465 | if (Version.getSubminor().hasValue()) |
| 2466 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2467 | Version.getSubminor().getValue()); |
| 2468 | else |
| 2469 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2470 | } |
Alex Lorenz | 0b43648 | 2019-03-20 20:02:00 +0000 | [diff] [blame] | 2471 | return VersionTuple(NewMajor); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | return VersionTuple(2, 0); |
| 2475 | }; |
| 2476 | |
| 2477 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2478 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2479 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2480 | |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2481 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2482 | ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated, |
| 2483 | NewObsoleted, IsUnavailable, Str, IsStrict, Replacement, |
| 2484 | Sema::AMK_None, |
| 2485 | PriorityModifier + Sema::AP_InferredFromOtherPlatform); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2486 | if (NewAttr) |
| 2487 | D->addAttr(NewAttr); |
| 2488 | } |
| 2489 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2490 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2491 | // matches before the start of the tvOS platform. |
| 2492 | IdentifierInfo *NewII = nullptr; |
| 2493 | if (II->getName() == "ios") |
| 2494 | NewII = &S.Context.Idents.get("tvos"); |
| 2495 | else if (II->getName() == "ios_app_extension") |
| 2496 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2497 | |
| 2498 | if (NewII) { |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2499 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2500 | ND, AL, NewII, true /*Implicit*/, Introduced.Version, |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2501 | Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict, |
| 2502 | Replacement, Sema::AMK_None, |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2503 | PriorityModifier + Sema::AP_InferredFromOtherPlatform); |
Alex Lorenz | 3cfe9d5 | 2019-01-24 19:14:39 +0000 | [diff] [blame] | 2504 | if (NewAttr) |
| 2505 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2506 | } |
| 2507 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2510 | static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2511 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2512 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2513 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2514 | assert(checkAttributeAtMostNumArgs(S, AL, 3) && |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2515 | "Invalid number of arguments in an external_source_symbol attribute"); |
| 2516 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2517 | StringRef Language; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2518 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2519 | Language = SE->getString(); |
| 2520 | StringRef DefinedIn; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2521 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2522 | DefinedIn = SE->getString(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2523 | bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2524 | |
| 2525 | D->addAttr(::new (S.Context) ExternalSourceSymbolAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2526 | S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration)); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2529 | template <class T> |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2530 | static T *mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI, |
| 2531 | typename T::VisibilityType value) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2532 | T *existingAttr = D->getAttr<T>(); |
| 2533 | if (existingAttr) { |
| 2534 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2535 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2536 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2537 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2538 | S.Diag(CI.getLoc(), diag::note_previous_attribute); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2539 | D->dropAttr<T>(); |
| 2540 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2541 | return ::new (S.Context) T(S.Context, CI, value); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2544 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, |
| 2545 | const AttributeCommonInfo &CI, |
| 2546 | VisibilityAttr::VisibilityType Vis) { |
| 2547 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2550 | TypeVisibilityAttr * |
| 2551 | Sema::mergeTypeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI, |
| 2552 | TypeVisibilityAttr::VisibilityType Vis) { |
| 2553 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2556 | static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2557 | bool isTypeVisibility) { |
| 2558 | // Visibility attributes don't mean anything on a typedef. |
| 2559 | if (isa<TypedefNameDecl>(D)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2560 | S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2561 | return; |
| 2562 | } |
| 2563 | |
| 2564 | // 'type_visibility' can only go on a type or namespace. |
| 2565 | if (isTypeVisibility && |
| 2566 | !(isa<TagDecl>(D) || |
| 2567 | isa<ObjCInterfaceDecl>(D) || |
| 2568 | isa<NamespaceDecl>(D))) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2569 | S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2570 | << AL << ExpectedTypeOrNamespace; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2571 | return; |
| 2572 | } |
| 2573 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2574 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2575 | StringRef TypeStr; |
| 2576 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2577 | if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2578 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2579 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2580 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2581 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2582 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL |
| 2583 | << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2584 | return; |
| 2585 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2586 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2587 | // Complain about attempts to use protected visibility on targets |
| 2588 | // (like Darwin) that don't support it. |
| 2589 | if (type == VisibilityAttr::Protected && |
| 2590 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2591 | S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2592 | type = VisibilityAttr::Default; |
| 2593 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2594 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2595 | Attr *newAttr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2596 | if (isTypeVisibility) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2597 | newAttr = S.mergeTypeVisibilityAttr( |
| 2598 | D, AL, (TypeVisibilityAttr::VisibilityType)type); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2599 | } else { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2600 | newAttr = S.mergeVisibilityAttr(D, AL, type); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2601 | } |
| 2602 | if (newAttr) |
| 2603 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame] | 2606 | static void handleObjCDirectAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2607 | // objc_direct cannot be set on methods declared in the context of a protocol |
| 2608 | if (isa<ObjCProtocolDecl>(D->getDeclContext())) { |
| 2609 | S.Diag(AL.getLoc(), diag::err_objc_direct_on_protocol) << false; |
| 2610 | return; |
| 2611 | } |
| 2612 | |
| 2613 | if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) { |
| 2614 | handleSimpleAttribute<ObjCDirectAttr>(S, D, AL); |
| 2615 | } else { |
| 2616 | S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL; |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | static void handleObjCDirectMembersAttr(Sema &S, Decl *D, |
| 2621 | const ParsedAttr &AL) { |
| 2622 | if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) { |
| 2623 | handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL); |
| 2624 | } else { |
| 2625 | S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL; |
| 2626 | } |
| 2627 | } |
| 2628 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2629 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2630 | const auto *M = cast<ObjCMethodDecl>(D); |
| 2631 | if (!AL.isArgIdent(0)) { |
| 2632 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2633 | << AL << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2634 | return; |
| 2635 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2636 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2637 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2638 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2639 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2640 | 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] | 2641 | return; |
| 2642 | } |
| 2643 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2644 | if (F == ObjCMethodFamilyAttr::OMF_init && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2645 | !M->getReturnType()->isObjCObjectPointerType()) { |
| 2646 | S.Diag(M->getLocation(), diag::err_init_method_bad_return_type) |
| 2647 | << M->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2648 | // Ignore the attribute. |
| 2649 | return; |
| 2650 | } |
| 2651 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2652 | D->addAttr(new (S.Context) ObjCMethodFamilyAttr(S.Context, AL, F)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2655 | static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2656 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2657 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2658 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2659 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2660 | return; |
| 2661 | } |
| 2662 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2663 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2664 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2665 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2666 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2667 | return; |
| 2668 | } |
| 2669 | } |
| 2670 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2671 | // It is okay to include this attribute on properties, e.g.: |
| 2672 | // |
| 2673 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2674 | // |
| 2675 | // 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] | 2676 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2677 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2678 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2679 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(S.Context, AL)); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2682 | static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2683 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2684 | QualType T = TD->getUnderlyingType(); |
| 2685 | if (!T->isObjCObjectPointerType()) { |
| 2686 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2687 | return; |
| 2688 | } |
| 2689 | } else { |
| 2690 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2691 | return; |
| 2692 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2693 | D->addAttr(::new (S.Context) ObjCIndependentClassAttr(S.Context, AL)); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2696 | static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2697 | if (!AL.isArgIdent(0)) { |
| 2698 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2699 | << AL << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2700 | return; |
| 2701 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2702 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2703 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2704 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2705 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2706 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2707 | return; |
| 2708 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2709 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2710 | D->addAttr(::new (S.Context) BlocksAttr(S.Context, AL, type)); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2713 | static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2714 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2715 | if (AL.getNumArgs() > 0) { |
| 2716 | Expr *E = AL.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2717 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2718 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2719 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2720 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2721 | << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2722 | return; |
| 2723 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2724 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2725 | if (Idx.isSigned() && Idx.isNegative()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2726 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2727 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2728 | return; |
| 2729 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2730 | |
| 2731 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2734 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2735 | if (AL.getNumArgs() > 1) { |
| 2736 | Expr *E = AL.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2737 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2738 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2739 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2740 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2741 | << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2742 | return; |
| 2743 | } |
| 2744 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2745 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2746 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2747 | // FIXME: This error message could be improved, it would be nice |
| 2748 | // to say what the bounds actually are. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2749 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2750 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2751 | return; |
| 2752 | } |
| 2753 | } |
| 2754 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2755 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2756 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2757 | if (isa<FunctionNoProtoType>(FT)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2758 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2761 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2762 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2763 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2764 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2765 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2766 | } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2767 | if (!MD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2768 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2769 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2770 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2771 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2772 | if (!BD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2773 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2774 | return; |
| 2775 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2776 | } else if (const auto *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2777 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2778 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2779 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2780 | ? D->getFunctionType() |
Simon Pilgrim | 237d0af | 2019-10-04 15:02:46 +0000 | [diff] [blame] | 2781 | : Ty->castAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2782 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2783 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2784 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2785 | return; |
| 2786 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2787 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2788 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2789 | << AL << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2790 | return; |
| 2791 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2792 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2793 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2794 | << AL << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2795 | return; |
| 2796 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2797 | D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos)); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2800 | static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2801 | if (D->getFunctionType() && |
Erich Keane | 46441fd | 2019-07-25 15:10:56 +0000 | [diff] [blame] | 2802 | D->getFunctionType()->getReturnType()->isVoidType() && |
| 2803 | !isa<CXXConstructorDecl>(D)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2804 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2805 | return; |
| 2806 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2807 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2808 | if (MD->getReturnType()->isVoidType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2809 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1; |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2810 | return; |
| 2811 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2812 | |
Aaron Ballman | 3bef014 | 2019-07-20 07:56:34 +0000 | [diff] [blame] | 2813 | StringRef Str; |
| 2814 | if ((AL.isCXX11Attribute() || AL.isC2xAttribute()) && !AL.getScopeName()) { |
| 2815 | // If this is spelled as the standard C++17 attribute, but not in C++17, |
| 2816 | // warn about using it as an extension. If there are attribute arguments, |
| 2817 | // then claim it's a C++2a extension instead. |
| 2818 | // FIXME: If WG14 does not seem likely to adopt the same feature, add an |
| 2819 | // extension warning for C2x mode. |
| 2820 | const LangOptions &LO = S.getLangOpts(); |
| 2821 | if (AL.getNumArgs() == 1) { |
| 2822 | if (LO.CPlusPlus && !LO.CPlusPlus2a) |
| 2823 | S.Diag(AL.getLoc(), diag::ext_cxx2a_attr) << AL; |
| 2824 | |
| 2825 | // Since this this is spelled [[nodiscard]], get the optional string |
| 2826 | // literal. If in C++ mode, but not in C++2a mode, diagnose as an |
| 2827 | // extension. |
| 2828 | // FIXME: C2x should support this feature as well, even as an extension. |
| 2829 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr)) |
| 2830 | return; |
| 2831 | } else if (LO.CPlusPlus && !LO.CPlusPlus17) |
| 2832 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
| 2833 | } |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2834 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2835 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str)); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2838 | static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2839 | // weak_import only applies to variable & function declarations. |
| 2840 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2841 | if (!D->canBeWeakImported(isDef)) { |
| 2842 | if (isDef) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2843 | S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2844 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2845 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2846 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2847 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2848 | // Nothing to warn about here. |
| 2849 | } else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2850 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2851 | << AL << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2852 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2853 | return; |
| 2854 | } |
| 2855 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2856 | D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL)); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2859 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2860 | template <typename WorkGroupAttr> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2861 | static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2862 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2863 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2864 | const Expr *E = AL.getArgAsExpr(i); |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 2865 | if (!checkUInt32Argument(S, AL, E, WGSize[i], i, |
| 2866 | /*StrictlyUnsigned=*/true)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2867 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2868 | if (WGSize[i] == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2869 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2870 | << AL << E->getSourceRange(); |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2871 | return; |
| 2872 | } |
| 2873 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2874 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2875 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2876 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2877 | Existing->getYDim() == WGSize[1] && |
| 2878 | Existing->getZDim() == WGSize[2])) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2879 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2880 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2881 | D->addAttr(::new (S.Context) |
| 2882 | WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2])); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2885 | // Handles intel_reqd_sub_group_size. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2886 | static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2887 | uint32_t SGSize; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2888 | const Expr *E = AL.getArgAsExpr(0); |
| 2889 | if (!checkUInt32Argument(S, AL, E, SGSize)) |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2890 | return; |
| 2891 | if (SGSize == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2892 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2893 | << AL << E->getSourceRange(); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2894 | return; |
| 2895 | } |
| 2896 | |
| 2897 | OpenCLIntelReqdSubGroupSizeAttr *Existing = |
| 2898 | D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>(); |
| 2899 | if (Existing && Existing->getSubGroupSize() != SGSize) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2900 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2901 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2902 | D->addAttr(::new (S.Context) |
| 2903 | OpenCLIntelReqdSubGroupSizeAttr(S.Context, AL, SGSize)); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2906 | static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2907 | if (!AL.hasParsedType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2908 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2909 | return; |
| 2910 | } |
| 2911 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2912 | TypeSourceInfo *ParmTSI = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2913 | QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2914 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2915 | |
| 2916 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2917 | (ParmType->isBooleanType() || |
| 2918 | !ParmType->isIntegralType(S.getASTContext()))) { |
Gabor Horvath | 247a603 | 2020-01-02 11:57:42 -0800 | [diff] [blame] | 2919 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 2 << AL; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2920 | return; |
| 2921 | } |
| 2922 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2923 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2924 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2925 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2926 | return; |
| 2927 | } |
| 2928 | } |
| 2929 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2930 | D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI)); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2933 | SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI, |
| 2934 | StringRef Name) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2935 | // Explicit or partial specializations do not inherit |
| 2936 | // the section attribute from the primary template. |
| 2937 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2938 | if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate && |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2939 | FD->isFunctionTemplateSpecialization()) |
| 2940 | return nullptr; |
| 2941 | } |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2942 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2943 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2944 | return nullptr; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2945 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 2946 | << 1 /*section*/; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2947 | Diag(CI.getLoc(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2948 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2949 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2950 | return ::new (Context) SectionAttr(Context, CI, Name); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2953 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2954 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2955 | if (!Error.empty()) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2956 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2957 | << 1 /*'section'*/; |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2958 | return false; |
| 2959 | } |
| 2960 | return true; |
| 2961 | } |
| 2962 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2963 | static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2964 | // Make sure that there is a string literal as the sections's single |
| 2965 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2966 | StringRef Str; |
| 2967 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2968 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2969 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2971 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2972 | return; |
| 2973 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2974 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2975 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2976 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2977 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2978 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2979 | return; |
| 2980 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 2982 | SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2983 | if (NewAttr) |
| 2984 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame] | 2987 | // This is used for `__declspec(code_seg("segname"))` on a decl. |
| 2988 | // `#pragma code_seg("segname")` uses checkSectionName() instead. |
| 2989 | static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc, |
| 2990 | StringRef CodeSegName) { |
| 2991 | std::string Error = |
| 2992 | S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName); |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2993 | if (!Error.empty()) { |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame] | 2994 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
| 2995 | << Error << 0 /*'code-seg'*/; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2996 | return false; |
| 2997 | } |
Nico Weber | 9801621 | 2019-07-09 00:02:23 +0000 | [diff] [blame] | 2998 | |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2999 | return true; |
| 3000 | } |
| 3001 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3002 | CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI, |
| 3003 | StringRef Name) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 3004 | // Explicit or partial specializations do not inherit |
| 3005 | // the code_seg attribute from the primary template. |
| 3006 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 3007 | if (FD->isFunctionTemplateSpecialization()) |
| 3008 | return nullptr; |
| 3009 | } |
| 3010 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 3011 | if (ExistingAttr->getName() == Name) |
| 3012 | return nullptr; |
| 3013 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 3014 | << 0 /*codeseg*/; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3015 | Diag(CI.getLoc(), diag::note_previous_attribute); |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 3016 | return nullptr; |
| 3017 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3018 | return ::new (Context) CodeSegAttr(Context, CI, Name); |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3022 | StringRef Str; |
| 3023 | SourceLocation LiteralLoc; |
| 3024 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
| 3025 | return; |
| 3026 | if (!checkCodeSegName(S, LiteralLoc, Str)) |
| 3027 | return; |
| 3028 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 3029 | if (!ExistingAttr->isImplicit()) { |
| 3030 | S.Diag(AL.getLoc(), |
| 3031 | ExistingAttr->getName() == Str |
| 3032 | ? diag::warn_duplicate_codeseg_attribute |
| 3033 | : diag::err_conflicting_codeseg_attribute); |
| 3034 | return; |
| 3035 | } |
| 3036 | D->dropAttr<CodeSegAttr>(); |
| 3037 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3038 | if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str)) |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 3039 | D->addAttr(CSA); |
| 3040 | } |
| 3041 | |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3042 | // Check for things we'd like to warn about. Multiversioning issues are |
| 3043 | // handled later in the process, once we know how many exist. |
| 3044 | bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 3045 | enum FirstParam { Unsupported, Duplicate }; |
| 3046 | enum SecondParam { None, Architecture }; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3047 | for (auto Str : {"tune=", "fpmath="}) |
| 3048 | if (AttrStr.find(Str) != StringRef::npos) |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3049 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3050 | << Unsupported << None << Str; |
| 3051 | |
Craig Topper | 505aa24 | 2019-12-09 10:34:24 -0800 | [diff] [blame] | 3052 | ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr); |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3053 | |
| 3054 | if (!ParsedAttrs.Architecture.empty() && |
| 3055 | !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture)) |
| 3056 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3057 | << Unsupported << Architecture << ParsedAttrs.Architecture; |
| 3058 | |
| 3059 | if (ParsedAttrs.DuplicateArchitecture) |
| 3060 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3061 | << Duplicate << None << "arch="; |
| 3062 | |
| 3063 | for (const auto &Feature : ParsedAttrs.Features) { |
| 3064 | auto CurFeature = StringRef(Feature).drop_front(); // remove + or -. |
| 3065 | if (!Context.getTargetInfo().isValidFeatureName(CurFeature)) |
| 3066 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3067 | << Unsupported << None << CurFeature; |
| 3068 | } |
| 3069 | |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 3070 | TargetInfo::BranchProtectionInfo BPI; |
| 3071 | StringRef Error; |
| 3072 | if (!ParsedAttrs.BranchProtection.empty() && |
| 3073 | !Context.getTargetInfo().validateBranchProtection( |
| 3074 | ParsedAttrs.BranchProtection, BPI, Error)) { |
| 3075 | if (Error.empty()) |
| 3076 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3077 | << Unsupported << None << "branch-protection"; |
| 3078 | else |
| 3079 | return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec) |
| 3080 | << Error; |
| 3081 | } |
| 3082 | |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3083 | return false; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3086 | static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3087 | StringRef Str; |
| 3088 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3089 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) || |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3090 | S.checkTargetAttr(LiteralLoc, Str)) |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3091 | return; |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3092 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3093 | TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3094 | D->addAttr(NewAttr); |
| 3095 | } |
| 3096 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3097 | static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3098 | Expr *E = AL.getArgAsExpr(0); |
| 3099 | uint32_t VecWidth; |
| 3100 | if (!checkUInt32Argument(S, AL, E, VecWidth)) { |
| 3101 | AL.setInvalid(); |
| 3102 | return; |
| 3103 | } |
| 3104 | |
| 3105 | MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>(); |
| 3106 | if (Existing && Existing->getVectorWidth() != VecWidth) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3107 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3108 | return; |
| 3109 | } |
| 3110 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3111 | D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth)); |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3114 | static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3115 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3116 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3117 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3118 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3119 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3120 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 3121 | // will warn the user. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3122 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3123 | if (DRE->hasQualifier()) |
| 3124 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3125 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3126 | NI = DRE->getNameInfo(); |
| 3127 | if (!FD) { |
| 3128 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 3129 | << NI.getName(); |
| 3130 | return; |
| 3131 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3132 | } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3133 | if (ULE->hasExplicitTemplateArgs()) |
| 3134 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3135 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 3136 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 3137 | if (!FD) { |
| 3138 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 3139 | << NI.getName(); |
| 3140 | if (ULE->getType() == S.Context.OverloadTy) |
| 3141 | S.NoteAllOverloadCandidates(ULE); |
| 3142 | return; |
| 3143 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3144 | } else { |
| 3145 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3146 | return; |
| 3147 | } |
| 3148 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3149 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3150 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3151 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3152 | return; |
| 3153 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3154 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3155 | // We're currently more strict than GCC about what function types we accept. |
| 3156 | // 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] | 3157 | QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType()); |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3158 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3159 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3160 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3161 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 3162 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3163 | return; |
| 3164 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3165 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3166 | D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD)); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3169 | static void handleEnumExtensibilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3170 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3171 | if (!AL.isArgIdent(0)) { |
| 3172 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3173 | << AL << 0 << AANT_ArgumentIdentifier; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3174 | return; |
| 3175 | } |
| 3176 | |
| 3177 | EnumExtensibilityAttr::Kind ExtensibilityKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3178 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3179 | if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(), |
| 3180 | ExtensibilityKind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3181 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3182 | return; |
| 3183 | } |
| 3184 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3185 | D->addAttr(::new (S.Context) |
| 3186 | EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind)); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3189 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3190 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3191 | static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3192 | Expr *IdxExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3193 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3194 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3195 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3196 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3197 | // Make sure the format string is really a string. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3198 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3199 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3200 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 3201 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3202 | !isCFStringType(Ty, S.Context) && |
| 3203 | (!Ty->isPointerType() || |
Simon Pilgrim | 237d0af | 2019-10-04 15:02:46 +0000 | [diff] [blame] | 3204 | !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3205 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3206 | << "a string type" << IdxExpr->getSourceRange() |
| 3207 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3208 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3209 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3210 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3211 | if (!isNSStringType(Ty, S.Context) && |
| 3212 | !isCFStringType(Ty, S.Context) && |
| 3213 | (!Ty->isPointerType() || |
Simon Pilgrim | 237d0af | 2019-10-04 15:02:46 +0000 | [diff] [blame] | 3214 | !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3215 | S.Diag(AL.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3216 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 3217 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3218 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3221 | D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx)); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3224 | enum FormatAttrKind { |
| 3225 | CFStringFormat, |
| 3226 | NSStringFormat, |
| 3227 | StrftimeFormat, |
| 3228 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3229 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3230 | InvalidFormat |
| 3231 | }; |
| 3232 | |
| 3233 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3234 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3235 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3236 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3237 | // Check for formats that get handled specially. |
| 3238 | .Case("NSString", NSStringFormat) |
| 3239 | .Case("CFString", CFStringFormat) |
| 3240 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3241 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3242 | // Otherwise, check for supported formats. |
| 3243 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3244 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3245 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 3246 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 3247 | .Case("os_trace", SupportedFormat) |
| 3248 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3249 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3250 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3251 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3254 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3255 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3256 | static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3257 | if (!S.getLangOpts().CPlusPlus) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3258 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3259 | return; |
| 3260 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3261 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3262 | if (S.getCurFunctionOrMethodDecl()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3263 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3264 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3265 | return; |
| 3266 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3267 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3268 | if (S.Context.getAsArrayType(T)) |
| 3269 | T = S.Context.getBaseElementType(T); |
| 3270 | if (!T->getAs<RecordType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3271 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3272 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3273 | return; |
| 3274 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3275 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3276 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3277 | uint32_t prioritynum; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3278 | if (!checkUInt32Argument(S, AL, E, prioritynum)) { |
| 3279 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3280 | return; |
| 3281 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3282 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3283 | if (prioritynum < 101 || prioritynum > 65535) { |
Aaron Ballman | 52c9ad2 | 2019-02-12 13:04:11 +0000 | [diff] [blame] | 3284 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3285 | << E->getSourceRange() << AL << 101 << 65535; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3286 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3287 | return; |
| 3288 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3289 | D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum)); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3292 | FormatAttr *Sema::mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI, |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3293 | IdentifierInfo *Format, int FormatIdx, |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3294 | int FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3295 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3296 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3297 | if (F->getType() == Format && |
| 3298 | F->getFormatIdx() == FormatIdx && |
| 3299 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3300 | // If we don't have a valid location for this attribute, adopt the |
| 3301 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3302 | if (F->getLocation().isInvalid()) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3303 | F->setRange(CI.getRange()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3304 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3305 | } |
| 3306 | } |
| 3307 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3308 | return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3311 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3312 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3313 | static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3314 | if (!AL.isArgIdent(0)) { |
| 3315 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3316 | << AL << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3317 | return; |
| 3318 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3319 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3320 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3321 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3322 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3323 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3324 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3325 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3326 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3327 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3328 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3329 | // If we've modified the string name, we need a new identifier for it. |
| 3330 | II = &S.Context.Idents.get(Format); |
| 3331 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3332 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3333 | // Check for supported formats. |
| 3334 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3335 | |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3336 | if (Kind == IgnoredFormat) |
| 3337 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3338 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3339 | if (Kind == InvalidFormat) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3340 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3341 | << AL << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3342 | return; |
| 3343 | } |
| 3344 | |
| 3345 | // checks for the 2nd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3346 | Expr *IdxExpr = AL.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3347 | uint32_t Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3348 | if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3349 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3350 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3351 | if (Idx < 1 || Idx > NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3352 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3353 | << AL << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3354 | return; |
| 3355 | } |
| 3356 | |
| 3357 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3358 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3359 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3360 | if (HasImplicitThisParam) { |
| 3361 | if (ArgIdx == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3362 | S.Diag(AL.getLoc(), |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3363 | diag::err_format_attribute_implicit_this_format_string) |
| 3364 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3365 | return; |
| 3366 | } |
| 3367 | ArgIdx--; |
| 3368 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3370 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3371 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3372 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3373 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3374 | if (!isCFStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3375 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3376 | << "a CFString" << IdxExpr->getSourceRange() |
| 3377 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3378 | return; |
| 3379 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3380 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3381 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3382 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3383 | if (!isNSStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3384 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3385 | << "an NSString" << IdxExpr->getSourceRange() |
| 3386 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3387 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3388 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3389 | } else if (!Ty->isPointerType() || |
Simon Pilgrim | 237d0af | 2019-10-04 15:02:46 +0000 | [diff] [blame] | 3390 | !Ty->castAs<PointerType>()->getPointeeType()->isCharType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3391 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3392 | << "a string type" << IdxExpr->getSourceRange() |
| 3393 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3394 | return; |
| 3395 | } |
| 3396 | |
| 3397 | // check the 3rd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3398 | Expr *FirstArgExpr = AL.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3399 | uint32_t FirstArg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3400 | if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3401 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3402 | |
| 3403 | // check if the function is variadic if the 3rd argument non-zero |
| 3404 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3405 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3406 | ++NumArgs; // +1 for ... |
| 3407 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3408 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3409 | return; |
| 3410 | } |
| 3411 | } |
| 3412 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3413 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3414 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3415 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3416 | if (FirstArg != 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3417 | S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3418 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3419 | return; |
| 3420 | } |
| 3421 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3422 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3423 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3424 | << AL << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3425 | return; |
| 3426 | } |
| 3427 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3428 | FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3429 | if (NewAttr) |
| 3430 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 3433 | /// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes. |
| 3434 | static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3435 | // The index that identifies the callback callee is mandatory. |
| 3436 | if (AL.getNumArgs() == 0) { |
| 3437 | S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee) |
| 3438 | << AL.getRange(); |
| 3439 | return; |
| 3440 | } |
| 3441 | |
| 3442 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3443 | int32_t NumArgs = getFunctionOrMethodNumParams(D); |
| 3444 | |
| 3445 | FunctionDecl *FD = D->getAsFunction(); |
| 3446 | assert(FD && "Expected a function declaration!"); |
| 3447 | |
| 3448 | llvm::StringMap<int> NameIdxMapping; |
| 3449 | NameIdxMapping["__"] = -1; |
| 3450 | |
| 3451 | NameIdxMapping["this"] = 0; |
| 3452 | |
| 3453 | int Idx = 1; |
| 3454 | for (const ParmVarDecl *PVD : FD->parameters()) |
| 3455 | NameIdxMapping[PVD->getName()] = Idx++; |
| 3456 | |
| 3457 | auto UnknownName = NameIdxMapping.end(); |
| 3458 | |
| 3459 | SmallVector<int, 8> EncodingIndices; |
| 3460 | for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) { |
| 3461 | SourceRange SR; |
| 3462 | int32_t ArgIdx; |
| 3463 | |
| 3464 | if (AL.isArgIdent(I)) { |
| 3465 | IdentifierLoc *IdLoc = AL.getArgAsIdent(I); |
| 3466 | auto It = NameIdxMapping.find(IdLoc->Ident->getName()); |
| 3467 | if (It == UnknownName) { |
| 3468 | S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown) |
| 3469 | << IdLoc->Ident << IdLoc->Loc; |
| 3470 | return; |
| 3471 | } |
| 3472 | |
| 3473 | SR = SourceRange(IdLoc->Loc); |
| 3474 | ArgIdx = It->second; |
| 3475 | } else if (AL.isArgExpr(I)) { |
| 3476 | Expr *IdxExpr = AL.getArgAsExpr(I); |
| 3477 | |
| 3478 | // If the expression is not parseable as an int32_t we have a problem. |
| 3479 | if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1, |
| 3480 | false)) { |
| 3481 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3482 | << AL << (I + 1) << IdxExpr->getSourceRange(); |
| 3483 | return; |
| 3484 | } |
| 3485 | |
| 3486 | // Check oob, excluding the special values, 0 and -1. |
| 3487 | if (ArgIdx < -1 || ArgIdx > NumArgs) { |
| 3488 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3489 | << AL << (I + 1) << IdxExpr->getSourceRange(); |
| 3490 | return; |
| 3491 | } |
| 3492 | |
| 3493 | SR = IdxExpr->getSourceRange(); |
| 3494 | } else { |
| 3495 | llvm_unreachable("Unexpected ParsedAttr argument type!"); |
| 3496 | } |
| 3497 | |
| 3498 | if (ArgIdx == 0 && !HasImplicitThisParam) { |
| 3499 | S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available) |
| 3500 | << (I + 1) << SR; |
| 3501 | return; |
| 3502 | } |
| 3503 | |
| 3504 | // Adjust for the case we do not have an implicit "this" parameter. In this |
| 3505 | // case we decrease all positive values by 1 to get LLVM argument indices. |
| 3506 | if (!HasImplicitThisParam && ArgIdx > 0) |
| 3507 | ArgIdx -= 1; |
| 3508 | |
| 3509 | EncodingIndices.push_back(ArgIdx); |
| 3510 | } |
| 3511 | |
| 3512 | int CalleeIdx = EncodingIndices.front(); |
| 3513 | // 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] | 3514 | // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam" |
| 3515 | // is false and positive if "HasImplicitThisParam" is true. |
| 3516 | if (CalleeIdx < (int)HasImplicitThisParam) { |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 3517 | S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee) |
| 3518 | << AL.getRange(); |
| 3519 | return; |
| 3520 | } |
| 3521 | |
| 3522 | // Get the callee type, note the index adjustment as the AST doesn't contain |
| 3523 | // the this type (which the callee cannot reference anyway!). |
| 3524 | const Type *CalleeType = |
| 3525 | getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam) |
| 3526 | .getTypePtr(); |
| 3527 | if (!CalleeType || !CalleeType->isFunctionPointerType()) { |
| 3528 | S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type) |
| 3529 | << AL.getRange(); |
| 3530 | return; |
| 3531 | } |
| 3532 | |
| 3533 | const Type *CalleeFnType = |
| 3534 | CalleeType->getPointeeType()->getUnqualifiedDesugaredType(); |
| 3535 | |
| 3536 | // TODO: Check the type of the callee arguments. |
| 3537 | |
| 3538 | const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType); |
| 3539 | if (!CalleeFnProtoType) { |
| 3540 | S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type) |
| 3541 | << AL.getRange(); |
| 3542 | return; |
| 3543 | } |
| 3544 | |
| 3545 | if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) { |
| 3546 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3547 | << AL << (unsigned)(EncodingIndices.size() - 1); |
| 3548 | return; |
| 3549 | } |
| 3550 | |
| 3551 | if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) { |
| 3552 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3553 | << AL << (unsigned)(EncodingIndices.size() - 1); |
| 3554 | return; |
| 3555 | } |
| 3556 | |
| 3557 | if (CalleeFnProtoType->isVariadic()) { |
| 3558 | S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange(); |
| 3559 | return; |
| 3560 | } |
| 3561 | |
| 3562 | // Do not allow multiple callback attributes. |
| 3563 | if (D->hasAttr<CallbackAttr>()) { |
| 3564 | S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange(); |
| 3565 | return; |
| 3566 | } |
| 3567 | |
| 3568 | D->addAttr(::new (S.Context) CallbackAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3569 | S.Context, AL, EncodingIndices.data(), EncodingIndices.size())); |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3572 | static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3573 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3574 | RecordDecl *RD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3575 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3576 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3577 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3578 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3579 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3580 | |
| 3581 | if (!RD || !RD->isUnion()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3582 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL |
| 3583 | << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3584 | return; |
| 3585 | } |
| 3586 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3587 | if (!RD->isCompleteDefinition()) { |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3588 | if (!RD->isBeingDefined()) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3589 | S.Diag(AL.getLoc(), |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3590 | diag::warn_transparent_union_attribute_not_definition); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3591 | return; |
| 3592 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3593 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3594 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3595 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3596 | if (Field == FieldEnd) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3597 | S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3598 | return; |
| 3599 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3600 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3601 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3602 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3603 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3604 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3605 | diag::warn_transparent_union_attribute_floating) |
| 3606 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3607 | return; |
| 3608 | } |
| 3609 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3610 | if (FirstType->isIncompleteType()) |
| 3611 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3612 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3613 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3614 | for (; Field != FieldEnd; ++Field) { |
| 3615 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3616 | if (FieldType->isIncompleteType()) |
| 3617 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3618 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3619 | // members of the union would all have the same calling convention as the |
| 3620 | // first member of the union. Checking just the size and alignment isn't |
| 3621 | // sufficient (consider structs passed on the stack instead of in registers |
| 3622 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3623 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3624 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3625 | // Warn if we drop the attribute. |
| 3626 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3627 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3628 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3629 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3630 | diag::warn_transparent_union_attribute_field_size_align) |
| 3631 | << isSize << Field->getDeclName() << FieldBits; |
| 3632 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3633 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3634 | diag::note_transparent_union_first_field_size_align) |
| 3635 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3636 | return; |
| 3637 | } |
| 3638 | } |
| 3639 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3640 | RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3643 | static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3644 | // Make sure that there is a string literal as the annotation's single |
| 3645 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3646 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3647 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3648 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3649 | |
| 3650 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3651 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3652 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3653 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3654 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3655 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3656 | D->addAttr(::new (S.Context) AnnotateAttr(S.Context, AL, Str)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3659 | static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3660 | S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3663 | void Sema::AddAlignValueAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E) { |
| 3664 | AlignValueAttr TmpAttr(Context, CI, E); |
| 3665 | SourceLocation AttrLoc = CI.getLoc(); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3666 | |
| 3667 | QualType T; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3668 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3669 | T = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3670 | else if (const auto *VD = dyn_cast<ValueDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3671 | T = VD->getType(); |
| 3672 | else |
| 3673 | llvm_unreachable("Unknown decl type for align_value"); |
| 3674 | |
| 3675 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3676 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3677 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
Aaron Ballman | dab43c8 | 2020-03-14 17:00:45 -0400 | [diff] [blame] | 3678 | << &TmpAttr << T << D->getSourceRange(); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3679 | return; |
| 3680 | } |
| 3681 | |
| 3682 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3683 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3684 | ExprResult ICE |
| 3685 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3686 | diag::err_align_value_attribute_argument_not_int, |
| 3687 | /*AllowFold*/ false); |
| 3688 | if (ICE.isInvalid()) |
| 3689 | return; |
| 3690 | |
| 3691 | if (!Alignment.isPowerOf2()) { |
| 3692 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3693 | << E->getSourceRange(); |
| 3694 | return; |
| 3695 | } |
| 3696 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3697 | D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get())); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3698 | return; |
| 3699 | } |
| 3700 | |
| 3701 | // Save dependent expressions in the AST to be instantiated. |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3702 | D->addAttr(::new (Context) AlignValueAttr(Context, CI, E)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3705 | static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3706 | // check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3707 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3708 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3709 | return; |
| 3710 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3711 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3712 | if (AL.getNumArgs() == 0) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3713 | D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr)); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3714 | return; |
| 3715 | } |
| 3716 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3717 | Expr *E = AL.getArgAsExpr(0); |
| 3718 | if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3719 | S.Diag(AL.getEllipsisLoc(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3720 | diag::err_pack_expansion_without_parameter_packs); |
| 3721 | return; |
| 3722 | } |
| 3723 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3724 | if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3725 | return; |
| 3726 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3727 | S.AddAlignedAttr(D, AL, E, AL.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3730 | void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, |
| 3731 | bool IsPackExpansion) { |
| 3732 | AlignedAttr TmpAttr(Context, CI, true, E); |
| 3733 | SourceLocation AttrLoc = CI.getLoc(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3734 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3735 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3736 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3737 | // C++11 [dcl.align]p1: |
| 3738 | // An alignment-specifier may be applied to a variable or to a class |
| 3739 | // data member, but it shall not be applied to a bit-field, a function |
| 3740 | // parameter, the formal parameter of a catch clause, or a variable |
| 3741 | // declared with the register storage class specifier. An |
| 3742 | // alignment-specifier may also be applied to the declaration of a class |
| 3743 | // or enumeration type. |
| 3744 | // C11 6.7.5/2: |
| 3745 | // An alignment attribute shall not be specified in a declaration of |
| 3746 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3747 | // object declared with the register storage-class specifier. |
| 3748 | int DiagKind = -1; |
| 3749 | if (isa<ParmVarDecl>(D)) { |
| 3750 | DiagKind = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3751 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3752 | if (VD->getStorageClass() == SC_Register) |
| 3753 | DiagKind = 1; |
| 3754 | if (VD->isExceptionVariable()) |
| 3755 | DiagKind = 2; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3756 | } else if (const auto *FD = dyn_cast<FieldDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3757 | if (FD->isBitField()) |
| 3758 | DiagKind = 3; |
| 3759 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3760 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3761 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3762 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3763 | return; |
| 3764 | } |
| 3765 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3766 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3767 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3768 | return; |
| 3769 | } |
| 3770 | } |
| 3771 | |
Richard Smith | 90ae967 | 2018-06-20 23:36:55 +0000 | [diff] [blame] | 3772 | if (E->isValueDependent()) { |
| 3773 | // We can't support a dependent alignment on a non-dependent type, |
| 3774 | // because we have no way to model that a type is "alignment-dependent" |
| 3775 | // but not dependent in any other way. |
| 3776 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3777 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3778 | Diag(AttrLoc, diag::err_alignment_dependent_typedef_name) |
| 3779 | << E->getSourceRange(); |
| 3780 | return; |
| 3781 | } |
| 3782 | } |
| 3783 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3784 | // Save dependent expressions in the AST to be instantiated. |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3785 | AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3786 | AA->setPackExpansion(IsPackExpansion); |
| 3787 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3788 | return; |
| 3789 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3790 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3791 | // FIXME: Cache the number on the AL object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3792 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3793 | ExprResult ICE |
| 3794 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3795 | diag::err_aligned_attribute_argument_not_int, |
| 3796 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3797 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3798 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3799 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3800 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3801 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3802 | // C++11 [dcl.align]p2: |
| 3803 | // -- if the constant expression evaluates to zero, the alignment |
| 3804 | // specifier shall have no effect |
| 3805 | // C11 6.7.5p6: |
| 3806 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3807 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3808 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3809 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3810 | << E->getSourceRange(); |
| 3811 | return; |
| 3812 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3813 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3814 | |
Roman Lebedev | 1d0972f | 2020-01-24 17:01:27 +0300 | [diff] [blame] | 3815 | unsigned MaximumAlignment = Sema::MaximumAlignment; |
| 3816 | if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF()) |
| 3817 | MaximumAlignment = std::min(MaximumAlignment, 8192u); |
| 3818 | if (AlignVal > MaximumAlignment) { |
Roman Lebedev | d096f8d | 2020-01-23 22:50:06 +0300 | [diff] [blame] | 3819 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) |
Roman Lebedev | 1d0972f | 2020-01-24 17:01:27 +0300 | [diff] [blame] | 3820 | << MaximumAlignment << E->getSourceRange(); |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3821 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3822 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3823 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3824 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3825 | unsigned MaxTLSAlign = |
| 3826 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3827 | .getQuantity(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3828 | const auto *VD = dyn_cast<VarDecl>(D); |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3829 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3830 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3831 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3832 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3833 | return; |
| 3834 | } |
| 3835 | } |
| 3836 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3837 | AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get()); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3838 | AA->setPackExpansion(IsPackExpansion); |
| 3839 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3842 | void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, |
| 3843 | TypeSourceInfo *TS, bool IsPackExpansion) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3844 | // FIXME: Cache the number on the AL object if non-dependent? |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3845 | // FIXME: Perform checking of type validity |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3846 | AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3847 | AA->setPackExpansion(IsPackExpansion); |
| 3848 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3849 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3850 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3851 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3852 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3853 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3854 | QualType UnderlyingTy, DiagTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3855 | if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3856 | UnderlyingTy = DiagTy = VD->getType(); |
| 3857 | } else { |
| 3858 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3859 | if (const auto *ED = dyn_cast<EnumDecl>(D)) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3860 | UnderlyingTy = ED->getIntegerType(); |
| 3861 | } |
| 3862 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3863 | return; |
| 3864 | |
| 3865 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3866 | // The combined effect of all alignment attributes in a declaration shall |
| 3867 | // not specify an alignment that is less strict than the alignment that |
| 3868 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3869 | AlignedAttr *AlignasAttr = nullptr; |
Richard Sandiford | 627b5c1 | 2020-03-02 17:37:58 +0000 | [diff] [blame] | 3870 | AlignedAttr *LastAlignedAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3871 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3872 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3873 | if (I->isAlignmentDependent()) |
| 3874 | return; |
| 3875 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3876 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3877 | Align = std::max(Align, I->getAlignment(Context)); |
Richard Sandiford | 627b5c1 | 2020-03-02 17:37:58 +0000 | [diff] [blame] | 3878 | LastAlignedAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Richard Sandiford | 627b5c1 | 2020-03-02 17:37:58 +0000 | [diff] [blame] | 3881 | if (Align && DiagTy->isSizelessType()) { |
| 3882 | Diag(LastAlignedAttr->getLocation(), diag::err_attribute_sizeless_type) |
| 3883 | << LastAlignedAttr << DiagTy; |
| 3884 | } else if (AlignasAttr && Align) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3885 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3886 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3887 | if (NaturalAlign > RequestedAlign) |
| 3888 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3889 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3893 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3894 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 3895 | MSInheritanceModel ExplicitModel) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3896 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3897 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3898 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3899 | // have to wait until the record is defined to catch any mismatches. |
| 3900 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3901 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3902 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3903 | // The unspecified model never matches what a definition could need. |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 3904 | if (ExplicitModel == MSInheritanceModel::Unspecified) |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3905 | return false; |
| 3906 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3907 | if (BestCase) { |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 3908 | if (RD->calculateInheritanceModel() == ExplicitModel) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3909 | return false; |
| 3910 | } else { |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 3911 | if (RD->calculateInheritanceModel() <= ExplicitModel) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3912 | return false; |
| 3913 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3914 | |
| 3915 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3916 | << 0 /*definition*/; |
Aaron Ballman | dab43c8 | 2020-03-14 17:00:45 -0400 | [diff] [blame] | 3917 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) << RD; |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3918 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3921 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3922 | /// attribute. |
| 3923 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3924 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3925 | IntegerMode = true; |
| 3926 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3927 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3928 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3929 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3930 | case 'Q': |
| 3931 | DestWidth = 8; |
| 3932 | break; |
| 3933 | case 'H': |
| 3934 | DestWidth = 16; |
| 3935 | break; |
| 3936 | case 'S': |
| 3937 | DestWidth = 32; |
| 3938 | break; |
| 3939 | case 'D': |
| 3940 | DestWidth = 64; |
| 3941 | break; |
| 3942 | case 'X': |
| 3943 | DestWidth = 96; |
| 3944 | break; |
| 3945 | case 'T': |
| 3946 | DestWidth = 128; |
| 3947 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3948 | } |
| 3949 | if (Str[1] == 'F') { |
| 3950 | IntegerMode = false; |
| 3951 | } else if (Str[1] == 'C') { |
| 3952 | IntegerMode = false; |
| 3953 | ComplexMode = true; |
| 3954 | } else if (Str[1] != 'I') { |
| 3955 | DestWidth = 0; |
| 3956 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3957 | break; |
| 3958 | case 4: |
| 3959 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3960 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3961 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3962 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3963 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3964 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3965 | break; |
| 3966 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3967 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3968 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3969 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3970 | case 11: |
| 3971 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3972 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3973 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3974 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3978 | /// type. |
| 3979 | /// |
| 3980 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3981 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3982 | /// HImode, not an intermediate pointer. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3983 | static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3984 | // This attribute isn't documented, but glibc uses it. It changes |
| 3985 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3986 | if (!AL.isArgIdent(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3987 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 3988 | << AL << AANT_ArgumentIdentifier; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3989 | return; |
| 3990 | } |
| 3991 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3992 | IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3993 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3994 | S.AddModeAttr(D, AL, Name); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 3997 | void Sema::AddModeAttr(Decl *D, const AttributeCommonInfo &CI, |
| 3998 | IdentifierInfo *Name, bool InInstantiation) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3999 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4000 | normalizeName(Str); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4001 | SourceLocation AttrLoc = CI.getLoc(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4002 | |
| 4003 | unsigned DestWidth = 0; |
| 4004 | bool IntegerMode = true; |
| 4005 | bool ComplexMode = false; |
| 4006 | llvm::APInt VectorSize(64, 0); |
| 4007 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 4008 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 4009 | size_t StrSize = Str.size(); |
| 4010 | size_t VectorStringLength = 0; |
| 4011 | while ((VectorStringLength + 1) < StrSize && |
| 4012 | isdigit(Str[VectorStringLength + 1])) |
| 4013 | ++VectorStringLength; |
| 4014 | if (VectorStringLength && |
| 4015 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 4016 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4017 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4018 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4019 | // Avoid duplicate warning from template instantiation. |
| 4020 | if (!InInstantiation) |
| 4021 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4022 | } else { |
| 4023 | VectorSize = 0; |
| 4024 | } |
| 4025 | } |
| 4026 | |
| 4027 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4028 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 4029 | |
| 4030 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 4031 | // and friends, at least with glibc. |
| 4032 | // FIXME: Make sure floating-point mappings are accurate |
| 4033 | // FIXME: Support XF and TF types |
| 4034 | if (!DestWidth) { |
| 4035 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 4036 | return; |
| 4037 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4038 | |
| 4039 | QualType OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4040 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4041 | OldTy = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4042 | else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4043 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 4044 | // Try to get type from enum declaration, default to int. |
| 4045 | OldTy = ED->getIntegerType(); |
| 4046 | if (OldTy.isNull()) |
| 4047 | OldTy = Context.IntTy; |
| 4048 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 4049 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4050 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4051 | if (OldTy->isDependentType()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4052 | D->addAttr(::new (Context) ModeAttr(Context, CI, Name)); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4053 | return; |
| 4054 | } |
| 4055 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4056 | // Base type can also be a vector type (see PR17453). |
| 4057 | // Distinguish between base type and base element type. |
| 4058 | QualType OldElemTy = OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4059 | if (const auto *VT = OldTy->getAs<VectorType>()) |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4060 | OldElemTy = VT->getElementType(); |
| 4061 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4062 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 4063 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 4064 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 4065 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 4066 | VectorSize.getBoolValue()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4067 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.getRange(); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4068 | return; |
| 4069 | } |
| 4070 | bool IntegralOrAnyEnumType = |
| 4071 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 4072 | |
| 4073 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 4074 | !IntegralOrAnyEnumType) |
| 4075 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4076 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4077 | if (!IntegralOrAnyEnumType) |
| 4078 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4079 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4080 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4081 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4082 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4083 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4084 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4087 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4088 | |
| 4089 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4090 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 4091 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4092 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4093 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 4094 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4095 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4096 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4097 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 4100 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4101 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 4105 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4106 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 4107 | VectorType::GenericVector); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4108 | } else if (const auto *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4109 | // Complex machine mode does not support base vector types. |
| 4110 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4111 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4112 | return; |
| 4113 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4114 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4115 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4116 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4117 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4118 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4122 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 4123 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | // Install the new type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4127 | if (auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 4128 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4129 | else if (auto *ED = dyn_cast<EnumDecl>(D)) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 4130 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 4131 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 4132 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 4133 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4134 | D->addAttr(::new (Context) ModeAttr(Context, CI, Name)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 4135 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4136 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4137 | static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4138 | D->addAttr(::new (S.Context) NoDebugAttr(S.Context, AL)); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4141 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, |
| 4142 | const AttributeCommonInfo &CI, |
| 4143 | const IdentifierInfo *Ident) { |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4144 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4145 | Diag(CI.getLoc(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4146 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4147 | return nullptr; |
| 4148 | } |
| 4149 | |
| 4150 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 4151 | return nullptr; |
| 4152 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4153 | return ::new (Context) AlwaysInlineAttr(Context, CI); |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4156 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) { |
| 4157 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4158 | return nullptr; |
| 4159 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4160 | return ::new (Context) CommonAttr(Context, AL); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4163 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) { |
| 4164 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
| 4165 | return nullptr; |
| 4166 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4167 | return ::new (Context) CommonAttr(Context, AL); |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D, |
| 4171 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4172 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4173 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 4174 | // ImplicitParm or VarTemplateSpecialization). |
| 4175 | if (VD->getKind() != Decl::Var) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4176 | Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4177 | << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4178 | : ExpectedVariableOrFunction); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4179 | return nullptr; |
| 4180 | } |
| 4181 | // Attribute does not apply to non-static local variables. |
| 4182 | if (VD->hasLocalStorage()) { |
| 4183 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4184 | return nullptr; |
| 4185 | } |
| 4186 | } |
| 4187 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4188 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
| 4189 | return nullptr; |
| 4190 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4191 | return ::new (Context) InternalLinkageAttr(Context, AL); |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4192 | } |
| 4193 | InternalLinkageAttr * |
| 4194 | Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) { |
| 4195 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 4196 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 4197 | // ImplicitParm or VarTemplateSpecialization). |
| 4198 | if (VD->getKind() != Decl::Var) { |
| 4199 | Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type) |
| 4200 | << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4201 | : ExpectedVariableOrFunction); |
| 4202 | return nullptr; |
| 4203 | } |
| 4204 | // Attribute does not apply to non-static local variables. |
| 4205 | if (VD->hasLocalStorage()) { |
| 4206 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4207 | return nullptr; |
| 4208 | } |
| 4209 | } |
| 4210 | |
| 4211 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4212 | return nullptr; |
| 4213 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4214 | return ::new (Context) InternalLinkageAttr(Context, AL); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4217 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, const AttributeCommonInfo &CI) { |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4218 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4219 | Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'minsize'"; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4220 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4221 | return nullptr; |
| 4222 | } |
| 4223 | |
| 4224 | if (D->hasAttr<MinSizeAttr>()) |
| 4225 | return nullptr; |
| 4226 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4227 | return ::new (Context) MinSizeAttr(Context, CI); |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 4230 | NoSpeculativeLoadHardeningAttr *Sema::mergeNoSpeculativeLoadHardeningAttr( |
| 4231 | Decl *D, const NoSpeculativeLoadHardeningAttr &AL) { |
| 4232 | if (checkAttrMutualExclusion<SpeculativeLoadHardeningAttr>(*this, D, AL)) |
| 4233 | return nullptr; |
| 4234 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4235 | return ::new (Context) NoSpeculativeLoadHardeningAttr(Context, AL); |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4238 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, |
| 4239 | const AttributeCommonInfo &CI) { |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4240 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 4241 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4242 | Diag(CI.getLoc(), diag::note_conflicting_attribute); |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4243 | D->dropAttr<AlwaysInlineAttr>(); |
| 4244 | } |
| 4245 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 4246 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4247 | Diag(CI.getLoc(), diag::note_conflicting_attribute); |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4248 | D->dropAttr<MinSizeAttr>(); |
| 4249 | } |
| 4250 | |
| 4251 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 4252 | return nullptr; |
| 4253 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4254 | return ::new (Context) OptimizeNoneAttr(Context, CI); |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 4257 | SpeculativeLoadHardeningAttr *Sema::mergeSpeculativeLoadHardeningAttr( |
| 4258 | Decl *D, const SpeculativeLoadHardeningAttr &AL) { |
| 4259 | if (checkAttrMutualExclusion<NoSpeculativeLoadHardeningAttr>(*this, D, AL)) |
| 4260 | return nullptr; |
| 4261 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4262 | return ::new (Context) SpeculativeLoadHardeningAttr(Context, AL); |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4265 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4266 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL)) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 4267 | return; |
| 4268 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4269 | if (AlwaysInlineAttr *Inline = |
| 4270 | S.mergeAlwaysInlineAttr(D, AL, AL.getAttrName())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4271 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4274 | static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4275 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(D, AL)) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4276 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4279 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4280 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(D, AL)) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4281 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4284 | static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4285 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL)) |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4286 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4287 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4288 | if (!VD->hasGlobalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4289 | S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4290 | return; |
| 4291 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4292 | D->addAttr(::new (S.Context) CUDAConstantAttr(S.Context, AL)); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4295 | static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4296 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL)) |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4297 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4298 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 4299 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 4300 | // "int x[]"). |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 4301 | if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() && |
Jonas Hahnfeld | ee47d8c | 2018-02-14 16:04:03 +0000 | [diff] [blame] | 4302 | !isa<IncompleteArrayType>(VD->getType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4303 | S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4304 | return; |
| 4305 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4306 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4307 | S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared) |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4308 | << S.CurrentCUDATarget()) |
| 4309 | return; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4310 | D->addAttr(::new (S.Context) CUDASharedAttr(S.Context, AL)); |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4313 | static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4314 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) || |
| 4315 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 4316 | return; |
| 4317 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4318 | const auto *FD = cast<FunctionDecl>(D); |
Michael Liao | 24337db | 2019-09-25 16:51:45 +0000 | [diff] [blame] | 4319 | if (!FD->getReturnType()->isVoidType() && |
| 4320 | !FD->getReturnType()->getAs<AutoType>() && |
| 4321 | !FD->getReturnType()->isInstantiationDependentType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4322 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 4323 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4324 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4325 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 4326 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4327 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4328 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4329 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 4330 | if (Method->isInstance()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4331 | S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method) |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4332 | << Method; |
| 4333 | return; |
| 4334 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4335 | S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method; |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4336 | } |
| 4337 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 4338 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4339 | S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4340 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4341 | D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL)); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4344 | static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4345 | const auto *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 4346 | if (!Fn->isInlineSpecified()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4347 | S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 4348 | return; |
| 4349 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4350 | |
Martin Storsjo | 71decf8 | 2019-09-27 12:25:19 +0000 | [diff] [blame] | 4351 | if (S.LangOpts.CPlusPlus && Fn->getStorageClass() != SC_Extern) |
| 4352 | S.Diag(AL.getLoc(), diag::warn_gnu_inline_cplusplus_without_extern); |
| 4353 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4354 | D->addAttr(::new (S.Context) GNUInlineAttr(S.Context, AL)); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4357 | static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4358 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4359 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4360 | // Diagnostic is emitted elsewhere: here we store the (valid) AL |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4361 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 4362 | CallingConv CC; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4363 | if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4364 | return; |
| 4365 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4366 | if (!isa<ObjCMethodDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4367 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4368 | << AL << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4369 | return; |
| 4370 | } |
| 4371 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4372 | switch (AL.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4373 | case ParsedAttr::AT_FastCall: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4374 | D->addAttr(::new (S.Context) FastCallAttr(S.Context, AL)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4375 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4376 | case ParsedAttr::AT_StdCall: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4377 | D->addAttr(::new (S.Context) StdCallAttr(S.Context, AL)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4378 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4379 | case ParsedAttr::AT_ThisCall: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4380 | D->addAttr(::new (S.Context) ThisCallAttr(S.Context, AL)); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 4381 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4382 | case ParsedAttr::AT_CDecl: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4383 | D->addAttr(::new (S.Context) CDeclAttr(S.Context, AL)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4384 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4385 | case ParsedAttr::AT_Pascal: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4386 | D->addAttr(::new (S.Context) PascalAttr(S.Context, AL)); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4387 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4388 | case ParsedAttr::AT_SwiftCall: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4389 | D->addAttr(::new (S.Context) SwiftCallAttr(S.Context, AL)); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4390 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4391 | case ParsedAttr::AT_VectorCall: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4392 | D->addAttr(::new (S.Context) VectorCallAttr(S.Context, AL)); |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4393 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4394 | case ParsedAttr::AT_MSABI: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4395 | D->addAttr(::new (S.Context) MSABIAttr(S.Context, AL)); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4396 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4397 | case ParsedAttr::AT_SysVABI: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4398 | D->addAttr(::new (S.Context) SysVABIAttr(S.Context, AL)); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4399 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4400 | case ParsedAttr::AT_RegCall: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4401 | D->addAttr(::new (S.Context) RegCallAttr(S.Context, AL)); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4402 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4403 | case ParsedAttr::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4404 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4405 | switch (CC) { |
| 4406 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4407 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4410 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4411 | break; |
| 4412 | default: |
| 4413 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4416 | D->addAttr(::new (S.Context) PcsAttr(S.Context, AL, PCS)); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4417 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4418 | } |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 4419 | case ParsedAttr::AT_AArch64VectorPcs: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4420 | D->addAttr(::new (S.Context) AArch64VectorPcsAttr(S.Context, AL)); |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 4421 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4422 | case ParsedAttr::AT_IntelOclBicc: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4423 | D->addAttr(::new (S.Context) IntelOclBiccAttr(S.Context, AL)); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4424 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4425 | case ParsedAttr::AT_PreserveMost: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4426 | D->addAttr(::new (S.Context) PreserveMostAttr(S.Context, AL)); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4427 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4428 | case ParsedAttr::AT_PreserveAll: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4429 | D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL)); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4430 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4431 | default: |
| 4432 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4433 | } |
| 4434 | } |
| 4435 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4436 | static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4437 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4438 | return; |
| 4439 | |
| 4440 | std::vector<StringRef> DiagnosticIdentifiers; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4441 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4442 | StringRef RuleName; |
| 4443 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4444 | if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4445 | return; |
| 4446 | |
| 4447 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 4448 | // clang-tidy knows about available rules. |
| 4449 | DiagnosticIdentifiers.push_back(RuleName); |
| 4450 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4451 | D->addAttr(::new (S.Context) |
| 4452 | SuppressAttr(S.Context, AL, DiagnosticIdentifiers.data(), |
| 4453 | DiagnosticIdentifiers.size())); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 4456 | static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 4457 | TypeSourceInfo *DerefTypeLoc = nullptr; |
| 4458 | QualType ParmType; |
| 4459 | if (AL.hasParsedType()) { |
| 4460 | ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc); |
| 4461 | |
| 4462 | unsigned SelectIdx = ~0U; |
Gabor Horvath | 247a603 | 2020-01-02 11:57:42 -0800 | [diff] [blame] | 4463 | if (ParmType->isReferenceType()) |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 4464 | SelectIdx = 0; |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 4465 | else if (ParmType->isArrayType()) |
Gabor Horvath | 247a603 | 2020-01-02 11:57:42 -0800 | [diff] [blame] | 4466 | SelectIdx = 1; |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 4467 | |
| 4468 | if (SelectIdx != ~0U) { |
| 4469 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) |
| 4470 | << SelectIdx << AL; |
| 4471 | return; |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | // To check if earlier decl attributes do not conflict the newly parsed ones |
| 4476 | // we always add (and check) the attribute to the cannonical decl. |
| 4477 | D = D->getCanonicalDecl(); |
| 4478 | if (AL.getKind() == ParsedAttr::AT_Owner) { |
| 4479 | if (checkAttrMutualExclusion<PointerAttr>(S, D, AL)) |
| 4480 | return; |
| 4481 | if (const auto *OAttr = D->getAttr<OwnerAttr>()) { |
| 4482 | const Type *ExistingDerefType = OAttr->getDerefTypeLoc() |
| 4483 | ? OAttr->getDerefType().getTypePtr() |
| 4484 | : nullptr; |
| 4485 | if (ExistingDerefType != ParmType.getTypePtrOrNull()) { |
| 4486 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 4487 | << AL << OAttr; |
| 4488 | S.Diag(OAttr->getLocation(), diag::note_conflicting_attribute); |
| 4489 | } |
| 4490 | return; |
| 4491 | } |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 4492 | for (Decl *Redecl : D->redecls()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4493 | Redecl->addAttr(::new (S.Context) OwnerAttr(S.Context, AL, DerefTypeLoc)); |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 4494 | } |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 4495 | } else { |
| 4496 | if (checkAttrMutualExclusion<OwnerAttr>(S, D, AL)) |
| 4497 | return; |
| 4498 | if (const auto *PAttr = D->getAttr<PointerAttr>()) { |
| 4499 | const Type *ExistingDerefType = PAttr->getDerefTypeLoc() |
| 4500 | ? PAttr->getDerefType().getTypePtr() |
| 4501 | : nullptr; |
| 4502 | if (ExistingDerefType != ParmType.getTypePtrOrNull()) { |
| 4503 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 4504 | << AL << PAttr; |
| 4505 | S.Diag(PAttr->getLocation(), diag::note_conflicting_attribute); |
| 4506 | } |
| 4507 | return; |
| 4508 | } |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 4509 | for (Decl *Redecl : D->redecls()) { |
| 4510 | Redecl->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4511 | PointerAttr(S.Context, AL, DerefTypeLoc)); |
Matthias Gehre | f64f488 | 2019-09-06 08:56:30 +0000 | [diff] [blame] | 4512 | } |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 4513 | } |
| 4514 | } |
| 4515 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4516 | bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC, |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4517 | const FunctionDecl *FD) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4518 | if (Attrs.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4519 | return true; |
| 4520 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4521 | if (Attrs.hasProcessingCache()) { |
| 4522 | CC = (CallingConv) Attrs.getProcessingCache(); |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4523 | return false; |
| 4524 | } |
| 4525 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4526 | unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4527 | if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) { |
| 4528 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4529 | return true; |
| 4530 | } |
| 4531 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4532 | // TODO: diagnose uses of these conventions on the wrong target. |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4533 | switch (Attrs.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4534 | case ParsedAttr::AT_CDecl: |
| 4535 | CC = CC_C; |
| 4536 | break; |
| 4537 | case ParsedAttr::AT_FastCall: |
| 4538 | CC = CC_X86FastCall; |
| 4539 | break; |
| 4540 | case ParsedAttr::AT_StdCall: |
| 4541 | CC = CC_X86StdCall; |
| 4542 | break; |
| 4543 | case ParsedAttr::AT_ThisCall: |
| 4544 | CC = CC_X86ThisCall; |
| 4545 | break; |
| 4546 | case ParsedAttr::AT_Pascal: |
| 4547 | CC = CC_X86Pascal; |
| 4548 | break; |
| 4549 | case ParsedAttr::AT_SwiftCall: |
| 4550 | CC = CC_Swift; |
| 4551 | break; |
| 4552 | case ParsedAttr::AT_VectorCall: |
| 4553 | CC = CC_X86VectorCall; |
| 4554 | break; |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 4555 | case ParsedAttr::AT_AArch64VectorPcs: |
| 4556 | CC = CC_AArch64VectorCall; |
| 4557 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4558 | case ParsedAttr::AT_RegCall: |
| 4559 | CC = CC_X86RegCall; |
| 4560 | break; |
| 4561 | case ParsedAttr::AT_MSABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4562 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 4563 | CC_Win64; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4564 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4565 | case ParsedAttr::AT_SysVABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4566 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4567 | CC_C; |
| 4568 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4569 | case ParsedAttr::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 4570 | StringRef StrRef; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4571 | if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) { |
| 4572 | Attrs.setInvalid(); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4573 | return true; |
| 4574 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4575 | if (StrRef == "aapcs") { |
| 4576 | CC = CC_AAPCS; |
| 4577 | break; |
| 4578 | } else if (StrRef == "aapcs-vfp") { |
| 4579 | CC = CC_AAPCS_VFP; |
| 4580 | break; |
| 4581 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4582 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4583 | Attrs.setInvalid(); |
| 4584 | Diag(Attrs.getLoc(), diag::err_invalid_pcs); |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4585 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4586 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4587 | case ParsedAttr::AT_IntelOclBicc: |
| 4588 | CC = CC_IntelOclBicc; |
| 4589 | break; |
| 4590 | case ParsedAttr::AT_PreserveMost: |
| 4591 | CC = CC_PreserveMost; |
| 4592 | break; |
| 4593 | case ParsedAttr::AT_PreserveAll: |
| 4594 | CC = CC_PreserveAll; |
| 4595 | break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4596 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Yaxun Liu | fa49c3a | 2019-02-26 22:24:49 +0000 | [diff] [blame] | 4599 | TargetInfo::CallingConvCheckResult A = TargetInfo::CCCR_OK; |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4600 | const TargetInfo &TI = Context.getTargetInfo(); |
Yaxun Liu | 785cbd8 | 2019-02-27 15:46:29 +0000 | [diff] [blame] | 4601 | // CUDA functions may have host and/or device attributes which indicate |
| 4602 | // their targeted execution environment, therefore the calling convention |
| 4603 | // of functions in CUDA should be checked against the target deduced based |
| 4604 | // on their host/device attributes. |
Yaxun Liu | fa49c3a | 2019-02-26 22:24:49 +0000 | [diff] [blame] | 4605 | if (LangOpts.CUDA) { |
Yaxun Liu | 785cbd8 | 2019-02-27 15:46:29 +0000 | [diff] [blame] | 4606 | auto *Aux = Context.getAuxTargetInfo(); |
Yaxun Liu | fa49c3a | 2019-02-26 22:24:49 +0000 | [diff] [blame] | 4607 | auto CudaTarget = IdentifyCUDATarget(FD); |
| 4608 | bool CheckHost = false, CheckDevice = false; |
| 4609 | switch (CudaTarget) { |
| 4610 | case CFT_HostDevice: |
| 4611 | CheckHost = true; |
| 4612 | CheckDevice = true; |
| 4613 | break; |
| 4614 | case CFT_Host: |
| 4615 | CheckHost = true; |
| 4616 | break; |
| 4617 | case CFT_Device: |
| 4618 | case CFT_Global: |
| 4619 | CheckDevice = true; |
| 4620 | break; |
| 4621 | case CFT_InvalidTarget: |
| 4622 | llvm_unreachable("unexpected cuda target"); |
| 4623 | } |
| 4624 | auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI; |
| 4625 | auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux; |
| 4626 | if (CheckHost && HostTI) |
| 4627 | A = HostTI->checkCallingConvention(CC); |
| 4628 | if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI) |
| 4629 | A = DeviceTI->checkCallingConvention(CC); |
| 4630 | } else { |
| 4631 | A = TI.checkCallingConvention(CC); |
| 4632 | } |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4633 | |
| 4634 | switch (A) { |
| 4635 | case TargetInfo::CCCR_OK: |
| 4636 | break; |
| 4637 | |
| 4638 | case TargetInfo::CCCR_Ignore: |
| 4639 | // Treat an ignored convention as if it was an explicit C calling convention |
| 4640 | // attribute. For example, __stdcall on Win x64 functions as __cdecl, so |
| 4641 | // that command line flags that change the default convention to |
| 4642 | // __vectorcall don't affect declarations marked __stdcall. |
| 4643 | CC = CC_C; |
| 4644 | break; |
| 4645 | |
Sunil Srivastava | f4038e7 | 2019-07-19 21:38:34 +0000 | [diff] [blame] | 4646 | case TargetInfo::CCCR_Error: |
| 4647 | Diag(Attrs.getLoc(), diag::error_cconv_unsupported) |
| 4648 | << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget; |
| 4649 | break; |
| 4650 | |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4651 | case TargetInfo::CCCR_Warning: { |
Sunil Srivastava | 85d667f | 2019-07-17 20:41:26 +0000 | [diff] [blame] | 4652 | Diag(Attrs.getLoc(), diag::warn_cconv_unsupported) |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4653 | << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4654 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4655 | // This convention is not valid for the target. Use the default function or |
| 4656 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 4657 | bool IsCXXMethod = false, IsVariadic = false; |
| 4658 | if (FD) { |
| 4659 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4660 | IsVariadic = FD->isVariadic(); |
| 4661 | } |
| 4662 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Reid Kleckner | 4586a19 | 2019-07-09 23:17:43 +0000 | [diff] [blame] | 4663 | break; |
| 4664 | } |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4667 | Attrs.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4668 | return false; |
| 4669 | } |
| 4670 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4671 | /// Pointer-like types in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4672 | static bool isValidSwiftContextType(QualType Ty) { |
| 4673 | if (!Ty->hasPointerRepresentation()) |
| 4674 | return Ty->isDependentType(); |
| 4675 | return Ty->getPointeeType().getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | /// Pointers and references in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4679 | static bool isValidSwiftIndirectResultType(QualType Ty) { |
| 4680 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4681 | Ty = PtrType->getPointeeType(); |
| 4682 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4683 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4684 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4685 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4686 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4687 | return Ty.getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | /// Pointers and references to pointers in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4691 | static bool isValidSwiftErrorResultType(QualType Ty) { |
| 4692 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4693 | Ty = PtrType->getPointeeType(); |
| 4694 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4695 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4696 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4697 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4698 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4699 | if (!Ty.getQualifiers().empty()) |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4700 | return false; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4701 | return isValidSwiftContextType(Ty); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4704 | void Sema::AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI, |
| 4705 | ParameterABI abi) { |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4706 | |
| 4707 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4708 | |
| 4709 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4710 | if (existingAttr->getABI() != abi) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4711 | Diag(CI.getLoc(), diag::err_attributes_are_not_compatible) |
| 4712 | << getParameterABISpelling(abi) << existingAttr; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4713 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4714 | return; |
| 4715 | } |
| 4716 | } |
| 4717 | |
| 4718 | switch (abi) { |
| 4719 | case ParameterABI::Ordinary: |
| 4720 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4721 | |
| 4722 | case ParameterABI::SwiftContext: |
| 4723 | if (!isValidSwiftContextType(type)) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4724 | Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type) |
| 4725 | << getParameterABISpelling(abi) << /*pointer to pointer */ 0 << type; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4726 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4727 | D->addAttr(::new (Context) SwiftContextAttr(Context, CI)); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | |
| 4730 | case ParameterABI::SwiftErrorResult: |
| 4731 | if (!isValidSwiftErrorResultType(type)) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4732 | Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type) |
| 4733 | << getParameterABISpelling(abi) << /*pointer to pointer */ 1 << type; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4734 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4735 | D->addAttr(::new (Context) SwiftErrorResultAttr(Context, CI)); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4736 | return; |
| 4737 | |
| 4738 | case ParameterABI::SwiftIndirectResult: |
| 4739 | if (!isValidSwiftIndirectResultType(type)) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4740 | Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type) |
| 4741 | << getParameterABISpelling(abi) << /*pointer*/ 0 << type; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4742 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4743 | D->addAttr(::new (Context) SwiftIndirectResultAttr(Context, CI)); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4744 | return; |
| 4745 | } |
| 4746 | llvm_unreachable("bad parameter ABI attribute"); |
| 4747 | } |
| 4748 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4749 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4750 | /// otherwise setting numParams to the appropriate value. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4751 | bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4752 | if (AL.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4753 | return true; |
| 4754 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4755 | if (!checkAttributeNumArgs(*this, AL, 1)) { |
| 4756 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4757 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4758 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4759 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4760 | uint32_t NP; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4761 | Expr *NumParamsExpr = AL.getArgAsExpr(0); |
| 4762 | if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) { |
| 4763 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4764 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4767 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4768 | Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4769 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4770 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4771 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4774 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4775 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4776 | Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4777 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4778 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4779 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4782 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4785 | // Checks whether an argument of launch_bounds attribute is |
| 4786 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4787 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4788 | // and may output an error. |
| 4789 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4790 | const CUDALaunchBoundsAttr &AL, |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4791 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4792 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4793 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4794 | |
| 4795 | // Accept template arguments for now as they depend on something else. |
| 4796 | // We'll get to check them when they eventually get instantiated. |
| 4797 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4798 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4799 | |
| 4800 | llvm::APSInt I(64); |
| 4801 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4802 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4803 | << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4804 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4805 | } |
| 4806 | // Make sure we can fit it in 32 bits. |
| 4807 | if (!I.isIntN(32)) { |
| 4808 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4809 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4810 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4811 | } |
| 4812 | if (I < 0) |
| 4813 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4814 | << &AL << Idx << E->getSourceRange(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4815 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4816 | // We may need to perform implicit conversion of the argument. |
| 4817 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4818 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4819 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4820 | assert(!ValArg.isInvalid() && |
| 4821 | "Unexpected PerformCopyInitialization() failure."); |
| 4822 | |
| 4823 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4824 | } |
| 4825 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4826 | void Sema::AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI, |
| 4827 | Expr *MaxThreads, Expr *MinBlocks) { |
| 4828 | CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4829 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4830 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4831 | return; |
| 4832 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4833 | if (MinBlocks) { |
| 4834 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4835 | if (MinBlocks == nullptr) |
| 4836 | return; |
| 4837 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4838 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4839 | D->addAttr(::new (Context) |
| 4840 | CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks)); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4843 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4844 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 4845 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4846 | return; |
| 4847 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4848 | S.AddLaunchBoundsAttr(D, AL, AL.getArgAsExpr(0), |
| 4849 | AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4852 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4853 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4854 | if (!AL.isArgIdent(0)) { |
| 4855 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4856 | << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4857 | return; |
| 4858 | } |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4859 | |
| 4860 | ParamIdx ArgumentIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4861 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4862 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4863 | return; |
| 4864 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4865 | ParamIdx TypeTagIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4866 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4867 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4868 | return; |
| 4869 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4870 | bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag"; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4871 | if (IsPointer) { |
| 4872 | // Ensure that buffer has a pointer type. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4873 | unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex(); |
| 4874 | if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) || |
| 4875 | !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType()) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4876 | S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 4879 | D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4880 | S.Context, AL, AL.getArgAsIdent(0)->Ident, ArgumentIdx, TypeTagIdx, |
| 4881 | IsPointer)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4885 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4886 | if (!AL.isArgIdent(0)) { |
| 4887 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4888 | << AL << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4889 | return; |
| 4890 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4891 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4892 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4893 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4894 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4895 | if (!isa<VarDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4896 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4897 | << AL << ExpectedVariable; |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4898 | return; |
| 4899 | } |
| 4900 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4901 | IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4902 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4903 | S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4904 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4905 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4906 | D->addAttr(::new (S.Context) TypeTagForDatatypeAttr( |
| 4907 | S.Context, AL, PointerKind, MatchingCTypeLoc, AL.getLayoutCompatible(), |
| 4908 | AL.getMustBeNull())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4911 | static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4912 | ParamIdx ArgCount; |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4913 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4914 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0), |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4915 | ArgCount, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4916 | true /* CanIndexImplicitThis */)) |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4917 | return; |
| 4918 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4919 | // ArgCount isn't a parameter index [0;n), it's a count [1;n] |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4920 | D->addAttr(::new (S.Context) |
| 4921 | XRayLogArgsAttr(S.Context, AL, ArgCount.getSourceIndex())); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
Fangrui Song | a44c434 | 2020-01-04 15:39:19 -0800 | [diff] [blame] | 4924 | static void handlePatchableFunctionEntryAttr(Sema &S, Decl *D, |
| 4925 | const ParsedAttr &AL) { |
| 4926 | uint32_t Count = 0, Offset = 0; |
| 4927 | if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Count, 0, true)) |
| 4928 | return; |
| 4929 | if (AL.getNumArgs() == 2) { |
| 4930 | Expr *Arg = AL.getArgAsExpr(1); |
| 4931 | if (!checkUInt32Argument(S, AL, Arg, Offset, 1, true)) |
| 4932 | return; |
Fangrui Song | 69bf40c | 2020-01-20 14:30:06 -0800 | [diff] [blame] | 4933 | if (Count < Offset) { |
Fangrui Song | a44c434 | 2020-01-04 15:39:19 -0800 | [diff] [blame] | 4934 | S.Diag(getAttrLoc(AL), diag::err_attribute_argument_out_of_range) |
Fangrui Song | 69bf40c | 2020-01-20 14:30:06 -0800 | [diff] [blame] | 4935 | << &AL << 0 << Count << Arg->getBeginLoc(); |
Fangrui Song | a44c434 | 2020-01-04 15:39:19 -0800 | [diff] [blame] | 4936 | return; |
| 4937 | } |
| 4938 | } |
| 4939 | D->addAttr(::new (S.Context) |
| 4940 | PatchableFunctionEntryAttr(S.Context, AL, Count, Offset)); |
| 4941 | } |
| 4942 | |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 4943 | namespace { |
| 4944 | struct IntrinToName { |
| 4945 | uint32_t Id; |
| 4946 | int32_t FullName; |
| 4947 | int32_t ShortName; |
| 4948 | }; |
| 4949 | } // unnamed namespace |
| 4950 | |
| 4951 | static bool ArmBuiltinAliasValid(unsigned BuiltinID, StringRef AliasName, |
| 4952 | ArrayRef<IntrinToName> Map, |
| 4953 | const char *IntrinNames) { |
Simon Tatham | 08074cc | 2019-09-02 15:50:50 +0100 | [diff] [blame] | 4954 | if (AliasName.startswith("__arm_")) |
| 4955 | AliasName = AliasName.substr(6); |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 4956 | const IntrinToName *It = std::lower_bound( |
| 4957 | Map.begin(), Map.end(), BuiltinID, |
| 4958 | [](const IntrinToName &L, unsigned Id) { return L.Id < Id; }); |
| 4959 | if (It == Map.end() || It->Id != BuiltinID) |
| 4960 | return false; |
| 4961 | StringRef FullName(&IntrinNames[It->FullName]); |
| 4962 | if (AliasName == FullName) |
| 4963 | return true; |
| 4964 | if (It->ShortName == -1) |
| 4965 | return false; |
| 4966 | StringRef ShortName(&IntrinNames[It->ShortName]); |
| 4967 | return AliasName == ShortName; |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 4968 | } |
| 4969 | |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 4970 | static bool ArmMveAliasValid(unsigned BuiltinID, StringRef AliasName) { |
| 4971 | #include "clang/Basic/arm_mve_builtin_aliases.inc" |
| 4972 | // The included file defines: |
| 4973 | // - ArrayRef<IntrinToName> Map |
| 4974 | // - const char IntrinNames[] |
| 4975 | return ArmBuiltinAliasValid(BuiltinID, AliasName, Map, IntrinNames); |
| 4976 | } |
| 4977 | |
| 4978 | static bool ArmCdeAliasValid(unsigned BuiltinID, StringRef AliasName) { |
| 4979 | #include "clang/Basic/arm_cde_builtin_aliases.inc" |
| 4980 | return ArmBuiltinAliasValid(BuiltinID, AliasName, Map, IntrinNames); |
| 4981 | } |
| 4982 | |
Sander de Smalen | 981f080 | 2020-03-18 15:05:08 +0000 | [diff] [blame^] | 4983 | static bool ArmSveAliasValid(unsigned BuiltinID, StringRef AliasName) { |
| 4984 | switch (BuiltinID) { |
| 4985 | default: |
| 4986 | return false; |
| 4987 | #define GET_SVE_BUILTINS |
| 4988 | #define BUILTIN(name, types, attr) case SVE::BI##name: |
| 4989 | #include "clang/Basic/arm_sve_builtins.inc" |
| 4990 | return true; |
| 4991 | } |
| 4992 | } |
| 4993 | |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 4994 | static void handleArmBuiltinAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 4995 | if (!AL.isArgIdent(0)) { |
| 4996 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
| 4997 | << AL << 1 << AANT_ArgumentIdentifier; |
| 4998 | return; |
| 4999 | } |
| 5000 | |
| 5001 | IdentifierInfo *Ident = AL.getArgAsIdent(0)->Ident; |
| 5002 | unsigned BuiltinID = Ident->getBuiltinID(); |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 5003 | StringRef AliasName = cast<FunctionDecl>(D)->getIdentifier()->getName(); |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 5004 | |
Sander de Smalen | 981f080 | 2020-03-18 15:05:08 +0000 | [diff] [blame^] | 5005 | bool IsAArch64 = S.Context.getTargetInfo().getTriple().isAArch64(); |
| 5006 | if ((IsAArch64 && !ArmSveAliasValid(BuiltinID, AliasName)) || |
| 5007 | (!IsAArch64 && !ArmMveAliasValid(BuiltinID, AliasName) && |
| 5008 | !ArmCdeAliasValid(BuiltinID, AliasName))) { |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 5009 | S.Diag(AL.getLoc(), diag::err_attribute_arm_builtin_alias); |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 5010 | return; |
| 5011 | } |
| 5012 | |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 5013 | D->addAttr(::new (S.Context) ArmBuiltinAliasAttr(S.Context, AL, Ident)); |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 5014 | } |
| 5015 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5016 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5017 | // Checker-specific attribute handlers. |
| 5018 | //===----------------------------------------------------------------------===// |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5019 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) { |
| 5020 | return QT->isDependentType() || QT->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5023 | static bool isValidSubjectOfNSAttribute(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5024 | return QT->isDependentType() || QT->isObjCObjectPointerType() || |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5025 | QT->isObjCNSObjectType(); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5026 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 5027 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5028 | static bool isValidSubjectOfCFAttribute(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5029 | return QT->isDependentType() || QT->isPointerType() || |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5030 | isValidSubjectOfNSAttribute(QT); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5033 | static bool isValidSubjectOfOSAttribute(QualType QT) { |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5034 | if (QT->isDependentType()) |
| 5035 | return true; |
| 5036 | QualType PT = QT->getPointeeType(); |
| 5037 | return !PT.isNull() && PT->getAsCXXRecordDecl() != nullptr; |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 5038 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5039 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5040 | void Sema::AddXConsumedAttr(Decl *D, const AttributeCommonInfo &CI, |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5041 | RetainOwnershipKind K, |
| 5042 | bool IsTemplateInstantiation) { |
| 5043 | ValueDecl *VD = cast<ValueDecl>(D); |
| 5044 | switch (K) { |
| 5045 | case RetainOwnershipKind::OS: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5046 | handleSimpleAttributeOrDiagnose<OSConsumedAttr>( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5047 | *this, VD, CI, isValidSubjectOfOSAttribute(VD->getType()), |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5048 | diag::warn_ns_attribute_wrong_parameter_type, |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5049 | /*ExtraArgs=*/CI.getRange(), "os_consumed", /*pointers*/ 1); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5050 | return; |
| 5051 | case RetainOwnershipKind::NS: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5052 | handleSimpleAttributeOrDiagnose<NSConsumedAttr>( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5053 | *this, VD, CI, isValidSubjectOfNSAttribute(VD->getType()), |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 5054 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5055 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 5056 | // is significant. Allow non-dependent code to contain inappropriate |
| 5057 | // attributes even in ARC, but require template instantiations to be |
| 5058 | // set up correctly. |
| 5059 | ((IsTemplateInstantiation && getLangOpts().ObjCAutoRefCount) |
| 5060 | ? diag::err_ns_attribute_wrong_parameter_type |
| 5061 | : diag::warn_ns_attribute_wrong_parameter_type), |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5062 | /*ExtraArgs=*/CI.getRange(), "ns_consumed", /*objc pointers*/ 0); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5063 | return; |
| 5064 | case RetainOwnershipKind::CF: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5065 | handleSimpleAttributeOrDiagnose<CFConsumedAttr>( |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5066 | *this, VD, CI, isValidSubjectOfCFAttribute(VD->getType()), |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5067 | diag::warn_ns_attribute_wrong_parameter_type, |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5068 | /*ExtraArgs=*/CI.getRange(), "cf_consumed", /*pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5069 | return; |
| 5070 | } |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5071 | } |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5072 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5073 | static Sema::RetainOwnershipKind |
| 5074 | parsedAttrToRetainOwnershipKind(const ParsedAttr &AL) { |
| 5075 | switch (AL.getKind()) { |
| 5076 | case ParsedAttr::AT_CFConsumed: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5077 | case ParsedAttr::AT_CFReturnsRetained: |
| 5078 | case ParsedAttr::AT_CFReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5079 | return Sema::RetainOwnershipKind::CF; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5080 | case ParsedAttr::AT_OSConsumesThis: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5081 | case ParsedAttr::AT_OSConsumed: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5082 | case ParsedAttr::AT_OSReturnsRetained: |
| 5083 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5084 | case ParsedAttr::AT_OSReturnsRetainedOnZero: |
| 5085 | case ParsedAttr::AT_OSReturnsRetainedOnNonZero: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5086 | return Sema::RetainOwnershipKind::OS; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5087 | case ParsedAttr::AT_NSConsumesSelf: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5088 | case ParsedAttr::AT_NSConsumed: |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5089 | case ParsedAttr::AT_NSReturnsRetained: |
| 5090 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 5091 | case ParsedAttr::AT_NSReturnsAutoreleased: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5092 | return Sema::RetainOwnershipKind::NS; |
| 5093 | default: |
| 5094 | llvm_unreachable("Wrong argument supplied"); |
| 5095 | } |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5098 | bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) { |
| 5099 | if (isValidSubjectOfNSReturnsRetainedAttribute(QT)) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5100 | return false; |
| 5101 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5102 | Diag(Loc, diag::warn_ns_attribute_wrong_return_type) |
| 5103 | << "'ns_returns_retained'" << 0 << 0; |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5104 | return true; |
| 5105 | } |
| 5106 | |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5107 | /// \return whether the parameter is a pointer to OSObject pointer. |
| 5108 | static bool isValidOSObjectOutParameter(const Decl *D) { |
| 5109 | const auto *PVD = dyn_cast<ParmVarDecl>(D); |
| 5110 | if (!PVD) |
| 5111 | return false; |
| 5112 | QualType QT = PVD->getType(); |
| 5113 | QualType PT = QT->getPointeeType(); |
| 5114 | return !PT.isNull() && isValidSubjectOfOSAttribute(PT); |
| 5115 | } |
| 5116 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5117 | static void handleXReturnsXRetainedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5118 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5119 | QualType ReturnType; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5120 | Sema::RetainOwnershipKind K = parsedAttrToRetainOwnershipKind(AL); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5121 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5122 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5123 | ReturnType = MD->getReturnType(); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5124 | } else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
| 5125 | (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5126 | return; // ignore: was handled as a type attribute |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5127 | } else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5128 | ReturnType = PD->getType(); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5129 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5130 | ReturnType = FD->getReturnType(); |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5131 | } else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 5132 | // Attributes on parameters are used for out-parameters, |
| 5133 | // passed as pointers-to-pointers. |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5134 | unsigned DiagID = K == Sema::RetainOwnershipKind::CF |
| 5135 | ? /*pointer-to-CF-pointer*/2 |
| 5136 | : /*pointer-to-OSObject-pointer*/3; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5137 | ReturnType = Param->getType()->getPointeeType(); |
| 5138 | if (ReturnType.isNull()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5139 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5140 | << AL << DiagID << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5141 | return; |
| 5142 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5143 | } else if (AL.isUsedAsTypeAttr()) { |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5144 | return; |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5145 | } else { |
| 5146 | AttributeDeclKind ExpectedDeclKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5147 | switch (AL.getKind()) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5148 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5149 | case ParsedAttr::AT_NSReturnsRetained: |
| 5150 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5151 | case ParsedAttr::AT_NSReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5152 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 5153 | break; |
| 5154 | |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5155 | case ParsedAttr::AT_OSReturnsRetained: |
| 5156 | case ParsedAttr::AT_OSReturnsNotRetained: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5157 | case ParsedAttr::AT_CFReturnsRetained: |
| 5158 | case ParsedAttr::AT_CFReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5159 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 5160 | break; |
| 5161 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5162 | S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5163 | << AL.getRange() << AL << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5164 | return; |
| 5165 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5166 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5167 | bool TypeOK; |
| 5168 | bool Cf; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5169 | unsigned ParmDiagID = 2; // Pointer-to-CF-pointer |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5170 | switch (AL.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5171 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5172 | case ParsedAttr::AT_NSReturnsRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5173 | TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType); |
| 5174 | Cf = false; |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 5175 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5176 | |
| 5177 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 5178 | case ParsedAttr::AT_NSReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5179 | TypeOK = isValidSubjectOfNSAttribute(ReturnType); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5180 | Cf = false; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5181 | break; |
| 5182 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5183 | case ParsedAttr::AT_CFReturnsRetained: |
| 5184 | case ParsedAttr::AT_CFReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5185 | TypeOK = isValidSubjectOfCFAttribute(ReturnType); |
| 5186 | Cf = true; |
| 5187 | break; |
| 5188 | |
| 5189 | case ParsedAttr::AT_OSReturnsRetained: |
| 5190 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5191 | TypeOK = isValidSubjectOfOSAttribute(ReturnType); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5192 | Cf = true; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5193 | ParmDiagID = 3; // Pointer-to-OSObject-pointer |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5194 | break; |
| 5195 | } |
| 5196 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5197 | if (!TypeOK) { |
| 5198 | if (AL.isUsedAsTypeAttr()) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 5199 | return; |
| 5200 | |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5201 | if (isa<ParmVarDecl>(D)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5202 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 5203 | << AL << ParmDiagID << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5204 | } else { |
| 5205 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 5206 | enum : unsigned { |
| 5207 | Function, |
| 5208 | Method, |
| 5209 | Property |
| 5210 | } SubjectKind = Function; |
| 5211 | if (isa<ObjCMethodDecl>(D)) |
| 5212 | SubjectKind = Method; |
| 5213 | else if (isa<ObjCPropertyDecl>(D)) |
| 5214 | SubjectKind = Property; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5215 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5216 | << AL << SubjectKind << Cf << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 5217 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5218 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 5219 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5220 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5221 | switch (AL.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5222 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5223 | llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5224 | case ParsedAttr::AT_NSReturnsAutoreleased: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5225 | handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5226 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5227 | case ParsedAttr::AT_CFReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5228 | handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 5229 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5230 | case ParsedAttr::AT_NSReturnsNotRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5231 | handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 5232 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5233 | case ParsedAttr::AT_CFReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5234 | handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5235 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5236 | case ParsedAttr::AT_NSReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 5237 | handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL); |
| 5238 | return; |
| 5239 | case ParsedAttr::AT_OSReturnsRetained: |
| 5240 | handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL); |
| 5241 | return; |
| 5242 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 5243 | handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5244 | return; |
| 5245 | }; |
| 5246 | } |
| 5247 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5248 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5249 | const ParsedAttr &Attrs) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 5250 | const int EP_ObjCMethod = 1; |
| 5251 | const int EP_ObjCProperty = 2; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5252 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 5253 | SourceLocation loc = Attrs.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 5254 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5255 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5256 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 5257 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5258 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5259 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 5260 | if (!resultType->isReferenceType() && |
| 5261 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5262 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5263 | << SourceRange(loc) << Attrs |
| 5264 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
| 5265 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5266 | |
| 5267 | // Drop the attribute. |
| 5268 | return; |
| 5269 | } |
| 5270 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5271 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(S.Context, Attrs)); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5274 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5275 | const ParsedAttr &Attrs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5276 | const auto *Method = cast<ObjCMethodDecl>(D); |
| 5277 | |
| 5278 | const DeclContext *DC = Method->getDeclContext(); |
| 5279 | if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5280 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5281 | << 0; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5282 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 5283 | return; |
| 5284 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5285 | if (Method->getMethodFamily() == OMF_dealloc) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5286 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5287 | << 1; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5288 | return; |
| 5289 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5290 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5291 | D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(S.Context, Attrs)); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5294 | static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5295 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 5296 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5297 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5298 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5299 | return; |
| 5300 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5301 | |
| 5302 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5303 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5304 | if (!Parm->Ident->isStr("id")) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5305 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL; |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5306 | return; |
| 5307 | } |
| 5308 | |
| 5309 | // Only allow 'cv void *'. |
| 5310 | QualType T = TD->getUnderlyingType(); |
| 5311 | if (!T->isVoidPointerType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5312 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 5313 | return; |
| 5314 | } |
| 5315 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5316 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5317 | D->addAttr(::new (S.Context) ObjCBridgeAttr(S.Context, AL, Parm->Ident)); |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5320 | static void handleObjCBridgeMutableAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5321 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5322 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5323 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5324 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5325 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5326 | return; |
| 5327 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5328 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5329 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5330 | ObjCBridgeMutableAttr(S.Context, AL, Parm->Ident)); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5333 | static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5334 | const ParsedAttr &AL) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5335 | IdentifierInfo *RelatedClass = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5336 | AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5337 | if (!RelatedClass) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5338 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5339 | return; |
| 5340 | } |
| 5341 | IdentifierInfo *ClassMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5342 | AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5343 | IdentifierInfo *InstanceMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5344 | AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5345 | D->addAttr(::new (S.Context) ObjCBridgeRelatedAttr( |
| 5346 | S.Context, AL, RelatedClass, ClassMethod, InstanceMethod)); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5349 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5350 | const ParsedAttr &AL) { |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 5351 | DeclContext *Ctx = D->getDeclContext(); |
| 5352 | |
| 5353 | // This attribute can only be applied to methods in interfaces or class |
| 5354 | // extensions. |
| 5355 | if (!isa<ObjCInterfaceDecl>(Ctx) && |
| 5356 | !(isa<ObjCCategoryDecl>(Ctx) && |
| 5357 | cast<ObjCCategoryDecl>(Ctx)->IsClassExtension())) { |
| 5358 | S.Diag(D->getLocation(), diag::err_designated_init_attr_non_init); |
| 5359 | return; |
| 5360 | } |
| 5361 | |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 5362 | ObjCInterfaceDecl *IFace; |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 5363 | if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx)) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 5364 | IFace = CatDecl->getClassInterface(); |
| 5365 | else |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 5366 | IFace = cast<ObjCInterfaceDecl>(Ctx); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5367 | |
| 5368 | if (!IFace) |
| 5369 | return; |
| 5370 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 5371 | IFace->setHasDesignatedInitializers(); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5372 | D->addAttr(::new (S.Context) ObjCDesignatedInitializerAttr(S.Context, AL)); |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5375 | static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5376 | StringRef MetaDataName; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5377 | if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName)) |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5378 | return; |
| 5379 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5380 | ObjCRuntimeNameAttr(S.Context, AL, MetaDataName)); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 5383 | // When a user wants to use objc_boxable with a union or struct |
| 5384 | // but they don't have access to the declaration (legacy/third-party code) |
| 5385 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5386 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5387 | static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5388 | bool notify = false; |
| 5389 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5390 | auto *RD = dyn_cast<RecordDecl>(D); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5391 | if (RD && RD->getDefinition()) { |
| 5392 | RD = RD->getDefinition(); |
| 5393 | notify = true; |
| 5394 | } |
| 5395 | |
| 5396 | if (RD) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5397 | ObjCBoxableAttr *BoxableAttr = |
| 5398 | ::new (S.Context) ObjCBoxableAttr(S.Context, AL); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5399 | RD->addAttr(BoxableAttr); |
| 5400 | if (notify) { |
| 5401 | // we need to notify ASTReader/ASTWriter about |
| 5402 | // modification of existing declaration |
| 5403 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 5404 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 5405 | } |
| 5406 | } |
| 5407 | } |
| 5408 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5409 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5410 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5411 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5412 | S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5413 | << AL.getRange() << AL << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5416 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5417 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5418 | const auto *VD = cast<ValueDecl>(D); |
| 5419 | QualType QT = VD->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5420 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5421 | if (!QT->isDependentType() && |
| 5422 | !QT->isObjCLifetimeType()) { |
| 5423 | S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
| 5424 | << QT; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5425 | return; |
| 5426 | } |
| 5427 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5428 | Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5429 | |
| 5430 | // If we have no lifetime yet, check the lifetime we're presumably |
| 5431 | // going to infer. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5432 | if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType()) |
| 5433 | Lifetime = QT->getObjCARCImplicitLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5434 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5435 | switch (Lifetime) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5436 | case Qualifiers::OCL_None: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5437 | assert(QT->isDependentType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5438 | "didn't infer lifetime for non-dependent type?"); |
| 5439 | break; |
| 5440 | |
| 5441 | case Qualifiers::OCL_Weak: // meaningful |
| 5442 | case Qualifiers::OCL_Strong: // meaningful |
| 5443 | break; |
| 5444 | |
| 5445 | case Qualifiers::OCL_ExplicitNone: |
| 5446 | case Qualifiers::OCL_Autoreleasing: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5447 | S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
| 5448 | << (Lifetime == Qualifiers::OCL_Autoreleasing); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5449 | break; |
| 5450 | } |
| 5451 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5452 | D->addAttr(::new (S.Context) ObjCPreciseLifetimeAttr(S.Context, AL)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5455 | //===----------------------------------------------------------------------===// |
| 5456 | // Microsoft specific attribute handlers. |
| 5457 | //===----------------------------------------------------------------------===// |
| 5458 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5459 | UuidAttr *Sema::mergeUuidAttr(Decl *D, const AttributeCommonInfo &CI, |
| 5460 | StringRef Uuid) { |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5461 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 5462 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5463 | return nullptr; |
Zachary Henkel | 0acfc49 | 2019-12-28 13:06:13 -0800 | [diff] [blame] | 5464 | if (!UA->getGuid().empty()) { |
| 5465 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 5466 | Diag(CI.getLoc(), diag::note_previous_uuid); |
| 5467 | D->dropAttr<UuidAttr>(); |
| 5468 | } |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5471 | return ::new (Context) UuidAttr(Context, CI, Uuid); |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5474 | static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5475 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5476 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5477 | << AL << AttributeLangSupport::C; |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5478 | return; |
| 5479 | } |
| 5480 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5481 | StringRef StrRef; |
| 5482 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5483 | if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5484 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5485 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5486 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 5487 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5488 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 5489 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5490 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5491 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5492 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5493 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5494 | return; |
| 5495 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 5496 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5497 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5498 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5499 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5500 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5501 | return; |
| 5502 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5503 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5504 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5505 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5506 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5507 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5508 | |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5509 | // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's |
| 5510 | // the only thing in the [] list, the [] too), and add an insertion of |
| 5511 | // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas |
| 5512 | // separating attributes nor of the [ and the ] are in the AST. |
Nico Weber | 0a23404 | 2017-05-05 17:15:08 +0000 | [diff] [blame] | 5513 | // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc" |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5514 | // on cfe-dev. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5515 | if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling. |
| 5516 | S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated); |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5517 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5518 | UuidAttr *UA = S.mergeUuidAttr(D, AL, StrRef); |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5519 | if (UA) |
| 5520 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5523 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5524 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5525 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5526 | << AL << AttributeLangSupport::C; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5527 | return; |
| 5528 | } |
| 5529 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 5530 | D, AL, /*BestCase=*/true, (MSInheritanceModel)AL.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5531 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5532 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5533 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 5534 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5537 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5538 | const auto *VD = cast<VarDecl>(D); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5539 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5540 | S.Diag(AL.getLoc(), diag::err_thread_unsupported); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5541 | return; |
| 5542 | } |
| 5543 | if (VD->getTSCSpec() != TSCS_unspecified) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5544 | S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5545 | return; |
| 5546 | } |
| 5547 | if (VD->hasLocalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5548 | S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5549 | return; |
| 5550 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5551 | D->addAttr(::new (S.Context) ThreadAttr(S.Context, AL)); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5554 | static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5555 | SmallVector<StringRef, 4> Tags; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5556 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5557 | StringRef Tag; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5558 | if (!S.checkStringLiteralArgumentAttr(AL, I, Tag)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5559 | return; |
| 5560 | Tags.push_back(Tag); |
| 5561 | } |
| 5562 | |
| 5563 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 5564 | if (!NS->isInline()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5565 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5566 | return; |
| 5567 | } |
| 5568 | if (NS->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5569 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5570 | return; |
| 5571 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5572 | if (AL.getNumArgs() == 0) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5573 | Tags.push_back(NS->getName()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5574 | } else if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5575 | return; |
| 5576 | |
| 5577 | // Store tags sorted and without duplicates. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 5578 | llvm::sort(Tags); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5579 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 5580 | |
| 5581 | D->addAttr(::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5582 | AbiTagAttr(S.Context, AL, Tags.data(), Tags.size())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5585 | static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5586 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5587 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5588 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5589 | return; |
| 5590 | } |
| 5591 | |
| 5592 | StringRef Str; |
| 5593 | SourceLocation ArgLoc; |
| 5594 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5595 | if (AL.getNumArgs() == 0) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5596 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5597 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5598 | return; |
| 5599 | |
| 5600 | ARMInterruptAttr::InterruptType Kind; |
| 5601 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5602 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5603 | << ArgLoc; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5604 | return; |
| 5605 | } |
| 5606 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5607 | D->addAttr(::new (S.Context) ARMInterruptAttr(S.Context, AL, Kind)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5610 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5611 | // MSP430 'interrupt' attribute is applied to |
| 5612 | // a function with no parameters and void return type. |
| 5613 | if (!isFunctionOrMethod(D)) { |
| 5614 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5615 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5616 | return; |
| 5617 | } |
| 5618 | |
| 5619 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5620 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5621 | << /*MSP430*/ 1 << 0; |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5622 | return; |
| 5623 | } |
| 5624 | |
| 5625 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5626 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5627 | << /*MSP430*/ 1 << 1; |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5628 | return; |
| 5629 | } |
| 5630 | |
| 5631 | // The attribute takes one integer argument. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5632 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5633 | return; |
| 5634 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5635 | if (!AL.isArgExpr(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5636 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 5637 | << AL << AANT_ArgumentIntegerConstant; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5638 | return; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5639 | } |
| 5640 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5641 | Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5642 | llvm::APSInt NumParams(32); |
| 5643 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5644 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5645 | << AL << AANT_ArgumentIntegerConstant |
| 5646 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5647 | return; |
| 5648 | } |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5649 | // The argument should be in range 0..63. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5650 | unsigned Num = NumParams.getLimitedValue(255); |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 5651 | if (Num > 63) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5652 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5653 | << AL << (int)NumParams.getSExtValue() |
| 5654 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5655 | return; |
| 5656 | } |
| 5657 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5658 | D->addAttr(::new (S.Context) MSP430InterruptAttr(S.Context, AL, Num)); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5659 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5662 | static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5663 | // Only one optional argument permitted. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5664 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5665 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5666 | return; |
| 5667 | } |
| 5668 | |
| 5669 | StringRef Str; |
| 5670 | SourceLocation ArgLoc; |
| 5671 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5672 | if (AL.getNumArgs() == 0) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5673 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5674 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5675 | return; |
| 5676 | |
| 5677 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 5678 | // a) Must be a function. |
| 5679 | // b) Must have no parameters. |
| 5680 | // c) Must have the 'void' return type. |
| 5681 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 5682 | // lacks the 'eret' instruction. |
| 5683 | // e) The attribute itself must either have no argument or one of the |
| 5684 | // valid interrupt types, see [MipsInterruptDocs]. |
| 5685 | |
| 5686 | if (!isFunctionOrMethod(D)) { |
| 5687 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5688 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5689 | return; |
| 5690 | } |
| 5691 | |
| 5692 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5693 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5694 | << /*MIPS*/ 0 << 0; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5695 | return; |
| 5696 | } |
| 5697 | |
| 5698 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5699 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5700 | << /*MIPS*/ 0 << 1; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5701 | return; |
| 5702 | } |
| 5703 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5704 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5705 | return; |
| 5706 | |
| 5707 | MipsInterruptAttr::InterruptType Kind; |
| 5708 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5709 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5710 | << AL << "'" + std::string(Str) + "'"; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5711 | return; |
| 5712 | } |
| 5713 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5714 | D->addAttr(::new (S.Context) MipsInterruptAttr(S.Context, AL, Kind)); |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5717 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5718 | // Semantic checks for a function with the 'interrupt' attribute. |
| 5719 | // a) Must be a function. |
| 5720 | // b) Must have the 'void' return type. |
| 5721 | // c) Must take 1 or 2 arguments. |
| 5722 | // d) The 1st argument must be a pointer. |
| 5723 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 5724 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5725 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5726 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5727 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5728 | << AL << ExpectedFunctionWithProtoType; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5729 | return; |
| 5730 | } |
| 5731 | // Interrupt handler must have void return type. |
| 5732 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5733 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5734 | diag::err_anyx86_interrupt_attribute) |
| 5735 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5736 | ? 0 |
| 5737 | : 1) |
| 5738 | << 0; |
| 5739 | return; |
| 5740 | } |
| 5741 | // Interrupt handler must have 1 or 2 parameters. |
| 5742 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5743 | if (NumParams < 1 || NumParams > 2) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5744 | S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute) |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5745 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5746 | ? 0 |
| 5747 | : 1) |
| 5748 | << 1; |
| 5749 | return; |
| 5750 | } |
| 5751 | // The first argument must be a pointer. |
| 5752 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5753 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5754 | diag::err_anyx86_interrupt_attribute) |
| 5755 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5756 | ? 0 |
| 5757 | : 1) |
| 5758 | << 2; |
| 5759 | return; |
| 5760 | } |
| 5761 | // The second argument, if present, must be an unsigned integer. |
| 5762 | unsigned TypeSize = |
| 5763 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5764 | ? 64 |
| 5765 | : 32; |
| 5766 | if (NumParams == 2 && |
| 5767 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5768 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5769 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5770 | diag::err_anyx86_interrupt_attribute) |
| 5771 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5772 | ? 0 |
| 5773 | : 1) |
| 5774 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 5775 | return; |
| 5776 | } |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5777 | D->addAttr(::new (S.Context) AnyX86InterruptAttr(S.Context, AL)); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5778 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5779 | } |
| 5780 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5781 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5782 | if (!isFunctionOrMethod(D)) { |
| 5783 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5784 | << "'interrupt'" << ExpectedFunction; |
| 5785 | return; |
| 5786 | } |
| 5787 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5788 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5789 | return; |
| 5790 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5791 | handleSimpleAttribute<AVRInterruptAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5794 | static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5795 | if (!isFunctionOrMethod(D)) { |
| 5796 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5797 | << "'signal'" << ExpectedFunction; |
| 5798 | return; |
| 5799 | } |
| 5800 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5801 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5802 | return; |
| 5803 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5804 | handleSimpleAttribute<AVRSignalAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5805 | } |
| 5806 | |
Yonghong Song | 4e2ce22 | 2019-11-01 22:16:59 -0700 | [diff] [blame] | 5807 | static void handleBPFPreserveAIRecord(Sema &S, RecordDecl *RD) { |
| 5808 | // Add preserve_access_index attribute to all fields and inner records. |
| 5809 | for (auto D : RD->decls()) { |
| 5810 | if (D->hasAttr<BPFPreserveAccessIndexAttr>()) |
| 5811 | continue; |
| 5812 | |
| 5813 | D->addAttr(BPFPreserveAccessIndexAttr::CreateImplicit(S.Context)); |
| 5814 | if (auto *Rec = dyn_cast<RecordDecl>(D)) |
| 5815 | handleBPFPreserveAIRecord(S, Rec); |
| 5816 | } |
| 5817 | } |
| 5818 | |
| 5819 | static void handleBPFPreserveAccessIndexAttr(Sema &S, Decl *D, |
| 5820 | const ParsedAttr &AL) { |
| 5821 | auto *Rec = cast<RecordDecl>(D); |
| 5822 | handleBPFPreserveAIRecord(S, Rec); |
| 5823 | Rec->addAttr(::new (S.Context) BPFPreserveAccessIndexAttr(S.Context, AL)); |
| 5824 | } |
| 5825 | |
Sam Clegg | 881d877 | 2019-11-05 10:15:56 -0800 | [diff] [blame] | 5826 | static void handleWebAssemblyExportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5827 | if (!isFunctionOrMethod(D)) { |
| 5828 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5829 | << "'export_name'" << ExpectedFunction; |
| 5830 | return; |
| 5831 | } |
| 5832 | |
| 5833 | auto *FD = cast<FunctionDecl>(D); |
| 5834 | if (FD->isThisDeclarationADefinition()) { |
| 5835 | S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; |
| 5836 | return; |
| 5837 | } |
| 5838 | |
| 5839 | StringRef Str; |
| 5840 | SourceLocation ArgLoc; |
| 5841 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5842 | return; |
| 5843 | |
Sam Clegg | 0a1e349 | 2019-12-13 14:44:06 -0800 | [diff] [blame] | 5844 | D->addAttr(::new (S.Context) WebAssemblyExportNameAttr(S.Context, AL, Str)); |
| 5845 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Sam Clegg | 881d877 | 2019-11-05 10:15:56 -0800 | [diff] [blame] | 5846 | } |
| 5847 | |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 5848 | static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5849 | if (!isFunctionOrMethod(D)) { |
| 5850 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5851 | << "'import_module'" << ExpectedFunction; |
| 5852 | return; |
| 5853 | } |
| 5854 | |
| 5855 | auto *FD = cast<FunctionDecl>(D); |
| 5856 | if (FD->isThisDeclarationADefinition()) { |
| 5857 | S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; |
| 5858 | return; |
| 5859 | } |
| 5860 | |
| 5861 | StringRef Str; |
| 5862 | SourceLocation ArgLoc; |
| 5863 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5864 | return; |
| 5865 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5866 | FD->addAttr(::new (S.Context) |
| 5867 | WebAssemblyImportModuleAttr(S.Context, AL, Str)); |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 5868 | } |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5869 | |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 5870 | static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 5871 | if (!isFunctionOrMethod(D)) { |
| 5872 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5873 | << "'import_name'" << ExpectedFunction; |
| 5874 | return; |
| 5875 | } |
| 5876 | |
| 5877 | auto *FD = cast<FunctionDecl>(D); |
| 5878 | if (FD->isThisDeclarationADefinition()) { |
| 5879 | S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; |
| 5880 | return; |
| 5881 | } |
| 5882 | |
| 5883 | StringRef Str; |
| 5884 | SourceLocation ArgLoc; |
| 5885 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5886 | return; |
| 5887 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5888 | FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(S.Context, AL, Str)); |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5891 | static void handleRISCVInterruptAttr(Sema &S, Decl *D, |
| 5892 | const ParsedAttr &AL) { |
| 5893 | // Warn about repeated attributes. |
| 5894 | if (const auto *A = D->getAttr<RISCVInterruptAttr>()) { |
| 5895 | S.Diag(AL.getRange().getBegin(), |
| 5896 | diag::warn_riscv_repeated_interrupt_attribute); |
| 5897 | S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute); |
| 5898 | return; |
| 5899 | } |
| 5900 | |
| 5901 | // Check the attribute argument. Argument is optional. |
| 5902 | if (!checkAttributeAtMostNumArgs(S, AL, 1)) |
| 5903 | return; |
| 5904 | |
| 5905 | StringRef Str; |
| 5906 | SourceLocation ArgLoc; |
| 5907 | |
| 5908 | // 'machine'is the default interrupt mode. |
| 5909 | if (AL.getNumArgs() == 0) |
| 5910 | Str = "machine"; |
| 5911 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5912 | return; |
| 5913 | |
| 5914 | // Semantic checks for a function with the 'interrupt' attribute: |
| 5915 | // - Must be a function. |
| 5916 | // - Must have no parameters. |
| 5917 | // - Must have the 'void' return type. |
| 5918 | // - The attribute itself must either have no argument or one of the |
| 5919 | // valid interrupt types, see [RISCVInterruptDocs]. |
| 5920 | |
| 5921 | if (D->getFunctionType() == nullptr) { |
| 5922 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5923 | << "'interrupt'" << ExpectedFunction; |
| 5924 | return; |
| 5925 | } |
| 5926 | |
| 5927 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5928 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5929 | << /*RISC-V*/ 2 << 0; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5930 | return; |
| 5931 | } |
| 5932 | |
| 5933 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
Aaron Ballman | b0d74bf | 2019-01-23 18:02:17 +0000 | [diff] [blame] | 5934 | S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid) |
| 5935 | << /*RISC-V*/ 2 << 1; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5936 | return; |
| 5937 | } |
| 5938 | |
| 5939 | RISCVInterruptAttr::InterruptType Kind; |
| 5940 | if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5941 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5942 | << ArgLoc; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5943 | return; |
| 5944 | } |
| 5945 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 5946 | D->addAttr(::new (S.Context) RISCVInterruptAttr(S.Context, AL, Kind)); |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5947 | } |
| 5948 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5949 | static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5950 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5951 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5952 | case llvm::Triple::msp430: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5953 | handleMSP430InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5954 | break; |
| 5955 | case llvm::Triple::mipsel: |
| 5956 | case llvm::Triple::mips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5957 | handleMipsInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5958 | break; |
| 5959 | case llvm::Triple::x86: |
| 5960 | case llvm::Triple::x86_64: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5961 | handleAnyX86InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5962 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5963 | case llvm::Triple::avr: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5964 | handleAVRInterruptAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5965 | break; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5966 | case llvm::Triple::riscv32: |
| 5967 | case llvm::Triple::riscv64: |
| 5968 | handleRISCVInterruptAttr(S, D, AL); |
| 5969 | break; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5970 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5971 | handleARMInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5972 | break; |
| 5973 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5976 | static bool |
| 5977 | checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr, |
| 5978 | const AMDGPUFlatWorkGroupSizeAttr &Attr) { |
| 5979 | // Accept template arguments for now as they depend on something else. |
| 5980 | // We'll get to check them when they eventually get instantiated. |
| 5981 | if (MinExpr->isValueDependent() || MaxExpr->isValueDependent()) |
| 5982 | return false; |
| 5983 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5984 | uint32_t Min = 0; |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5985 | if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0)) |
| 5986 | return true; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5987 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5988 | uint32_t Max = 0; |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5989 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1)) |
| 5990 | return true; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5991 | |
| 5992 | if (Min == 0 && Max != 0) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5993 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5994 | << &Attr << 0; |
| 5995 | return true; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5996 | } |
| 5997 | if (Min > Max) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 5998 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 5999 | << &Attr << 1; |
| 6000 | return true; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6001 | } |
| 6002 | |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6003 | return false; |
| 6004 | } |
| 6005 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6006 | void Sema::addAMDGPUFlatWorkGroupSizeAttr(Decl *D, |
| 6007 | const AttributeCommonInfo &CI, |
| 6008 | Expr *MinExpr, Expr *MaxExpr) { |
| 6009 | AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr); |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6010 | |
| 6011 | if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr, MaxExpr, TmpAttr)) |
| 6012 | return; |
| 6013 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6014 | D->addAttr(::new (Context) |
| 6015 | AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr)); |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
| 6019 | const ParsedAttr &AL) { |
| 6020 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 6021 | Expr *MaxExpr = AL.getArgAsExpr(1); |
| 6022 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6023 | S.addAMDGPUFlatWorkGroupSizeAttr(D, AL, MinExpr, MaxExpr); |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
| 6026 | static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr, |
| 6027 | Expr *MaxExpr, |
| 6028 | const AMDGPUWavesPerEUAttr &Attr) { |
| 6029 | if (S.DiagnoseUnexpandedParameterPack(MinExpr) || |
| 6030 | (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr))) |
| 6031 | return true; |
| 6032 | |
| 6033 | // Accept template arguments for now as they depend on something else. |
| 6034 | // We'll get to check them when they eventually get instantiated. |
| 6035 | if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent())) |
| 6036 | return false; |
| 6037 | |
| 6038 | uint32_t Min = 0; |
| 6039 | if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0)) |
| 6040 | return true; |
| 6041 | |
| 6042 | uint32_t Max = 0; |
| 6043 | if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1)) |
| 6044 | return true; |
| 6045 | |
| 6046 | if (Min == 0 && Max != 0) { |
| 6047 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 6048 | << &Attr << 0; |
| 6049 | return true; |
| 6050 | } |
| 6051 | if (Max != 0 && Min > Max) { |
| 6052 | S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid) |
| 6053 | << &Attr << 1; |
| 6054 | return true; |
| 6055 | } |
| 6056 | |
| 6057 | return false; |
| 6058 | } |
| 6059 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6060 | void Sema::addAMDGPUWavesPerEUAttr(Decl *D, const AttributeCommonInfo &CI, |
| 6061 | Expr *MinExpr, Expr *MaxExpr) { |
| 6062 | AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr); |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6063 | |
| 6064 | if (checkAMDGPUWavesPerEUArguments(*this, MinExpr, MaxExpr, TmpAttr)) |
| 6065 | return; |
| 6066 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6067 | D->addAttr(::new (Context) |
| 6068 | AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr)); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6069 | } |
| 6070 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6071 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6072 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 6073 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
| 6074 | return; |
| 6075 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6076 | Expr *MinExpr = AL.getArgAsExpr(0); |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 6077 | Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6078 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6079 | S.addAMDGPUWavesPerEUAttr(D, AL, MinExpr, MaxExpr); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6080 | } |
| 6081 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6082 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6083 | uint32_t NumSGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6084 | Expr *NumSGPRExpr = AL.getArgAsExpr(0); |
| 6085 | if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6086 | return; |
| 6087 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6088 | D->addAttr(::new (S.Context) AMDGPUNumSGPRAttr(S.Context, AL, NumSGPR)); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6089 | } |
| 6090 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6091 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6092 | uint32_t NumVGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6093 | Expr *NumVGPRExpr = AL.getArgAsExpr(0); |
| 6094 | if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6095 | return; |
| 6096 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6097 | D->addAttr(::new (S.Context) AMDGPUNumVGPRAttr(S.Context, AL, NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6100 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6101 | const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6102 | // If we try to apply it to a function pointer, don't warn, but don't |
| 6103 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 6104 | // special about calling a force_align_arg_pointer function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6105 | const auto *VD = dyn_cast<ValueDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6106 | if (VD && VD->getType()->isFunctionPointerType()) |
| 6107 | return; |
| 6108 | // Also don't warn on function pointer typedefs. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6109 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6110 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 6111 | TD->getUnderlyingType()->isFunctionType())) |
| 6112 | return; |
| 6113 | // Attribute can only be applied to function types. |
| 6114 | if (!isa<FunctionDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6115 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6116 | << AL << ExpectedFunction; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6117 | return; |
| 6118 | } |
| 6119 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6120 | D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(S.Context, AL)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6123 | static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6124 | uint32_t Version; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6125 | Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
| 6126 | if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version)) |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6127 | return; |
| 6128 | |
| 6129 | // TODO: Investigate what happens with the next major version of MSVC. |
Reid Kleckner | 1a94d87 | 2018-12-17 23:16:43 +0000 | [diff] [blame] | 6130 | if (Version != LangOptions::MSVC2015 / 100) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6131 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6132 | << AL << Version << VersionExpr->getSourceRange(); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6133 | return; |
| 6134 | } |
| 6135 | |
Reid Kleckner | 1a94d87 | 2018-12-17 23:16:43 +0000 | [diff] [blame] | 6136 | // The attribute expects a "major" version number like 19, but new versions of |
| 6137 | // MSVC have moved to updating the "minor", or less significant numbers, so we |
| 6138 | // have to multiply by 100 now. |
| 6139 | Version *= 100; |
| 6140 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6141 | D->addAttr(::new (S.Context) LayoutVersionAttr(S.Context, AL, Version)); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6144 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, |
| 6145 | const AttributeCommonInfo &CI) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6146 | if (D->hasAttr<DLLExportAttr>()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6147 | Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6148 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6152 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6153 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6154 | return ::new (Context) DLLImportAttr(Context, CI); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6157 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, |
| 6158 | const AttributeCommonInfo &CI) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6159 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 6160 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6161 | D->dropAttr<DLLImportAttr>(); |
| 6162 | } |
| 6163 | |
| 6164 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6165 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6166 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6167 | return ::new (Context) DLLExportAttr(Context, CI); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6170 | static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 6171 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 6172 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6173 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A; |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 6174 | return; |
| 6175 | } |
| 6176 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6177 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6178 | if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport && |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 6179 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 6180 | // MinGW doesn't allow dllimport on inline functions. |
| 6181 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6182 | << A; |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 6183 | return; |
| 6184 | } |
| 6185 | } |
| 6186 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6187 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 6188 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 6189 | MD->getParent()->isLambda()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6190 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A; |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 6191 | return; |
| 6192 | } |
| 6193 | } |
| 6194 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6195 | Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6196 | ? (Attr *)S.mergeDLLExportAttr(D, A) |
| 6197 | : (Attr *)S.mergeDLLImportAttr(D, A); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6198 | if (NewAttr) |
| 6199 | D->addAttr(NewAttr); |
| 6200 | } |
| 6201 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6202 | MSInheritanceAttr * |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6203 | Sema::mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI, |
| 6204 | bool BestCase, |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 6205 | MSInheritanceModel Model) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6206 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 6207 | if (IA->getInheritanceModel() == Model) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6208 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6209 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 6210 | << 1 /*previous declaration*/; |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6211 | Diag(CI.getLoc(), diag::note_previous_ms_inheritance); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6212 | D->dropAttr<MSInheritanceAttr>(); |
| 6213 | } |
| 6214 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6215 | auto *RD = cast<CXXRecordDecl>(D); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6216 | if (RD->hasDefinition()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6217 | if (checkMSInheritanceAttrOnDefinition(RD, CI.getRange(), BestCase, |
Reid Kleckner | a9cc64e | 2019-11-15 18:49:32 -0800 | [diff] [blame] | 6218 | Model)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6219 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6220 | } |
| 6221 | } else { |
| 6222 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6223 | Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance) |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6224 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6225 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6226 | } |
| 6227 | if (RD->getDescribedClassTemplate()) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6228 | Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance) |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6229 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6230 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6231 | } |
| 6232 | } |
| 6233 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6234 | return ::new (Context) MSInheritanceAttr(Context, CI, BestCase); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6237 | static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6238 | // The capability attributes take a single string parameter for the name of |
| 6239 | // the capability they represent. The lockable attribute does not take any |
| 6240 | // parameters. However, semantically, both attributes represent the same |
| 6241 | // concept, and so they use the same semantic attribute. Eventually, the |
| 6242 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 6243 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 6244 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 6245 | // literal will be considered a "mutex." |
| 6246 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6247 | SourceLocation LiteralLoc; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6248 | if (AL.getKind() == ParsedAttr::AT_Capability && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6249 | !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6250 | return; |
| 6251 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6252 | D->addAttr(::new (S.Context) CapabilityAttr(S.Context, AL, N)); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6255 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 6256 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6257 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 6258 | return; |
| 6259 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6260 | D->addAttr(::new (S.Context) |
| 6261 | AssertCapabilityAttr(S.Context, AL, Args.data(), Args.size())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6262 | } |
| 6263 | |
| 6264 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6265 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6266 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6267 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6268 | return; |
| 6269 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6270 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(S.Context, AL, Args.data(), |
| 6271 | Args.size())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6275 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6276 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6277 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6278 | return; |
| 6279 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6280 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr( |
| 6281 | S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6285 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6286 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 6287 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6288 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6289 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6290 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr(S.Context, AL, Args.data(), |
| 6291 | Args.size())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6294 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6295 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6296 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6297 | return; |
| 6298 | |
| 6299 | // check that all arguments are lockable objects |
| 6300 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6301 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6302 | if (Args.empty()) |
| 6303 | return; |
| 6304 | |
| 6305 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6306 | RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size()); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6307 | |
| 6308 | D->addAttr(RCA); |
| 6309 | } |
| 6310 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6311 | static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6312 | if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6313 | if (NSD->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6314 | S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace); |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6315 | // Do not want to attach the attribute to the namespace because that will |
| 6316 | // cause confusing diagnostic reports for uses of declarations within the |
| 6317 | // namespace. |
| 6318 | return; |
| 6319 | } |
| 6320 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 6321 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6322 | // Handle the cases where the attribute has a text message. |
| 6323 | StringRef Str, Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6324 | if (AL.isArgExpr(0) && AL.getArgAsExpr(0) && |
| 6325 | !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6326 | return; |
| 6327 | |
| 6328 | // Only support a single optional message for Declspec and CXX11. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6329 | if (AL.isDeclspecAttribute() || AL.isCXX11Attribute()) |
| 6330 | checkAttributeAtMostNumArgs(S, AL, 1); |
| 6331 | else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) && |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 6332 | !S.checkStringLiteralArgumentAttr(AL, 1, Replacement)) |
| 6333 | return; |
| 6334 | |
| 6335 | if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope()) |
| 6336 | S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL; |
| 6337 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6338 | D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement)); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | static bool isGlobalVar(const Decl *D) { |
| 6342 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 6343 | return S->hasGlobalStorage(); |
| 6344 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6347 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6348 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6349 | return; |
| 6350 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 6351 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6352 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6353 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6354 | StringRef SanitizerName; |
| 6355 | SourceLocation LiteralLoc; |
| 6356 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6357 | if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6358 | return; |
| 6359 | |
Pierre Gousseau | ae5303d | 2019-03-01 10:05:15 +0000 | [diff] [blame] | 6360 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == |
| 6361 | SanitizerMask()) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6362 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 6363 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 6364 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6365 | << AL << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6366 | Sanitizers.push_back(SanitizerName); |
| 6367 | } |
| 6368 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6369 | D->addAttr(::new (S.Context) NoSanitizeAttr(S.Context, AL, Sanitizers.data(), |
| 6370 | Sanitizers.size())); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6374 | const ParsedAttr &AL) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6375 | StringRef AttrName = AL.getAttrName()->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 6376 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 6377 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 6378 | .Case("no_address_safety_analysis", "address") |
| 6379 | .Case("no_sanitize_address", "address") |
| 6380 | .Case("no_sanitize_thread", "thread") |
| 6381 | .Case("no_sanitize_memory", "memory"); |
| 6382 | if (isGlobalVar(D) && SanitizerName != "address") |
| 6383 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6384 | << AL << ExpectedFunction; |
Aaron Ballman | 31ca49b | 2019-05-21 17:24:49 +0000 | [diff] [blame] | 6385 | |
| 6386 | // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a |
| 6387 | // NoSanitizeAttr object; but we need to calculate the correct spelling list |
| 6388 | // index rather than incorrectly assume the index for NoSanitizeSpecificAttr |
| 6389 | // has the same spellings as the index for NoSanitizeAttr. We don't have a |
| 6390 | // general way to "translate" between the two, so this hack attempts to work |
| 6391 | // around the issue with hard-coded indicies. This is critical for calling |
| 6392 | // getSpelling() or prettyPrint() on the resulting semantic attribute object |
| 6393 | // without failing assertions. |
| 6394 | unsigned TranslatedSpellingIndex = 0; |
| 6395 | if (AL.isC2xAttribute() || AL.isCXX11Attribute()) |
| 6396 | TranslatedSpellingIndex = 1; |
| 6397 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6398 | AttributeCommonInfo Info = AL; |
| 6399 | Info.setAttributeSpellingListIndex(TranslatedSpellingIndex); |
| 6400 | D->addAttr(::new (S.Context) |
| 6401 | NoSanitizeAttr(S.Context, Info, &SanitizerName, 1)); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6402 | } |
| 6403 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6404 | static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6405 | if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 6406 | D->addAttr(Internal); |
| 6407 | } |
| 6408 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6409 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 6410 | if (S.LangOpts.OpenCLVersion != 200) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6411 | S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6412 | << AL << "2.0" << 0; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 6413 | else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6414 | S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL |
| 6415 | << "2.0"; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6418 | /// Handles semantic checking for features that are common to all attributes, |
| 6419 | /// such as checking whether a parameter was properly specified, or the correct |
| 6420 | /// number of arguments were passed, etc. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6421 | static bool handleCommonAttributeFeatures(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6422 | const ParsedAttr &AL) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6423 | // Several attributes carry different semantics than the parsing requires, so |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6424 | // those are opted out of the common argument checks. |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6425 | // |
| 6426 | // We also bail on unknown and ignored attributes because those are handled |
| 6427 | // as part of the target-specific handling logic. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6428 | if (AL.getKind() == ParsedAttr::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6429 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6430 | // Check whether the attribute requires specific language extensions to be |
| 6431 | // enabled. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6432 | if (!AL.diagnoseLangOpts(S)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6433 | return true; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6434 | // Check whether the attribute appertains to the given subject. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6435 | if (!AL.diagnoseAppertainsTo(S, D)) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6436 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6437 | if (AL.hasCustomParsing()) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 6438 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6439 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6440 | if (AL.getMinArgs() == AL.getMaxArgs()) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6441 | // If there are no optional arguments, then checking for the argument count |
| 6442 | // is trivial. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6443 | if (!checkAttributeNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6444 | return true; |
| 6445 | } else { |
| 6446 | // There are optional arguments, so checking is slightly more involved. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6447 | if (AL.getMinArgs() && |
| 6448 | !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6449 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6450 | else if (!AL.hasVariadicArg() && AL.getMaxArgs() && |
| 6451 | !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 6452 | return true; |
| 6453 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 6454 | |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 6455 | if (S.CheckAttrTarget(AL)) |
| 6456 | return true; |
| 6457 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6458 | return false; |
| 6459 | } |
| 6460 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6461 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6462 | if (D->isInvalidDecl()) |
| 6463 | return; |
| 6464 | |
| 6465 | // Check if there is only one access qualifier. |
| 6466 | if (D->hasAttr<OpenCLAccessAttr>()) { |
Andrew Savonichev | 05a15af | 2018-09-06 15:10:26 +0000 | [diff] [blame] | 6467 | if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() == |
| 6468 | AL.getSemanticSpelling()) { |
| 6469 | S.Diag(AL.getLoc(), diag::warn_duplicate_declspec) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6470 | << AL.getAttrName()->getName() << AL.getRange(); |
Andrew Savonichev | 05a15af | 2018-09-06 15:10:26 +0000 | [diff] [blame] | 6471 | } else { |
| 6472 | S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 6473 | << D->getSourceRange(); |
| 6474 | D->setInvalidDecl(true); |
| 6475 | return; |
| 6476 | } |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 6480 | // image object can be read and written. |
| 6481 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 6482 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 6483 | // qualifier is a compilation error. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6484 | if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6485 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6486 | if (AL.getAttrName()->getName().find("read_write") != StringRef::npos) { |
Anastasia Stulova | 2c4730d | 2019-02-15 12:07:57 +0000 | [diff] [blame] | 6487 | if ((!S.getLangOpts().OpenCLCPlusPlus && |
| 6488 | S.getLangOpts().OpenCLVersion < 200) || |
| 6489 | DeclTy->isPipeType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6490 | S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6491 | << AL << PDecl->getType() << DeclTy->isImageType(); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6492 | D->setInvalidDecl(true); |
| 6493 | return; |
| 6494 | } |
| 6495 | } |
| 6496 | } |
| 6497 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6498 | D->addAttr(::new (S.Context) OpenCLAccessAttr(S.Context, AL)); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
Mariya Podchishchaeva | c094e7d | 2019-11-06 17:35:50 +0300 | [diff] [blame] | 6501 | static void handleSYCLKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6502 | // The 'sycl_kernel' attribute applies only to function templates. |
| 6503 | const auto *FD = cast<FunctionDecl>(D); |
| 6504 | const FunctionTemplateDecl *FT = FD->getDescribedFunctionTemplate(); |
| 6505 | assert(FT && "Function template is expected"); |
| 6506 | |
| 6507 | // Function template must have at least two template parameters. |
| 6508 | const TemplateParameterList *TL = FT->getTemplateParameters(); |
| 6509 | if (TL->size() < 2) { |
| 6510 | S.Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_template_params); |
| 6511 | return; |
| 6512 | } |
| 6513 | |
| 6514 | // Template parameters must be typenames. |
| 6515 | for (unsigned I = 0; I < 2; ++I) { |
| 6516 | const NamedDecl *TParam = TL->getParam(I); |
| 6517 | if (isa<NonTypeTemplateParmDecl>(TParam)) { |
| 6518 | S.Diag(FT->getLocation(), |
| 6519 | diag::warn_sycl_kernel_invalid_template_param_type); |
| 6520 | return; |
| 6521 | } |
| 6522 | } |
| 6523 | |
| 6524 | // Function must have at least one argument. |
| 6525 | if (getFunctionOrMethodNumParams(D) != 1) { |
| 6526 | S.Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_function_params); |
| 6527 | return; |
| 6528 | } |
| 6529 | |
| 6530 | // Function must return void. |
| 6531 | QualType RetTy = getFunctionOrMethodResultType(D); |
| 6532 | if (!RetTy->isVoidType()) { |
| 6533 | S.Diag(FT->getLocation(), diag::warn_sycl_kernel_return_type); |
| 6534 | return; |
| 6535 | } |
| 6536 | |
| 6537 | handleSimpleAttribute<SYCLKernelAttr>(S, D, AL); |
| 6538 | } |
| 6539 | |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6540 | static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6541 | if (!cast<VarDecl>(D)->hasGlobalStorage()) { |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6542 | S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var) |
| 6543 | << (A.getKind() == ParsedAttr::AT_AlwaysDestroy); |
| 6544 | return; |
| 6545 | } |
| 6546 | |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6547 | if (A.getKind() == ParsedAttr::AT_AlwaysDestroy) |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6548 | handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A); |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6549 | else |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6550 | handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A); |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 6553 | static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6554 | assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic && |
| 6555 | "uninitialized is only valid on automatic duration variables"); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 6556 | D->addAttr(::new (S.Context) UninitializedAttr(S.Context, AL)); |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 6559 | static bool tryMakeVariablePseudoStrong(Sema &S, VarDecl *VD, |
| 6560 | bool DiagnoseFailure) { |
| 6561 | QualType Ty = VD->getType(); |
| 6562 | if (!Ty->isObjCRetainableType()) { |
| 6563 | if (DiagnoseFailure) { |
| 6564 | S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6565 | << 0; |
| 6566 | } |
| 6567 | return false; |
| 6568 | } |
| 6569 | |
| 6570 | Qualifiers::ObjCLifetime LifetimeQual = Ty.getQualifiers().getObjCLifetime(); |
| 6571 | |
| 6572 | // Sema::inferObjCARCLifetime must run after processing decl attributes |
| 6573 | // (because __block lowers to an attribute), so if the lifetime hasn't been |
| 6574 | // explicitly specified, infer it locally now. |
| 6575 | if (LifetimeQual == Qualifiers::OCL_None) |
| 6576 | LifetimeQual = Ty->getObjCARCImplicitLifetime(); |
| 6577 | |
| 6578 | // The attributes only really makes sense for __strong variables; ignore any |
| 6579 | // attempts to annotate a parameter with any other lifetime qualifier. |
| 6580 | if (LifetimeQual != Qualifiers::OCL_Strong) { |
| 6581 | if (DiagnoseFailure) { |
| 6582 | S.Diag(VD->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6583 | << 1; |
| 6584 | } |
| 6585 | return false; |
| 6586 | } |
| 6587 | |
| 6588 | // Tampering with the type of a VarDecl here is a bit of a hack, but we need |
| 6589 | // to ensure that the variable is 'const' so that we can error on |
| 6590 | // modification, which can otherwise over-release. |
| 6591 | VD->setType(Ty.withConst()); |
| 6592 | VD->setARCPseudoStrong(true); |
| 6593 | return true; |
| 6594 | } |
| 6595 | |
| 6596 | static void handleObjCExternallyRetainedAttr(Sema &S, Decl *D, |
| 6597 | const ParsedAttr &AL) { |
| 6598 | if (auto *VD = dyn_cast<VarDecl>(D)) { |
| 6599 | assert(!isa<ParmVarDecl>(VD) && "should be diagnosed automatically"); |
| 6600 | if (!VD->hasLocalStorage()) { |
| 6601 | S.Diag(D->getBeginLoc(), diag::warn_ignored_objc_externally_retained) |
| 6602 | << 0; |
| 6603 | return; |
| 6604 | } |
| 6605 | |
| 6606 | if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true)) |
| 6607 | return; |
| 6608 | |
| 6609 | handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL); |
| 6610 | return; |
| 6611 | } |
| 6612 | |
| 6613 | // If D is a function-like declaration (method, block, or function), then we |
| 6614 | // make every parameter psuedo-strong. |
Erik Pilkington | 2e4f5e6 | 2020-02-28 15:24:23 -0800 | [diff] [blame] | 6615 | unsigned NumParams = |
| 6616 | hasFunctionProto(D) ? getFunctionOrMethodNumParams(D) : 0; |
| 6617 | for (unsigned I = 0; I != NumParams; ++I) { |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 6618 | auto *PVD = const_cast<ParmVarDecl *>(getFunctionOrMethodParam(D, I)); |
| 6619 | QualType Ty = PVD->getType(); |
| 6620 | |
| 6621 | // If a user wrote a parameter with __strong explicitly, then assume they |
| 6622 | // want "real" strong semantics for that parameter. This works because if |
| 6623 | // the parameter was written with __strong, then the strong qualifier will |
| 6624 | // be non-local. |
| 6625 | if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() == |
| 6626 | Qualifiers::OCL_Strong) |
| 6627 | continue; |
| 6628 | |
| 6629 | tryMakeVariablePseudoStrong(S, PVD, /*DiagnoseFailure=*/false); |
| 6630 | } |
| 6631 | handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL); |
| 6632 | } |
| 6633 | |
Artem Dergachev | c333d77 | 2019-02-21 00:01:02 +0000 | [diff] [blame] | 6634 | static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6635 | // Check that the return type is a `typedef int kern_return_t` or a typedef |
| 6636 | // around it, because otherwise MIG convention checks make no sense. |
| 6637 | // BlockDecl doesn't store a return type, so it's annoying to check, |
| 6638 | // so let's skip it for now. |
| 6639 | if (!isa<BlockDecl>(D)) { |
| 6640 | QualType T = getFunctionOrMethodResultType(D); |
| 6641 | bool IsKernReturnT = false; |
| 6642 | while (const auto *TT = T->getAs<TypedefType>()) { |
| 6643 | IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t"); |
| 6644 | T = TT->desugar(); |
| 6645 | } |
| 6646 | if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) { |
| 6647 | S.Diag(D->getBeginLoc(), |
| 6648 | diag::warn_mig_server_routine_does_not_return_kern_return_t); |
| 6649 | return; |
| 6650 | } |
| 6651 | } |
| 6652 | |
| 6653 | handleSimpleAttribute<MIGServerRoutineAttr>(S, D, AL); |
| 6654 | } |
| 6655 | |
Reid Kleckner | 1181c9f | 2019-03-25 23:20:18 +0000 | [diff] [blame] | 6656 | static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6657 | // Warn if the return type is not a pointer or reference type. |
| 6658 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 6659 | QualType RetTy = FD->getReturnType(); |
| 6660 | if (!RetTy->isPointerType() && !RetTy->isReferenceType()) { |
| 6661 | S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer) |
| 6662 | << AL.getRange() << RetTy; |
| 6663 | return; |
| 6664 | } |
| 6665 | } |
| 6666 | |
| 6667 | handleSimpleAttribute<MSAllocatorAttr>(S, D, AL); |
| 6668 | } |
| 6669 | |
Gabor Horvath | fe17b30 | 2019-12-04 16:12:50 -0800 | [diff] [blame] | 6670 | static void handeAcquireHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6671 | if (AL.isUsedAsTypeAttr()) |
| 6672 | return; |
| 6673 | // Warn if the parameter is definitely not an output parameter. |
| 6674 | if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) { |
| 6675 | if (PVD->getType()->isIntegerType()) { |
| 6676 | S.Diag(AL.getLoc(), diag::err_attribute_output_parameter) |
| 6677 | << AL.getRange(); |
| 6678 | return; |
| 6679 | } |
| 6680 | } |
| 6681 | StringRef Argument; |
| 6682 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument)) |
| 6683 | return; |
| 6684 | D->addAttr(AcquireHandleAttr::Create(S.Context, Argument, AL)); |
| 6685 | } |
| 6686 | |
| 6687 | template<typename Attr> |
| 6688 | static void handleHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6689 | StringRef Argument; |
| 6690 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument)) |
| 6691 | return; |
| 6692 | D->addAttr(Attr::Create(S.Context, Argument, AL)); |
| 6693 | } |
| 6694 | |
Andrew Paverd | bdd88b7 | 2020-01-10 11:08:18 +0000 | [diff] [blame] | 6695 | static void handleCFGuardAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 6696 | // The guard attribute takes a single identifier argument. |
| 6697 | |
| 6698 | if (!AL.isArgIdent(0)) { |
| 6699 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 6700 | << AL << AANT_ArgumentIdentifier; |
| 6701 | return; |
| 6702 | } |
| 6703 | |
| 6704 | CFGuardAttr::GuardArg Arg; |
| 6705 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
| 6706 | if (!CFGuardAttr::ConvertStrToGuardArg(II->getName(), Arg)) { |
| 6707 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
| 6708 | return; |
| 6709 | } |
| 6710 | |
| 6711 | D->addAttr(::new (S.Context) CFGuardAttr(S.Context, AL, Arg)); |
| 6712 | } |
| 6713 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 6714 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6715 | // Top Level Sema Entry Points |
| 6716 | //===----------------------------------------------------------------------===// |
| 6717 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6718 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 6719 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 6720 | /// silently ignore it if a GNU attribute. |
| 6721 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6722 | const ParsedAttr &AL, |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6723 | bool IncludeCXX11Attributes) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6724 | if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6725 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 6726 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6727 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 6728 | // instead. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6729 | if (AL.isCXX11Attribute() && !IncludeCXX11Attributes) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6730 | return; |
| 6731 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6732 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 6733 | // which do not apply to the current target architecture are treated as |
| 6734 | // though they were unknown attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6735 | if (AL.getKind() == ParsedAttr::UnknownAttribute || |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6736 | !AL.existsInTarget(S.Context.getTargetInfo())) { |
Simon Pilgrim | fc0ff61 | 2018-12-17 12:17:37 +0000 | [diff] [blame] | 6737 | S.Diag(AL.getLoc(), |
| 6738 | AL.isDeclspecAttribute() |
| 6739 | ? (unsigned)diag::warn_unhandled_ms_attribute_ignored |
| 6740 | : (unsigned)diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6741 | << AL; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6742 | return; |
| 6743 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6744 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6745 | if (handleCommonAttributeFeatures(S, D, AL)) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6746 | return; |
| 6747 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6748 | switch (AL.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6749 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6750 | if (!AL.isStmtAttr()) { |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 6751 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6752 | assert(AL.isTypeAttr() && "Non-type attribute not handled"); |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 6753 | break; |
| 6754 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6755 | S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6756 | << AL << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6757 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6758 | case ParsedAttr::AT_Interrupt: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6759 | handleInterruptAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6760 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6761 | case ParsedAttr::AT_X86ForceAlignArgPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6762 | handleX86ForceAlignArgPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6763 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6764 | case ParsedAttr::AT_DLLExport: |
| 6765 | case ParsedAttr::AT_DLLImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6766 | handleDLLAttr(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_Mips16: |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6769 | handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6770 | MipsInterruptAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6771 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6772 | case ParsedAttr::AT_NoMips16: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6773 | handleSimpleAttribute<NoMips16Attr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6774 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6775 | case ParsedAttr::AT_MicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6776 | handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6777 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6778 | case ParsedAttr::AT_NoMicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6779 | handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6780 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6781 | case ParsedAttr::AT_MipsLongCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6782 | handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6783 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6784 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6785 | case ParsedAttr::AT_MipsShortCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6786 | handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6787 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6788 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6789 | case ParsedAttr::AT_AMDGPUFlatWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6790 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6791 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6792 | case ParsedAttr::AT_AMDGPUWavesPerEU: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6793 | handleAMDGPUWavesPerEUAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6794 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6795 | case ParsedAttr::AT_AMDGPUNumSGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6796 | handleAMDGPUNumSGPRAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6797 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6798 | case ParsedAttr::AT_AMDGPUNumVGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6799 | handleAMDGPUNumVGPRAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6800 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6801 | case ParsedAttr::AT_AVRSignal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6802 | handleAVRSignalAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 6803 | break; |
Yonghong Song | 4e2ce22 | 2019-11-01 22:16:59 -0700 | [diff] [blame] | 6804 | case ParsedAttr::AT_BPFPreserveAccessIndex: |
| 6805 | handleBPFPreserveAccessIndexAttr(S, D, AL); |
| 6806 | break; |
Sam Clegg | 881d877 | 2019-11-05 10:15:56 -0800 | [diff] [blame] | 6807 | case ParsedAttr::AT_WebAssemblyExportName: |
| 6808 | handleWebAssemblyExportNameAttr(S, D, AL); |
| 6809 | break; |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 6810 | case ParsedAttr::AT_WebAssemblyImportModule: |
| 6811 | handleWebAssemblyImportModuleAttr(S, D, AL); |
| 6812 | break; |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 6813 | case ParsedAttr::AT_WebAssemblyImportName: |
| 6814 | handleWebAssemblyImportNameAttr(S, D, AL); |
| 6815 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6816 | case ParsedAttr::AT_IBAction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6817 | handleSimpleAttribute<IBActionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6818 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6819 | case ParsedAttr::AT_IBOutlet: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6820 | handleIBOutlet(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_IBOutletCollection: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6823 | handleIBOutletCollection(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6824 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6825 | case ParsedAttr::AT_IFunc: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6826 | handleIFuncAttr(S, D, AL); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 6827 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6828 | case ParsedAttr::AT_Alias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6829 | handleAliasAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6830 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6831 | case ParsedAttr::AT_Aligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6832 | handleAlignedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6833 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6834 | case ParsedAttr::AT_AlignValue: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6835 | handleAlignValueAttr(S, D, AL); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 6836 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6837 | case ParsedAttr::AT_AllocSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6838 | handleAllocSizeAttr(S, D, AL); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6839 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6840 | case ParsedAttr::AT_AlwaysInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6841 | handleAlwaysInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6842 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6843 | case ParsedAttr::AT_Artificial: |
Aaron Ballman | 736c09b | 2018-02-15 16:28:10 +0000 | [diff] [blame] | 6844 | handleSimpleAttribute<ArtificialAttr>(S, D, AL); |
Erich Keane | 293a055 | 2018-02-14 00:14:07 +0000 | [diff] [blame] | 6845 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6846 | case ParsedAttr::AT_AnalyzerNoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6847 | handleAnalyzerNoReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6848 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6849 | case ParsedAttr::AT_TLSModel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6850 | handleTLSModelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6851 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6852 | case ParsedAttr::AT_Annotate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6853 | handleAnnotateAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6854 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6855 | case ParsedAttr::AT_Availability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6856 | handleAvailabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6857 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6858 | case ParsedAttr::AT_CarriesDependency: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6859 | handleDependencyAttr(S, scope, D, AL); |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6860 | break; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 6861 | case ParsedAttr::AT_CPUDispatch: |
| 6862 | case ParsedAttr::AT_CPUSpecific: |
| 6863 | handleCPUSpecificAttr(S, D, AL); |
| 6864 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6865 | case ParsedAttr::AT_Common: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6866 | handleCommonAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6867 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6868 | case ParsedAttr::AT_CUDAConstant: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6869 | handleConstantAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6870 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6871 | case ParsedAttr::AT_PassObjectSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6872 | handlePassObjectSizeAttr(S, D, AL); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6873 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6874 | case ParsedAttr::AT_Constructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6875 | handleConstructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6876 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6877 | case ParsedAttr::AT_CXX11NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6878 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6879 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6880 | case ParsedAttr::AT_Deprecated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6881 | handleDeprecatedAttr(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6882 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6883 | case ParsedAttr::AT_Destructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6884 | handleDestructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6885 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6886 | case ParsedAttr::AT_EnableIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6887 | handleEnableIfAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6888 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6889 | case ParsedAttr::AT_DiagnoseIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6890 | handleDiagnoseIfAttr(S, D, AL); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6891 | break; |
Guillaume Chatelet | 98f3151 | 2019-09-25 11:31:28 +0200 | [diff] [blame] | 6892 | case ParsedAttr::AT_NoBuiltin: |
| 6893 | handleNoBuiltinAttr(S, D, AL); |
| 6894 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6895 | case ParsedAttr::AT_ExtVectorType: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6896 | handleExtVectorTypeAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6897 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6898 | case ParsedAttr::AT_ExternalSourceSymbol: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6899 | handleExternalSourceSymbolAttr(S, D, AL); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 6900 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6901 | case ParsedAttr::AT_MinSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6902 | handleMinSizeAttr(S, D, AL); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6903 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6904 | case ParsedAttr::AT_OptimizeNone: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6905 | handleOptimizeNoneAttr(S, D, AL); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 6906 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6907 | case ParsedAttr::AT_FlagEnum: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6908 | handleSimpleAttribute<FlagEnumAttr>(S, D, AL); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 6909 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6910 | case ParsedAttr::AT_EnumExtensibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6911 | handleEnumExtensibilityAttr(S, D, AL); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 6912 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6913 | case ParsedAttr::AT_Flatten: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6914 | handleSimpleAttribute<FlattenAttr>(S, D, AL); |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 6915 | break; |
Mariya Podchishchaeva | c094e7d | 2019-11-06 17:35:50 +0300 | [diff] [blame] | 6916 | case ParsedAttr::AT_SYCLKernel: |
| 6917 | handleSYCLKernelAttr(S, D, AL); |
| 6918 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6919 | case ParsedAttr::AT_Format: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6920 | handleFormatAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6921 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6922 | case ParsedAttr::AT_FormatArg: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6923 | handleFormatArgAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6924 | break; |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 6925 | case ParsedAttr::AT_Callback: |
| 6926 | handleCallbackAttr(S, D, AL); |
| 6927 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6928 | case ParsedAttr::AT_CUDAGlobal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6929 | handleGlobalAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6930 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6931 | case ParsedAttr::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6932 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6933 | AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6934 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6935 | case ParsedAttr::AT_CUDAHost: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6936 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6937 | break; |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 6938 | case ParsedAttr::AT_HIPPinnedShadow: |
| 6939 | handleSimpleAttributeWithExclusions<HIPPinnedShadowAttr, CUDADeviceAttr, |
| 6940 | CUDAConstantAttr>(S, D, AL); |
| 6941 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6942 | case ParsedAttr::AT_GNUInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6943 | handleGNUInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6944 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6945 | case ParsedAttr::AT_CUDALaunchBounds: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6946 | handleLaunchBoundsAttr(S, D, AL); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 6947 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6948 | case ParsedAttr::AT_Restrict: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6949 | handleRestrictAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6950 | break; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6951 | case ParsedAttr::AT_LifetimeBound: |
| 6952 | handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL); |
| 6953 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6954 | case ParsedAttr::AT_MayAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6955 | handleSimpleAttribute<MayAliasAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6956 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6957 | case ParsedAttr::AT_Mode: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6958 | handleModeAttr(S, D, AL); |
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_NoAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6961 | handleSimpleAttribute<NoAliasAttr>(S, D, AL); |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 6962 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6963 | case ParsedAttr::AT_NoCommon: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6964 | handleSimpleAttribute<NoCommonAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6965 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6966 | case ParsedAttr::AT_NoSplitStack: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6967 | handleSimpleAttribute<NoSplitStackAttr>(S, D, AL); |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 6968 | break; |
Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 6969 | case ParsedAttr::AT_NoUniqueAddress: |
| 6970 | handleSimpleAttribute<NoUniqueAddressAttr>(S, D, AL); |
| 6971 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6972 | case ParsedAttr::AT_NonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6973 | if (auto *PVD = dyn_cast<ParmVarDecl>(D)) |
| 6974 | handleNonNullAttrParameter(S, PVD, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6975 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6976 | handleNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6977 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6978 | case ParsedAttr::AT_ReturnsNonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6979 | handleReturnsNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6980 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6981 | case ParsedAttr::AT_NoEscape: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6982 | handleNoEscapeAttr(S, D, AL); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 6983 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6984 | case ParsedAttr::AT_AssumeAligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6985 | handleAssumeAlignedAttr(S, D, AL); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 6986 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6987 | case ParsedAttr::AT_AllocAlign: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6988 | handleAllocAlignAttr(S, D, AL); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 6989 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6990 | case ParsedAttr::AT_Overloadable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6991 | handleSimpleAttribute<OverloadableAttr>(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_Ownership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6994 | handleOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6995 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6996 | case ParsedAttr::AT_Cold: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6997 | handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(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_Hot: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 7000 | handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7001 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7002 | case ParsedAttr::AT_Naked: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7003 | handleNakedAttr(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_NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7006 | handleNoReturnAttr(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_AnyX86NoCfCheck: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 7009 | handleNoCfCheckAttr(S, D, AL); |
| 7010 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7011 | case ParsedAttr::AT_NoThrow: |
Erich Keane | d02f4a1 | 2019-05-30 17:31:54 +0000 | [diff] [blame] | 7012 | if (!AL.isUsedAsTypeAttr()) |
| 7013 | handleSimpleAttribute<NoThrowAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7014 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7015 | case ParsedAttr::AT_CUDAShared: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7016 | handleSharedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7017 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7018 | case ParsedAttr::AT_VecReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7019 | handleVecReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7020 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7021 | case ParsedAttr::AT_ObjCOwnership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7022 | handleObjCOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7023 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7024 | case ParsedAttr::AT_ObjCPreciseLifetime: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7025 | handleObjCPreciseLifetimeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7026 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7027 | case ParsedAttr::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7028 | handleObjCReturnsInnerPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7029 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7030 | case ParsedAttr::AT_ObjCRequiresSuper: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7031 | handleObjCRequiresSuperAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7032 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7033 | case ParsedAttr::AT_ObjCBridge: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7034 | handleObjCBridgeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7035 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7036 | case ParsedAttr::AT_ObjCBridgeMutable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7037 | handleObjCBridgeMutableAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7038 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7039 | case ParsedAttr::AT_ObjCBridgeRelated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7040 | handleObjCBridgeRelatedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7041 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7042 | case ParsedAttr::AT_ObjCDesignatedInitializer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7043 | handleObjCDesignatedInitializer(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7044 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7045 | case ParsedAttr::AT_ObjCRuntimeName: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7046 | handleObjCRuntimeName(S, D, AL); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 7047 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7048 | case ParsedAttr::AT_ObjCRuntimeVisible: |
| 7049 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL); |
| 7050 | break; |
| 7051 | case ParsedAttr::AT_ObjCBoxable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7052 | handleObjCBoxable(S, D, AL); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 7053 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7054 | case ParsedAttr::AT_CFAuditedTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 7055 | handleSimpleAttributeWithExclusions<CFAuditedTransferAttr, |
| 7056 | CFUnknownTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7057 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7058 | case ParsedAttr::AT_CFUnknownTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 7059 | handleSimpleAttributeWithExclusions<CFUnknownTransferAttr, |
| 7060 | CFAuditedTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7061 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7062 | case ParsedAttr::AT_CFConsumed: |
| 7063 | case ParsedAttr::AT_NSConsumed: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 7064 | case ParsedAttr::AT_OSConsumed: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 7065 | S.AddXConsumedAttr(D, AL, parsedAttrToRetainOwnershipKind(AL), |
| 7066 | /*IsTemplateInstantiation=*/false); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7067 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7068 | case ParsedAttr::AT_NSConsumesSelf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7069 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7070 | break; |
George Karpenkov | da2c77f | 2018-12-06 22:06:59 +0000 | [diff] [blame] | 7071 | case ParsedAttr::AT_OSConsumesThis: |
| 7072 | handleSimpleAttribute<OSConsumesThisAttr>(S, D, AL); |
| 7073 | break; |
George Karpenkov | 3a50a9f | 2019-01-11 18:02:08 +0000 | [diff] [blame] | 7074 | case ParsedAttr::AT_OSReturnsRetainedOnZero: |
| 7075 | handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnZeroAttr>( |
| 7076 | S, D, AL, isValidOSObjectOutParameter(D), |
| 7077 | diag::warn_ns_attribute_wrong_parameter_type, |
| 7078 | /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange()); |
| 7079 | break; |
| 7080 | case ParsedAttr::AT_OSReturnsRetainedOnNonZero: |
| 7081 | handleSimpleAttributeOrDiagnose<OSReturnsRetainedOnNonZeroAttr>( |
| 7082 | S, D, AL, isValidOSObjectOutParameter(D), |
| 7083 | diag::warn_ns_attribute_wrong_parameter_type, |
| 7084 | /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange()); |
| 7085 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7086 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 7087 | case ParsedAttr::AT_NSReturnsNotRetained: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7088 | case ParsedAttr::AT_NSReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 7089 | case ParsedAttr::AT_CFReturnsNotRetained: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7090 | case ParsedAttr::AT_CFReturnsRetained: |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 7091 | case ParsedAttr::AT_OSReturnsNotRetained: |
| 7092 | case ParsedAttr::AT_OSReturnsRetained: |
| 7093 | handleXReturnsXRetainedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7094 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7095 | case ParsedAttr::AT_WorkGroupSizeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7096 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7097 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7098 | case ParsedAttr::AT_ReqdWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7099 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7100 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7101 | case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7102 | handleSubGroupSize(S, D, AL); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 7103 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7104 | case ParsedAttr::AT_VecTypeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7105 | handleVecTypeHint(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7106 | break; |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 7107 | case ParsedAttr::AT_ConstInit: |
| 7108 | handleSimpleAttribute<ConstInitAttr>(S, D, AL); |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 7109 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7110 | case ParsedAttr::AT_InitPriority: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7111 | handleInitPriorityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7112 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7113 | case ParsedAttr::AT_Packed: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7114 | handlePackedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7115 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7116 | case ParsedAttr::AT_Section: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7117 | handleSectionAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7118 | break; |
Zola Bridges | cbac3ad | 2018-11-27 19:56:46 +0000 | [diff] [blame] | 7119 | case ParsedAttr::AT_SpeculativeLoadHardening: |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 7120 | handleSimpleAttributeWithExclusions<SpeculativeLoadHardeningAttr, |
| 7121 | NoSpeculativeLoadHardeningAttr>(S, D, |
| 7122 | AL); |
| 7123 | break; |
| 7124 | case ParsedAttr::AT_NoSpeculativeLoadHardening: |
| 7125 | handleSimpleAttributeWithExclusions<NoSpeculativeLoadHardeningAttr, |
| 7126 | SpeculativeLoadHardeningAttr>(S, D, AL); |
Zola Bridges | cbac3ad | 2018-11-27 19:56:46 +0000 | [diff] [blame] | 7127 | break; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 7128 | case ParsedAttr::AT_CodeSeg: |
| 7129 | handleCodeSegAttr(S, D, AL); |
| 7130 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7131 | case ParsedAttr::AT_Target: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7132 | handleTargetAttr(S, D, AL); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 7133 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7134 | case ParsedAttr::AT_MinVectorWidth: |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 7135 | handleMinVectorWidthAttr(S, D, AL); |
| 7136 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7137 | case ParsedAttr::AT_Unavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7138 | handleAttrWithMessage<UnavailableAttr>(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 7139 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7140 | case ParsedAttr::AT_ArcWeakrefUnavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7141 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7142 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7143 | case ParsedAttr::AT_ObjCRootClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7144 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7145 | break; |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame] | 7146 | case ParsedAttr::AT_ObjCDirect: |
| 7147 | handleObjCDirectAttr(S, D, AL); |
| 7148 | break; |
| 7149 | case ParsedAttr::AT_ObjCDirectMembers: |
| 7150 | handleObjCDirectMembersAttr(S, D, AL); |
| 7151 | handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL); |
| 7152 | break; |
Joe Daniels | f7393d2 | 2019-02-04 23:32:55 +0000 | [diff] [blame] | 7153 | case ParsedAttr::AT_ObjCNonLazyClass: |
| 7154 | handleSimpleAttribute<ObjCNonLazyClassAttr>(S, D, AL); |
| 7155 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7156 | case ParsedAttr::AT_ObjCSubclassingRestricted: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7157 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL); |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 7158 | break; |
John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 7159 | case ParsedAttr::AT_ObjCClassStub: |
| 7160 | handleSimpleAttribute<ObjCClassStubAttr>(S, D, AL); |
| 7161 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7162 | case ParsedAttr::AT_ObjCExplicitProtocolImpl: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7163 | handleObjCSuppresProtocolAttr(S, D, AL); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 7164 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7165 | case ParsedAttr::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7166 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7167 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7168 | case ParsedAttr::AT_Unused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7169 | handleUnusedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7170 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7171 | case ParsedAttr::AT_ReturnsTwice: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7172 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7173 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7174 | case ParsedAttr::AT_NotTailCalled: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7175 | handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>( |
| 7176 | S, D, AL); |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 7177 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7178 | case ParsedAttr::AT_DisableTailCalls: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7179 | handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D, |
| 7180 | AL); |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 7181 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7182 | case ParsedAttr::AT_Used: |
Aaron Ballman | 1a3901c | 2018-03-03 21:02:09 +0000 | [diff] [blame] | 7183 | handleSimpleAttribute<UsedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7184 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7185 | case ParsedAttr::AT_Visibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7186 | handleVisibilityAttr(S, D, AL, false); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 7187 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7188 | case ParsedAttr::AT_TypeVisibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7189 | handleVisibilityAttr(S, D, AL, true); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 7190 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7191 | case ParsedAttr::AT_WarnUnused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7192 | handleSimpleAttribute<WarnUnusedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7193 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7194 | case ParsedAttr::AT_WarnUnusedResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7195 | handleWarnUnusedResult(S, D, AL); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 7196 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7197 | case ParsedAttr::AT_Weak: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7198 | handleSimpleAttribute<WeakAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7199 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7200 | case ParsedAttr::AT_WeakRef: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7201 | handleWeakRefAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7202 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7203 | case ParsedAttr::AT_WeakImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7204 | handleWeakImportAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7205 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7206 | case ParsedAttr::AT_TransparentUnion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7207 | handleTransparentUnionAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7208 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7209 | case ParsedAttr::AT_ObjCException: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7210 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7211 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7212 | case ParsedAttr::AT_ObjCMethodFamily: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7213 | handleObjCMethodFamilyAttr(S, D, AL); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 7214 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7215 | case ParsedAttr::AT_ObjCNSObject: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7216 | handleObjCNSObject(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7217 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7218 | case ParsedAttr::AT_ObjCIndependentClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7219 | handleObjCIndependentClass(S, D, AL); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 7220 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7221 | case ParsedAttr::AT_Blocks: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7222 | handleBlocksAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7223 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7224 | case ParsedAttr::AT_Sentinel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7225 | handleSentinelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7226 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7227 | case ParsedAttr::AT_Const: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7228 | handleSimpleAttribute<ConstAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7229 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7230 | case ParsedAttr::AT_Pure: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7231 | handleSimpleAttribute<PureAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7232 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7233 | case ParsedAttr::AT_Cleanup: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7234 | handleCleanupAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7235 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7236 | case ParsedAttr::AT_NoDebug: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7237 | handleNoDebugAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7238 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7239 | case ParsedAttr::AT_NoDuplicate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7240 | handleSimpleAttribute<NoDuplicateAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7241 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7242 | case ParsedAttr::AT_Convergent: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7243 | handleSimpleAttribute<ConvergentAttr>(S, D, AL); |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 7244 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7245 | case ParsedAttr::AT_NoInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7246 | handleSimpleAttribute<NoInlineAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7247 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7248 | case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7249 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7250 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7251 | case ParsedAttr::AT_NoStackProtector: |
Manoj Gupta | 4fbf84c | 2018-05-09 21:41:18 +0000 | [diff] [blame] | 7252 | // Interacts with -fstack-protector options. |
| 7253 | handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL); |
| 7254 | break; |
Peter Collingbourne | 0e497d1 | 2019-08-09 22:31:59 +0000 | [diff] [blame] | 7255 | case ParsedAttr::AT_CFICanonicalJumpTable: |
| 7256 | handleSimpleAttribute<CFICanonicalJumpTableAttr>(S, D, AL); |
| 7257 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7258 | case ParsedAttr::AT_StdCall: |
| 7259 | case ParsedAttr::AT_CDecl: |
| 7260 | case ParsedAttr::AT_FastCall: |
| 7261 | case ParsedAttr::AT_ThisCall: |
| 7262 | case ParsedAttr::AT_Pascal: |
| 7263 | case ParsedAttr::AT_RegCall: |
| 7264 | case ParsedAttr::AT_SwiftCall: |
| 7265 | case ParsedAttr::AT_VectorCall: |
| 7266 | case ParsedAttr::AT_MSABI: |
| 7267 | case ParsedAttr::AT_SysVABI: |
| 7268 | case ParsedAttr::AT_Pcs: |
| 7269 | case ParsedAttr::AT_IntelOclBicc: |
| 7270 | case ParsedAttr::AT_PreserveMost: |
| 7271 | case ParsedAttr::AT_PreserveAll: |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 7272 | case ParsedAttr::AT_AArch64VectorPcs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7273 | handleCallConvAttr(S, D, AL); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 7274 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7275 | case ParsedAttr::AT_Suppress: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7276 | handleSuppressAttr(S, D, AL); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 7277 | break; |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 7278 | case ParsedAttr::AT_Owner: |
| 7279 | case ParsedAttr::AT_Pointer: |
| 7280 | handleLifetimeCategoryAttr(S, D, AL); |
| 7281 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7282 | case ParsedAttr::AT_OpenCLKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7283 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7284 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7285 | case ParsedAttr::AT_OpenCLAccess: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7286 | handleOpenCLAccessAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7287 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7288 | case ParsedAttr::AT_OpenCLNoSVM: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7289 | handleOpenCLNoSVMAttr(S, D, AL); |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 7290 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7291 | case ParsedAttr::AT_SwiftContext: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 7292 | S.AddParameterABIAttr(D, AL, ParameterABI::SwiftContext); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 7293 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7294 | case ParsedAttr::AT_SwiftErrorResult: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 7295 | S.AddParameterABIAttr(D, AL, ParameterABI::SwiftErrorResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 7296 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7297 | case ParsedAttr::AT_SwiftIndirectResult: |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 7298 | S.AddParameterABIAttr(D, AL, ParameterABI::SwiftIndirectResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 7299 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7300 | case ParsedAttr::AT_InternalLinkage: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7301 | handleInternalLinkageAttr(S, D, AL); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 7302 | break; |
Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 7303 | case ParsedAttr::AT_ExcludeFromExplicitInstantiation: |
| 7304 | handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL); |
| 7305 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7306 | case ParsedAttr::AT_LTOVisibilityPublic: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7307 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL); |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 7308 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 7309 | |
| 7310 | // Microsoft attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7311 | case ParsedAttr::AT_EmptyBases: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7312 | handleSimpleAttribute<EmptyBasesAttr>(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 7313 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7314 | case ParsedAttr::AT_LayoutVersion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7315 | handleLayoutVersion(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 7316 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7317 | case ParsedAttr::AT_TrivialABI: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7318 | handleSimpleAttribute<TrivialABIAttr>(S, D, AL); |
Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 7319 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7320 | case ParsedAttr::AT_MSNoVTable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7321 | handleSimpleAttribute<MSNoVTableAttr>(S, D, AL); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 7322 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7323 | case ParsedAttr::AT_MSStruct: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7324 | handleSimpleAttribute<MSStructAttr>(S, D, AL); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 7325 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7326 | case ParsedAttr::AT_Uuid: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7327 | handleUuidAttr(S, D, AL); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 7328 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7329 | case ParsedAttr::AT_MSInheritance: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7330 | handleMSInheritanceAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7331 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7332 | case ParsedAttr::AT_SelectAny: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7333 | handleSimpleAttribute<SelectAnyAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7334 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7335 | case ParsedAttr::AT_Thread: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7336 | handleDeclspecThreadAttr(S, D, AL); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 7337 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 7338 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7339 | case ParsedAttr::AT_AbiTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7340 | handleAbiTagAttr(S, D, AL); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 7341 | break; |
Andrew Paverd | bdd88b7 | 2020-01-10 11:08:18 +0000 | [diff] [blame] | 7342 | case ParsedAttr::AT_CFGuard: |
| 7343 | handleCFGuardAttr(S, D, AL); |
| 7344 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 7345 | |
| 7346 | // Thread safety attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7347 | case ParsedAttr::AT_AssertExclusiveLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7348 | handleAssertExclusiveLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 7349 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7350 | case ParsedAttr::AT_AssertSharedLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7351 | handleAssertSharedLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 7352 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7353 | case ParsedAttr::AT_GuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7354 | handleSimpleAttribute<GuardedVarAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7355 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7356 | case ParsedAttr::AT_PtGuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7357 | handlePtGuardedVarAttr(S, D, AL); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 7358 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7359 | case ParsedAttr::AT_ScopedLockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7360 | handleSimpleAttribute<ScopedLockableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7361 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7362 | case ParsedAttr::AT_NoSanitize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7363 | handleNoSanitizeAttr(S, D, AL); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 7364 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7365 | case ParsedAttr::AT_NoSanitizeSpecific: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7366 | handleNoSanitizeSpecificAttr(S, D, AL); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 7367 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7368 | case ParsedAttr::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7369 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 7370 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7371 | case ParsedAttr::AT_GuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7372 | handleGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7373 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7374 | case ParsedAttr::AT_PtGuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7375 | handlePtGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7376 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7377 | case ParsedAttr::AT_ExclusiveTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7378 | handleExclusiveTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7379 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7380 | case ParsedAttr::AT_LockReturned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7381 | handleLockReturnedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7382 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7383 | case ParsedAttr::AT_LocksExcluded: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7384 | handleLocksExcludedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7385 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7386 | case ParsedAttr::AT_SharedTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7387 | handleSharedTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7388 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7389 | case ParsedAttr::AT_AcquiredBefore: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7390 | handleAcquiredBeforeAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7391 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7392 | case ParsedAttr::AT_AcquiredAfter: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7393 | handleAcquiredAfterAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 7394 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 7395 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 7396 | // Capability analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7397 | case ParsedAttr::AT_Capability: |
| 7398 | case ParsedAttr::AT_Lockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7399 | handleCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7400 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7401 | case ParsedAttr::AT_RequiresCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7402 | handleRequiresCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7403 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 7404 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7405 | case ParsedAttr::AT_AssertCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7406 | handleAssertCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7407 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7408 | case ParsedAttr::AT_AcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7409 | handleAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7410 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7411 | case ParsedAttr::AT_ReleaseCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7412 | handleReleaseCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7413 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7414 | case ParsedAttr::AT_TryAcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7415 | handleTryAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7416 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 7417 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 7418 | // Consumed analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7419 | case ParsedAttr::AT_Consumable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7420 | handleConsumableAttr(S, D, AL); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 7421 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7422 | case ParsedAttr::AT_ConsumableAutoCast: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7423 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7424 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7425 | case ParsedAttr::AT_ConsumableSetOnRead: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7426 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 7427 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7428 | case ParsedAttr::AT_CallableWhen: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7429 | handleCallableWhenAttr(S, D, AL); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 7430 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7431 | case ParsedAttr::AT_ParamTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7432 | handleParamTypestateAttr(S, D, AL); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 7433 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7434 | case ParsedAttr::AT_ReturnTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7435 | handleReturnTypestateAttr(S, D, AL); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 7436 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7437 | case ParsedAttr::AT_SetTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7438 | handleSetTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 7439 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7440 | case ParsedAttr::AT_TestTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7441 | handleTestTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 7442 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 7443 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7444 | // Type safety attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7445 | case ParsedAttr::AT_ArgumentWithTypeTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7446 | handleArgumentWithTypeTagAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7447 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7448 | case ParsedAttr::AT_TypeTagForDatatype: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7449 | handleTypeTagForDatatypeAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 7450 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7451 | case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 7452 | handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 7453 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7454 | case ParsedAttr::AT_RenderScriptKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7455 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 7456 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 7457 | // XRay attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7458 | case ParsedAttr::AT_XRayInstrument: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7459 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL); |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 7460 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7461 | case ParsedAttr::AT_XRayLogArgs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7462 | handleXRayLogArgsAttr(S, D, AL); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 7463 | break; |
Martin Bohme | 4e1293b | 2018-08-13 14:11:03 +0000 | [diff] [blame] | 7464 | |
Fangrui Song | a44c434 | 2020-01-04 15:39:19 -0800 | [diff] [blame] | 7465 | case ParsedAttr::AT_PatchableFunctionEntry: |
| 7466 | handlePatchableFunctionEntryAttr(S, D, AL); |
| 7467 | break; |
| 7468 | |
Martin Bohme | 4e1293b | 2018-08-13 14:11:03 +0000 | [diff] [blame] | 7469 | // Move semantics attribute. |
| 7470 | case ParsedAttr::AT_Reinitializes: |
| 7471 | handleSimpleAttribute<ReinitializesAttr>(S, D, AL); |
| 7472 | break; |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 7473 | |
| 7474 | case ParsedAttr::AT_AlwaysDestroy: |
| 7475 | case ParsedAttr::AT_NoDestroy: |
| 7476 | handleDestroyAttr(S, D, AL); |
| 7477 | break; |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 7478 | |
| 7479 | case ParsedAttr::AT_Uninitialized: |
| 7480 | handleUninitializedAttr(S, D, AL); |
| 7481 | break; |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 7482 | |
Jon Chesterfield | c45eaea | 2020-03-17 21:22:04 +0000 | [diff] [blame] | 7483 | case ParsedAttr::AT_LoaderUninitialized: |
| 7484 | handleSimpleAttribute<LoaderUninitializedAttr>(S, D, AL); |
| 7485 | break; |
| 7486 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 7487 | case ParsedAttr::AT_ObjCExternallyRetained: |
| 7488 | handleObjCExternallyRetainedAttr(S, D, AL); |
| 7489 | break; |
Erik Pilkington | e3cd735 | 2019-02-11 23:21:39 +0000 | [diff] [blame] | 7490 | |
Artem Dergachev | c333d77 | 2019-02-21 00:01:02 +0000 | [diff] [blame] | 7491 | case ParsedAttr::AT_MIGServerRoutine: |
| 7492 | handleMIGServerRoutineAttr(S, D, AL); |
| 7493 | break; |
Reid Kleckner | 1181c9f | 2019-03-25 23:20:18 +0000 | [diff] [blame] | 7494 | |
| 7495 | case ParsedAttr::AT_MSAllocator: |
| 7496 | handleMSAllocatorAttr(S, D, AL); |
| 7497 | break; |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 7498 | |
Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 7499 | case ParsedAttr::AT_ArmBuiltinAlias: |
| 7500 | handleArmBuiltinAliasAttr(S, D, AL); |
Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 7501 | break; |
Gabor Horvath | fe17b30 | 2019-12-04 16:12:50 -0800 | [diff] [blame] | 7502 | |
| 7503 | case ParsedAttr::AT_AcquireHandle: |
| 7504 | handeAcquireHandleAttr(S, D, AL); |
| 7505 | break; |
| 7506 | |
| 7507 | case ParsedAttr::AT_ReleaseHandle: |
| 7508 | handleHandleAttr<ReleaseHandleAttr>(S, D, AL); |
| 7509 | break; |
| 7510 | |
| 7511 | case ParsedAttr::AT_UseHandle: |
| 7512 | handleHandleAttr<UseHandleAttr>(S, D, AL); |
| 7513 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7514 | } |
| 7515 | } |
| 7516 | |
| 7517 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 7518 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 7519 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7520 | const ParsedAttributesView &AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 7521 | bool IncludeCXX11Attributes) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7522 | if (AttrList.empty()) |
| 7523 | return; |
| 7524 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7525 | for (const ParsedAttr &AL : AttrList) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7526 | ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 7527 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7528 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 7529 | // GCC accepts |
| 7530 | // static int a9 __attribute__((weakref)); |
| 7531 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 7532 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7533 | Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias) |
| 7534 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 7535 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 7536 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7537 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7538 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 7539 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 7540 | // good to have a way to specify "these attributes must appear as a group", |
| 7541 | // for these. Additionally, it would be good to have a way to specify "these |
| 7542 | // 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] | 7543 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 7544 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7545 | if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 7546 | // FIXME: This emits a different error message than |
| 7547 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 7548 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7549 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7550 | } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 7551 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7552 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7553 | } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 7554 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7555 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7556 | } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 7557 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 7558 | D->setInvalidDecl(); |
Yaxun Liu | aa24601 | 2018-06-12 23:58:59 +0000 | [diff] [blame] | 7559 | } else if (!D->hasAttr<CUDAGlobalAttr>()) { |
| 7560 | if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 7561 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7562 | << A << ExpectedKernelFunction; |
| 7563 | D->setInvalidDecl(); |
| 7564 | } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7565 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7566 | << A << ExpectedKernelFunction; |
| 7567 | D->setInvalidDecl(); |
| 7568 | } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 7569 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7570 | << A << ExpectedKernelFunction; |
| 7571 | D->setInvalidDecl(); |
| 7572 | } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7573 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 7574 | << A << ExpectedKernelFunction; |
| 7575 | D->setInvalidDecl(); |
| 7576 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 7577 | } |
| 7578 | } |
Erik Pilkington | 81d3f45 | 2019-02-13 20:32:37 +0000 | [diff] [blame] | 7579 | |
| 7580 | // Do this check after processing D's attributes because the attribute |
| 7581 | // objc_method_family can change whether the given method is in the init |
| 7582 | // family, and it can be applied after objc_designated_initializer. This is a |
| 7583 | // bit of a hack, but we need it to be compatible with versions of clang that |
| 7584 | // processed the attribute list in the wrong order. |
| 7585 | if (D->hasAttr<ObjCDesignatedInitializerAttr>() && |
| 7586 | cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) { |
| 7587 | Diag(D->getLocation(), diag::err_designated_init_attr_non_init); |
| 7588 | D->dropAttr<ObjCDesignatedInitializerAttr>(); |
| 7589 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 7590 | } |
| 7591 | |
Yonghong Song | 4e2ce22 | 2019-11-01 22:16:59 -0700 | [diff] [blame] | 7592 | // Helper for delayed processing TransparentUnion or BPFPreserveAccessIndexAttr |
| 7593 | // attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7594 | void Sema::ProcessDeclAttributeDelayed(Decl *D, |
| 7595 | const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7596 | for (const ParsedAttr &AL : AttrList) |
| 7597 | if (AL.getKind() == ParsedAttr::AT_TransparentUnion) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7598 | handleTransparentUnionAttr(*this, D, AL); |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 7599 | break; |
| 7600 | } |
Yonghong Song | 4e2ce22 | 2019-11-01 22:16:59 -0700 | [diff] [blame] | 7601 | |
| 7602 | // For BPFPreserveAccessIndexAttr, we want to populate the attributes |
| 7603 | // to fields and inner records as well. |
| 7604 | if (D && D->hasAttr<BPFPreserveAccessIndexAttr>()) |
| 7605 | handleBPFPreserveAIRecord(*this, cast<RecordDecl>(D)); |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 7606 | } |
| 7607 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7608 | // Annotation attributes are the only attributes allowed after an access |
| 7609 | // specifier. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7610 | bool Sema::ProcessAccessDeclAttributeList( |
| 7611 | AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7612 | for (const ParsedAttr &AL : AttrList) { |
| 7613 | if (AL.getKind() == ParsedAttr::AT_Annotate) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7614 | ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7615 | } else { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7616 | Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7617 | return true; |
| 7618 | } |
| 7619 | } |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 7620 | return false; |
| 7621 | } |
| 7622 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7623 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 7624 | /// contains any decl attributes that we should warn about. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7625 | static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7626 | for (const ParsedAttr &AL : A) { |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7627 | // Only warn if the attribute is an unignored, non-type attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7628 | if (AL.isUsedAsTypeAttr() || AL.isInvalid()) |
| 7629 | continue; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7630 | if (AL.getKind() == ParsedAttr::IgnoredAttribute) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7631 | continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7632 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 7633 | if (AL.getKind() == ParsedAttr::UnknownAttribute) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7634 | S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 7635 | << AL << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7636 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 7637 | S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL |
| 7638 | << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7639 | } |
| 7640 | } |
| 7641 | } |
| 7642 | |
| 7643 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 7644 | /// used to build a declaration, complain about any decl attributes |
| 7645 | /// which might be lying around on it. |
| 7646 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7647 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes()); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 7648 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 7649 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 7650 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 7651 | } |
| 7652 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7653 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7654 | /// \#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] | 7655 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 7656 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 7657 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7658 | NamedDecl *NewD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7659 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 7660 | FunctionDecl *NewFD; |
| 7661 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7662 | // FIXME: Mangling? |
| 7663 | // FIXME: Is the qualifier info correct? |
| 7664 | // FIXME: Is the DeclContext correct? |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 7665 | NewFD = FunctionDecl::Create( |
| 7666 | FD->getASTContext(), FD->getDeclContext(), Loc, Loc, |
| 7667 | DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None, |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 7668 | false /*isInlineSpecified*/, FD->hasPrototype(), CSK_unspecified, |
| 7669 | FD->getTrailingRequiresClause()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7670 | NewD = NewFD; |
| 7671 | |
| 7672 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 7673 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7674 | |
| 7675 | // Fake up parameter variables; they are declared as if this were |
| 7676 | // a typedef. |
| 7677 | QualType FDTy = FD->getType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7678 | if (const auto *FT = FDTy->getAs<FunctionProtoType>()) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7679 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 7680 | for (const auto &AI : FT->param_types()) { |
| 7681 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7682 | Param->setScopeInfo(0, Params.size()); |
| 7683 | Params.push_back(Param); |
| 7684 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7685 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7686 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7687 | } else if (auto *VD = dyn_cast<VarDecl>(ND)) { |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7688 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7689 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 7690 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7691 | VD->getStorageClass()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7692 | if (VD->getQualifier()) |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame] | 7693 | cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc()); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7694 | } |
| 7695 | return NewD; |
| 7696 | } |
| 7697 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7698 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7699 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 7700 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 7701 | if (W.getUsed()) return; // only do this once |
| 7702 | W.setUsed(true); |
| 7703 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 7704 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 7705 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 7706 | NewD->addAttr( |
| 7707 | AliasAttr::CreateImplicit(Context, NDId->getName(), W.getLocation())); |
| 7708 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(), |
| 7709 | AttributeCommonInfo::AS_Pragma)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 7710 | WeakTopLevelDecl.push_back(NewD); |
| 7711 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 7712 | // to insert Decl at TU scope, sorry. |
| 7713 | DeclContext *SavedContext = CurContext; |
| 7714 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 7715 | NewD->setDeclContext(CurContext); |
| 7716 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 7717 | PushOnScopeChains(NewD, S); |
| 7718 | CurContext = SavedContext; |
| 7719 | } else { // just add weak to existing |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 7720 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation(), |
| 7721 | AttributeCommonInfo::AS_Pragma)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 7722 | } |
| 7723 | } |
| 7724 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7725 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 7726 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 7727 | // have to do this. |
| 7728 | LoadExternalWeakUndeclaredIdentifiers(); |
| 7729 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7730 | NamedDecl *ND = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7731 | if (auto *VD = dyn_cast<VarDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7732 | if (VD->isExternC()) |
| 7733 | ND = VD; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7734 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7735 | if (FD->isExternC()) |
| 7736 | ND = FD; |
| 7737 | if (ND) { |
| 7738 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 7739 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 7740 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 7741 | WeakInfo W = I->second; |
| 7742 | DeclApplyPragmaWeak(S, ND, W); |
| 7743 | WeakUndeclaredIdentifiers[Id] = W; |
| 7744 | } |
| 7745 | } |
| 7746 | } |
| 7747 | } |
| 7748 | } |
| 7749 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7750 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 7751 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 7752 | /// 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] | 7753 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7754 | // Apply decl attributes from the DeclSpec if present. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7755 | if (!PD.getDeclSpec().getAttributes().empty()) |
| 7756 | ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 7757 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7758 | // Walk the declarator structure, applying decl attributes that were in a type |
| 7759 | // position to the decl itself. This handles cases like: |
| 7760 | // int *__attr__(x)** D; |
| 7761 | // when X is a decl attribute. |
| 7762 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7763 | ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(), |
| 7764 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 7765 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7766 | // Finally, apply any attributes on the decl itself. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7767 | ProcessDeclAttributeList(S, D, PD.getAttributes()); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 7768 | |
| 7769 | // Apply additional attributes specified by '#pragma clang attribute'. |
| 7770 | AddPragmaAttributes(S, D); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 7771 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7772 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7773 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7774 | /// If so, it'll still be annotated with an attribute that makes it |
| 7775 | /// illegal to actually use. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7776 | static bool isForbiddenTypeAllowed(Sema &S, Decl *D, |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7777 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7778 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7779 | // Private ivars are always okay. Unfortunately, people don't |
| 7780 | // always properly make their ivars private, even in system headers. |
| 7781 | // Plus we need to make fields okay, too. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7782 | if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) && |
| 7783 | !isa<FunctionDecl>(D)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7784 | return false; |
| 7785 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7786 | // Silently accept unsupported uses of __weak in both user and system |
| 7787 | // declarations when it's been disabled, for ease of integration with |
| 7788 | // -fno-objc-arc files. We do have to take some care against attempts |
| 7789 | // to define such things; for now, we've only done that for ivars |
| 7790 | // and properties. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7791 | if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7792 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 7793 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 7794 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 7795 | return true; |
| 7796 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7799 | // Allow all sorts of things in system headers. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7800 | if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7801 | // Currently, all the failures dealt with this way are due to ARC |
| 7802 | // restrictions. |
| 7803 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 7804 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7808 | } |
| 7809 | |
| 7810 | /// Handle a delayed forbidden-type diagnostic. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7811 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD, |
| 7812 | Decl *D) { |
| 7813 | auto Reason = UnavailableAttr::IR_None; |
| 7814 | if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) { |
| 7815 | assert(Reason && "didn't set reason?"); |
| 7816 | D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7817 | return; |
| 7818 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7819 | if (S.getLangOpts().ObjCAutoRefCount) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7820 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 7821 | // FIXME: we may want to suppress diagnostics for all |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7822 | // kind of forbidden type messages on unavailable functions. |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 7823 | if (FD->hasAttr<UnavailableAttr>() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7824 | DD.getForbiddenTypeDiagnostic() == |
| 7825 | diag::err_arc_array_param_no_ownership) { |
| 7826 | DD.Triggered = true; |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 7827 | return; |
| 7828 | } |
| 7829 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7830 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7831 | S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic()) |
| 7832 | << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument(); |
| 7833 | DD.Triggered = true; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7834 | } |
| 7835 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7836 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7837 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 7838 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7839 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7840 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7841 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7842 | // When delaying diagnostics to run in the context of a parsed |
| 7843 | // declaration, we only want to actually emit anything if parsing |
| 7844 | // succeeds. |
| 7845 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7846 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7847 | // We emit all the active diagnostics in this pool or any of its |
| 7848 | // parents. In general, we'll get one pool for the decl spec |
| 7849 | // and a child pool for each declarator; in a decl group like: |
| 7850 | // deprecated_typedef foo, *bar, baz(); |
| 7851 | // only the declarator pops will be passed decls. This is correct; |
| 7852 | // we really do need to consider delayed diagnostics from the decl spec |
| 7853 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7854 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7855 | do { |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 7856 | bool AnyAccessFailures = false; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7857 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7858 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 7859 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 7860 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7861 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7862 | continue; |
| 7863 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7864 | switch (diag.Kind) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7865 | case DelayedDiagnostic::Availability: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 7866 | // Don't bother giving deprecation/unavailable diagnostics if |
| 7867 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 7868 | if (!decl->isInvalidDecl()) |
Reid Kleckner | dd8e0a0 | 2020-01-24 15:16:22 -0800 | [diff] [blame] | 7869 | handleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7870 | break; |
| 7871 | |
| 7872 | case DelayedDiagnostic::Access: |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 7873 | // Only produce one access control diagnostic for a structured binding |
| 7874 | // declaration: we don't need to tell the user that all the fields are |
| 7875 | // inaccessible one at a time. |
| 7876 | if (AnyAccessFailures && isa<DecompositionDecl>(decl)) |
| 7877 | continue; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7878 | HandleDelayedAccessCheck(diag, decl); |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 7879 | if (diag.Triggered) |
| 7880 | AnyAccessFailures = true; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7881 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7882 | |
| 7883 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7884 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7885 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7886 | } |
| 7887 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7888 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7891 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 7892 | /// been delayed in the current context instead of in the given pool. |
| 7893 | /// Essentially, this just moves them to the current pool. |
| 7894 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 7895 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 7896 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 7897 | curPool->steal(pool); |
| 7898 | } |