Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 23 | #include "clang/AST/Mangle.h" |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecursiveASTVisitor.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 25 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 29 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 30 | #include "clang/Sema/DelayedDiagnostic.h" |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Initialization.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 33 | #include "clang/Sema/Scope.h" |
Reid Kleckner | 04f9bca | 2018-03-07 22:48:35 +0000 | [diff] [blame] | 34 | #include "clang/Sema/ScopeInfo.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 35 | #include "clang/Sema/SemaInternal.h" |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 40 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 41 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 42 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 43 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 44 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 45 | C, |
| 46 | Cpp, |
| 47 | ObjC |
| 48 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 49 | } // end namespace AttributeLangSupport |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Helper functions |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 55 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 56 | /// type (function or function-typed variable) or an Objective-C |
| 57 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 58 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 59 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 60 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 61 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 62 | /// Return true if the given decl has function type (function or |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 63 | /// function-typed variable) or an Objective-C method or a block. |
| 64 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 65 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 66 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 67 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 68 | /// Return true if the given decl has a declarator that should have |
| 69 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 71 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 73 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 76 | /// hasFunctionProto - Return true if the given decl has a argument |
| 77 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 78 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 80 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 81 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 82 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 85 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 86 | /// parameters. It is an error to call this on a K&R function (use |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 87 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 88 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 89 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 90 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 91 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 92 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 96 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 97 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 98 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 99 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 100 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 101 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 102 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 105 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 106 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 107 | return FD->getParamDecl(Idx)->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 108 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 109 | return MD->parameters()[Idx]->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 110 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 111 | return BD->getParamDecl(Idx)->getSourceRange(); |
| 112 | return SourceRange(); |
| 113 | } |
| 114 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 116 | if (const FunctionType *FnTy = D->getFunctionType()) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 117 | return FnTy->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 118 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 121 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 122 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 123 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 124 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 125 | return MD->getReturnTypeSourceRange(); |
| 126 | return SourceRange(); |
| 127 | } |
| 128 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 130 | if (const FunctionType *FnTy = D->getFunctionType()) |
| 131 | return cast<FunctionProtoType>(FnTy)->isVariadic(); |
| 132 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 133 | return BD->isVariadic(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 134 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | static bool isInstanceMethod(const Decl *D) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 138 | if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 139 | return MethodDecl->isInstance(); |
| 140 | return false; |
| 141 | } |
| 142 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 143 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 144 | const auto *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 145 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 146 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 147 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 148 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 149 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 150 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 151 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 152 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 154 | // FIXME: Should we walk the chain of classes? |
| 155 | return ClsName == &Ctx.Idents.get("NSString") || |
| 156 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 157 | } |
| 158 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 159 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 160 | const auto *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 161 | if (!PT) |
| 162 | return false; |
| 163 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 164 | const auto *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 165 | if (!RT) |
| 166 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 168 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 169 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 170 | return false; |
| 171 | |
| 172 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 173 | } |
| 174 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 175 | static unsigned getNumAttributeArgs(const ParsedAttr &AL) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 176 | // FIXME: Include the type in the argument list. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 177 | return AL.getNumArgs() + AL.hasParsedType(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 180 | template <typename Compare> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 181 | static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 182 | unsigned Num, unsigned Diag, |
| 183 | Compare Comp) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 184 | if (Comp(getNumAttributeArgs(AL), Num)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 185 | S.Diag(AL.getLoc(), Diag) << AL << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 192 | /// Check if the attribute has exactly as many args as Num. May |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 193 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 194 | static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 195 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 196 | diag::err_attribute_wrong_number_arguments, |
| 197 | std::not_equal_to<unsigned>()); |
| 198 | } |
| 199 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 200 | /// Check if the attribute has at least as many args as Num. May |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 201 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 202 | static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 203 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 204 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 205 | diag::err_attribute_too_few_arguments, |
| 206 | std::less<unsigned>()); |
| 207 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 208 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 209 | /// Check if the attribute has at most as many args as Num. May |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 210 | /// output an error. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 211 | static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL, |
| 212 | unsigned Num) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 213 | return checkAttributeNumArgsImpl(S, AL, Num, |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 214 | diag::err_attribute_too_many_arguments, |
| 215 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 218 | /// A helper function to provide Attribute Location for the Attr types |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 219 | /// AND the ParsedAttr. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 220 | template <typename AttrInfo> |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 221 | static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 222 | SourceLocation>::type |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 223 | getAttrLoc(const AttrInfo &AL) { |
| 224 | return AL.getLocation(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 225 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 226 | static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); } |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 227 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 228 | /// If Expr is a valid integer constant, get the value of the integer |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 229 | /// expression and return success or failure. May output an error. |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 230 | /// |
| 231 | /// Negative argument is implicitly converted to unsigned, unless |
| 232 | /// \p StrictlyUnsigned is true. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 233 | template <typename AttrInfo> |
| 234 | static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 235 | uint32_t &Val, unsigned Idx = UINT_MAX, |
| 236 | bool StrictlyUnsigned = false) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 237 | llvm::APSInt I(32); |
| 238 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 239 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 240 | if (Idx != UINT_MAX) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 241 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 242 | << AI << Idx << AANT_ArgumentIntegerConstant |
| 243 | << Expr->getSourceRange(); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 244 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 245 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 246 | << AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange(); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 247 | return false; |
| 248 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 249 | |
| 250 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 251 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 252 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 256 | if (StrictlyUnsigned && I.isSigned() && I.isNegative()) { |
Andrew Savonichev | 1a562348 | 2018-09-17 10:39:46 +0000 | [diff] [blame] | 257 | S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer) |
| 258 | << AI << /*non-negative*/ 1; |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 262 | Val = (uint32_t)I.getZExtValue(); |
| 263 | return true; |
| 264 | } |
| 265 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 266 | /// Wrapper around checkUInt32Argument, with an extra check to be sure |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 267 | /// that the result will fit into a regular (signed) int. All args have the same |
| 268 | /// purpose as they do in checkUInt32Argument. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 269 | template <typename AttrInfo> |
| 270 | static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 271 | int &Val, unsigned Idx = UINT_MAX) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 272 | uint32_t UVal; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 273 | if (!checkUInt32Argument(S, AI, Expr, UVal, Idx)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 274 | return false; |
| 275 | |
George Burgess IV | a804957 | 2016-12-22 19:00:31 +0000 | [diff] [blame] | 276 | if (UVal > (uint32_t)std::numeric_limits<int>::max()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 277 | llvm::APSInt I(32); // for toString |
| 278 | I = UVal; |
| 279 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 280 | << I.toString(10, false) << 32 << /* Unsigned */ 0; |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | Val = UVal; |
| 285 | return true; |
| 286 | } |
| 287 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 288 | /// Diagnose mutually exclusive attributes when present on a given |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 289 | /// declaration. Returns true if diagnosed. |
| 290 | template <typename AttrTy> |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 291 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 292 | if (const auto *A = D->getAttr<AttrTy>()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 293 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A; |
| 294 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
| 295 | return true; |
| 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | template <typename AttrTy> |
| 301 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) { |
| 302 | if (const auto *A = D->getAttr<AttrTy>()) { |
| 303 | S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 304 | << A; |
| 305 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 306 | return true; |
| 307 | } |
| 308 | return false; |
| 309 | } |
| 310 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 311 | /// Check if IdxExpr is a valid parameter index for a function or |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 312 | /// instance method D. May output an error. |
| 313 | /// |
| 314 | /// \returns true if IdxExpr is a valid index. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 315 | template <typename AttrInfo> |
| 316 | static bool checkFunctionOrMethodParameterIndex( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 317 | Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 318 | const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 319 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 320 | |
| 321 | // In C++ the implicit 'this' function parameter also counts. |
| 322 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 323 | bool HP = hasFunctionProto(D); |
| 324 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 325 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 326 | unsigned NumParams = |
| 327 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 328 | |
| 329 | llvm::APSInt IdxInt; |
| 330 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 331 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 332 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 333 | << &AI << AttrArgNum << AANT_ArgumentIntegerConstant |
| 334 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 338 | unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX); |
| 339 | if (IdxSource < 1 || (!IV && IdxSource > NumParams)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 340 | S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 341 | << &AI << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 342 | return false; |
| 343 | } |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 344 | if (HasImplicitThisParam && !CanIndexImplicitThis) { |
| 345 | if (IdxSource == 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 346 | S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument) |
| 347 | << &AI << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 348 | return false; |
| 349 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 352 | Idx = ParamIdx(IdxSource, D); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 356 | /// 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] | 357 | /// If not emit an error and return false. If the argument is an identifier it |
| 358 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 359 | /// literal. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 360 | bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum, |
| 361 | StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 362 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 363 | // 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] | 364 | if (AL.isArgIdent(ArgNum)) { |
| 365 | IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 366 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 367 | << AL << AANT_ArgumentString |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 368 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 369 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 370 | Str = Loc->Ident->getName(); |
| 371 | if (ArgLocation) |
| 372 | *ArgLocation = Loc->Loc; |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | // Now check for an actual string literal. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 377 | Expr *ArgExpr = AL.getArgAsExpr(ArgNum); |
| 378 | const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 379 | if (ArgLocation) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 380 | *ArgLocation = ArgExpr->getBeginLoc(); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 381 | |
| 382 | if (!Literal || !Literal->isAscii()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 383 | Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 384 | << AL << AANT_ArgumentString; |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
| 388 | Str = Literal->getString(); |
| 389 | return true; |
| 390 | } |
| 391 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 392 | /// Applies the given attribute to the Decl without performing any |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 393 | /// additional semantic checking. |
| 394 | template <typename AttrType> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 395 | static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 396 | D->addAttr(::new (S.Context) AttrType(AL.getRange(), S.Context, |
| 397 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 400 | template <typename AttrType> |
| 401 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 402 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 403 | handleSimpleAttribute<AttrType>(S, D, AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 406 | /// 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] | 407 | /// already have one of the given incompatible attributes. |
| 408 | template <typename AttrType, typename IncompatibleAttrType, |
| 409 | typename... IncompatibleAttrTypes> |
| 410 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 411 | const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 412 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL)) |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 413 | return; |
| 414 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 415 | AL); |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 418 | /// Check if the passed-in expression is of type int or bool. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 419 | static bool isIntOrBool(Expr *Exp) { |
| 420 | QualType QT = Exp->getType(); |
| 421 | return QT->isBooleanType() || QT->isIntegerType(); |
| 422 | } |
| 423 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 424 | |
| 425 | // Check to see if the type is a smart pointer of some kind. We assume |
| 426 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 427 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 428 | auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record, |
| 429 | OverloadedOperatorKind Op) { |
| 430 | DeclContextLookupResult Result = |
| 431 | Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op)); |
| 432 | return !Result.empty(); |
| 433 | }; |
| 434 | |
| 435 | const RecordDecl *Record = RT->getDecl(); |
| 436 | bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star); |
| 437 | bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow); |
| 438 | if (foundStarOperator && foundArrowOperator) |
| 439 | return true; |
| 440 | |
| 441 | const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record); |
| 442 | if (!CXXRecord) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 443 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 444 | |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 445 | for (auto BaseSpecifier : CXXRecord->bases()) { |
| 446 | if (!foundStarOperator) |
| 447 | foundStarOperator = IsOverloadedOperatorPresent( |
| 448 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Star); |
| 449 | if (!foundArrowOperator) |
| 450 | foundArrowOperator = IsOverloadedOperatorPresent( |
| 451 | BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow); |
| 452 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 453 | |
Richard Trieu | 8d3fa39 | 2018-09-22 01:50:52 +0000 | [diff] [blame] | 454 | if (foundStarOperator && foundArrowOperator) |
| 455 | return true; |
| 456 | |
| 457 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 460 | /// Check if passed in Decl is a pointer type. |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 461 | /// Note that this function may produce an error message. |
| 462 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 463 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 464 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 465 | const auto *VD = cast<ValueDecl>(D); |
| 466 | QualType QT = VD->getType(); |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 467 | if (QT->isAnyPointerType()) |
| 468 | return true; |
| 469 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 470 | if (const auto *RT = QT->getAs<RecordType>()) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 471 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 472 | // (We don't want to force template instantiation if we can avoid it, |
| 473 | // since that would alter the order in which templates are instantiated.) |
| 474 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 475 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 476 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 477 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 478 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 479 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 480 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 481 | 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] | 482 | return false; |
| 483 | } |
| 484 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 485 | /// Checks that the passed in QualType either is of RecordType or points |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 486 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 487 | static const RecordType *getRecordType(QualType QT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 488 | if (const auto *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 489 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 490 | |
| 491 | // Now check if we point to record type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 492 | if (const auto *PT = QT->getAs<PointerType>()) |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 493 | return PT->getPointeeType()->getAs<RecordType>(); |
| 494 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 495 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 498 | template <typename AttrType> |
| 499 | static bool checkRecordDeclForAttr(const RecordDecl *RD) { |
| 500 | // Check if the record itself has the attribute. |
| 501 | if (RD->hasAttr<AttrType>()) |
| 502 | return true; |
| 503 | |
| 504 | // Else check if any base classes have the attribute. |
| 505 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 506 | CXXBasePaths BPaths(false, false); |
| 507 | if (CRD->lookupInBases( |
| 508 | [](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 509 | const auto &Ty = *BS->getType(); |
| 510 | // If it's type-dependent, we assume it could have the attribute. |
| 511 | if (Ty.isDependentType()) |
| 512 | return true; |
| 513 | return Ty.getAs<RecordType>()->getDecl()->hasAttr<AttrType>(); |
| 514 | }, |
| 515 | BPaths, true)) |
| 516 | return true; |
| 517 | } |
| 518 | return false; |
| 519 | } |
| 520 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 521 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 522 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 523 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 524 | if (!RT) |
| 525 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 526 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 527 | // 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] | 528 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 529 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 530 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 531 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 532 | // FIXME -- Check the type that the smart pointer points to. |
| 533 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 534 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 535 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 536 | return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl()); |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 539 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 540 | const auto *TD = Ty->getAs<TypedefType>(); |
| 541 | if (!TD) |
| 542 | return false; |
| 543 | |
| 544 | TypedefNameDecl *TN = TD->getDecl(); |
| 545 | if (!TN) |
| 546 | return false; |
| 547 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 548 | return TN->hasAttr<CapabilityAttr>(); |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 551 | static bool typeHasCapability(Sema &S, QualType Ty) { |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 552 | if (checkTypedefTypeForCapability(Ty)) |
| 553 | return true; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 554 | |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 555 | if (checkRecordTypeForCapability(S, Ty)) |
| 556 | return true; |
| 557 | |
| 558 | return false; |
Josh Gao | b40c177 | 2017-08-08 19:44:35 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 561 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 562 | // Capability expressions are simple expressions involving the boolean logic |
| 563 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 564 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 565 | // is a capability or not. |
| 566 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 567 | if (const auto *E = dyn_cast<CastExpr>(Ex)) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 568 | return isCapabilityExpr(S, E->getSubExpr()); |
| 569 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 570 | return isCapabilityExpr(S, E->getSubExpr()); |
| 571 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 572 | if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf || |
| 573 | E->getOpcode() == UO_Deref) |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 574 | return isCapabilityExpr(S, E->getSubExpr()); |
| 575 | return false; |
| 576 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 577 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 578 | return isCapabilityExpr(S, E->getLHS()) && |
| 579 | isCapabilityExpr(S, E->getRHS()); |
| 580 | return false; |
| 581 | } |
| 582 | |
Yi Kong | 2d58d19 | 2017-12-14 22:24:45 +0000 | [diff] [blame] | 583 | return typeHasCapability(S, Ex->getType()); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 586 | /// Checks that all attribute arguments, starting from Sidx, resolve to |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 587 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 588 | /// \param Sidx The attribute argument index to start checking with. |
| 589 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 590 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 591 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 592 | const ParsedAttr &AL, |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 593 | SmallVectorImpl<Expr *> &Args, |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 594 | unsigned Sidx = 0, |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 595 | bool ParamIdxOk = false) { |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 596 | if (Sidx == AL.getNumArgs()) { |
| 597 | // If we don't have any capability arguments, the attribute implicitly |
| 598 | // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're |
| 599 | // a non-static method, and that the class is a (scoped) capability. |
| 600 | const auto *MD = dyn_cast<const CXXMethodDecl>(D); |
| 601 | if (MD && !MD->isStatic()) { |
| 602 | const CXXRecordDecl *RD = MD->getParent(); |
| 603 | // FIXME -- need to check this again on template instantiation |
| 604 | if (!checkRecordDeclForAttr<CapabilityAttr>(RD) && |
| 605 | !checkRecordDeclForAttr<ScopedLockableAttr>(RD)) |
| 606 | S.Diag(AL.getLoc(), |
| 607 | diag::warn_thread_attribute_not_on_capability_member) |
| 608 | << AL << MD->getParent(); |
| 609 | } else { |
| 610 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member) |
| 611 | << AL; |
| 612 | } |
| 613 | } |
| 614 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 615 | for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) { |
| 616 | Expr *ArgExp = AL.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 617 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 618 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 619 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 620 | Args.push_back(ArgExp); |
| 621 | continue; |
| 622 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 623 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 624 | if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 625 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 626 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 627 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 628 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 629 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 630 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 631 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 632 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 633 | // We allow constant strings to be used as a placeholder for expressions |
| 634 | // that are not valid C++ syntax, but warn that they are ignored. |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 635 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 636 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 637 | continue; |
| 638 | } |
| 639 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 640 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 641 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 642 | // A pointer to member expression of the form &MyClass::mu is treated |
| 643 | // specially -- we need to look at the type of the member. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 644 | if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 645 | if (UOp->getOpcode() == UO_AddrOf) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 646 | if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 647 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 648 | ArgTy = DRE->getDecl()->getType(); |
| 649 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 650 | // 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] | 651 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 652 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 653 | // Now check if we index into a record type function param. |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 654 | if(!RT && ParamIdxOk) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 655 | const auto *FD = dyn_cast<FunctionDecl>(D); |
| 656 | const auto *IL = dyn_cast<IntegerLiteral>(ArgExp); |
Josh Gao | 55afa75 | 2017-08-11 07:54:35 +0000 | [diff] [blame] | 657 | if(FD && IL) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 658 | unsigned int NumParams = FD->getNumParams(); |
| 659 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 660 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 661 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 662 | if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
| 663 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 664 | << AL << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 665 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 666 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 667 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 671 | // If the type does not have a capability, see if the components of the |
| 672 | // expression have capabilities. This allows for writing C code where the |
| 673 | // capability may be on the type, and the expression is a capability |
| 674 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 675 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 676 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 677 | << AL << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 678 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 679 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 680 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 683 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 684 | // Attribute Implementations |
| 685 | //===----------------------------------------------------------------------===// |
| 686 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 687 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 688 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 689 | return; |
| 690 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 691 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 692 | PtGuardedVarAttr(AL.getRange(), S.Context, |
| 693 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 696 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 697 | Expr *&Arg) { |
| 698 | SmallVector<Expr *, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 699 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 700 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 701 | unsigned Size = Args.size(); |
| 702 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 703 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 704 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 705 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 706 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 707 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 710 | static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 711 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 712 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 713 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 714 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 715 | D->addAttr(::new (S.Context) GuardedByAttr( |
| 716 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 719 | static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 720 | Expr *Arg = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 721 | if (!checkGuardedByAttrCommon(S, D, AL, Arg)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 722 | return; |
| 723 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 724 | if (!threadSafetyCheckIsPointer(S, D, AL)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 725 | return; |
| 726 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 727 | D->addAttr(::new (S.Context) PtGuardedByAttr( |
| 728 | AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 731 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 732 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 733 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 734 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 735 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 736 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 737 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 738 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 739 | S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL; |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 740 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 741 | } |
| 742 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 743 | // Check that all arguments are lockable objects. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 744 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 745 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 746 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 747 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 748 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 751 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 752 | SmallVector<Expr *, 1> Args; |
| 753 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 754 | return; |
| 755 | |
| 756 | Expr **StartArg = &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 757 | D->addAttr(::new (S.Context) AcquiredAfterAttr( |
| 758 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 759 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 762 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 763 | SmallVector<Expr *, 1> Args; |
| 764 | if (!checkAcquireOrderAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 765 | return; |
| 766 | |
| 767 | Expr **StartArg = &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 768 | D->addAttr(::new (S.Context) AcquiredBeforeAttr( |
| 769 | AL.getRange(), S.Context, StartArg, Args.size(), |
| 770 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 773 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 774 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 775 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 776 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 777 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 778 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 779 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 782 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 783 | SmallVector<Expr *, 1> Args; |
| 784 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 785 | return; |
| 786 | |
| 787 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 788 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 789 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 790 | AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size, |
| 791 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 795 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 796 | SmallVector<Expr *, 1> Args; |
| 797 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 798 | return; |
| 799 | |
| 800 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 801 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 802 | D->addAttr(::new (S.Context) AssertExclusiveLockAttr( |
| 803 | AL.getRange(), S.Context, StartArg, Size, |
| 804 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 807 | /// 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] | 808 | /// is an integral type. Will emit appropriate diagnostics if this returns |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 809 | /// false. |
| 810 | /// |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 811 | /// 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] | 812 | template <typename AttrInfo> |
| 813 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 814 | const AttrInfo &AI, unsigned AttrArgNo) { |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 815 | assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument"); |
| 816 | Expr *AttrArg = AI.getArgAsExpr(AttrArgNo); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 817 | ParamIdx Idx; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 818 | if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 819 | Idx)) |
| 820 | return false; |
| 821 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 822 | const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 823 | if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 824 | SourceLocation SrcLoc = AttrArg->getBeginLoc(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 825 | S.Diag(SrcLoc, diag::err_attribute_integers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 826 | << AI << Param->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 827 | return false; |
| 828 | } |
| 829 | return true; |
| 830 | } |
| 831 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 832 | static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 833 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 834 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 835 | return; |
| 836 | |
| 837 | const auto *FD = cast<FunctionDecl>(D); |
| 838 | if (!FD->getReturnType()->isPointerType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 839 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 840 | return; |
| 841 | } |
| 842 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 843 | const Expr *SizeExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 844 | int SizeArgNoVal; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 845 | // Parameter indices are 1-indexed, hence Index=1 |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 846 | if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 847 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 848 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 849 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 850 | ParamIdx SizeArgNo(SizeArgNoVal, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 851 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 852 | ParamIdx NumberArgNo; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 853 | if (AL.getNumArgs() == 2) { |
| 854 | const Expr *NumberExpr = AL.getArgAsExpr(1); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 855 | int Val; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 856 | // Parameter indices are 1-based, hence Index=2 |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 857 | if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 858 | return; |
Aaron Ballman | 836684a | 2018-02-25 20:40:06 +0000 | [diff] [blame] | 859 | if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 860 | return; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 861 | NumberArgNo = ParamIdx(Val, D); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 864 | D->addAttr(::new (S.Context) |
| 865 | AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo, |
| 866 | AL.getAttributeSpellingListIndex())); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 867 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 868 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 869 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 870 | SmallVectorImpl<Expr *> &Args) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 871 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 872 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 873 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 874 | if (!isIntOrBool(AL.getArgAsExpr(0))) { |
| 875 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 876 | << AL << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 877 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | // check that all arguments are lockable objects |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 881 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 882 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 883 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 886 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 887 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 888 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 889 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 890 | return; |
| 891 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 892 | D->addAttr(::new (S.Context) SharedTrylockFunctionAttr( |
| 893 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(), |
| 894 | AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 897 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 898 | const ParsedAttr &AL) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 899 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 900 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 901 | return; |
| 902 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 903 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 904 | AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), |
| 905 | Args.size(), AL.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 908 | static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 909 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 910 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 911 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 912 | unsigned Size = Args.size(); |
| 913 | if (Size == 0) |
| 914 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 915 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 916 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 917 | LockReturnedAttr(AL.getRange(), S.Context, Args[0], |
| 918 | AL.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 921 | static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 922 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 923 | return; |
| 924 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 925 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 926 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 927 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 928 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 929 | if (Size == 0) |
| 930 | return; |
| 931 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 932 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 933 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 934 | LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size, |
| 935 | AL.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 938 | static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 939 | Expr *&Cond, StringRef &Msg) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 940 | Cond = AL.getArgAsExpr(0); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 941 | if (!Cond->isTypeDependent()) { |
| 942 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 943 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 944 | return false; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 945 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 948 | if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 949 | return false; |
| 950 | |
| 951 | if (Msg.empty()) |
| 952 | Msg = "<no message provided>"; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 953 | |
| 954 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 955 | if (isa<FunctionDecl>(D) && !Cond->isValueDependent() && |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 956 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 957 | Diags)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 958 | 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] | 959 | for (const PartialDiagnosticAt &PDiag : Diags) |
| 960 | S.Diag(PDiag.first, PDiag.second); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 961 | return false; |
| 962 | } |
| 963 | return true; |
| 964 | } |
| 965 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 966 | static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 967 | S.Diag(AL.getLoc(), diag::ext_clang_enable_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 968 | |
| 969 | Expr *Cond; |
| 970 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 971 | if (checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 972 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 973 | EnableIfAttr(AL.getRange(), S.Context, Cond, Msg, |
| 974 | AL.getAttributeSpellingListIndex())); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | namespace { |
| 978 | /// Determines if a given Expr references any of the given function's |
| 979 | /// ParmVarDecls, or the function's implicit `this` parameter (if applicable). |
| 980 | class ArgumentDependenceChecker |
| 981 | : public RecursiveASTVisitor<ArgumentDependenceChecker> { |
| 982 | #ifndef NDEBUG |
| 983 | const CXXRecordDecl *ClassType; |
| 984 | #endif |
| 985 | llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms; |
| 986 | bool Result; |
| 987 | |
| 988 | public: |
| 989 | ArgumentDependenceChecker(const FunctionDecl *FD) { |
| 990 | #ifndef NDEBUG |
| 991 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 992 | ClassType = MD->getParent(); |
| 993 | else |
| 994 | ClassType = nullptr; |
| 995 | #endif |
| 996 | Parms.insert(FD->param_begin(), FD->param_end()); |
| 997 | } |
| 998 | |
| 999 | bool referencesArgs(Expr *E) { |
| 1000 | Result = false; |
| 1001 | TraverseStmt(E); |
| 1002 | return Result; |
| 1003 | } |
| 1004 | |
| 1005 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 1006 | assert(E->getType()->getPointeeCXXRecordDecl() == ClassType && |
| 1007 | "`this` doesn't refer to the enclosing class?"); |
| 1008 | Result = true; |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 1013 | if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 1014 | if (Parms.count(PVD)) { |
| 1015 | Result = true; |
| 1016 | return false; |
| 1017 | } |
| 1018 | return true; |
| 1019 | } |
| 1020 | }; |
| 1021 | } |
| 1022 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1023 | static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1024 | S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1025 | |
| 1026 | Expr *Cond; |
| 1027 | StringRef Msg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1028 | if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1029 | return; |
| 1030 | |
| 1031 | StringRef DiagTypeStr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1032 | if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1033 | return; |
| 1034 | |
| 1035 | DiagnoseIfAttr::DiagnosticType DiagType; |
| 1036 | if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1037 | S.Diag(AL.getArgAsExpr(2)->getBeginLoc(), |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1038 | diag::err_diagnose_if_invalid_diagnostic_type); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1039 | return; |
| 1040 | } |
| 1041 | |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1042 | bool ArgDependent = false; |
Argyrios Kyrtzidis | 5f0c0aa | 2017-05-24 18:35:01 +0000 | [diff] [blame] | 1043 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1044 | ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1045 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1046 | AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent, |
| 1047 | cast<NamedDecl>(D), AL.getAttributeSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1050 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1051 | if (D->hasAttr<PassObjectSizeAttr>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1052 | 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] | 1053 | return; |
| 1054 | } |
| 1055 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1056 | Expr *E = AL.getArgAsExpr(0); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1057 | uint32_t Type; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1058 | if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1)) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1059 | return; |
| 1060 | |
| 1061 | // pass_object_size's argument is passed in as the second argument of |
| 1062 | // __builtin_object_size. So, it has the same constraints as that second |
| 1063 | // argument; namely, it must be in the range [0, 3]. |
| 1064 | if (Type > 3) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1065 | S.Diag(E->getBeginLoc(), diag::err_attribute_argument_outof_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1066 | << AL << 0 << 3 << E->getSourceRange(); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | // pass_object_size is only supported on constant pointer parameters; as a |
| 1071 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 1072 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 1073 | // definition, so we defer the constness check until later. |
| 1074 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1075 | S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1076 | return; |
| 1077 | } |
| 1078 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1079 | D->addAttr(::new (S.Context) PassObjectSizeAttr( |
| 1080 | AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex())); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1083 | static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1084 | ConsumableAttr::ConsumedState DefaultState; |
| 1085 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1086 | if (AL.isArgIdent(0)) { |
| 1087 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1088 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1089 | DefaultState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1090 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1091 | << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1092 | return; |
| 1093 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1094 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1095 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1096 | << AL << AANT_ArgumentIdentifier; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1097 | return; |
| 1098 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1099 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1100 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1101 | ConsumableAttr(AL.getRange(), S.Context, DefaultState, |
| 1102 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1106 | const ParsedAttr &AL) { |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1107 | ASTContext &CurrContext = S.getASTContext(); |
| 1108 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1109 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1110 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1111 | if (!RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1112 | S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1113 | RD->getNameAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1114 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1115 | return false; |
| 1116 | } |
| 1117 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1118 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1119 | return true; |
| 1120 | } |
| 1121 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1122 | static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1123 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1124 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1125 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1126 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1127 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1128 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1129 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1130 | for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1131 | CallableWhenAttr::ConsumedState CallableState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1132 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1133 | StringRef StateString; |
| 1134 | SourceLocation Loc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1135 | if (AL.isArgIdent(ArgIndex)) { |
| 1136 | IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex); |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1137 | StateString = Ident->Ident->getName(); |
| 1138 | Loc = Ident->Loc; |
| 1139 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1140 | if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc)) |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1143 | |
| 1144 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1145 | CallableState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1146 | S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1147 | return; |
| 1148 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1149 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1150 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1151 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1152 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1153 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1154 | CallableWhenAttr(AL.getRange(), S.Context, States.data(), |
| 1155 | States.size(), AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1158 | static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1159 | ParamTypestateAttr::ConsumedState ParamState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1160 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1161 | if (AL.isArgIdent(0)) { |
| 1162 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1163 | StringRef StateString = Ident->Ident->getName(); |
| 1164 | |
| 1165 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1166 | ParamState)) { |
| 1167 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1168 | << AL << StateString; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1169 | return; |
| 1170 | } |
| 1171 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1172 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1173 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1174 | return; |
| 1175 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1176 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1177 | // FIXME: This check is currently being done in the analysis. It can be |
| 1178 | // enabled here only after the parser propagates attributes at |
| 1179 | // template specialization definition, not declaration. |
| 1180 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1181 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1182 | // |
| 1183 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1184 | // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1185 | // ReturnType.getAsString(); |
| 1186 | // return; |
| 1187 | //} |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1188 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1189 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1190 | ParamTypestateAttr(AL.getRange(), S.Context, ParamState, |
| 1191 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1194 | static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1195 | ReturnTypestateAttr::ConsumedState ReturnState; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1196 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1197 | if (AL.isArgIdent(0)) { |
| 1198 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1199 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1200 | ReturnState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1201 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1202 | << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1205 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1206 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1207 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1208 | return; |
| 1209 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1210 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1211 | // FIXME: This check is currently being done in the analysis. It can be |
| 1212 | // enabled here only after the parser propagates attributes at |
| 1213 | // template specialization definition, not declaration. |
| 1214 | //QualType ReturnType; |
| 1215 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1216 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1217 | // ReturnType = Param->getType(); |
| 1218 | // |
| 1219 | //} else if (const CXXConstructorDecl *Constructor = |
| 1220 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1221 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1222 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1223 | //} else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1224 | // |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1225 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1226 | //} |
| 1227 | // |
| 1228 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1229 | // |
| 1230 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1231 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1232 | // ReturnType.getAsString(); |
| 1233 | // return; |
| 1234 | //} |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1235 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1236 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1237 | ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState, |
| 1238 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1241 | static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1242 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1243 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1244 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1245 | SetTypestateAttr::ConsumedState NewState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1246 | if (AL.isArgIdent(0)) { |
| 1247 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1248 | StringRef Param = Ident->Ident->getName(); |
| 1249 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1250 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1251 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1252 | return; |
| 1253 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1254 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1255 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1256 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1257 | return; |
| 1258 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1259 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1260 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1261 | SetTypestateAttr(AL.getRange(), S.Context, NewState, |
| 1262 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1265 | static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1266 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL)) |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1267 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1268 | |
| 1269 | TestTypestateAttr::ConsumedState TestState; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1270 | if (AL.isArgIdent(0)) { |
| 1271 | IdentifierLoc *Ident = AL.getArgAsIdent(0); |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1272 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1273 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1274 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL |
| 1275 | << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1276 | return; |
| 1277 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1278 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1279 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 1280 | << AL << AANT_ArgumentIdentifier; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1281 | return; |
| 1282 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1283 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1284 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1285 | TestTypestateAttr(AL.getRange(), S.Context, TestState, |
| 1286 | AL.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1289 | static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1290 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1291 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1294 | static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1295 | if (auto *TD = dyn_cast<TagDecl>(D)) |
| 1296 | TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context, |
| 1297 | AL.getAttributeSpellingListIndex())); |
| 1298 | else if (auto *FD = dyn_cast<FieldDecl>(D)) { |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1299 | bool BitfieldByteAligned = (!FD->getType()->isDependentType() && |
| 1300 | !FD->getType()->isIncompleteType() && |
| 1301 | FD->isBitField() && |
| 1302 | S.Context.getTypeAlign(FD->getType()) <= 8); |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1303 | |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1304 | if (S.getASTContext().getTargetInfo().getTriple().isPS4()) { |
| 1305 | if (BitfieldByteAligned) |
| 1306 | // The PS4 target needs to maintain ABI backwards compatibility. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1307 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1308 | << AL << FD->getType(); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1309 | else |
| 1310 | FD->addAttr(::new (S.Context) PackedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1311 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1312 | } else { |
| 1313 | // Report warning about changed offset in the newer compiler versions. |
| 1314 | if (BitfieldByteAligned) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1315 | S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1316 | |
| 1317 | FD->addAttr(::new (S.Context) PackedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1318 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1321 | } else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1322 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1325 | static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1326 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1327 | // variables or properties of Objective-C classes. The outlet must also |
| 1328 | // have an object reference type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1329 | if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1330 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1331 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1332 | << AL << VD->getType() << 0; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1333 | return false; |
| 1334 | } |
| 1335 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1336 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1337 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1338 | S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1339 | << AL << PD->getType() << 1; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1340 | return false; |
| 1341 | } |
| 1342 | } |
| 1343 | else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1344 | S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1345 | return false; |
| 1346 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1347 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1348 | return true; |
| 1349 | } |
| 1350 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1351 | static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1352 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1353 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1354 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1355 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1356 | IBOutletAttr(AL.getRange(), S.Context, |
| 1357 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1360 | static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1361 | |
| 1362 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1363 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1364 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1365 | return; |
| 1366 | } |
| 1367 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1368 | if (!checkIBOutletCommon(S, D, AL)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1369 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1370 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1371 | ParsedType PT; |
| 1372 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1373 | if (AL.hasParsedType()) |
| 1374 | PT = AL.getTypeArg(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1375 | else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1376 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1377 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1378 | if (!PT) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1379 | S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1380 | return; |
| 1381 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1382 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1383 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1384 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1385 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1386 | if (!QTLoc) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1387 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1388 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1389 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1390 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1391 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1392 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1393 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1394 | S.Diag(AL.getLoc(), |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1395 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1396 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1397 | return; |
| 1398 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1399 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1400 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1401 | IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc, |
| 1402 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1405 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1406 | if (RefOkay) { |
| 1407 | if (T->isReferenceType()) |
| 1408 | return true; |
| 1409 | } else { |
| 1410 | T = T.getNonReferenceType(); |
| 1411 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1412 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1413 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1414 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1415 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1416 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1417 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1418 | for (const auto *I : UD->fields()) { |
| 1419 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1420 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1421 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1429 | static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1430 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1431 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1432 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1433 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1434 | if (isReturnValue) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1435 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1436 | << AL << AttrParmRange << TypeRange; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1437 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1438 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1439 | << AL << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1440 | return false; |
| 1441 | } |
| 1442 | return true; |
| 1443 | } |
| 1444 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1445 | static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1446 | SmallVector<ParamIdx, 8> NonNullArgs; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1447 | for (unsigned I = 0; I < AL.getNumArgs(); ++I) { |
| 1448 | Expr *Ex = AL.getArgAsExpr(I); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1449 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1450 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1451 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1454 | if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) && |
| 1455 | !attrNonNullArgCheck( |
| 1456 | S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL, |
| 1457 | Ex->getSourceRange(), |
| 1458 | getFunctionOrMethodParamRange(D, Idx.getASTIndex()))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1459 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1460 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1461 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1462 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1463 | |
| 1464 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1465 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1466 | // check if the attribute came from a macro expansion or a template |
| 1467 | // instantiation. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1468 | if (NonNullArgs.empty() && AL.getLoc().isFileID() && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1469 | !S.inTemplateInstantiation()) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1470 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1471 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1472 | I != E && !AnyPointers; ++I) { |
| 1473 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1474 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1475 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1476 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1477 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1478 | if (!AnyPointers) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1479 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1480 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1481 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1482 | ParamIdx *Start = NonNullArgs.data(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1483 | unsigned Size = NonNullArgs.size(); |
| 1484 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1485 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1486 | NonNullAttr(AL.getRange(), S.Context, Start, Size, |
| 1487 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1490 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1491 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1492 | if (AL.getNumArgs() > 0) { |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1493 | if (D->getFunctionType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1494 | handleNonNullAttr(S, D, AL); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1495 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1496 | S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1497 | << D->getSourceRange(); |
| 1498 | } |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | // Is the argument a pointer type? |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1503 | if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(), |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1504 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1505 | return; |
| 1506 | |
| 1507 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1508 | NonNullAttr(AL.getRange(), S.Context, nullptr, 0, |
| 1509 | AL.getAttributeSpellingListIndex())); |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1512 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1513 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1514 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1515 | if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1516 | /* isReturnValue */ true)) |
| 1517 | return; |
| 1518 | |
| 1519 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1520 | ReturnsNonNullAttr(AL.getRange(), S.Context, |
| 1521 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1524 | static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1525 | if (D->isInvalidDecl()) |
| 1526 | return; |
| 1527 | |
| 1528 | // noescape only applies to pointer types. |
| 1529 | QualType T = cast<ParmVarDecl>(D)->getType(); |
| 1530 | if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1531 | S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1532 | << AL << AL.getRange() << 0; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1533 | return; |
| 1534 | } |
| 1535 | |
| 1536 | D->addAttr(::new (S.Context) NoEscapeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1537 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1540 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1541 | Expr *E = AL.getArgAsExpr(0), |
| 1542 | *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr; |
| 1543 | S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE, |
| 1544 | AL.getAttributeSpellingListIndex()); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1547 | static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1548 | S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 1549 | AL.getAttributeSpellingListIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1552 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1553 | Expr *OE, unsigned SpellingListIndex) { |
| 1554 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1555 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1556 | |
| 1557 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1558 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1559 | |
| 1560 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1561 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1562 | << &TmpAttr << AttrRange << SR; |
| 1563 | return; |
| 1564 | } |
| 1565 | |
| 1566 | if (!E->isValueDependent()) { |
| 1567 | llvm::APSInt I(64); |
| 1568 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1569 | if (OE) |
| 1570 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1571 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1572 | << E->getSourceRange(); |
| 1573 | else |
| 1574 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1575 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1576 | << E->getSourceRange(); |
| 1577 | return; |
| 1578 | } |
| 1579 | |
| 1580 | if (!I.isPowerOf2()) { |
| 1581 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1582 | << E->getSourceRange(); |
| 1583 | return; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | if (OE) { |
| 1588 | if (!OE->isValueDependent()) { |
| 1589 | llvm::APSInt I(64); |
| 1590 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1591 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1592 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1593 | << OE->getSourceRange(); |
| 1594 | return; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | D->addAttr(::new (Context) |
| 1600 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1601 | } |
| 1602 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1603 | void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr, |
| 1604 | unsigned SpellingListIndex) { |
| 1605 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1606 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1607 | AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1608 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1609 | |
| 1610 | if (!ResultType->isDependentType() && |
| 1611 | !isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1612 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1613 | << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D); |
| 1614 | return; |
| 1615 | } |
| 1616 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1617 | ParamIdx Idx; |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1618 | const auto *FuncDecl = cast<FunctionDecl>(D); |
| 1619 | if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1620 | /*AttrArgNo=*/1, ParamExpr, Idx)) |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1621 | return; |
| 1622 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1623 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1624 | if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1625 | Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1626 | << &TmpAttr |
| 1627 | << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange(); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1628 | return; |
| 1629 | } |
| 1630 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1631 | D->addAttr(::new (Context) |
| 1632 | AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex)); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1635 | /// Normalize the attribute, __foo__ becomes foo. |
| 1636 | /// Returns true if normalization was applied. |
| 1637 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1638 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1639 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1640 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1641 | return true; |
| 1642 | } |
| 1643 | return false; |
| 1644 | } |
| 1645 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1646 | static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1647 | // This attribute must be applied to a function declaration. The first |
| 1648 | // argument to the attribute must be an identifier, the name of the resource, |
| 1649 | // for example: malloc. The following arguments must be argument indexes, the |
| 1650 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1651 | // 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] | 1652 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1653 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1654 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1655 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1656 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1657 | << AL << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1660 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1661 | // Figure out our Kind. |
| 1662 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1663 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1664 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1665 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1666 | // Check arguments. |
| 1667 | switch (K) { |
| 1668 | case OwnershipAttr::Takes: |
| 1669 | case OwnershipAttr::Holds: |
| 1670 | if (AL.getNumArgs() < 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1671 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1672 | return; |
| 1673 | } |
| 1674 | break; |
| 1675 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1676 | if (AL.getNumArgs() > 2) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1677 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1678 | return; |
| 1679 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1680 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1683 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1684 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1685 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1686 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1687 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1688 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1689 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1690 | SmallVector<ParamIdx, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1691 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1692 | Expr *Ex = AL.getArgAsExpr(i); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1693 | ParamIdx Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1694 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1695 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1696 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1697 | // Is the function argument a pointer type? |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1698 | QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1699 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1700 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1701 | case OwnershipAttr::Takes: |
| 1702 | case OwnershipAttr::Holds: |
| 1703 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1704 | Err = 0; |
| 1705 | break; |
| 1706 | case OwnershipAttr::Returns: |
| 1707 | if (!T->isIntegerType()) |
| 1708 | Err = 1; |
| 1709 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1710 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1711 | if (-1 != Err) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1712 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err |
| 1713 | << Ex->getSourceRange(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1714 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1715 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1718 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1719 | // Cannot have two ownership attributes of different kinds for the same |
| 1720 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1721 | if (I->getOwnKind() != K && I->args_end() != |
| 1722 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1723 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1724 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1725 | } else if (K == OwnershipAttr::Returns && |
| 1726 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1727 | // A returns attribute conflicts with any other returns attribute using |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1728 | // a different index. |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1729 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1730 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1731 | << I->args_begin()->getSourceIndex(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1732 | if (I->args_size()) |
| 1733 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1734 | << Idx.getSourceIndex() << Ex->getSourceRange(); |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1735 | return; |
| 1736 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1739 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1742 | ParamIdx *Start = OwnershipArgs.data(); |
| 1743 | unsigned Size = OwnershipArgs.size(); |
| 1744 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1745 | D->addAttr(::new (S.Context) |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1746 | OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size, |
| 1747 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1750 | static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1751 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1752 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1753 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1754 | return; |
| 1755 | } |
| 1756 | |
| 1757 | // gcc rejects |
| 1758 | // class c { |
| 1759 | // static int a __attribute__((weakref ("v2"))); |
| 1760 | // static int b() __attribute__((weakref ("f3"))); |
| 1761 | // }; |
| 1762 | // and ignores the attributes of |
| 1763 | // void f(void) { |
| 1764 | // static int a __attribute__((weakref ("v2"))); |
| 1765 | // } |
| 1766 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1767 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1768 | if (!Ctx->isFileContext()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1769 | S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1770 | << cast<NamedDecl>(D); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1771 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | // The GCC manual says |
| 1775 | // |
| 1776 | // At present, a declaration to which `weakref' is attached can only |
| 1777 | // be `static'. |
| 1778 | // |
| 1779 | // It also says |
| 1780 | // |
| 1781 | // Without a TARGET, |
| 1782 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1783 | // equivalent to `weak'. |
| 1784 | // |
| 1785 | // gcc 4.4.1 will accept |
| 1786 | // int a7 __attribute__((weakref)); |
| 1787 | // as |
| 1788 | // int a7 __attribute__((weak)); |
| 1789 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1790 | // it if this behaviour is actually used. |
| 1791 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1792 | // GCC rejects |
| 1793 | // static ((alias ("y"), weakref)). |
| 1794 | // Should we? How to check that weakref is before or after alias? |
| 1795 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1796 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1797 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1798 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1799 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1800 | if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1801 | // GCC will accept anything as the argument of weakref. Should we |
| 1802 | // check for an existing decl? |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1803 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1804 | AL.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1805 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1806 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1807 | WeakRefAttr(AL.getRange(), S.Context, |
| 1808 | AL.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1811 | static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1812 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1813 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1814 | return; |
| 1815 | |
| 1816 | // Aliases should be on declarations, not definitions. |
| 1817 | const auto *FD = cast<FunctionDecl>(D); |
| 1818 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1819 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1820 | return; |
| 1821 | } |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1822 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1823 | D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str, |
| 1824 | AL.getAttributeSpellingListIndex())); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1827 | static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1828 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1829 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1830 | return; |
| 1831 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1832 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1833 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin); |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1834 | return; |
| 1835 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1836 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1837 | S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx); |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1838 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1839 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1840 | // Aliases should be on declarations, not definitions. |
| 1841 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1842 | if (FD->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1843 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1844 | return; |
| 1845 | } |
| 1846 | } else { |
| 1847 | const auto *VD = cast<VarDecl>(D); |
| 1848 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1849 | S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1850 | return; |
| 1851 | } |
| 1852 | } |
| 1853 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1854 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1855 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1856 | D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, |
| 1857 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1860 | static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1861 | StringRef Model; |
| 1862 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1863 | // Check that it is a string. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1864 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1865 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1866 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1867 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1868 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1869 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1870 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1871 | return; |
| 1872 | } |
| 1873 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1874 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1875 | TLSModelAttr(AL.getRange(), S.Context, Model, |
| 1876 | AL.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1879 | static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1880 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1881 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1882 | D->addAttr(::new (S.Context) RestrictAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1883 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1884 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1887 | S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1888 | << AL << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1891 | static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 1892 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Erich Keane | 659c871 | 2018-09-10 14:31:56 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 1895 | if (MD->getParent()->isLambda()) { |
| 1896 | S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL; |
| 1897 | return; |
| 1898 | } |
| 1899 | } |
| 1900 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1901 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
| 1902 | return; |
| 1903 | |
| 1904 | SmallVector<IdentifierInfo *, 8> CPUs; |
| 1905 | for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) { |
| 1906 | if (!AL.isArgIdent(ArgNo)) { |
| 1907 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1908 | << AL << AANT_ArgumentIdentifier; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1909 | return; |
| 1910 | } |
| 1911 | |
| 1912 | IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo); |
| 1913 | StringRef CPUName = CPUArg->Ident->getName().trim(); |
| 1914 | |
| 1915 | if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) { |
| 1916 | S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value) |
| 1917 | << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch); |
| 1918 | return; |
| 1919 | } |
| 1920 | |
| 1921 | const TargetInfo &Target = S.Context.getTargetInfo(); |
| 1922 | if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) { |
| 1923 | return Target.CPUSpecificManglingCharacter(CPUName) == |
| 1924 | Target.CPUSpecificManglingCharacter(Cur->getName()); |
| 1925 | })) { |
| 1926 | S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries); |
| 1927 | return; |
| 1928 | } |
| 1929 | CPUs.push_back(CPUArg->Ident); |
| 1930 | } |
| 1931 | |
| 1932 | FD->setIsMultiVersion(true); |
| 1933 | if (AL.getKind() == ParsedAttr::AT_CPUSpecific) |
| 1934 | D->addAttr(::new (S.Context) CPUSpecificAttr( |
| 1935 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 1936 | AL.getAttributeSpellingListIndex())); |
| 1937 | else |
| 1938 | D->addAttr(::new (S.Context) CPUDispatchAttr( |
| 1939 | AL.getRange(), S.Context, CPUs.data(), CPUs.size(), |
| 1940 | AL.getAttributeSpellingListIndex())); |
| 1941 | } |
| 1942 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1943 | static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1944 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1945 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1946 | << AL << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1947 | return; |
| 1948 | } |
| 1949 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1950 | if (CommonAttr *CA = S.mergeCommonAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1951 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1954 | static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1955 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL)) |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1956 | return; |
| 1957 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1958 | if (AL.isDeclspecAttribute()) { |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 1959 | const auto &Triple = S.getASTContext().getTargetInfo().getTriple(); |
| 1960 | const auto &Arch = Triple.getArch(); |
| 1961 | if (Arch != llvm::Triple::x86 && |
| 1962 | (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1963 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1964 | << AL << Triple.getArchName(); |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 1965 | return; |
| 1966 | } |
| 1967 | } |
| 1968 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 1969 | D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context, |
| 1970 | AL.getAttributeSpellingListIndex())); |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1973 | static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1974 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1975 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1976 | if (!isa<ObjCMethodDecl>(D)) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 1977 | S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 1978 | << Attrs << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1979 | return; |
| 1980 | } |
| 1981 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1982 | D->addAttr(::new (S.Context) NoReturnAttr( |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 1983 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1986 | static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) { |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 1987 | if (!S.getLangOpts().CFProtectionBranch) |
| 1988 | S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored); |
| 1989 | else |
| 1990 | handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1993 | bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 1994 | if (!checkAttributeNumArgs(*this, Attrs, 0)) { |
| 1995 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1996 | return true; |
| 1997 | } |
| 1998 | |
| 1999 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2002 | bool Sema::CheckAttrTarget(const ParsedAttr &AL) { |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2003 | // Check whether the attribute is valid on the current target. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2004 | if (!AL.existsInTarget(Context.getTargetInfo())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2005 | Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2006 | AL.setInvalid(); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2007 | return true; |
| 2008 | } |
| 2009 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 2010 | return false; |
| 2011 | } |
| 2012 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2013 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2014 | |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2015 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 2016 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 2017 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2018 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2019 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 2020 | !VD->getType()->isFunctionPointerType())) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2021 | S.Diag(AL.getLoc(), AL.isCXX11Attribute() |
| 2022 | ? diag::err_attribute_wrong_decl_type |
| 2023 | : diag::warn_attribute_wrong_decl_type) |
| 2024 | << AL << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
| 2027 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2028 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2029 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2030 | AnalyzerNoReturnAttr(AL.getRange(), S.Context, |
| 2031 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2034 | // PS3 PPU-specific. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2035 | static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 2036 | /* |
| 2037 | Returning a Vector Class in Registers |
| 2038 | |
| 2039 | According to the PPU ABI specifications, a class with a single member of |
| 2040 | vector type is returned in memory when used as the return value of a |
| 2041 | function. |
| 2042 | This results in inefficient code when implementing vector classes. To return |
| 2043 | the value in a single vector register, add the vecreturn attribute to the |
| 2044 | class definition. This attribute is also applicable to struct types. |
| 2045 | |
| 2046 | Example: |
| 2047 | |
| 2048 | struct Vector |
| 2049 | { |
| 2050 | __vector float xyzw; |
| 2051 | } __attribute__((vecreturn)); |
| 2052 | |
| 2053 | Vector Add(Vector lhs, Vector rhs) |
| 2054 | { |
| 2055 | Vector result; |
| 2056 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 2057 | return result; // This will be returned in a register |
| 2058 | } |
| 2059 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 2060 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2061 | S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | } |
| 2064 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2065 | const auto *R = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2066 | int count = 0; |
| 2067 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2068 | if (!isa<CXXRecordDecl>(R)) { |
| 2069 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2070 | return; |
| 2071 | } |
| 2072 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2073 | if (!cast<CXXRecordDecl>(R)->isPOD()) { |
| 2074 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2075 | return; |
| 2076 | } |
| 2077 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2078 | for (const auto *I : R->fields()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2079 | if ((count == 1) || !I->getType()->isVectorType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2080 | S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2081 | return; |
| 2082 | } |
| 2083 | count++; |
| 2084 | } |
| 2085 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2086 | D->addAttr(::new (S.Context) VecReturnAttr( |
| 2087 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2090 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2091 | const ParsedAttr &AL) { |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2092 | if (isa<ParmVarDecl>(D)) { |
| 2093 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 2094 | // parameter of a function declaration or lambda. |
| 2095 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2096 | S.Diag(AL.getLoc(), |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2097 | diag::err_carries_dependency_param_not_function_decl); |
| 2098 | return; |
| 2099 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2100 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2101 | |
| 2102 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2103 | AL.getRange(), S.Context, |
| 2104 | AL.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2107 | static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2108 | bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName(); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2109 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2110 | // 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] | 2111 | // about using it as an extension. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2112 | if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2113 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2114 | |
| 2115 | D->addAttr(::new (S.Context) UnusedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2116 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2119 | static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2120 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2121 | if (AL.getNumArgs() && |
| 2122 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2123 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2124 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2125 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2126 | ConstructorAttr(AL.getRange(), S.Context, priority, |
| 2127 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2130 | static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 2131 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2132 | if (AL.getNumArgs() && |
| 2133 | !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority)) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2134 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2135 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2136 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2137 | DestructorAttr(AL.getRange(), S.Context, priority, |
| 2138 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2141 | template <typename AttrTy> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2142 | static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2143 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2144 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2145 | if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2146 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2147 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2148 | D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str, |
| 2149 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2152 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2153 | const ParsedAttr &AL) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2154 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2155 | S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2156 | << AL << AL.getRange(); |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 2157 | return; |
| 2158 | } |
| 2159 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2160 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2161 | ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context, |
| 2162 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2165 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2166 | IdentifierInfo *Platform, |
| 2167 | VersionTuple Introduced, |
| 2168 | VersionTuple Deprecated, |
| 2169 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2170 | StringRef PlatformName |
| 2171 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2172 | if (PlatformName.empty()) |
| 2173 | PlatformName = Platform->getName(); |
| 2174 | |
| 2175 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2176 | // of these steps are needed). |
| 2177 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2178 | !(Introduced <= Deprecated)) { |
| 2179 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2180 | << 1 << PlatformName << Deprecated.getAsString() |
| 2181 | << 0 << Introduced.getAsString(); |
| 2182 | return true; |
| 2183 | } |
| 2184 | |
| 2185 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2186 | !(Introduced <= Obsoleted)) { |
| 2187 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2188 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2189 | << 0 << Introduced.getAsString(); |
| 2190 | return true; |
| 2191 | } |
| 2192 | |
| 2193 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2194 | !(Deprecated <= Obsoleted)) { |
| 2195 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2196 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2197 | << 1 << Deprecated.getAsString(); |
| 2198 | return true; |
| 2199 | } |
| 2200 | |
| 2201 | return false; |
| 2202 | } |
| 2203 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2204 | /// Check whether the two versions match. |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2205 | /// |
| 2206 | /// If either version tuple is empty, then they are assumed to match. If |
| 2207 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2208 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2209 | bool BeforeIsOkay) { |
| 2210 | if (X.empty() || Y.empty()) |
| 2211 | return true; |
| 2212 | |
| 2213 | if (X == Y) |
| 2214 | return true; |
| 2215 | |
| 2216 | if (BeforeIsOkay && X < Y) |
| 2217 | return true; |
| 2218 | |
| 2219 | return false; |
| 2220 | } |
| 2221 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2222 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2223 | IdentifierInfo *Platform, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2224 | bool Implicit, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2225 | VersionTuple Introduced, |
| 2226 | VersionTuple Deprecated, |
| 2227 | VersionTuple Obsoleted, |
| 2228 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2229 | StringRef Message, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2230 | bool IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2231 | StringRef Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2232 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2233 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2234 | VersionTuple MergedIntroduced = Introduced; |
| 2235 | VersionTuple MergedDeprecated = Deprecated; |
| 2236 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2237 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2238 | bool OverrideOrImpl = false; |
| 2239 | switch (AMK) { |
| 2240 | case AMK_None: |
| 2241 | case AMK_Redeclaration: |
| 2242 | OverrideOrImpl = false; |
| 2243 | break; |
| 2244 | |
| 2245 | case AMK_Override: |
| 2246 | case AMK_ProtocolImplementation: |
| 2247 | OverrideOrImpl = true; |
| 2248 | break; |
| 2249 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2250 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2251 | if (D->hasAttrs()) { |
| 2252 | AttrVec &Attrs = D->getAttrs(); |
| 2253 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2254 | const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2255 | if (!OldAA) { |
| 2256 | ++i; |
| 2257 | continue; |
| 2258 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2259 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2260 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2261 | if (OldPlatform != Platform) { |
| 2262 | ++i; |
| 2263 | continue; |
| 2264 | } |
| 2265 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2266 | // If there is an existing availability attribute for this platform that |
| 2267 | // is explicit and the new one is implicit use the explicit one and |
| 2268 | // discard the new implicit attribute. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2269 | if (!OldAA->isImplicit() && Implicit) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2270 | return nullptr; |
| 2271 | } |
| 2272 | |
| 2273 | // If there is an existing attribute for this platform that is implicit |
| 2274 | // and the new attribute is explicit then erase the old one and |
| 2275 | // continue processing the attributes. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2276 | if (!Implicit && OldAA->isImplicit()) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2277 | Attrs.erase(Attrs.begin() + i); |
| 2278 | --e; |
| 2279 | continue; |
| 2280 | } |
| 2281 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2282 | FoundAny = true; |
| 2283 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2284 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2285 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2286 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2287 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2288 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2289 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2290 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2291 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2292 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2293 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2294 | int Which = -1; |
| 2295 | VersionTuple FirstVersion; |
| 2296 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2297 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2298 | Which = 0; |
| 2299 | FirstVersion = OldIntroduced; |
| 2300 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2301 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2302 | Which = 1; |
| 2303 | FirstVersion = Deprecated; |
| 2304 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2305 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2306 | Which = 2; |
| 2307 | FirstVersion = Obsoleted; |
| 2308 | SecondVersion = OldObsoleted; |
| 2309 | } |
| 2310 | |
| 2311 | if (Which == -1) { |
| 2312 | Diag(OldAA->getLocation(), |
| 2313 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2314 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2315 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2316 | } else { |
| 2317 | Diag(OldAA->getLocation(), |
| 2318 | diag::warn_mismatched_availability_override) |
| 2319 | << Which |
| 2320 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2321 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2322 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2323 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2324 | if (AMK == AMK_Override) |
| 2325 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2326 | else |
| 2327 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2328 | } else { |
| 2329 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2330 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2331 | } |
| 2332 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2333 | Attrs.erase(Attrs.begin() + i); |
| 2334 | --e; |
| 2335 | continue; |
| 2336 | } |
| 2337 | |
| 2338 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2339 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2340 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2341 | |
| 2342 | if (MergedIntroduced2.empty()) |
| 2343 | MergedIntroduced2 = OldIntroduced; |
| 2344 | if (MergedDeprecated2.empty()) |
| 2345 | MergedDeprecated2 = OldDeprecated; |
| 2346 | if (MergedObsoleted2.empty()) |
| 2347 | MergedObsoleted2 = OldObsoleted; |
| 2348 | |
| 2349 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2350 | MergedIntroduced2, MergedDeprecated2, |
| 2351 | MergedObsoleted2)) { |
| 2352 | Attrs.erase(Attrs.begin() + i); |
| 2353 | --e; |
| 2354 | continue; |
| 2355 | } |
| 2356 | |
| 2357 | MergedIntroduced = MergedIntroduced2; |
| 2358 | MergedDeprecated = MergedDeprecated2; |
| 2359 | MergedObsoleted = MergedObsoleted2; |
| 2360 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2361 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | if (FoundAny && |
| 2365 | MergedIntroduced == Introduced && |
| 2366 | MergedDeprecated == Deprecated && |
| 2367 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2368 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2369 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2370 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2371 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2372 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2373 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2374 | !OverrideOrImpl) { |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2375 | auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2376 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2377 | Obsoleted, IsUnavailable, Message, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2378 | IsStrict, Replacement, |
| 2379 | AttrSpellingListIndex); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2380 | Avail->setImplicit(Implicit); |
| 2381 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2382 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2383 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2386 | static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2387 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2388 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2389 | IdentifierLoc *Platform = AL.getArgAsIdent(0); |
| 2390 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2391 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2392 | IdentifierInfo *II = Platform->Ident; |
| 2393 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2394 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2395 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2396 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2397 | auto *ND = dyn_cast<NamedDecl>(D); |
Alex Lorenz | 472cc79 | 2017-04-20 09:35:02 +0000 | [diff] [blame] | 2398 | if (!ND) // We warned about this already, so just return. |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2399 | return; |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2400 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2401 | AvailabilityChange Introduced = AL.getAvailabilityIntroduced(); |
| 2402 | AvailabilityChange Deprecated = AL.getAvailabilityDeprecated(); |
| 2403 | AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted(); |
| 2404 | bool IsUnavailable = AL.getUnavailableLoc().isValid(); |
| 2405 | bool IsStrict = AL.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2406 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2407 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2408 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2409 | StringRef Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2410 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr())) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2411 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2412 | |
Michael Wu | 260e962 | 2018-11-12 02:44:33 +0000 | [diff] [blame] | 2413 | if (II->isStr("swift")) { |
| 2414 | if (Introduced.isValid() || Obsoleted.isValid() || |
| 2415 | (!IsUnavailable && !Deprecated.isValid())) { |
| 2416 | S.Diag(AL.getLoc(), |
| 2417 | diag::warn_availability_swift_unavailable_deprecated_only); |
| 2418 | return; |
| 2419 | } |
| 2420 | } |
| 2421 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2422 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2423 | false/*Implicit*/, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2424 | Introduced.Version, |
| 2425 | Deprecated.Version, |
| 2426 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2427 | IsUnavailable, Str, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2428 | IsStrict, Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2429 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2430 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2431 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2432 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2435 | // matches before the start of the watchOS platform. |
| 2436 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2437 | IdentifierInfo *NewII = nullptr; |
| 2438 | if (II->getName() == "ios") |
| 2439 | NewII = &S.Context.Idents.get("watchos"); |
| 2440 | else if (II->getName() == "ios_app_extension") |
| 2441 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2442 | |
| 2443 | if (NewII) { |
| 2444 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2445 | if (Version.empty()) |
| 2446 | return Version; |
| 2447 | auto Major = Version.getMajor(); |
| 2448 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2449 | if (NewMajor >= 2) { |
| 2450 | if (Version.getMinor().hasValue()) { |
| 2451 | if (Version.getSubminor().hasValue()) |
| 2452 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2453 | Version.getSubminor().getValue()); |
| 2454 | else |
| 2455 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | return VersionTuple(2, 0); |
| 2460 | }; |
| 2461 | |
| 2462 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2463 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2464 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2465 | |
| 2466 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2467 | AL.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2468 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2469 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2470 | NewIntroduced, |
| 2471 | NewDeprecated, |
| 2472 | NewObsoleted, |
| 2473 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2474 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2475 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2476 | Sema::AMK_None, |
| 2477 | Index); |
| 2478 | if (NewAttr) |
| 2479 | D->addAttr(NewAttr); |
| 2480 | } |
| 2481 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2482 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2483 | // matches before the start of the tvOS platform. |
| 2484 | IdentifierInfo *NewII = nullptr; |
| 2485 | if (II->getName() == "ios") |
| 2486 | NewII = &S.Context.Idents.get("tvos"); |
| 2487 | else if (II->getName() == "ios_app_extension") |
| 2488 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2489 | |
| 2490 | if (NewII) { |
| 2491 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2492 | AL.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2493 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2494 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2495 | Introduced.Version, |
| 2496 | Deprecated.Version, |
| 2497 | Obsoleted.Version, |
| 2498 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2499 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2500 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2501 | Sema::AMK_None, |
| 2502 | Index); |
| 2503 | if (NewAttr) |
| 2504 | D->addAttr(NewAttr); |
| 2505 | } |
| 2506 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2509 | static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2510 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2511 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2512 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2513 | assert(checkAttributeAtMostNumArgs(S, AL, 3) && |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2514 | "Invalid number of arguments in an external_source_symbol attribute"); |
| 2515 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2516 | StringRef Language; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2517 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2518 | Language = SE->getString(); |
| 2519 | StringRef DefinedIn; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2520 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1))) |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2521 | DefinedIn = SE->getString(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2522 | bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2523 | |
| 2524 | D->addAttr(::new (S.Context) ExternalSourceSymbolAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2525 | AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration, |
| 2526 | AL.getAttributeSpellingListIndex())); |
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> |
| 2530 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2531 | typename T::VisibilityType value, |
| 2532 | unsigned attrSpellingListIndex) { |
| 2533 | T *existingAttr = D->getAttr<T>(); |
| 2534 | if (existingAttr) { |
| 2535 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2536 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2537 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2538 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2539 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2540 | D->dropAttr<T>(); |
| 2541 | } |
| 2542 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2543 | } |
| 2544 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2545 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2546 | VisibilityAttr::VisibilityType Vis, |
| 2547 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2548 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2549 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2552 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2553 | TypeVisibilityAttr::VisibilityType Vis, |
| 2554 | unsigned AttrSpellingListIndex) { |
| 2555 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2556 | AttrSpellingListIndex); |
| 2557 | } |
| 2558 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2559 | static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2560 | bool isTypeVisibility) { |
| 2561 | // Visibility attributes don't mean anything on a typedef. |
| 2562 | if (isa<TypedefNameDecl>(D)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2563 | S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2564 | return; |
| 2565 | } |
| 2566 | |
| 2567 | // 'type_visibility' can only go on a type or namespace. |
| 2568 | if (isTypeVisibility && |
| 2569 | !(isa<TagDecl>(D) || |
| 2570 | isa<ObjCInterfaceDecl>(D) || |
| 2571 | isa<NamespaceDecl>(D))) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2572 | S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2573 | << AL << ExpectedTypeOrNamespace; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2574 | return; |
| 2575 | } |
| 2576 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2577 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2578 | StringRef TypeStr; |
| 2579 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2580 | if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2581 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2582 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2583 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2584 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2585 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL |
| 2586 | << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2587 | return; |
| 2588 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2589 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2590 | // Complain about attempts to use protected visibility on targets |
| 2591 | // (like Darwin) that don't support it. |
| 2592 | if (type == VisibilityAttr::Protected && |
| 2593 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2594 | S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2595 | type = VisibilityAttr::Default; |
| 2596 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2597 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2598 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 2599 | Attr *newAttr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2600 | if (isTypeVisibility) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2601 | newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(), |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2602 | (TypeVisibilityAttr::VisibilityType) type, |
| 2603 | Index); |
| 2604 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2605 | newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2606 | } |
| 2607 | if (newAttr) |
| 2608 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2611 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2612 | const auto *M = cast<ObjCMethodDecl>(D); |
| 2613 | if (!AL.isArgIdent(0)) { |
| 2614 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2615 | << AL << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2616 | return; |
| 2617 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2618 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2619 | IdentifierLoc *IL = AL.getArgAsIdent(0); |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2620 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2621 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2622 | 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] | 2623 | return; |
| 2624 | } |
| 2625 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2626 | if (F == ObjCMethodFamilyAttr::OMF_init && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2627 | !M->getReturnType()->isObjCObjectPointerType()) { |
| 2628 | S.Diag(M->getLocation(), diag::err_init_method_bad_return_type) |
| 2629 | << M->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2630 | // Ignore the attribute. |
| 2631 | return; |
| 2632 | } |
| 2633 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2634 | D->addAttr(new (S.Context) ObjCMethodFamilyAttr( |
| 2635 | AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2638 | static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2639 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2640 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2641 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2642 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2643 | return; |
| 2644 | } |
| 2645 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2646 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2647 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2648 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2649 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2650 | return; |
| 2651 | } |
| 2652 | } |
| 2653 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2654 | // It is okay to include this attribute on properties, e.g.: |
| 2655 | // |
| 2656 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2657 | // |
| 2658 | // 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] | 2659 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2660 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2661 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2662 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2663 | ObjCNSObjectAttr(AL.getRange(), S.Context, |
| 2664 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2667 | static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2668 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2669 | QualType T = TD->getUnderlyingType(); |
| 2670 | if (!T->isObjCObjectPointerType()) { |
| 2671 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2672 | return; |
| 2673 | } |
| 2674 | } else { |
| 2675 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2676 | return; |
| 2677 | } |
| 2678 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2679 | ObjCIndependentClassAttr(AL.getRange(), S.Context, |
| 2680 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2683 | static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2684 | if (!AL.isArgIdent(0)) { |
| 2685 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2686 | << AL << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2687 | return; |
| 2688 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2689 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2690 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2691 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2692 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2693 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2694 | return; |
| 2695 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2696 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2697 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2698 | BlocksAttr(AL.getRange(), S.Context, type, |
| 2699 | AL.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2702 | static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2703 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2704 | if (AL.getNumArgs() > 0) { |
| 2705 | Expr *E = AL.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2706 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2707 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2708 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2709 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2710 | << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2711 | return; |
| 2712 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2713 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2714 | if (Idx.isSigned() && Idx.isNegative()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2715 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2716 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2717 | return; |
| 2718 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2719 | |
| 2720 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2723 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2724 | if (AL.getNumArgs() > 1) { |
| 2725 | Expr *E = AL.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2726 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2727 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2728 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2729 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2730 | << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2731 | return; |
| 2732 | } |
| 2733 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2734 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2735 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2736 | // FIXME: This error message could be improved, it would be nice |
| 2737 | // to say what the bounds actually are. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2738 | S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2739 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
| 2742 | } |
| 2743 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2744 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2745 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2746 | if (isa<FunctionNoProtoType>(FT)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2747 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2748 | return; |
| 2749 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2751 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2752 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2753 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2754 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2755 | } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2756 | if (!MD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2757 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2758 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2759 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2760 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2761 | if (!BD->isVariadic()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2762 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2763 | return; |
| 2764 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2765 | } else if (const auto *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2766 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2767 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2768 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2769 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2770 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2771 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2772 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2773 | S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2774 | return; |
| 2775 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2776 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2777 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2778 | << AL << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2779 | return; |
| 2780 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2781 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2782 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2783 | << AL << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2784 | return; |
| 2785 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2786 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2787 | SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos, |
| 2788 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2791 | static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2792 | if (D->getFunctionType() && |
| 2793 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2794 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2795 | return; |
| 2796 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2797 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2798 | if (MD->getReturnType()->isVoidType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2799 | S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1; |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2800 | return; |
| 2801 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2802 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2803 | // If this is spelled as the standard C++17 attribute, but not in C++17, warn |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2804 | // about using it as an extension. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2805 | if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() && |
| 2806 | !AL.getScopeName()) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2807 | S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL; |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2808 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2809 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2810 | WarnUnusedResultAttr(AL.getRange(), S.Context, |
| 2811 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2814 | static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2815 | // weak_import only applies to variable & function declarations. |
| 2816 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2817 | if (!D->canBeWeakImported(isDef)) { |
| 2818 | if (isDef) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2819 | S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2820 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2821 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2822 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2823 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2824 | // Nothing to warn about here. |
| 2825 | } else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2826 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2827 | << AL << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2828 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2829 | return; |
| 2830 | } |
| 2831 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2832 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2833 | WeakImportAttr(AL.getRange(), S.Context, |
| 2834 | AL.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2837 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2838 | template <typename WorkGroupAttr> |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2839 | static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2840 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2841 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2842 | const Expr *E = AL.getArgAsExpr(i); |
Andrew Savonichev | d353e6d | 2018-09-06 11:54:09 +0000 | [diff] [blame] | 2843 | if (!checkUInt32Argument(S, AL, E, WGSize[i], i, |
| 2844 | /*StrictlyUnsigned=*/true)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2845 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2846 | if (WGSize[i] == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2847 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2848 | << AL << E->getSourceRange(); |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2849 | return; |
| 2850 | } |
| 2851 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2852 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2853 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2854 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2855 | Existing->getYDim() == WGSize[1] && |
| 2856 | Existing->getZDim() == WGSize[2])) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2857 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2858 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2859 | D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context, |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2860 | WGSize[0], WGSize[1], WGSize[2], |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2861 | AL.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2864 | // Handles intel_reqd_sub_group_size. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2865 | static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2866 | uint32_t SGSize; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2867 | const Expr *E = AL.getArgAsExpr(0); |
| 2868 | if (!checkUInt32Argument(S, AL, E, SGSize)) |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2869 | return; |
| 2870 | if (SGSize == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2871 | S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2872 | << AL << E->getSourceRange(); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2873 | return; |
| 2874 | } |
| 2875 | |
| 2876 | OpenCLIntelReqdSubGroupSizeAttr *Existing = |
| 2877 | D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>(); |
| 2878 | if (Existing && Existing->getSubGroupSize() != SGSize) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2879 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2880 | |
| 2881 | D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2882 | AL.getRange(), S.Context, SGSize, |
| 2883 | AL.getAttributeSpellingListIndex())); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2886 | static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2887 | if (!AL.hasParsedType()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2888 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2889 | return; |
| 2890 | } |
| 2891 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2892 | TypeSourceInfo *ParmTSI = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2893 | QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2894 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2895 | |
| 2896 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2897 | (ParmType->isBooleanType() || |
| 2898 | !ParmType->isIntegralType(S.getASTContext()))) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2899 | S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint) |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2900 | << ParmType; |
| 2901 | return; |
| 2902 | } |
| 2903 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2904 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2905 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 2906 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2907 | return; |
| 2908 | } |
| 2909 | } |
| 2910 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2911 | D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2912 | ParmTSI, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2913 | AL.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2916 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2917 | StringRef Name, |
| 2918 | unsigned AttrSpellingListIndex) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2919 | // Explicit or partial specializations do not inherit |
| 2920 | // the section attribute from the primary template. |
| 2921 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 2922 | if (AttrSpellingListIndex == SectionAttr::Declspec_allocate && |
| 2923 | FD->isFunctionTemplateSpecialization()) |
| 2924 | return nullptr; |
| 2925 | } |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2926 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2927 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2928 | return nullptr; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2929 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 2930 | << 1 /*section*/; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2931 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2932 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2933 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2934 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2935 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2938 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2939 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2940 | if (!Error.empty()) { |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2941 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2942 | << 1 /*'section'*/; |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2943 | return false; |
| 2944 | } |
| 2945 | return true; |
| 2946 | } |
| 2947 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2948 | static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2949 | // Make sure that there is a string literal as the sections's single |
| 2950 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2951 | StringRef Str; |
| 2952 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2953 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2954 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2956 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2957 | return; |
| 2958 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2959 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2960 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2961 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2962 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2963 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2964 | return; |
| 2965 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 2967 | unsigned Index = AL.getAttributeSpellingListIndex(); |
| 2968 | SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2969 | if (NewAttr) |
| 2970 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 2973 | static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) { |
| 2974 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName); |
| 2975 | if (!Error.empty()) { |
| 2976 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error |
| 2977 | << 0 /*'code-seg'*/; |
| 2978 | return false; |
| 2979 | } |
| 2980 | return true; |
| 2981 | } |
| 2982 | |
| 2983 | CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range, |
| 2984 | StringRef Name, |
| 2985 | unsigned AttrSpellingListIndex) { |
| 2986 | // Explicit or partial specializations do not inherit |
| 2987 | // the code_seg attribute from the primary template. |
| 2988 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 2989 | if (FD->isFunctionTemplateSpecialization()) |
| 2990 | return nullptr; |
| 2991 | } |
| 2992 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 2993 | if (ExistingAttr->getName() == Name) |
| 2994 | return nullptr; |
| 2995 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section) |
| 2996 | << 0 /*codeseg*/; |
| 2997 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2998 | return nullptr; |
| 2999 | } |
| 3000 | return ::new (Context) CodeSegAttr(Range, Context, Name, |
| 3001 | AttrSpellingListIndex); |
| 3002 | } |
| 3003 | |
| 3004 | static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
| 3005 | StringRef Str; |
| 3006 | SourceLocation LiteralLoc; |
| 3007 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc)) |
| 3008 | return; |
| 3009 | if (!checkCodeSegName(S, LiteralLoc, Str)) |
| 3010 | return; |
| 3011 | if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) { |
| 3012 | if (!ExistingAttr->isImplicit()) { |
| 3013 | S.Diag(AL.getLoc(), |
| 3014 | ExistingAttr->getName() == Str |
| 3015 | ? diag::warn_duplicate_codeseg_attribute |
| 3016 | : diag::err_conflicting_codeseg_attribute); |
| 3017 | return; |
| 3018 | } |
| 3019 | D->dropAttr<CodeSegAttr>(); |
| 3020 | } |
| 3021 | if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str, |
| 3022 | AL.getAttributeSpellingListIndex())) |
| 3023 | D->addAttr(CSA); |
| 3024 | } |
| 3025 | |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3026 | // Check for things we'd like to warn about. Multiversioning issues are |
| 3027 | // handled later in the process, once we know how many exist. |
| 3028 | bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 3029 | enum FirstParam { Unsupported, Duplicate }; |
| 3030 | enum SecondParam { None, Architecture }; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3031 | for (auto Str : {"tune=", "fpmath="}) |
| 3032 | if (AttrStr.find(Str) != StringRef::npos) |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3033 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3034 | << Unsupported << None << Str; |
| 3035 | |
| 3036 | TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr); |
| 3037 | |
| 3038 | if (!ParsedAttrs.Architecture.empty() && |
| 3039 | !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture)) |
| 3040 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3041 | << Unsupported << Architecture << ParsedAttrs.Architecture; |
| 3042 | |
| 3043 | if (ParsedAttrs.DuplicateArchitecture) |
| 3044 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3045 | << Duplicate << None << "arch="; |
| 3046 | |
| 3047 | for (const auto &Feature : ParsedAttrs.Features) { |
| 3048 | auto CurFeature = StringRef(Feature).drop_front(); // remove + or -. |
| 3049 | if (!Context.getTargetInfo().isValidFeatureName(CurFeature)) |
| 3050 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3051 | << Unsupported << None << CurFeature; |
| 3052 | } |
| 3053 | |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3054 | return false; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3057 | static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3058 | StringRef Str; |
| 3059 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3060 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) || |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3061 | S.checkTargetAttr(LiteralLoc, Str)) |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3062 | return; |
Erich Keane | 29636aa | 2018-02-16 17:31:59 +0000 | [diff] [blame] | 3063 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3064 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3065 | TargetAttr *NewAttr = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3066 | ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3067 | D->addAttr(NewAttr); |
| 3068 | } |
| 3069 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3070 | static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3071 | Expr *E = AL.getArgAsExpr(0); |
| 3072 | uint32_t VecWidth; |
| 3073 | if (!checkUInt32Argument(S, AL, E, VecWidth)) { |
| 3074 | AL.setInvalid(); |
| 3075 | return; |
| 3076 | } |
| 3077 | |
| 3078 | MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>(); |
| 3079 | if (Existing && Existing->getVectorWidth() != VecWidth) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3080 | S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL; |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| 3083 | |
| 3084 | D->addAttr(::new (S.Context) |
| 3085 | MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth, |
| 3086 | AL.getAttributeSpellingListIndex())); |
| 3087 | } |
| 3088 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3089 | static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3090 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3091 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3092 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3093 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3094 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3095 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 3096 | // will warn the user. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3097 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3098 | if (DRE->hasQualifier()) |
| 3099 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3100 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3101 | NI = DRE->getNameInfo(); |
| 3102 | if (!FD) { |
| 3103 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 3104 | << NI.getName(); |
| 3105 | return; |
| 3106 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3107 | } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3108 | if (ULE->hasExplicitTemplateArgs()) |
| 3109 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3110 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 3111 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 3112 | if (!FD) { |
| 3113 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 3114 | << NI.getName(); |
| 3115 | if (ULE->getType() == S.Context.OverloadTy) |
| 3116 | S.NoteAllOverloadCandidates(ULE); |
| 3117 | return; |
| 3118 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3119 | } else { |
| 3120 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3121 | return; |
| 3122 | } |
| 3123 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3124 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3125 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3126 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3127 | return; |
| 3128 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3129 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3130 | // We're currently more strict than GCC about what function types we accept. |
| 3131 | // 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] | 3132 | QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType()); |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3133 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3134 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3135 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3136 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 3137 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3138 | return; |
| 3139 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3140 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3141 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3142 | CleanupAttr(AL.getRange(), S.Context, FD, |
| 3143 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3146 | static void handleEnumExtensibilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3147 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3148 | if (!AL.isArgIdent(0)) { |
| 3149 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3150 | << AL << 0 << AANT_ArgumentIdentifier; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3151 | return; |
| 3152 | } |
| 3153 | |
| 3154 | EnumExtensibilityAttr::Kind ExtensibilityKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3155 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3156 | if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(), |
| 3157 | ExtensibilityKind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3158 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3159 | return; |
| 3160 | } |
| 3161 | |
| 3162 | D->addAttr(::new (S.Context) EnumExtensibilityAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3163 | AL.getRange(), S.Context, ExtensibilityKind, |
| 3164 | AL.getAttributeSpellingListIndex())); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3167 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3168 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3169 | static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3170 | Expr *IdxExpr = AL.getArgAsExpr(0); |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3171 | ParamIdx Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3172 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3173 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3174 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3175 | // Make sure the format string is really a string. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3176 | QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3177 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3178 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 3179 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3180 | !isCFStringType(Ty, S.Context) && |
| 3181 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3182 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3183 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3184 | << "a string type" << IdxExpr->getSourceRange() |
| 3185 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3186 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3187 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3188 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3189 | if (!isNSStringType(Ty, S.Context) && |
| 3190 | !isCFStringType(Ty, S.Context) && |
| 3191 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3192 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3193 | S.Diag(AL.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3194 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 3195 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3196 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 3199 | D->addAttr(::new (S.Context) FormatArgAttr( |
| 3200 | AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3203 | enum FormatAttrKind { |
| 3204 | CFStringFormat, |
| 3205 | NSStringFormat, |
| 3206 | StrftimeFormat, |
| 3207 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3208 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3209 | InvalidFormat |
| 3210 | }; |
| 3211 | |
| 3212 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3213 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3214 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3215 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3216 | // Check for formats that get handled specially. |
| 3217 | .Case("NSString", NSStringFormat) |
| 3218 | .Case("CFString", CFStringFormat) |
| 3219 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3220 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3221 | // Otherwise, check for supported formats. |
| 3222 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3223 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3224 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 3225 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 3226 | .Case("os_trace", SupportedFormat) |
| 3227 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3228 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3229 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3230 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3233 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3234 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3235 | static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3236 | if (!S.getLangOpts().CPlusPlus) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3237 | S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3238 | return; |
| 3239 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3240 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3241 | if (S.getCurFunctionOrMethodDecl()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3242 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3243 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3244 | return; |
| 3245 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3246 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3247 | if (S.Context.getAsArrayType(T)) |
| 3248 | T = S.Context.getBaseElementType(T); |
| 3249 | if (!T->getAs<RecordType>()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3250 | S.Diag(AL.getLoc(), diag::err_init_priority_object_attr); |
| 3251 | AL.setInvalid(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3252 | return; |
| 3253 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3254 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3255 | Expr *E = AL.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3256 | uint32_t prioritynum; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3257 | if (!checkUInt32Argument(S, AL, E, prioritynum)) { |
| 3258 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3259 | return; |
| 3260 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3261 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3262 | if (prioritynum < 101 || prioritynum > 65535) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3263 | S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3264 | << E->getSourceRange() << AL << 101 << 65535; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3265 | AL.setInvalid(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3266 | return; |
| 3267 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3268 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3269 | InitPriorityAttr(AL.getRange(), S.Context, prioritynum, |
| 3270 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3273 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3274 | IdentifierInfo *Format, int FormatIdx, |
| 3275 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3276 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3277 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3278 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3279 | if (F->getType() == Format && |
| 3280 | F->getFormatIdx() == FormatIdx && |
| 3281 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3282 | // If we don't have a valid location for this attribute, adopt the |
| 3283 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3284 | if (F->getLocation().isInvalid()) |
| 3285 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3286 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3287 | } |
| 3288 | } |
| 3289 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3290 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3291 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3294 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3295 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3296 | static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3297 | if (!AL.isArgIdent(0)) { |
| 3298 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3299 | << AL << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3302 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3303 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3304 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3305 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3306 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3307 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3308 | IdentifierInfo *II = AL.getArgAsIdent(0)->Ident; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3309 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3310 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3311 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3312 | // If we've modified the string name, we need a new identifier for it. |
| 3313 | II = &S.Context.Idents.get(Format); |
| 3314 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3315 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3316 | // Check for supported formats. |
| 3317 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3318 | |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3319 | if (Kind == IgnoredFormat) |
| 3320 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3321 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3322 | if (Kind == InvalidFormat) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3323 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3324 | << AL << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3325 | return; |
| 3326 | } |
| 3327 | |
| 3328 | // checks for the 2nd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3329 | Expr *IdxExpr = AL.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3330 | uint32_t Idx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3331 | if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3332 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3333 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3334 | if (Idx < 1 || Idx > NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3335 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3336 | << AL << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3337 | return; |
| 3338 | } |
| 3339 | |
| 3340 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3341 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3342 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3343 | if (HasImplicitThisParam) { |
| 3344 | if (ArgIdx == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3345 | S.Diag(AL.getLoc(), |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3346 | diag::err_format_attribute_implicit_this_format_string) |
| 3347 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3348 | return; |
| 3349 | } |
| 3350 | ArgIdx--; |
| 3351 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3353 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3354 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3355 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3356 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3357 | if (!isCFStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3358 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3359 | << "a CFString" << IdxExpr->getSourceRange() |
| 3360 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3361 | return; |
| 3362 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3363 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3364 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3365 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3366 | if (!isNSStringType(Ty, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3367 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3368 | << "an NSString" << IdxExpr->getSourceRange() |
| 3369 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3370 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3371 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3372 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3373 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3374 | S.Diag(AL.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3375 | << "a string type" << IdxExpr->getSourceRange() |
| 3376 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3377 | return; |
| 3378 | } |
| 3379 | |
| 3380 | // check the 3rd argument |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3381 | Expr *FirstArgExpr = AL.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3382 | uint32_t FirstArg; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3383 | if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3384 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3385 | |
| 3386 | // check if the function is variadic if the 3rd argument non-zero |
| 3387 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3388 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3389 | ++NumArgs; // +1 for ... |
| 3390 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3391 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3392 | return; |
| 3393 | } |
| 3394 | } |
| 3395 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3396 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3397 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3398 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3399 | if (FirstArg != 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3400 | S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3401 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3402 | return; |
| 3403 | } |
| 3404 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3405 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3406 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3407 | << AL << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3408 | return; |
| 3409 | } |
| 3410 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3411 | FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3412 | Idx, FirstArg, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3413 | AL.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3414 | if (NewAttr) |
| 3415 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3418 | static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3419 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3420 | RecordDecl *RD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3421 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3422 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3423 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3424 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3425 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3426 | |
| 3427 | if (!RD || !RD->isUnion()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3428 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL |
| 3429 | << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3430 | return; |
| 3431 | } |
| 3432 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3433 | if (!RD->isCompleteDefinition()) { |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3434 | if (!RD->isBeingDefined()) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3435 | S.Diag(AL.getLoc(), |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3436 | diag::warn_transparent_union_attribute_not_definition); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3437 | return; |
| 3438 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3439 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3440 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3441 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3442 | if (Field == FieldEnd) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3443 | S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3444 | return; |
| 3445 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3446 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3447 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3448 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3449 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3450 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3451 | diag::warn_transparent_union_attribute_floating) |
| 3452 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3453 | return; |
| 3454 | } |
| 3455 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3456 | if (FirstType->isIncompleteType()) |
| 3457 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3458 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3459 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3460 | for (; Field != FieldEnd; ++Field) { |
| 3461 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3462 | if (FieldType->isIncompleteType()) |
| 3463 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3464 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3465 | // members of the union would all have the same calling convention as the |
| 3466 | // first member of the union. Checking just the size and alignment isn't |
| 3467 | // sufficient (consider structs passed on the stack instead of in registers |
| 3468 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3469 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3470 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3471 | // Warn if we drop the attribute. |
| 3472 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3473 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3474 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3475 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3476 | diag::warn_transparent_union_attribute_field_size_align) |
| 3477 | << isSize << Field->getDeclName() << FieldBits; |
| 3478 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3479 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3480 | diag::note_transparent_union_first_field_size_align) |
| 3481 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3482 | return; |
| 3483 | } |
| 3484 | } |
| 3485 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3486 | RD->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3487 | TransparentUnionAttr(AL.getRange(), S.Context, |
| 3488 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3491 | static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3492 | // Make sure that there is a string literal as the annotation's single |
| 3493 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3494 | StringRef Str; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3495 | if (!S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3496 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3497 | |
| 3498 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3499 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3500 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3501 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3502 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3503 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3504 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3505 | AnnotateAttr(AL.getRange(), S.Context, Str, |
| 3506 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3509 | static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3510 | S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 3511 | AL.getAttributeSpellingListIndex()); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3515 | unsigned SpellingListIndex) { |
| 3516 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3517 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3518 | |
| 3519 | QualType T; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3520 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3521 | T = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3522 | else if (const auto *VD = dyn_cast<ValueDecl>(D)) |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3523 | T = VD->getType(); |
| 3524 | else |
| 3525 | llvm_unreachable("Unknown decl type for align_value"); |
| 3526 | |
| 3527 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3528 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3529 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3530 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3531 | return; |
| 3532 | } |
| 3533 | |
| 3534 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3535 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3536 | ExprResult ICE |
| 3537 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3538 | diag::err_align_value_attribute_argument_not_int, |
| 3539 | /*AllowFold*/ false); |
| 3540 | if (ICE.isInvalid()) |
| 3541 | return; |
| 3542 | |
| 3543 | if (!Alignment.isPowerOf2()) { |
| 3544 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3545 | << E->getSourceRange(); |
| 3546 | return; |
| 3547 | } |
| 3548 | |
| 3549 | D->addAttr(::new (Context) |
| 3550 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3551 | SpellingListIndex)); |
| 3552 | return; |
| 3553 | } |
| 3554 | |
| 3555 | // Save dependent expressions in the AST to be instantiated. |
| 3556 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3559 | static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3560 | // check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3561 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3562 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3563 | return; |
| 3564 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3565 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3566 | if (AL.getNumArgs() == 0) { |
| 3567 | D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context, |
| 3568 | true, nullptr, AL.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3569 | return; |
| 3570 | } |
| 3571 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3572 | Expr *E = AL.getArgAsExpr(0); |
| 3573 | if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3574 | S.Diag(AL.getEllipsisLoc(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3575 | diag::err_pack_expansion_without_parameter_packs); |
| 3576 | return; |
| 3577 | } |
| 3578 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3579 | if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3580 | return; |
| 3581 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3582 | S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(), |
| 3583 | AL.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3587 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3588 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3589 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3590 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3591 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3592 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3593 | // C++11 [dcl.align]p1: |
| 3594 | // An alignment-specifier may be applied to a variable or to a class |
| 3595 | // data member, but it shall not be applied to a bit-field, a function |
| 3596 | // parameter, the formal parameter of a catch clause, or a variable |
| 3597 | // declared with the register storage class specifier. An |
| 3598 | // alignment-specifier may also be applied to the declaration of a class |
| 3599 | // or enumeration type. |
| 3600 | // C11 6.7.5/2: |
| 3601 | // An alignment attribute shall not be specified in a declaration of |
| 3602 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3603 | // object declared with the register storage-class specifier. |
| 3604 | int DiagKind = -1; |
| 3605 | if (isa<ParmVarDecl>(D)) { |
| 3606 | DiagKind = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3607 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3608 | if (VD->getStorageClass() == SC_Register) |
| 3609 | DiagKind = 1; |
| 3610 | if (VD->isExceptionVariable()) |
| 3611 | DiagKind = 2; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3612 | } else if (const auto *FD = dyn_cast<FieldDecl>(D)) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3613 | if (FD->isBitField()) |
| 3614 | DiagKind = 3; |
| 3615 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3616 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3617 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3618 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3619 | return; |
| 3620 | } |
| 3621 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3622 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3623 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3624 | return; |
| 3625 | } |
| 3626 | } |
| 3627 | |
Richard Smith | 90ae967 | 2018-06-20 23:36:55 +0000 | [diff] [blame] | 3628 | if (E->isValueDependent()) { |
| 3629 | // We can't support a dependent alignment on a non-dependent type, |
| 3630 | // because we have no way to model that a type is "alignment-dependent" |
| 3631 | // but not dependent in any other way. |
| 3632 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3633 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3634 | Diag(AttrLoc, diag::err_alignment_dependent_typedef_name) |
| 3635 | << E->getSourceRange(); |
| 3636 | return; |
| 3637 | } |
| 3638 | } |
| 3639 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3640 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3641 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3642 | AA->setPackExpansion(IsPackExpansion); |
| 3643 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3644 | return; |
| 3645 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3646 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3647 | // FIXME: Cache the number on the AL object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3648 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3649 | ExprResult ICE |
| 3650 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3651 | diag::err_aligned_attribute_argument_not_int, |
| 3652 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3653 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3654 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3655 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3656 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3657 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3658 | // C++11 [dcl.align]p2: |
| 3659 | // -- if the constant expression evaluates to zero, the alignment |
| 3660 | // specifier shall have no effect |
| 3661 | // C11 6.7.5p6: |
| 3662 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3663 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3664 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3665 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3666 | << E->getSourceRange(); |
| 3667 | return; |
| 3668 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3669 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3670 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3671 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3672 | unsigned MaxValidAlignment = |
| 3673 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3674 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3675 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3676 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3677 | << E->getSourceRange(); |
| 3678 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3679 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3680 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3681 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3682 | unsigned MaxTLSAlign = |
| 3683 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3684 | .getQuantity(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3685 | const auto *VD = dyn_cast<VarDecl>(D); |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3686 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3687 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3688 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3689 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3690 | return; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3694 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3695 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3696 | AA->setPackExpansion(IsPackExpansion); |
| 3697 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3700 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3701 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3702 | // FIXME: Cache the number on the AL object if non-dependent? |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3703 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3704 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3705 | SpellingListIndex); |
| 3706 | AA->setPackExpansion(IsPackExpansion); |
| 3707 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3708 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3709 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3710 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3711 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3712 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3713 | QualType UnderlyingTy, DiagTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3714 | if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3715 | UnderlyingTy = DiagTy = VD->getType(); |
| 3716 | } else { |
| 3717 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3718 | if (const auto *ED = dyn_cast<EnumDecl>(D)) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3719 | UnderlyingTy = ED->getIntegerType(); |
| 3720 | } |
| 3721 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3722 | return; |
| 3723 | |
| 3724 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3725 | // The combined effect of all alignment attributes in a declaration shall |
| 3726 | // not specify an alignment that is less strict than the alignment that |
| 3727 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3728 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3729 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3730 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3731 | if (I->isAlignmentDependent()) |
| 3732 | return; |
| 3733 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3734 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3735 | Align = std::max(Align, I->getAlignment(Context)); |
| 3736 | } |
| 3737 | |
| 3738 | if (AlignasAttr && Align) { |
| 3739 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3740 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3741 | if (NaturalAlign > RequestedAlign) |
| 3742 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3743 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3747 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3748 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3749 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3750 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3751 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3752 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3753 | // have to wait until the record is defined to catch any mismatches. |
| 3754 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3755 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3756 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3757 | // The unspecified model never matches what a definition could need. |
| 3758 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3759 | return false; |
| 3760 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3761 | if (BestCase) { |
| 3762 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3763 | return false; |
| 3764 | } else { |
| 3765 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3766 | return false; |
| 3767 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3768 | |
| 3769 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3770 | << 0 /*definition*/; |
| 3771 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3772 | << RD->getNameAsString(); |
| 3773 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3776 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3777 | /// attribute. |
| 3778 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3779 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3780 | IntegerMode = true; |
| 3781 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3782 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3783 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3784 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3785 | case 'Q': |
| 3786 | DestWidth = 8; |
| 3787 | break; |
| 3788 | case 'H': |
| 3789 | DestWidth = 16; |
| 3790 | break; |
| 3791 | case 'S': |
| 3792 | DestWidth = 32; |
| 3793 | break; |
| 3794 | case 'D': |
| 3795 | DestWidth = 64; |
| 3796 | break; |
| 3797 | case 'X': |
| 3798 | DestWidth = 96; |
| 3799 | break; |
| 3800 | case 'T': |
| 3801 | DestWidth = 128; |
| 3802 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3803 | } |
| 3804 | if (Str[1] == 'F') { |
| 3805 | IntegerMode = false; |
| 3806 | } else if (Str[1] == 'C') { |
| 3807 | IntegerMode = false; |
| 3808 | ComplexMode = true; |
| 3809 | } else if (Str[1] != 'I') { |
| 3810 | DestWidth = 0; |
| 3811 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3812 | break; |
| 3813 | case 4: |
| 3814 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3815 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3816 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3817 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3818 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3819 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3820 | break; |
| 3821 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3822 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3823 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3824 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3825 | case 11: |
| 3826 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3827 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3828 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3829 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3833 | /// type. |
| 3834 | /// |
| 3835 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3836 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3837 | /// HImode, not an intermediate pointer. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3838 | static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3839 | // This attribute isn't documented, but glibc uses it. It changes |
| 3840 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3841 | if (!AL.isArgIdent(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3842 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 3843 | << AL << AANT_ArgumentIdentifier; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3844 | return; |
| 3845 | } |
| 3846 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3847 | IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3848 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3849 | S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex()); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 3853 | unsigned SpellingListIndex, bool InInstantiation) { |
| 3854 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3855 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3856 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3857 | |
| 3858 | unsigned DestWidth = 0; |
| 3859 | bool IntegerMode = true; |
| 3860 | bool ComplexMode = false; |
| 3861 | llvm::APInt VectorSize(64, 0); |
| 3862 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3863 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3864 | size_t StrSize = Str.size(); |
| 3865 | size_t VectorStringLength = 0; |
| 3866 | while ((VectorStringLength + 1) < StrSize && |
| 3867 | isdigit(Str[VectorStringLength + 1])) |
| 3868 | ++VectorStringLength; |
| 3869 | if (VectorStringLength && |
| 3870 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3871 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3872 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3873 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3874 | // Avoid duplicate warning from template instantiation. |
| 3875 | if (!InInstantiation) |
| 3876 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3877 | } else { |
| 3878 | VectorSize = 0; |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3883 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 3884 | |
| 3885 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3886 | // and friends, at least with glibc. |
| 3887 | // FIXME: Make sure floating-point mappings are accurate |
| 3888 | // FIXME: Support XF and TF types |
| 3889 | if (!DestWidth) { |
| 3890 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 3891 | return; |
| 3892 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3893 | |
| 3894 | QualType OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3895 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3896 | OldTy = TD->getUnderlyingType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3897 | else if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3898 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 3899 | // Try to get type from enum declaration, default to int. |
| 3900 | OldTy = ED->getIntegerType(); |
| 3901 | if (OldTy.isNull()) |
| 3902 | OldTy = Context.IntTy; |
| 3903 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3904 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3905 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3906 | if (OldTy->isDependentType()) { |
| 3907 | D->addAttr(::new (Context) |
| 3908 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 3909 | return; |
| 3910 | } |
| 3911 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3912 | // Base type can also be a vector type (see PR17453). |
| 3913 | // Distinguish between base type and base element type. |
| 3914 | QualType OldElemTy = OldTy; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3915 | if (const auto *VT = OldTy->getAs<VectorType>()) |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3916 | OldElemTy = VT->getElementType(); |
| 3917 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3918 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 3919 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 3920 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 3921 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 3922 | VectorSize.getBoolValue()) { |
| 3923 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 3924 | return; |
| 3925 | } |
| 3926 | bool IntegralOrAnyEnumType = |
| 3927 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 3928 | |
| 3929 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 3930 | !IntegralOrAnyEnumType) |
| 3931 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3932 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3933 | if (!IntegralOrAnyEnumType) |
| 3934 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3935 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3936 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3937 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3938 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3939 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3940 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3943 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3944 | |
| 3945 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3946 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 3947 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3948 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3949 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3950 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3951 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3952 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3953 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3956 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3957 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3961 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3962 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3963 | VectorType::GenericVector); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3964 | } else if (const auto *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3965 | // Complex machine mode does not support base vector types. |
| 3966 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3967 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3968 | return; |
| 3969 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3970 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3971 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3972 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3973 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3974 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3978 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3979 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | // Install the new type. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3983 | if (auto *TD = dyn_cast<TypedefNameDecl>(D)) |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3984 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3985 | else if (auto *ED = dyn_cast<EnumDecl>(D)) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3986 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3987 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3988 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3989 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3990 | D->addAttr(::new (Context) |
| 3991 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3992 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3993 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3994 | static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3995 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 3996 | NoDebugAttr(AL.getRange(), S.Context, |
| 3997 | AL.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4000 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4001 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4002 | unsigned AttrSpellingListIndex) { |
| 4003 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4004 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4005 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4006 | return nullptr; |
| 4007 | } |
| 4008 | |
| 4009 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 4010 | return nullptr; |
| 4011 | |
| 4012 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 4013 | AttrSpellingListIndex); |
| 4014 | } |
| 4015 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4016 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const ParsedAttr &AL) { |
| 4017 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4018 | return nullptr; |
| 4019 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4020 | return ::new (Context) |
| 4021 | CommonAttr(AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4024 | CommonAttr *Sema::mergeCommonAttr(Decl *D, const CommonAttr &AL) { |
| 4025 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, AL)) |
| 4026 | return nullptr; |
| 4027 | |
| 4028 | return ::new (Context) |
| 4029 | CommonAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
| 4030 | } |
| 4031 | |
| 4032 | InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D, |
| 4033 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4034 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4035 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 4036 | // ImplicitParm or VarTemplateSpecialization). |
| 4037 | if (VD->getKind() != Decl::Var) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4038 | Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4039 | << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4040 | : ExpectedVariableOrFunction); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4041 | return nullptr; |
| 4042 | } |
| 4043 | // Attribute does not apply to non-static local variables. |
| 4044 | if (VD->hasLocalStorage()) { |
| 4045 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4046 | return nullptr; |
| 4047 | } |
| 4048 | } |
| 4049 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4050 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
| 4051 | return nullptr; |
| 4052 | |
| 4053 | return ::new (Context) InternalLinkageAttr( |
| 4054 | AL.getRange(), Context, AL.getAttributeSpellingListIndex()); |
| 4055 | } |
| 4056 | InternalLinkageAttr * |
| 4057 | Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) { |
| 4058 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 4059 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 4060 | // ImplicitParm or VarTemplateSpecialization). |
| 4061 | if (VD->getKind() != Decl::Var) { |
| 4062 | Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type) |
| 4063 | << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4064 | : ExpectedVariableOrFunction); |
| 4065 | return nullptr; |
| 4066 | } |
| 4067 | // Attribute does not apply to non-static local variables. |
| 4068 | if (VD->hasLocalStorage()) { |
| 4069 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4070 | return nullptr; |
| 4071 | } |
| 4072 | } |
| 4073 | |
| 4074 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4075 | return nullptr; |
| 4076 | |
| 4077 | return ::new (Context) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4078 | InternalLinkageAttr(AL.getRange(), Context, AL.getSpellingListIndex()); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4081 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 4082 | unsigned AttrSpellingListIndex) { |
| 4083 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 4084 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 4085 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4086 | return nullptr; |
| 4087 | } |
| 4088 | |
| 4089 | if (D->hasAttr<MinSizeAttr>()) |
| 4090 | return nullptr; |
| 4091 | |
| 4092 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 4093 | } |
| 4094 | |
| 4095 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 4096 | unsigned AttrSpellingListIndex) { |
| 4097 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 4098 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 4099 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4100 | D->dropAttr<AlwaysInlineAttr>(); |
| 4101 | } |
| 4102 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 4103 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 4104 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4105 | D->dropAttr<MinSizeAttr>(); |
| 4106 | } |
| 4107 | |
| 4108 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 4109 | return nullptr; |
| 4110 | |
| 4111 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 4112 | AttrSpellingListIndex); |
| 4113 | } |
| 4114 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4115 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4116 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL)) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 4117 | return; |
| 4118 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4119 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4120 | D, AL.getRange(), AL.getName(), |
| 4121 | AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4122 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4125 | static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4126 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4127 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4128 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4131 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4132 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4133 | D, AL.getRange(), AL.getAttributeSpellingListIndex())) |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4134 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4137 | static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4138 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL)) |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4139 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4140 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4141 | if (!VD->hasGlobalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4142 | S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4143 | return; |
| 4144 | } |
| 4145 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4146 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4149 | static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4150 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL)) |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4151 | return; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4152 | const auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 4153 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 4154 | // "int x[]"). |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 4155 | if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() && |
Jonas Hahnfeld | ee47d8c | 2018-02-14 16:04:03 +0000 | [diff] [blame] | 4156 | !isa<IncompleteArrayType>(VD->getType())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4157 | S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4158 | return; |
| 4159 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4160 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4161 | S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared) |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4162 | << S.CurrentCUDATarget()) |
| 4163 | return; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4164 | D->addAttr(::new (S.Context) CUDASharedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4165 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4168 | static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4169 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL) || |
| 4170 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL)) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 4171 | return; |
| 4172 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4173 | const auto *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4174 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4175 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 4176 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4177 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4178 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 4179 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4180 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4181 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4182 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 4183 | if (Method->isInstance()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4184 | S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method) |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4185 | << Method; |
| 4186 | return; |
| 4187 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4188 | S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method; |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4189 | } |
| 4190 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 4191 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4192 | S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4193 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4194 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4195 | CUDAGlobalAttr(AL.getRange(), S.Context, |
| 4196 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4199 | static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4200 | const auto *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 4201 | if (!Fn->isInlineSpecified()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4202 | S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 4203 | return; |
| 4204 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4205 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4206 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4207 | GNUInlineAttr(AL.getRange(), S.Context, |
| 4208 | AL.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4211 | static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4212 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4213 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4214 | // Diagnostic is emitted elsewhere: here we store the (valid) AL |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4215 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 4216 | CallingConv CC; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4217 | if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4218 | return; |
| 4219 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4220 | if (!isa<ObjCMethodDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4221 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4222 | << AL << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4223 | return; |
| 4224 | } |
| 4225 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4226 | switch (AL.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4227 | case ParsedAttr::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4228 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4229 | FastCallAttr(AL.getRange(), S.Context, |
| 4230 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4231 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4232 | case ParsedAttr::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4233 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4234 | StdCallAttr(AL.getRange(), S.Context, |
| 4235 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4236 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4237 | case ParsedAttr::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4238 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4239 | ThisCallAttr(AL.getRange(), S.Context, |
| 4240 | AL.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 4241 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4242 | case ParsedAttr::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4243 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4244 | CDeclAttr(AL.getRange(), S.Context, |
| 4245 | AL.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4246 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4247 | case ParsedAttr::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4248 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4249 | PascalAttr(AL.getRange(), S.Context, |
| 4250 | AL.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4251 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4252 | case ParsedAttr::AT_SwiftCall: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4253 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4254 | SwiftCallAttr(AL.getRange(), S.Context, |
| 4255 | AL.getAttributeSpellingListIndex())); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4256 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4257 | case ParsedAttr::AT_VectorCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4258 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4259 | VectorCallAttr(AL.getRange(), S.Context, |
| 4260 | AL.getAttributeSpellingListIndex())); |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4261 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4262 | case ParsedAttr::AT_MSABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4263 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4264 | MSABIAttr(AL.getRange(), S.Context, |
| 4265 | AL.getAttributeSpellingListIndex())); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4266 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4267 | case ParsedAttr::AT_SysVABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4268 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4269 | SysVABIAttr(AL.getRange(), S.Context, |
| 4270 | AL.getAttributeSpellingListIndex())); |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4271 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4272 | case ParsedAttr::AT_RegCall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4273 | D->addAttr(::new (S.Context) RegCallAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4274 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4275 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4276 | case ParsedAttr::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4277 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4278 | switch (CC) { |
| 4279 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4280 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4281 | break; |
| 4282 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4283 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4284 | break; |
| 4285 | default: |
| 4286 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4289 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4290 | PcsAttr(AL.getRange(), S.Context, PCS, |
| 4291 | AL.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4292 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4293 | } |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 4294 | case ParsedAttr::AT_AArch64VectorPcs: |
| 4295 | D->addAttr(::new(S.Context) |
| 4296 | AArch64VectorPcsAttr(AL.getRange(), S.Context, |
| 4297 | AL.getAttributeSpellingListIndex())); |
| 4298 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4299 | case ParsedAttr::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4300 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4301 | IntelOclBiccAttr(AL.getRange(), S.Context, |
| 4302 | AL.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4303 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4304 | case ParsedAttr::AT_PreserveMost: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4305 | D->addAttr(::new (S.Context) PreserveMostAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4306 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4307 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4308 | case ParsedAttr::AT_PreserveAll: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4309 | D->addAttr(::new (S.Context) PreserveAllAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4310 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4311 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4312 | default: |
| 4313 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4314 | } |
| 4315 | } |
| 4316 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4317 | static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4318 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4319 | return; |
| 4320 | |
| 4321 | std::vector<StringRef> DiagnosticIdentifiers; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4322 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4323 | StringRef RuleName; |
| 4324 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4325 | if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr)) |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4326 | return; |
| 4327 | |
| 4328 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 4329 | // clang-tidy knows about available rules. |
| 4330 | DiagnosticIdentifiers.push_back(RuleName); |
| 4331 | } |
| 4332 | D->addAttr(::new (S.Context) SuppressAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4333 | AL.getRange(), S.Context, DiagnosticIdentifiers.data(), |
| 4334 | DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex())); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4337 | bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC, |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4338 | const FunctionDecl *FD) { |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4339 | if (Attrs.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4340 | return true; |
| 4341 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4342 | if (Attrs.hasProcessingCache()) { |
| 4343 | CC = (CallingConv) Attrs.getProcessingCache(); |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4344 | return false; |
| 4345 | } |
| 4346 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4347 | unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4348 | if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) { |
| 4349 | Attrs.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4350 | return true; |
| 4351 | } |
| 4352 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4353 | // TODO: diagnose uses of these conventions on the wrong target. |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4354 | switch (Attrs.getKind()) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4355 | case ParsedAttr::AT_CDecl: |
| 4356 | CC = CC_C; |
| 4357 | break; |
| 4358 | case ParsedAttr::AT_FastCall: |
| 4359 | CC = CC_X86FastCall; |
| 4360 | break; |
| 4361 | case ParsedAttr::AT_StdCall: |
| 4362 | CC = CC_X86StdCall; |
| 4363 | break; |
| 4364 | case ParsedAttr::AT_ThisCall: |
| 4365 | CC = CC_X86ThisCall; |
| 4366 | break; |
| 4367 | case ParsedAttr::AT_Pascal: |
| 4368 | CC = CC_X86Pascal; |
| 4369 | break; |
| 4370 | case ParsedAttr::AT_SwiftCall: |
| 4371 | CC = CC_Swift; |
| 4372 | break; |
| 4373 | case ParsedAttr::AT_VectorCall: |
| 4374 | CC = CC_X86VectorCall; |
| 4375 | break; |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 4376 | case ParsedAttr::AT_AArch64VectorPcs: |
| 4377 | CC = CC_AArch64VectorCall; |
| 4378 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4379 | case ParsedAttr::AT_RegCall: |
| 4380 | CC = CC_X86RegCall; |
| 4381 | break; |
| 4382 | case ParsedAttr::AT_MSABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4383 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 4384 | CC_Win64; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4385 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4386 | case ParsedAttr::AT_SysVABI: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4387 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4388 | CC_C; |
| 4389 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4390 | case ParsedAttr::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 4391 | StringRef StrRef; |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4392 | if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) { |
| 4393 | Attrs.setInvalid(); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4394 | return true; |
| 4395 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4396 | if (StrRef == "aapcs") { |
| 4397 | CC = CC_AAPCS; |
| 4398 | break; |
| 4399 | } else if (StrRef == "aapcs-vfp") { |
| 4400 | CC = CC_AAPCS_VFP; |
| 4401 | break; |
| 4402 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4403 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4404 | Attrs.setInvalid(); |
| 4405 | Diag(Attrs.getLoc(), diag::err_invalid_pcs); |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4406 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4407 | } |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4408 | case ParsedAttr::AT_IntelOclBicc: |
| 4409 | CC = CC_IntelOclBicc; |
| 4410 | break; |
| 4411 | case ParsedAttr::AT_PreserveMost: |
| 4412 | CC = CC_PreserveMost; |
| 4413 | break; |
| 4414 | case ParsedAttr::AT_PreserveAll: |
| 4415 | CC = CC_PreserveAll; |
| 4416 | break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4417 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4420 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4421 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4422 | if (A != TargetInfo::CCCR_OK) { |
| 4423 | if (A == TargetInfo::CCCR_Warning) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4424 | Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4425 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4426 | // This convention is not valid for the target. Use the default function or |
| 4427 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 4428 | bool IsCXXMethod = false, IsVariadic = false; |
| 4429 | if (FD) { |
| 4430 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4431 | IsVariadic = FD->isVariadic(); |
| 4432 | } |
| 4433 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4436 | Attrs.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4437 | return false; |
| 4438 | } |
| 4439 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4440 | /// Pointer-like types in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4441 | static bool isValidSwiftContextType(QualType Ty) { |
| 4442 | if (!Ty->hasPointerRepresentation()) |
| 4443 | return Ty->isDependentType(); |
| 4444 | return Ty->getPointeeType().getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | /// Pointers and references in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4448 | static bool isValidSwiftIndirectResultType(QualType Ty) { |
| 4449 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4450 | Ty = PtrType->getPointeeType(); |
| 4451 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4452 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4453 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4454 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4455 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4456 | return Ty.getAddressSpace() == LangAS::Default; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | /// Pointers and references to pointers in the default address space. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4460 | static bool isValidSwiftErrorResultType(QualType Ty) { |
| 4461 | if (const auto *PtrType = Ty->getAs<PointerType>()) { |
| 4462 | Ty = PtrType->getPointeeType(); |
| 4463 | } else if (const auto *RefType = Ty->getAs<ReferenceType>()) { |
| 4464 | Ty = RefType->getPointeeType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4465 | } else { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4466 | return Ty->isDependentType(); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4467 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4468 | if (!Ty.getQualifiers().empty()) |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4469 | return false; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4470 | return isValidSwiftContextType(Ty); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4473 | static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs, |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4474 | ParameterABI Abi) { |
| 4475 | S.AddParameterABIAttr(Attrs.getRange(), D, Abi, |
| 4476 | Attrs.getAttributeSpellingListIndex()); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 4480 | unsigned spellingIndex) { |
| 4481 | |
| 4482 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4483 | |
| 4484 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4485 | if (existingAttr->getABI() != abi) { |
| 4486 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4487 | << getParameterABISpelling(abi) << existingAttr; |
| 4488 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4489 | return; |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | switch (abi) { |
| 4494 | case ParameterABI::Ordinary: |
| 4495 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4496 | |
| 4497 | case ParameterABI::SwiftContext: |
| 4498 | if (!isValidSwiftContextType(type)) { |
| 4499 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4500 | << getParameterABISpelling(abi) |
| 4501 | << /*pointer to pointer */ 0 << type; |
| 4502 | } |
| 4503 | D->addAttr(::new (Context) |
| 4504 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4505 | return; |
| 4506 | |
| 4507 | case ParameterABI::SwiftErrorResult: |
| 4508 | if (!isValidSwiftErrorResultType(type)) { |
| 4509 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4510 | << getParameterABISpelling(abi) |
| 4511 | << /*pointer to pointer */ 1 << type; |
| 4512 | } |
| 4513 | D->addAttr(::new (Context) |
| 4514 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4515 | return; |
| 4516 | |
| 4517 | case ParameterABI::SwiftIndirectResult: |
| 4518 | if (!isValidSwiftIndirectResultType(type)) { |
| 4519 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4520 | << getParameterABISpelling(abi) |
| 4521 | << /*pointer*/ 0 << type; |
| 4522 | } |
| 4523 | D->addAttr(::new (Context) |
| 4524 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4525 | return; |
| 4526 | } |
| 4527 | llvm_unreachable("bad parameter ABI attribute"); |
| 4528 | } |
| 4529 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4530 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4531 | /// otherwise setting numParams to the appropriate value. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4532 | bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4533 | if (AL.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4534 | return true; |
| 4535 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4536 | if (!checkAttributeNumArgs(*this, AL, 1)) { |
| 4537 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4538 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4539 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4540 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4541 | uint32_t NP; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4542 | Expr *NumParamsExpr = AL.getArgAsExpr(0); |
| 4543 | if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) { |
| 4544 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4545 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4548 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4549 | Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4550 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4551 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4552 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4553 | } |
| 4554 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4555 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4556 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4557 | Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4558 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4559 | AL.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4560 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4563 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4566 | // Checks whether an argument of launch_bounds attribute is |
| 4567 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4568 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4569 | // and may output an error. |
| 4570 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4571 | const CUDALaunchBoundsAttr &AL, |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4572 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4573 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4574 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4575 | |
| 4576 | // Accept template arguments for now as they depend on something else. |
| 4577 | // We'll get to check them when they eventually get instantiated. |
| 4578 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4579 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4580 | |
| 4581 | llvm::APSInt I(64); |
| 4582 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4583 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4584 | << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4585 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4586 | } |
| 4587 | // Make sure we can fit it in 32 bits. |
| 4588 | if (!I.isIntN(32)) { |
| 4589 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4590 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4591 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4592 | } |
| 4593 | if (I < 0) |
| 4594 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4595 | << &AL << Idx << E->getSourceRange(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4596 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4597 | // We may need to perform implicit conversion of the argument. |
| 4598 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4599 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4600 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4601 | assert(!ValArg.isInvalid() && |
| 4602 | "Unexpected PerformCopyInitialization() failure."); |
| 4603 | |
| 4604 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4608 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4609 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4610 | SpellingListIndex); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4611 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4612 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4613 | return; |
| 4614 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4615 | if (MinBlocks) { |
| 4616 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4617 | if (MinBlocks == nullptr) |
| 4618 | return; |
| 4619 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4620 | |
| 4621 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4622 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4623 | } |
| 4624 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4625 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4626 | if (!checkAttributeAtLeastNumArgs(S, AL, 1) || |
| 4627 | !checkAttributeAtMostNumArgs(S, AL, 2)) |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4628 | return; |
| 4629 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4630 | S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0), |
| 4631 | AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr, |
| 4632 | AL.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4635 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4636 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4637 | if (!AL.isArgIdent(0)) { |
| 4638 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4639 | << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4640 | return; |
| 4641 | } |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4642 | |
| 4643 | ParamIdx ArgumentIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4644 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4645 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4646 | return; |
| 4647 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4648 | ParamIdx TypeTagIdx; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4649 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2), |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4650 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4651 | return; |
| 4652 | |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 4653 | bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag"; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4654 | if (IsPointer) { |
| 4655 | // Ensure that buffer has a pointer type. |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4656 | unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex(); |
| 4657 | if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) || |
| 4658 | !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType()) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4659 | S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 4662 | D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr( |
| 4663 | AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx, |
| 4664 | TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4668 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4669 | if (!AL.isArgIdent(0)) { |
| 4670 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4671 | << AL << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4672 | return; |
| 4673 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4674 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4675 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4676 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4677 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4678 | if (!isa<VarDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4679 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4680 | << AL << ExpectedVariable; |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4681 | return; |
| 4682 | } |
| 4683 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4684 | IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4685 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4686 | S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4687 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4688 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4689 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4690 | TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4691 | MatchingCTypeLoc, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4692 | AL.getLayoutCompatible(), |
| 4693 | AL.getMustBeNull(), |
| 4694 | AL.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4697 | static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4698 | ParamIdx ArgCount; |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4699 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4700 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0), |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4701 | ArgCount, |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4702 | true /* CanIndexImplicitThis */)) |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4703 | return; |
| 4704 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 4705 | // ArgCount isn't a parameter index [0;n), it's a count [1;n] |
| 4706 | D->addAttr(::new (S.Context) XRayLogArgsAttr( |
| 4707 | AL.getRange(), S.Context, ArgCount.getSourceIndex(), |
| 4708 | AL.getAttributeSpellingListIndex())); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4711 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4712 | // Checker-specific attribute handlers. |
| 4713 | //===----------------------------------------------------------------------===// |
| 4714 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4715 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) { |
| 4716 | return QT->isDependentType() || QT->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4719 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) { |
| 4720 | return QT->isDependentType() || QT->isObjCObjectPointerType() || |
| 4721 | S.Context.isObjCNSObjectType(QT); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4722 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4723 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4724 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) { |
| 4725 | return QT->isDependentType() || QT->isPointerType() || |
| 4726 | isValidSubjectOfNSAttribute(S, QT); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4729 | static void handleNSConsumedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4730 | S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(), |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4731 | AL.getKind() == ParsedAttr::AT_NSConsumed, |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4732 | /*template instantiation*/ false); |
| 4733 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4734 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4735 | void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D, |
| 4736 | unsigned SpellingIndex, bool IsNSConsumed, |
| 4737 | bool IsTemplateInstantiation) { |
| 4738 | const auto *Param = cast<ParmVarDecl>(D); |
| 4739 | bool TypeOK; |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4740 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4741 | if (IsNSConsumed) |
| 4742 | TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType()); |
| 4743 | else |
| 4744 | TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4745 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4746 | if (!TypeOK) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4747 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 4748 | // is significant. Allow non-dependent code to contain inappropriate |
| 4749 | // attributes even in ARC, but require template instantiations to be |
| 4750 | // set up correctly. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4751 | Diag(D->getBeginLoc(), (IsTemplateInstantiation && IsNSConsumed && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4752 | getLangOpts().ObjCAutoRefCount |
| 4753 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4754 | : diag::warn_ns_attribute_wrong_parameter_type)) |
| 4755 | << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed") |
| 4756 | << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4757 | return; |
| 4758 | } |
| 4759 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4760 | if (IsNSConsumed) |
| 4761 | D->addAttr(::new (Context) |
| 4762 | NSConsumedAttr(AttrRange, Context, SpellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4763 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4764 | D->addAttr(::new (Context) |
| 4765 | CFConsumedAttr(AttrRange, Context, SpellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4768 | bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) { |
| 4769 | if (isValidSubjectOfNSReturnsRetainedAttribute(QT)) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4770 | return false; |
| 4771 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4772 | Diag(Loc, diag::warn_ns_attribute_wrong_return_type) |
| 4773 | << "'ns_returns_retained'" << 0 << 0; |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4774 | return true; |
| 4775 | } |
| 4776 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4777 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4778 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4779 | QualType ReturnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4780 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4781 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 4782 | ReturnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4783 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4784 | (AL.getKind() == ParsedAttr::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4785 | return; // ignore: was handled as a type attribute |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4786 | else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4787 | ReturnType = PD->getType(); |
| 4788 | else if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 4789 | ReturnType = FD->getReturnType(); |
| 4790 | else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4791 | ReturnType = Param->getType()->getPointeeType(); |
| 4792 | if (ReturnType.isNull()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4793 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4794 | << AL << /*pointer-to-CF*/ 2 << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4795 | return; |
| 4796 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4797 | } else if (AL.isUsedAsTypeAttr()) { |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4798 | return; |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4799 | } else { |
| 4800 | AttributeDeclKind ExpectedDeclKind; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4801 | switch (AL.getKind()) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4802 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4803 | case ParsedAttr::AT_NSReturnsRetained: |
| 4804 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 4805 | case ParsedAttr::AT_NSReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4806 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4807 | break; |
| 4808 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4809 | case ParsedAttr::AT_CFReturnsRetained: |
| 4810 | case ParsedAttr::AT_CFReturnsNotRetained: |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4811 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4812 | break; |
| 4813 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4814 | S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4815 | << AL.getRange() << AL << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4816 | return; |
| 4817 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4818 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4819 | bool TypeOK; |
| 4820 | bool Cf; |
| 4821 | switch (AL.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4822 | default: llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4823 | case ParsedAttr::AT_NSReturnsRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4824 | TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType); |
| 4825 | Cf = false; |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4826 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4827 | |
| 4828 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 4829 | case ParsedAttr::AT_NSReturnsNotRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4830 | TypeOK = isValidSubjectOfNSAttribute(S, ReturnType); |
| 4831 | Cf = false; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4832 | break; |
| 4833 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4834 | case ParsedAttr::AT_CFReturnsRetained: |
| 4835 | case ParsedAttr::AT_CFReturnsNotRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4836 | TypeOK = isValidSubjectOfCFAttribute(S, ReturnType); |
| 4837 | Cf = true; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4838 | break; |
| 4839 | } |
| 4840 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4841 | if (!TypeOK) { |
| 4842 | if (AL.isUsedAsTypeAttr()) |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4843 | return; |
| 4844 | |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4845 | if (isa<ParmVarDecl>(D)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4846 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4847 | << AL << /*pointer-to-CF*/ 2 << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4848 | } else { |
| 4849 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4850 | enum : unsigned { |
| 4851 | Function, |
| 4852 | Method, |
| 4853 | Property |
| 4854 | } SubjectKind = Function; |
| 4855 | if (isa<ObjCMethodDecl>(D)) |
| 4856 | SubjectKind = Method; |
| 4857 | else if (isa<ObjCPropertyDecl>(D)) |
| 4858 | SubjectKind = Property; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4859 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4860 | << AL << SubjectKind << Cf << AL.getRange(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4861 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4862 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4863 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4864 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4865 | switch (AL.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4866 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4867 | llvm_unreachable("invalid ownership attribute"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4868 | case ParsedAttr::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4869 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4870 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4871 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4872 | case ParsedAttr::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4873 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4874 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4875 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4876 | case ParsedAttr::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4877 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4878 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4879 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4880 | case ParsedAttr::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4881 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4882 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4883 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4884 | case ParsedAttr::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4885 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4886 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4887 | return; |
| 4888 | }; |
| 4889 | } |
| 4890 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4891 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4892 | const ParsedAttr &Attrs) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4893 | const int EP_ObjCMethod = 1; |
| 4894 | const int EP_ObjCProperty = 2; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4895 | |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4896 | SourceLocation loc = Attrs.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4897 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4898 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4899 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4900 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4901 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4902 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4903 | if (!resultType->isReferenceType() && |
| 4904 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4905 | S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4906 | << SourceRange(loc) << Attrs |
| 4907 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
| 4908 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4909 | |
| 4910 | // Drop the attribute. |
| 4911 | return; |
| 4912 | } |
| 4913 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4914 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
Erich Keane | b11ebc5 | 2017-09-27 03:20:13 +0000 | [diff] [blame] | 4915 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4918 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4919 | const ParsedAttr &Attrs) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4920 | const auto *Method = cast<ObjCMethodDecl>(D); |
| 4921 | |
| 4922 | const DeclContext *DC = Method->getDeclContext(); |
| 4923 | if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4924 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4925 | << 0; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4926 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4927 | return; |
| 4928 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4929 | if (Method->getMethodFamily() == OMF_dealloc) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4930 | S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4931 | << 1; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4932 | return; |
| 4933 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4934 | |
| 4935 | D->addAttr(::new (S.Context) ObjCRequiresSuperAttr( |
| 4936 | Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4939 | static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4940 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4941 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4942 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4943 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4944 | return; |
| 4945 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4946 | |
| 4947 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4948 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4949 | if (!Parm->Ident->isStr("id")) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 4950 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id) << AL; |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4951 | return; |
| 4952 | } |
| 4953 | |
| 4954 | // Only allow 'cv void *'. |
| 4955 | QualType T = TD->getUnderlyingType(); |
| 4956 | if (!T->isVoidPointerType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4957 | S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4958 | return; |
| 4959 | } |
| 4960 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4961 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4962 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4963 | ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident, |
| 4964 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4967 | static void handleObjCBridgeMutableAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4968 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4969 | IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4970 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4971 | if (!Parm) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4972 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4973 | return; |
| 4974 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4975 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4976 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4977 | ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident, |
| 4978 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4981 | static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4982 | const ParsedAttr &AL) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4983 | IdentifierInfo *RelatedClass = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4984 | AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4985 | if (!RelatedClass) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4986 | S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4987 | return; |
| 4988 | } |
| 4989 | IdentifierInfo *ClassMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4990 | AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4991 | IdentifierInfo *InstanceMethod = |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4992 | AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4993 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4994 | ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4995 | ClassMethod, InstanceMethod, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 4996 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4999 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5000 | const ParsedAttr &AL) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 5001 | ObjCInterfaceDecl *IFace; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5002 | if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 5003 | IFace = CatDecl->getClassInterface(); |
| 5004 | else |
| 5005 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5006 | |
| 5007 | if (!IFace) |
| 5008 | return; |
| 5009 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 5010 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 5011 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5012 | ObjCDesignatedInitializerAttr(AL.getRange(), S.Context, |
| 5013 | AL.getAttributeSpellingListIndex())); |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5016 | static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5017 | StringRef MetaDataName; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5018 | if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName)) |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5019 | return; |
| 5020 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5021 | ObjCRuntimeNameAttr(AL.getRange(), S.Context, |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 5022 | MetaDataName, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5023 | AL.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 5026 | // When a user wants to use objc_boxable with a union or struct |
| 5027 | // but they don't have access to the declaration (legacy/third-party code) |
| 5028 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5029 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5030 | static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5031 | bool notify = false; |
| 5032 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5033 | auto *RD = dyn_cast<RecordDecl>(D); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5034 | if (RD && RD->getDefinition()) { |
| 5035 | RD = RD->getDefinition(); |
| 5036 | notify = true; |
| 5037 | } |
| 5038 | |
| 5039 | if (RD) { |
| 5040 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5041 | ObjCBoxableAttr(AL.getRange(), S.Context, |
| 5042 | AL.getAttributeSpellingListIndex()); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5043 | RD->addAttr(BoxableAttr); |
| 5044 | if (notify) { |
| 5045 | // we need to notify ASTReader/ASTWriter about |
| 5046 | // modification of existing declaration |
| 5047 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 5048 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 5049 | } |
| 5050 | } |
| 5051 | } |
| 5052 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5053 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5054 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5055 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5056 | S.Diag(D->getBeginLoc(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5057 | << AL.getRange() << AL << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5060 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5061 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5062 | const auto *VD = cast<ValueDecl>(D); |
| 5063 | QualType QT = VD->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5064 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5065 | if (!QT->isDependentType() && |
| 5066 | !QT->isObjCLifetimeType()) { |
| 5067 | S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
| 5068 | << QT; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5069 | return; |
| 5070 | } |
| 5071 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5072 | Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5073 | |
| 5074 | // If we have no lifetime yet, check the lifetime we're presumably |
| 5075 | // going to infer. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5076 | if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType()) |
| 5077 | Lifetime = QT->getObjCARCImplicitLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5078 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5079 | switch (Lifetime) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5080 | case Qualifiers::OCL_None: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5081 | assert(QT->isDependentType() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5082 | "didn't infer lifetime for non-dependent type?"); |
| 5083 | break; |
| 5084 | |
| 5085 | case Qualifiers::OCL_Weak: // meaningful |
| 5086 | case Qualifiers::OCL_Strong: // meaningful |
| 5087 | break; |
| 5088 | |
| 5089 | case Qualifiers::OCL_ExplicitNone: |
| 5090 | case Qualifiers::OCL_Autoreleasing: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5091 | S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
| 5092 | << (Lifetime == Qualifiers::OCL_Autoreleasing); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5093 | break; |
| 5094 | } |
| 5095 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5096 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5097 | ObjCPreciseLifetimeAttr(AL.getRange(), S.Context, |
| 5098 | AL.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5101 | //===----------------------------------------------------------------------===// |
| 5102 | // Microsoft specific attribute handlers. |
| 5103 | //===----------------------------------------------------------------------===// |
| 5104 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5105 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 5106 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 5107 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 5108 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5109 | return nullptr; |
| 5110 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 5111 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 5112 | D->dropAttr<UuidAttr>(); |
| 5113 | } |
| 5114 | |
| 5115 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 5116 | } |
| 5117 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5118 | static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5119 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5120 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5121 | << AL << AttributeLangSupport::C; |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5122 | return; |
| 5123 | } |
| 5124 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5125 | StringRef StrRef; |
| 5126 | SourceLocation LiteralLoc; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5127 | if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5128 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5129 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5130 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 5131 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5132 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 5133 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5134 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5135 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5136 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5137 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5138 | return; |
| 5139 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 5140 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5141 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5142 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5143 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5144 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5145 | return; |
| 5146 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5147 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5148 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5149 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5150 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5151 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5152 | |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5153 | // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's |
| 5154 | // the only thing in the [] list, the [] too), and add an insertion of |
| 5155 | // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas |
| 5156 | // separating attributes nor of the [ and the ] are in the AST. |
Nico Weber | 0a23404 | 2017-05-05 17:15:08 +0000 | [diff] [blame] | 5157 | // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc" |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5158 | // on cfe-dev. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5159 | if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling. |
| 5160 | S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated); |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5161 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5162 | UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(), |
| 5163 | AL.getAttributeSpellingListIndex(), StrRef); |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5164 | if (UA) |
| 5165 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5168 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5169 | if (!S.LangOpts.CPlusPlus) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5170 | S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5171 | << AL << AttributeLangSupport::C; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5172 | return; |
| 5173 | } |
| 5174 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5175 | D, AL.getRange(), /*BestCase=*/true, |
| 5176 | AL.getAttributeSpellingListIndex(), |
| 5177 | (MSInheritanceAttr::Spelling)AL.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5178 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5179 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5180 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 5181 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5184 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5185 | const auto *VD = cast<VarDecl>(D); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5186 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5187 | S.Diag(AL.getLoc(), diag::err_thread_unsupported); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5188 | return; |
| 5189 | } |
| 5190 | if (VD->getTSCSpec() != TSCS_unspecified) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5191 | S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5192 | return; |
| 5193 | } |
| 5194 | if (VD->hasLocalStorage()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5195 | S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5196 | return; |
| 5197 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5198 | D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context, |
| 5199 | AL.getAttributeSpellingListIndex())); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5202 | static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5203 | SmallVector<StringRef, 4> Tags; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5204 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5205 | StringRef Tag; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5206 | if (!S.checkStringLiteralArgumentAttr(AL, I, Tag)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5207 | return; |
| 5208 | Tags.push_back(Tag); |
| 5209 | } |
| 5210 | |
| 5211 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 5212 | if (!NS->isInline()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5213 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5214 | return; |
| 5215 | } |
| 5216 | if (NS->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5217 | S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5218 | return; |
| 5219 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5220 | if (AL.getNumArgs() == 0) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5221 | Tags.push_back(NS->getName()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5222 | } else if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5223 | return; |
| 5224 | |
| 5225 | // Store tags sorted and without duplicates. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 5226 | llvm::sort(Tags); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5227 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 5228 | |
| 5229 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5230 | AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(), |
| 5231 | AL.getAttributeSpellingListIndex())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5234 | static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5235 | // Check the attribute arguments. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5236 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5237 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5238 | return; |
| 5239 | } |
| 5240 | |
| 5241 | StringRef Str; |
| 5242 | SourceLocation ArgLoc; |
| 5243 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5244 | if (AL.getNumArgs() == 0) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5245 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5246 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5247 | return; |
| 5248 | |
| 5249 | ARMInterruptAttr::InterruptType Kind; |
| 5250 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5251 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5252 | << ArgLoc; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5253 | return; |
| 5254 | } |
| 5255 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5256 | unsigned Index = AL.getAttributeSpellingListIndex(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5257 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5258 | ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5261 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5262 | if (!checkAttributeNumArgs(S, AL, 1)) |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5263 | return; |
| 5264 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5265 | if (!AL.isArgExpr(0)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5266 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
| 5267 | << AL << AANT_ArgumentIntegerConstant; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5268 | return; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | // FIXME: Check for decl - it should be void ()(void). |
| 5272 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5273 | Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5274 | llvm::APSInt NumParams(32); |
| 5275 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5276 | S.Diag(AL.getLoc(), diag::err_attribute_argument_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5277 | << AL << AANT_ArgumentIntegerConstant |
| 5278 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5279 | return; |
| 5280 | } |
| 5281 | |
| 5282 | unsigned Num = NumParams.getLimitedValue(255); |
| 5283 | if ((Num & 1) || Num > 30) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5284 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5285 | << AL << (int)NumParams.getSExtValue() |
| 5286 | << NumParamsExpr->getSourceRange(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5287 | return; |
| 5288 | } |
| 5289 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5290 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5291 | MSP430InterruptAttr(AL.getLoc(), S.Context, Num, |
| 5292 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5293 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5296 | static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5297 | // Only one optional argument permitted. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5298 | if (AL.getNumArgs() > 1) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5299 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5300 | return; |
| 5301 | } |
| 5302 | |
| 5303 | StringRef Str; |
| 5304 | SourceLocation ArgLoc; |
| 5305 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5306 | if (AL.getNumArgs() == 0) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5307 | Str = ""; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5308 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5309 | return; |
| 5310 | |
| 5311 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 5312 | // a) Must be a function. |
| 5313 | // b) Must have no parameters. |
| 5314 | // c) Must have the 'void' return type. |
| 5315 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 5316 | // lacks the 'eret' instruction. |
| 5317 | // e) The attribute itself must either have no argument or one of the |
| 5318 | // valid interrupt types, see [MipsInterruptDocs]. |
| 5319 | |
| 5320 | if (!isFunctionOrMethod(D)) { |
| 5321 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5322 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5323 | return; |
| 5324 | } |
| 5325 | |
| 5326 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5327 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5328 | << 0; |
| 5329 | return; |
| 5330 | } |
| 5331 | |
| 5332 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5333 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5334 | << 1; |
| 5335 | return; |
| 5336 | } |
| 5337 | |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5338 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL)) |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5339 | return; |
| 5340 | |
| 5341 | MipsInterruptAttr::InterruptType Kind; |
| 5342 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5343 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5344 | << AL << "'" + std::string(Str) + "'"; |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5345 | return; |
| 5346 | } |
| 5347 | |
| 5348 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5349 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5352 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5353 | // Semantic checks for a function with the 'interrupt' attribute. |
| 5354 | // a) Must be a function. |
| 5355 | // b) Must have the 'void' return type. |
| 5356 | // c) Must take 1 or 2 arguments. |
| 5357 | // d) The 1st argument must be a pointer. |
| 5358 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 5359 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5360 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5361 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5362 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5363 | << AL << ExpectedFunctionWithProtoType; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5364 | return; |
| 5365 | } |
| 5366 | // Interrupt handler must have void return type. |
| 5367 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5368 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5369 | diag::err_anyx86_interrupt_attribute) |
| 5370 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5371 | ? 0 |
| 5372 | : 1) |
| 5373 | << 0; |
| 5374 | return; |
| 5375 | } |
| 5376 | // Interrupt handler must have 1 or 2 parameters. |
| 5377 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5378 | if (NumParams < 1 || NumParams > 2) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5379 | S.Diag(D->getBeginLoc(), diag::err_anyx86_interrupt_attribute) |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5380 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5381 | ? 0 |
| 5382 | : 1) |
| 5383 | << 1; |
| 5384 | return; |
| 5385 | } |
| 5386 | // The first argument must be a pointer. |
| 5387 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5388 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5389 | diag::err_anyx86_interrupt_attribute) |
| 5390 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5391 | ? 0 |
| 5392 | : 1) |
| 5393 | << 2; |
| 5394 | return; |
| 5395 | } |
| 5396 | // The second argument, if present, must be an unsigned integer. |
| 5397 | unsigned TypeSize = |
| 5398 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5399 | ? 64 |
| 5400 | : 32; |
| 5401 | if (NumParams == 2 && |
| 5402 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5403 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5404 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5405 | diag::err_anyx86_interrupt_attribute) |
| 5406 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5407 | ? 0 |
| 5408 | : 1) |
| 5409 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 5410 | return; |
| 5411 | } |
| 5412 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5413 | AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex())); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5414 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5415 | } |
| 5416 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5417 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5418 | if (!isFunctionOrMethod(D)) { |
| 5419 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5420 | << "'interrupt'" << ExpectedFunction; |
| 5421 | return; |
| 5422 | } |
| 5423 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5424 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5425 | return; |
| 5426 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5427 | handleSimpleAttribute<AVRInterruptAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5430 | static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5431 | if (!isFunctionOrMethod(D)) { |
| 5432 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5433 | << "'signal'" << ExpectedFunction; |
| 5434 | return; |
| 5435 | } |
| 5436 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5437 | if (!checkAttributeNumArgs(S, AL, 0)) |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5438 | return; |
| 5439 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5440 | handleSimpleAttribute<AVRSignalAttr>(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5443 | |
| 5444 | static void handleRISCVInterruptAttr(Sema &S, Decl *D, |
| 5445 | const ParsedAttr &AL) { |
| 5446 | // Warn about repeated attributes. |
| 5447 | if (const auto *A = D->getAttr<RISCVInterruptAttr>()) { |
| 5448 | S.Diag(AL.getRange().getBegin(), |
| 5449 | diag::warn_riscv_repeated_interrupt_attribute); |
| 5450 | S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute); |
| 5451 | return; |
| 5452 | } |
| 5453 | |
| 5454 | // Check the attribute argument. Argument is optional. |
| 5455 | if (!checkAttributeAtMostNumArgs(S, AL, 1)) |
| 5456 | return; |
| 5457 | |
| 5458 | StringRef Str; |
| 5459 | SourceLocation ArgLoc; |
| 5460 | |
| 5461 | // 'machine'is the default interrupt mode. |
| 5462 | if (AL.getNumArgs() == 0) |
| 5463 | Str = "machine"; |
| 5464 | else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) |
| 5465 | return; |
| 5466 | |
| 5467 | // Semantic checks for a function with the 'interrupt' attribute: |
| 5468 | // - Must be a function. |
| 5469 | // - Must have no parameters. |
| 5470 | // - Must have the 'void' return type. |
| 5471 | // - The attribute itself must either have no argument or one of the |
| 5472 | // valid interrupt types, see [RISCVInterruptDocs]. |
| 5473 | |
| 5474 | if (D->getFunctionType() == nullptr) { |
| 5475 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5476 | << "'interrupt'" << ExpectedFunction; |
| 5477 | return; |
| 5478 | } |
| 5479 | |
| 5480 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5481 | S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0; |
| 5482 | return; |
| 5483 | } |
| 5484 | |
| 5485 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5486 | S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1; |
| 5487 | return; |
| 5488 | } |
| 5489 | |
| 5490 | RISCVInterruptAttr::InterruptType Kind; |
| 5491 | if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5492 | S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str |
| 5493 | << ArgLoc; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5494 | return; |
| 5495 | } |
| 5496 | |
| 5497 | D->addAttr(::new (S.Context) RISCVInterruptAttr( |
| 5498 | AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex())); |
| 5499 | } |
| 5500 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5501 | static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5502 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5503 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5504 | case llvm::Triple::msp430: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5505 | handleMSP430InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5506 | break; |
| 5507 | case llvm::Triple::mipsel: |
| 5508 | case llvm::Triple::mips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5509 | handleMipsInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5510 | break; |
| 5511 | case llvm::Triple::x86: |
| 5512 | case llvm::Triple::x86_64: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5513 | handleAnyX86InterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5514 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5515 | case llvm::Triple::avr: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5516 | handleAVRInterruptAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5517 | break; |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 5518 | case llvm::Triple::riscv32: |
| 5519 | case llvm::Triple::riscv64: |
| 5520 | handleRISCVInterruptAttr(S, D, AL); |
| 5521 | break; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5522 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5523 | handleARMInterruptAttr(S, D, AL); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5524 | break; |
| 5525 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5526 | } |
| 5527 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5528 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5529 | const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5530 | uint32_t Min = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5531 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 5532 | if (!checkUInt32Argument(S, AL, MinExpr, Min)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5533 | return; |
| 5534 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5535 | uint32_t Max = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5536 | Expr *MaxExpr = AL.getArgAsExpr(1); |
| 5537 | if (!checkUInt32Argument(S, AL, MaxExpr, Max)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5538 | return; |
| 5539 | |
| 5540 | if (Min == 0 && Max != 0) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5541 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5542 | return; |
| 5543 | } |
| 5544 | if (Min > Max) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5545 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5546 | return; |
| 5547 | } |
| 5548 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5549 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5550 | AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max, |
| 5551 | AL.getAttributeSpellingListIndex())); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5554 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5555 | uint32_t Min = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5556 | Expr *MinExpr = AL.getArgAsExpr(0); |
| 5557 | if (!checkUInt32Argument(S, AL, MinExpr, Min)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5558 | return; |
| 5559 | |
| 5560 | uint32_t Max = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5561 | if (AL.getNumArgs() == 2) { |
| 5562 | Expr *MaxExpr = AL.getArgAsExpr(1); |
| 5563 | if (!checkUInt32Argument(S, AL, MaxExpr, Max)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5564 | return; |
| 5565 | } |
| 5566 | |
| 5567 | if (Min == 0 && Max != 0) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5568 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5569 | return; |
| 5570 | } |
| 5571 | if (Max != 0 && Min > Max) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5572 | S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5573 | return; |
| 5574 | } |
| 5575 | |
| 5576 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5577 | AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max, |
| 5578 | AL.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5581 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5582 | uint32_t NumSGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5583 | Expr *NumSGPRExpr = AL.getArgAsExpr(0); |
| 5584 | if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5585 | return; |
| 5586 | |
| 5587 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5588 | AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR, |
| 5589 | AL.getAttributeSpellingListIndex())); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5592 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5593 | uint32_t NumVGPR = 0; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5594 | Expr *NumVGPRExpr = AL.getArgAsExpr(0); |
| 5595 | if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR)) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5596 | return; |
| 5597 | |
| 5598 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5599 | AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR, |
| 5600 | AL.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5601 | } |
| 5602 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5603 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5604 | const ParsedAttr &AL) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5605 | // If we try to apply it to a function pointer, don't warn, but don't |
| 5606 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 5607 | // special about calling a force_align_arg_pointer function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5608 | const auto *VD = dyn_cast<ValueDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5609 | if (VD && VD->getType()->isFunctionPointerType()) |
| 5610 | return; |
| 5611 | // Also don't warn on function pointer typedefs. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5612 | const auto *TD = dyn_cast<TypedefNameDecl>(D); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5613 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 5614 | TD->getUnderlyingType()->isFunctionType())) |
| 5615 | return; |
| 5616 | // Attribute can only be applied to function types. |
| 5617 | if (!isa<FunctionDecl>(D)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5618 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5619 | << AL << ExpectedFunction; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5620 | return; |
| 5621 | } |
| 5622 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5623 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5624 | X86ForceAlignArgPointerAttr(AL.getRange(), S.Context, |
| 5625 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5628 | static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) { |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5629 | uint32_t Version; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5630 | Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0)); |
| 5631 | if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version)) |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5632 | return; |
| 5633 | |
| 5634 | // TODO: Investigate what happens with the next major version of MSVC. |
| 5635 | if (Version != LangOptions::MSVC2015) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5636 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5637 | << AL << Version << VersionExpr->getSourceRange(); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5638 | return; |
| 5639 | } |
| 5640 | |
| 5641 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5642 | LayoutVersionAttr(AL.getRange(), S.Context, Version, |
| 5643 | AL.getAttributeSpellingListIndex())); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5646 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 5647 | unsigned AttrSpellingListIndex) { |
| 5648 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5649 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5650 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5651 | } |
| 5652 | |
| 5653 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5654 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5655 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5656 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5659 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 5660 | unsigned AttrSpellingListIndex) { |
| 5661 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5662 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5663 | D->dropAttr<DLLImportAttr>(); |
| 5664 | } |
| 5665 | |
| 5666 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5667 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5668 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5669 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5672 | static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5673 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 5674 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5675 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A; |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5676 | return; |
| 5677 | } |
| 5678 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5679 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5680 | if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport && |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5681 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5682 | // MinGW doesn't allow dllimport on inline functions. |
| 5683 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5684 | << A; |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5685 | return; |
| 5686 | } |
| 5687 | } |
| 5688 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5689 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5690 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5691 | MD->getParent()->isLambda()) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5692 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A; |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5693 | return; |
| 5694 | } |
| 5695 | } |
| 5696 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5697 | unsigned Index = A.getAttributeSpellingListIndex(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5698 | Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5699 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 5700 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5701 | if (NewAttr) |
| 5702 | D->addAttr(NewAttr); |
| 5703 | } |
| 5704 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5705 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5706 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5707 | unsigned AttrSpellingListIndex, |
| 5708 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 5709 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 5710 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5711 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5712 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 5713 | << 1 /*previous declaration*/; |
| 5714 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 5715 | D->dropAttr<MSInheritanceAttr>(); |
| 5716 | } |
| 5717 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5718 | auto *RD = cast<CXXRecordDecl>(D); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5719 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5720 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 5721 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5722 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } else { |
| 5725 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 5726 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5727 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5728 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5729 | } |
| 5730 | if (RD->getDescribedClassTemplate()) { |
| 5731 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5732 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5733 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5738 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5741 | static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5742 | // The capability attributes take a single string parameter for the name of |
| 5743 | // the capability they represent. The lockable attribute does not take any |
| 5744 | // parameters. However, semantically, both attributes represent the same |
| 5745 | // concept, and so they use the same semantic attribute. Eventually, the |
| 5746 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5747 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 5748 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5749 | // literal will be considered a "mutex." |
| 5750 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5751 | SourceLocation LiteralLoc; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5752 | if (AL.getKind() == ParsedAttr::AT_Capability && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5753 | !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5754 | return; |
| 5755 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5756 | // Currently, there are only two names allowed for a capability: role and |
| 5757 | // mutex (case insensitive). Diagnose other capability names. |
| 5758 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 5759 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 5760 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5761 | D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N, |
| 5762 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5765 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 5766 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5767 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 5768 | return; |
| 5769 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5770 | D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context, |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame] | 5771 | Args.data(), Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5772 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5776 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5777 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5778 | if (!checkLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5779 | return; |
| 5780 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5781 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5782 | S.Context, |
| 5783 | Args.data(), Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5784 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5788 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5789 | SmallVector<Expr*, 2> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5790 | if (!checkTryLockFunAttrCommon(S, D, AL, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5791 | return; |
| 5792 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5793 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5794 | S.Context, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5795 | AL.getArgAsExpr(0), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5796 | Args.data(), |
| 5797 | Args.size(), |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5798 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5802 | const ParsedAttr &AL) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5803 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5804 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5805 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5806 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5807 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5808 | AL.getRange(), S.Context, Args.data(), Args.size(), |
| 5809 | AL.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5812 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5813 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5814 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5815 | return; |
| 5816 | |
| 5817 | // check that all arguments are lockable objects |
| 5818 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5819 | checkAttrArgsAreCapabilityObjs(S, D, AL, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5820 | if (Args.empty()) |
| 5821 | return; |
| 5822 | |
| 5823 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5824 | RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(), |
| 5825 | Args.size(), AL.getAttributeSpellingListIndex()); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5826 | |
| 5827 | D->addAttr(RCA); |
| 5828 | } |
| 5829 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5830 | static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5831 | if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5832 | if (NSD->isAnonymousNamespace()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5833 | S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace); |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5834 | // Do not want to attach the attribute to the namespace because that will |
| 5835 | // cause confusing diagnostic reports for uses of declarations within the |
| 5836 | // namespace. |
| 5837 | return; |
| 5838 | } |
| 5839 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5840 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5841 | // Handle the cases where the attribute has a text message. |
| 5842 | StringRef Str, Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5843 | if (AL.isArgExpr(0) && AL.getArgAsExpr(0) && |
| 5844 | !S.checkStringLiteralArgumentAttr(AL, 0, Str)) |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5845 | return; |
| 5846 | |
| 5847 | // Only support a single optional message for Declspec and CXX11. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5848 | if (AL.isDeclspecAttribute() || AL.isCXX11Attribute()) |
| 5849 | checkAttributeAtMostNumArgs(S, AL, 1); |
| 5850 | else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) && |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 5851 | !S.checkStringLiteralArgumentAttr(AL, 1, Replacement)) |
| 5852 | return; |
| 5853 | |
| 5854 | if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope()) |
| 5855 | S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL; |
| 5856 | |
| 5857 | D->addAttr(::new (S.Context) |
| 5858 | DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5859 | AL.getAttributeSpellingListIndex())); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
| 5862 | static bool isGlobalVar(const Decl *D) { |
| 5863 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 5864 | return S->hasGlobalStorage(); |
| 5865 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5868 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5869 | if (!checkAttributeAtLeastNumArgs(S, AL, 1)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5870 | return; |
| 5871 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5872 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5873 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5874 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5875 | StringRef SanitizerName; |
| 5876 | SourceLocation LiteralLoc; |
| 5877 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5878 | if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc)) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5879 | return; |
| 5880 | |
| 5881 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 5882 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5883 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 5884 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5885 | << AL << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5886 | Sanitizers.push_back(SanitizerName); |
| 5887 | } |
| 5888 | |
| 5889 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5890 | AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 5891 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5895 | const ParsedAttr &AL) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5896 | StringRef AttrName = AL.getName()->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 5897 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5898 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 5899 | .Case("no_address_safety_analysis", "address") |
| 5900 | .Case("no_sanitize_address", "address") |
| 5901 | .Case("no_sanitize_thread", "thread") |
| 5902 | .Case("no_sanitize_memory", "memory"); |
| 5903 | if (isGlobalVar(D) && SanitizerName != "address") |
| 5904 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5905 | << AL << ExpectedFunction; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5906 | D->addAttr(::new (S.Context) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5907 | NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1, |
| 5908 | AL.getAttributeSpellingListIndex())); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5911 | static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5912 | if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL)) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5913 | D->addAttr(Internal); |
| 5914 | } |
| 5915 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5916 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5917 | if (S.LangOpts.OpenCLVersion != 200) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5918 | S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5919 | << AL << "2.0" << 0; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5920 | else |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5921 | S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL |
| 5922 | << "2.0"; |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5923 | } |
| 5924 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5925 | /// Handles semantic checking for features that are common to all attributes, |
| 5926 | /// such as checking whether a parameter was properly specified, or the correct |
| 5927 | /// number of arguments were passed, etc. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5928 | static bool handleCommonAttributeFeatures(Sema &S, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5929 | const ParsedAttr &AL) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5930 | // Several attributes carry different semantics than the parsing requires, so |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5931 | // those are opted out of the common argument checks. |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5932 | // |
| 5933 | // We also bail on unknown and ignored attributes because those are handled |
| 5934 | // as part of the target-specific handling logic. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5935 | if (AL.getKind() == ParsedAttr::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5936 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5937 | // Check whether the attribute requires specific language extensions to be |
| 5938 | // enabled. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5939 | if (!AL.diagnoseLangOpts(S)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5940 | return true; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5941 | // Check whether the attribute appertains to the given subject. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5942 | if (!AL.diagnoseAppertainsTo(S, D)) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5943 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5944 | if (AL.hasCustomParsing()) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5945 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5946 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5947 | if (AL.getMinArgs() == AL.getMaxArgs()) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5948 | // If there are no optional arguments, then checking for the argument count |
| 5949 | // is trivial. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5950 | if (!checkAttributeNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5951 | return true; |
| 5952 | } else { |
| 5953 | // There are optional arguments, so checking is slightly more involved. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5954 | if (AL.getMinArgs() && |
| 5955 | !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5956 | return true; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5957 | else if (!AL.hasVariadicArg() && AL.getMaxArgs() && |
| 5958 | !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs())) |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5959 | return true; |
| 5960 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5961 | |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 5962 | if (S.CheckAttrTarget(AL)) |
| 5963 | return true; |
| 5964 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5965 | return false; |
| 5966 | } |
| 5967 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5968 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5969 | if (D->isInvalidDecl()) |
| 5970 | return; |
| 5971 | |
| 5972 | // Check if there is only one access qualifier. |
| 5973 | if (D->hasAttr<OpenCLAccessAttr>()) { |
Andrew Savonichev | 05a15af | 2018-09-06 15:10:26 +0000 | [diff] [blame] | 5974 | if (D->getAttr<OpenCLAccessAttr>()->getSemanticSpelling() == |
| 5975 | AL.getSemanticSpelling()) { |
| 5976 | S.Diag(AL.getLoc(), diag::warn_duplicate_declspec) |
| 5977 | << AL.getName()->getName() << AL.getRange(); |
| 5978 | } else { |
| 5979 | S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 5980 | << D->getSourceRange(); |
| 5981 | D->setInvalidDecl(true); |
| 5982 | return; |
| 5983 | } |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 5987 | // image object can be read and written. |
| 5988 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 5989 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 5990 | // qualifier is a compilation error. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5991 | if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5992 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5993 | if (AL.getName()->getName().find("read_write") != StringRef::npos) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5994 | if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 5995 | S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 5996 | << AL << PDecl->getType() << DeclTy->isImageType(); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5997 | D->setInvalidDecl(true); |
| 5998 | return; |
| 5999 | } |
| 6000 | } |
| 6001 | } |
| 6002 | |
| 6003 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6004 | AL.getRange(), S.Context, AL.getAttributeSpellingListIndex())); |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6007 | static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) { |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6008 | if (!cast<VarDecl>(D)->hasGlobalStorage()) { |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6009 | S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var) |
| 6010 | << (A.getKind() == ParsedAttr::AT_AlwaysDestroy); |
| 6011 | return; |
| 6012 | } |
| 6013 | |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6014 | if (A.getKind() == ParsedAttr::AT_AlwaysDestroy) |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6015 | handleSimpleAttributeWithExclusions<AlwaysDestroyAttr, NoDestroyAttr>(S, D, A); |
Erik Pilkington | 63e7ab1 | 2018-08-21 17:50:10 +0000 | [diff] [blame] | 6016 | else |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6017 | handleSimpleAttributeWithExclusions<NoDestroyAttr, AlwaysDestroyAttr>(S, D, A); |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 6020 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6021 | // Top Level Sema Entry Points |
| 6022 | //===----------------------------------------------------------------------===// |
| 6023 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6024 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 6025 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 6026 | /// silently ignore it if a GNU attribute. |
| 6027 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6028 | const ParsedAttr &AL, |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6029 | bool IncludeCXX11Attributes) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6030 | if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6031 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 6032 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6033 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 6034 | // instead. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6035 | if (AL.isCXX11Attribute() && !IncludeCXX11Attributes) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6036 | return; |
| 6037 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6038 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 6039 | // which do not apply to the current target architecture are treated as |
| 6040 | // though they were unknown attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6041 | if (AL.getKind() == ParsedAttr::UnknownAttribute || |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6042 | !AL.existsInTarget(S.Context.getTargetInfo())) { |
| 6043 | S.Diag(AL.getLoc(), AL.isDeclspecAttribute() |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6044 | ? diag::warn_unhandled_ms_attribute_ignored |
| 6045 | : diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6046 | << AL; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6047 | return; |
| 6048 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6049 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6050 | if (handleCommonAttributeFeatures(S, D, AL)) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 6051 | return; |
| 6052 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6053 | switch (AL.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 6054 | default: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6055 | if (!AL.isStmtAttr()) { |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 6056 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6057 | assert(AL.isTypeAttr() && "Non-type attribute not handled"); |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 6058 | break; |
| 6059 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6060 | S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6061 | << AL << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6062 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6063 | case ParsedAttr::AT_Interrupt: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6064 | handleInterruptAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6065 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6066 | case ParsedAttr::AT_X86ForceAlignArgPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6067 | handleX86ForceAlignArgPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6068 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6069 | case ParsedAttr::AT_DLLExport: |
| 6070 | case ParsedAttr::AT_DLLImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6071 | handleDLLAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6072 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6073 | case ParsedAttr::AT_Mips16: |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6074 | handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6075 | MipsInterruptAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6076 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6077 | case ParsedAttr::AT_NoMips16: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6078 | handleSimpleAttribute<NoMips16Attr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6079 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6080 | case ParsedAttr::AT_MicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6081 | handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6082 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6083 | case ParsedAttr::AT_NoMicroMips: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6084 | handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL); |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6085 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6086 | case ParsedAttr::AT_MipsLongCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6087 | handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6088 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6089 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6090 | case ParsedAttr::AT_MipsShortCall: |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6091 | handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>( |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6092 | S, D, AL); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6093 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6094 | case ParsedAttr::AT_AMDGPUFlatWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6095 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6096 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6097 | case ParsedAttr::AT_AMDGPUWavesPerEU: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6098 | handleAMDGPUWavesPerEUAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6099 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6100 | case ParsedAttr::AT_AMDGPUNumSGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6101 | handleAMDGPUNumSGPRAttr(S, D, AL); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6102 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6103 | case ParsedAttr::AT_AMDGPUNumVGPR: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6104 | handleAMDGPUNumVGPRAttr(S, D, AL); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6105 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6106 | case ParsedAttr::AT_AVRSignal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6107 | handleAVRSignalAttr(S, D, AL); |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 6108 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6109 | case ParsedAttr::AT_IBAction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6110 | handleSimpleAttribute<IBActionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6111 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6112 | case ParsedAttr::AT_IBOutlet: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6113 | handleIBOutlet(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6114 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6115 | case ParsedAttr::AT_IBOutletCollection: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6116 | handleIBOutletCollection(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6117 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6118 | case ParsedAttr::AT_IFunc: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6119 | handleIFuncAttr(S, D, AL); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 6120 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6121 | case ParsedAttr::AT_Alias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6122 | handleAliasAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6123 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6124 | case ParsedAttr::AT_Aligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6125 | handleAlignedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6126 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6127 | case ParsedAttr::AT_AlignValue: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6128 | handleAlignValueAttr(S, D, AL); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 6129 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6130 | case ParsedAttr::AT_AllocSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6131 | handleAllocSizeAttr(S, D, AL); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6132 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6133 | case ParsedAttr::AT_AlwaysInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6134 | handleAlwaysInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6135 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6136 | case ParsedAttr::AT_Artificial: |
Aaron Ballman | 736c09b | 2018-02-15 16:28:10 +0000 | [diff] [blame] | 6137 | handleSimpleAttribute<ArtificialAttr>(S, D, AL); |
Erich Keane | 293a055 | 2018-02-14 00:14:07 +0000 | [diff] [blame] | 6138 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6139 | case ParsedAttr::AT_AnalyzerNoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6140 | handleAnalyzerNoReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6141 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6142 | case ParsedAttr::AT_TLSModel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6143 | handleTLSModelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6144 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6145 | case ParsedAttr::AT_Annotate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6146 | handleAnnotateAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6147 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6148 | case ParsedAttr::AT_Availability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6149 | handleAvailabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6150 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6151 | case ParsedAttr::AT_CarriesDependency: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6152 | handleDependencyAttr(S, scope, D, AL); |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6153 | break; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 6154 | case ParsedAttr::AT_CPUDispatch: |
| 6155 | case ParsedAttr::AT_CPUSpecific: |
| 6156 | handleCPUSpecificAttr(S, D, AL); |
| 6157 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6158 | case ParsedAttr::AT_Common: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6159 | handleCommonAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6160 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6161 | case ParsedAttr::AT_CUDAConstant: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6162 | handleConstantAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6163 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6164 | case ParsedAttr::AT_PassObjectSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6165 | handlePassObjectSizeAttr(S, D, AL); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6166 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6167 | case ParsedAttr::AT_Constructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6168 | handleConstructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6169 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6170 | case ParsedAttr::AT_CXX11NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6171 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6172 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6173 | case ParsedAttr::AT_Deprecated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6174 | handleDeprecatedAttr(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6175 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6176 | case ParsedAttr::AT_Destructor: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6177 | handleDestructorAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6178 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6179 | case ParsedAttr::AT_EnableIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6180 | handleEnableIfAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6181 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6182 | case ParsedAttr::AT_DiagnoseIf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6183 | handleDiagnoseIfAttr(S, D, AL); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6184 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6185 | case ParsedAttr::AT_ExtVectorType: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6186 | handleExtVectorTypeAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6187 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6188 | case ParsedAttr::AT_ExternalSourceSymbol: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6189 | handleExternalSourceSymbolAttr(S, D, AL); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 6190 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6191 | case ParsedAttr::AT_MinSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6192 | handleMinSizeAttr(S, D, AL); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6193 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6194 | case ParsedAttr::AT_OptimizeNone: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6195 | handleOptimizeNoneAttr(S, D, AL); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 6196 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6197 | case ParsedAttr::AT_FlagEnum: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6198 | handleSimpleAttribute<FlagEnumAttr>(S, D, AL); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 6199 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6200 | case ParsedAttr::AT_EnumExtensibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6201 | handleEnumExtensibilityAttr(S, D, AL); |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 6202 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6203 | case ParsedAttr::AT_Flatten: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6204 | handleSimpleAttribute<FlattenAttr>(S, D, AL); |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 6205 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6206 | case ParsedAttr::AT_Format: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6207 | handleFormatAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6208 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6209 | case ParsedAttr::AT_FormatArg: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6210 | handleFormatArgAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6211 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6212 | case ParsedAttr::AT_CUDAGlobal: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6213 | handleGlobalAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6214 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6215 | case ParsedAttr::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6216 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6217 | AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6218 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6219 | case ParsedAttr::AT_CUDAHost: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6220 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6221 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6222 | case ParsedAttr::AT_GNUInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6223 | handleGNUInlineAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6224 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6225 | case ParsedAttr::AT_CUDALaunchBounds: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6226 | handleLaunchBoundsAttr(S, D, AL); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 6227 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6228 | case ParsedAttr::AT_Restrict: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6229 | handleRestrictAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6230 | break; |
Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 6231 | case ParsedAttr::AT_LifetimeBound: |
| 6232 | handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL); |
| 6233 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6234 | case ParsedAttr::AT_MayAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6235 | handleSimpleAttribute<MayAliasAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6236 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6237 | case ParsedAttr::AT_Mode: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6238 | handleModeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6239 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6240 | case ParsedAttr::AT_NoAlias: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6241 | handleSimpleAttribute<NoAliasAttr>(S, D, AL); |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 6242 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6243 | case ParsedAttr::AT_NoCommon: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6244 | handleSimpleAttribute<NoCommonAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6245 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6246 | case ParsedAttr::AT_NoSplitStack: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6247 | handleSimpleAttribute<NoSplitStackAttr>(S, D, AL); |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 6248 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6249 | case ParsedAttr::AT_NonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6250 | if (auto *PVD = dyn_cast<ParmVarDecl>(D)) |
| 6251 | handleNonNullAttrParameter(S, PVD, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6252 | else |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6253 | handleNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6254 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6255 | case ParsedAttr::AT_ReturnsNonNull: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6256 | handleReturnsNonNullAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6257 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6258 | case ParsedAttr::AT_NoEscape: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6259 | handleNoEscapeAttr(S, D, AL); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 6260 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6261 | case ParsedAttr::AT_AssumeAligned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6262 | handleAssumeAlignedAttr(S, D, AL); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 6263 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6264 | case ParsedAttr::AT_AllocAlign: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6265 | handleAllocAlignAttr(S, D, AL); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 6266 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6267 | case ParsedAttr::AT_Overloadable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6268 | handleSimpleAttribute<OverloadableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6269 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6270 | case ParsedAttr::AT_Ownership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6271 | handleOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6272 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6273 | case ParsedAttr::AT_Cold: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6274 | handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6275 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6276 | case ParsedAttr::AT_Hot: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6277 | handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6278 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6279 | case ParsedAttr::AT_Naked: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6280 | handleNakedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6281 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6282 | case ParsedAttr::AT_NoReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6283 | handleNoReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6284 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6285 | case ParsedAttr::AT_AnyX86NoCfCheck: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 6286 | handleNoCfCheckAttr(S, D, AL); |
| 6287 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6288 | case ParsedAttr::AT_NoThrow: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6289 | handleSimpleAttribute<NoThrowAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6290 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6291 | case ParsedAttr::AT_CUDAShared: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6292 | handleSharedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6293 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6294 | case ParsedAttr::AT_VecReturn: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6295 | handleVecReturnAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6296 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6297 | case ParsedAttr::AT_ObjCOwnership: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6298 | handleObjCOwnershipAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6299 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6300 | case ParsedAttr::AT_ObjCPreciseLifetime: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6301 | handleObjCPreciseLifetimeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6302 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6303 | case ParsedAttr::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6304 | handleObjCReturnsInnerPointerAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6305 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6306 | case ParsedAttr::AT_ObjCRequiresSuper: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6307 | handleObjCRequiresSuperAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6308 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6309 | case ParsedAttr::AT_ObjCBridge: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6310 | handleObjCBridgeAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6311 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6312 | case ParsedAttr::AT_ObjCBridgeMutable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6313 | handleObjCBridgeMutableAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6314 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6315 | case ParsedAttr::AT_ObjCBridgeRelated: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6316 | handleObjCBridgeRelatedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6317 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6318 | case ParsedAttr::AT_ObjCDesignatedInitializer: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6319 | handleObjCDesignatedInitializer(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6320 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6321 | case ParsedAttr::AT_ObjCRuntimeName: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6322 | handleObjCRuntimeName(S, D, AL); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6323 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6324 | case ParsedAttr::AT_ObjCRuntimeVisible: |
| 6325 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL); |
| 6326 | break; |
| 6327 | case ParsedAttr::AT_ObjCBoxable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6328 | handleObjCBoxable(S, D, AL); |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 6329 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6330 | case ParsedAttr::AT_CFAuditedTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6331 | handleSimpleAttributeWithExclusions<CFAuditedTransferAttr, |
| 6332 | CFUnknownTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6333 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6334 | case ParsedAttr::AT_CFUnknownTransfer: |
Aaron Ballman | 3cfa9d1 | 2018-03-04 15:32:01 +0000 | [diff] [blame] | 6335 | handleSimpleAttributeWithExclusions<CFUnknownTransferAttr, |
| 6336 | CFAuditedTransferAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6337 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6338 | case ParsedAttr::AT_CFConsumed: |
| 6339 | case ParsedAttr::AT_NSConsumed: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6340 | handleNSConsumedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6341 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6342 | case ParsedAttr::AT_NSConsumesSelf: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6343 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6344 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6345 | case ParsedAttr::AT_NSReturnsAutoreleased: |
| 6346 | case ParsedAttr::AT_NSReturnsNotRetained: |
| 6347 | case ParsedAttr::AT_CFReturnsNotRetained: |
| 6348 | case ParsedAttr::AT_NSReturnsRetained: |
| 6349 | case ParsedAttr::AT_CFReturnsRetained: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6350 | handleNSReturnsRetainedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6351 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6352 | case ParsedAttr::AT_WorkGroupSizeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6353 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6354 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6355 | case ParsedAttr::AT_ReqdWorkGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6356 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6357 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6358 | case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6359 | handleSubGroupSize(S, D, AL); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 6360 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6361 | case ParsedAttr::AT_VecTypeHint: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6362 | handleVecTypeHint(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6363 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6364 | case ParsedAttr::AT_RequireConstantInit: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6365 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL); |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 6366 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6367 | case ParsedAttr::AT_InitPriority: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6368 | handleInitPriorityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6369 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6370 | case ParsedAttr::AT_Packed: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6371 | handlePackedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6372 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6373 | case ParsedAttr::AT_Section: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6374 | handleSectionAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6375 | break; |
Zola Bridges | cbac3ad | 2018-11-27 19:56:46 +0000 | [diff] [blame] | 6376 | case ParsedAttr::AT_SpeculativeLoadHardening: |
| 6377 | handleSimpleAttribute<SpeculativeLoadHardeningAttr>(S, D, AL); |
| 6378 | break; |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 6379 | case ParsedAttr::AT_CodeSeg: |
| 6380 | handleCodeSegAttr(S, D, AL); |
| 6381 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6382 | case ParsedAttr::AT_Target: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6383 | handleTargetAttr(S, D, AL); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 6384 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6385 | case ParsedAttr::AT_MinVectorWidth: |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 6386 | handleMinVectorWidthAttr(S, D, AL); |
| 6387 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6388 | case ParsedAttr::AT_Unavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6389 | handleAttrWithMessage<UnavailableAttr>(S, D, AL); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6390 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6391 | case ParsedAttr::AT_ArcWeakrefUnavailable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6392 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6393 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6394 | case ParsedAttr::AT_ObjCRootClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6395 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6396 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6397 | case ParsedAttr::AT_ObjCSubclassingRestricted: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6398 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL); |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 6399 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6400 | case ParsedAttr::AT_ObjCExplicitProtocolImpl: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6401 | handleObjCSuppresProtocolAttr(S, D, AL); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 6402 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6403 | case ParsedAttr::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6404 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6405 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6406 | case ParsedAttr::AT_Unused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6407 | handleUnusedAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6408 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6409 | case ParsedAttr::AT_ReturnsTwice: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6410 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6411 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6412 | case ParsedAttr::AT_NotTailCalled: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6413 | handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>( |
| 6414 | S, D, AL); |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 6415 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6416 | case ParsedAttr::AT_DisableTailCalls: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6417 | handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D, |
| 6418 | AL); |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 6419 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6420 | case ParsedAttr::AT_Used: |
Aaron Ballman | 1a3901c | 2018-03-03 21:02:09 +0000 | [diff] [blame] | 6421 | handleSimpleAttribute<UsedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6422 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6423 | case ParsedAttr::AT_Visibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6424 | handleVisibilityAttr(S, D, AL, false); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 6425 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6426 | case ParsedAttr::AT_TypeVisibility: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6427 | handleVisibilityAttr(S, D, AL, true); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 6428 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6429 | case ParsedAttr::AT_WarnUnused: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6430 | handleSimpleAttribute<WarnUnusedAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6431 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6432 | case ParsedAttr::AT_WarnUnusedResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6433 | handleWarnUnusedResult(S, D, AL); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 6434 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6435 | case ParsedAttr::AT_Weak: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6436 | handleSimpleAttribute<WeakAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6437 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6438 | case ParsedAttr::AT_WeakRef: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6439 | handleWeakRefAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6440 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6441 | case ParsedAttr::AT_WeakImport: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6442 | handleWeakImportAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6443 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6444 | case ParsedAttr::AT_TransparentUnion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6445 | handleTransparentUnionAttr(S, D, AL); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6446 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6447 | case ParsedAttr::AT_ObjCException: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6448 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6449 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6450 | case ParsedAttr::AT_ObjCMethodFamily: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6451 | handleObjCMethodFamilyAttr(S, D, AL); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 6452 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6453 | case ParsedAttr::AT_ObjCNSObject: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6454 | handleObjCNSObject(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6455 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6456 | case ParsedAttr::AT_ObjCIndependentClass: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6457 | handleObjCIndependentClass(S, D, AL); |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 6458 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6459 | case ParsedAttr::AT_Blocks: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6460 | handleBlocksAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6461 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6462 | case ParsedAttr::AT_Sentinel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6463 | handleSentinelAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6464 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6465 | case ParsedAttr::AT_Const: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6466 | handleSimpleAttribute<ConstAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6467 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6468 | case ParsedAttr::AT_Pure: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6469 | handleSimpleAttribute<PureAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6470 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6471 | case ParsedAttr::AT_Cleanup: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6472 | handleCleanupAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6473 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6474 | case ParsedAttr::AT_NoDebug: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6475 | handleNoDebugAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6476 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6477 | case ParsedAttr::AT_NoDuplicate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6478 | handleSimpleAttribute<NoDuplicateAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6479 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6480 | case ParsedAttr::AT_Convergent: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6481 | handleSimpleAttribute<ConvergentAttr>(S, D, AL); |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 6482 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6483 | case ParsedAttr::AT_NoInline: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6484 | handleSimpleAttribute<NoInlineAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6485 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6486 | case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6487 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6488 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6489 | case ParsedAttr::AT_NoStackProtector: |
Manoj Gupta | 4fbf84c | 2018-05-09 21:41:18 +0000 | [diff] [blame] | 6490 | // Interacts with -fstack-protector options. |
| 6491 | handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL); |
| 6492 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6493 | case ParsedAttr::AT_StdCall: |
| 6494 | case ParsedAttr::AT_CDecl: |
| 6495 | case ParsedAttr::AT_FastCall: |
| 6496 | case ParsedAttr::AT_ThisCall: |
| 6497 | case ParsedAttr::AT_Pascal: |
| 6498 | case ParsedAttr::AT_RegCall: |
| 6499 | case ParsedAttr::AT_SwiftCall: |
| 6500 | case ParsedAttr::AT_VectorCall: |
| 6501 | case ParsedAttr::AT_MSABI: |
| 6502 | case ParsedAttr::AT_SysVABI: |
| 6503 | case ParsedAttr::AT_Pcs: |
| 6504 | case ParsedAttr::AT_IntelOclBicc: |
| 6505 | case ParsedAttr::AT_PreserveMost: |
| 6506 | case ParsedAttr::AT_PreserveAll: |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 6507 | case ParsedAttr::AT_AArch64VectorPcs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6508 | handleCallConvAttr(S, D, AL); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 6509 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6510 | case ParsedAttr::AT_Suppress: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6511 | handleSuppressAttr(S, D, AL); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 6512 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6513 | case ParsedAttr::AT_OpenCLKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6514 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6515 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6516 | case ParsedAttr::AT_OpenCLAccess: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6517 | handleOpenCLAccessAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6518 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6519 | case ParsedAttr::AT_OpenCLNoSVM: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6520 | handleOpenCLNoSVMAttr(S, D, AL); |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 6521 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6522 | case ParsedAttr::AT_SwiftContext: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6523 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6524 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6525 | case ParsedAttr::AT_SwiftErrorResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6526 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6527 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6528 | case ParsedAttr::AT_SwiftIndirectResult: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6529 | handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult); |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6530 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6531 | case ParsedAttr::AT_InternalLinkage: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6532 | handleInternalLinkageAttr(S, D, AL); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 6533 | break; |
Louis Dionne | d269579 | 2018-10-04 15:49:42 +0000 | [diff] [blame] | 6534 | case ParsedAttr::AT_ExcludeFromExplicitInstantiation: |
| 6535 | handleSimpleAttribute<ExcludeFromExplicitInstantiationAttr>(S, D, AL); |
| 6536 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6537 | case ParsedAttr::AT_LTOVisibilityPublic: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6538 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL); |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 6539 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6540 | |
| 6541 | // Microsoft attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6542 | case ParsedAttr::AT_EmptyBases: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6543 | handleSimpleAttribute<EmptyBasesAttr>(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6544 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6545 | case ParsedAttr::AT_LayoutVersion: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6546 | handleLayoutVersion(S, D, AL); |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6547 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6548 | case ParsedAttr::AT_TrivialABI: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6549 | handleSimpleAttribute<TrivialABIAttr>(S, D, AL); |
Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 6550 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6551 | case ParsedAttr::AT_MSNoVTable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6552 | handleSimpleAttribute<MSNoVTableAttr>(S, D, AL); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 6553 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6554 | case ParsedAttr::AT_MSStruct: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6555 | handleSimpleAttribute<MSStructAttr>(S, D, AL); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6556 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6557 | case ParsedAttr::AT_Uuid: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6558 | handleUuidAttr(S, D, AL); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 6559 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6560 | case ParsedAttr::AT_MSInheritance: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6561 | handleMSInheritanceAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6562 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6563 | case ParsedAttr::AT_SelectAny: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6564 | handleSimpleAttribute<SelectAnyAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6565 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6566 | case ParsedAttr::AT_Thread: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6567 | handleDeclspecThreadAttr(S, D, AL); |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 6568 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6569 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6570 | case ParsedAttr::AT_AbiTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6571 | handleAbiTagAttr(S, D, AL); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 6572 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6573 | |
| 6574 | // Thread safety attributes: |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6575 | case ParsedAttr::AT_AssertExclusiveLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6576 | handleAssertExclusiveLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 6577 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6578 | case ParsedAttr::AT_AssertSharedLock: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6579 | handleAssertSharedLockAttr(S, D, AL); |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 6580 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6581 | case ParsedAttr::AT_GuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6582 | handleSimpleAttribute<GuardedVarAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6583 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6584 | case ParsedAttr::AT_PtGuardedVar: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6585 | handlePtGuardedVarAttr(S, D, AL); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6586 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6587 | case ParsedAttr::AT_ScopedLockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6588 | handleSimpleAttribute<ScopedLockableAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6589 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6590 | case ParsedAttr::AT_NoSanitize: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6591 | handleNoSanitizeAttr(S, D, AL); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6592 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6593 | case ParsedAttr::AT_NoSanitizeSpecific: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6594 | handleNoSanitizeSpecificAttr(S, D, AL); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 6595 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6596 | case ParsedAttr::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6597 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 6598 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6599 | case ParsedAttr::AT_GuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6600 | handleGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6601 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6602 | case ParsedAttr::AT_PtGuardedBy: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6603 | handlePtGuardedByAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6604 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6605 | case ParsedAttr::AT_ExclusiveTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6606 | handleExclusiveTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6607 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6608 | case ParsedAttr::AT_LockReturned: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6609 | handleLockReturnedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6610 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6611 | case ParsedAttr::AT_LocksExcluded: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6612 | handleLocksExcludedAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6613 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6614 | case ParsedAttr::AT_SharedTrylockFunction: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6615 | handleSharedTrylockFunctionAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6616 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6617 | case ParsedAttr::AT_AcquiredBefore: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6618 | handleAcquiredBeforeAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6619 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6620 | case ParsedAttr::AT_AcquiredAfter: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6621 | handleAcquiredAfterAttr(S, D, AL); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6622 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6623 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6624 | // Capability analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6625 | case ParsedAttr::AT_Capability: |
| 6626 | case ParsedAttr::AT_Lockable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6627 | handleCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6628 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6629 | case ParsedAttr::AT_RequiresCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6630 | handleRequiresCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6631 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6632 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6633 | case ParsedAttr::AT_AssertCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6634 | handleAssertCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6635 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6636 | case ParsedAttr::AT_AcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6637 | handleAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6638 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6639 | case ParsedAttr::AT_ReleaseCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6640 | handleReleaseCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6641 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6642 | case ParsedAttr::AT_TryAcquireCapability: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6643 | handleTryAcquireCapabilityAttr(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6644 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6645 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 6646 | // Consumed analysis attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6647 | case ParsedAttr::AT_Consumable: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6648 | handleConsumableAttr(S, D, AL); |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 6649 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6650 | case ParsedAttr::AT_ConsumableAutoCast: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6651 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6652 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6653 | case ParsedAttr::AT_ConsumableSetOnRead: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6654 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6655 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6656 | case ParsedAttr::AT_CallableWhen: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6657 | handleCallableWhenAttr(S, D, AL); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6658 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6659 | case ParsedAttr::AT_ParamTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6660 | handleParamTypestateAttr(S, D, AL); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 6661 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6662 | case ParsedAttr::AT_ReturnTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6663 | handleReturnTypestateAttr(S, D, AL); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 6664 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6665 | case ParsedAttr::AT_SetTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6666 | handleSetTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6667 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6668 | case ParsedAttr::AT_TestTypestate: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6669 | handleTestTypestateAttr(S, D, AL); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6670 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6671 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6672 | // Type safety attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6673 | case ParsedAttr::AT_ArgumentWithTypeTag: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6674 | handleArgumentWithTypeTagAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6675 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6676 | case ParsedAttr::AT_TypeTagForDatatype: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6677 | handleTypeTagForDatatypeAttr(S, D, AL); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6678 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6679 | case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 6680 | handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 6681 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6682 | case ParsedAttr::AT_RenderScriptKernel: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6683 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 6684 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6685 | // XRay attributes. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6686 | case ParsedAttr::AT_XRayInstrument: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6687 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL); |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6688 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6689 | case ParsedAttr::AT_XRayLogArgs: |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6690 | handleXRayLogArgsAttr(S, D, AL); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 6691 | break; |
Martin Bohme | 4e1293b | 2018-08-13 14:11:03 +0000 | [diff] [blame] | 6692 | |
| 6693 | // Move semantics attribute. |
| 6694 | case ParsedAttr::AT_Reinitializes: |
| 6695 | handleSimpleAttribute<ReinitializesAttr>(S, D, AL); |
| 6696 | break; |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 6697 | |
| 6698 | case ParsedAttr::AT_AlwaysDestroy: |
| 6699 | case ParsedAttr::AT_NoDestroy: |
| 6700 | handleDestroyAttr(S, D, AL); |
| 6701 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 6706 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 6707 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6708 | const ParsedAttributesView &AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6709 | bool IncludeCXX11Attributes) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6710 | if (AttrList.empty()) |
| 6711 | return; |
| 6712 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6713 | for (const ParsedAttr &AL : AttrList) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6714 | ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6715 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6716 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6717 | // GCC accepts |
| 6718 | // static int a9 __attribute__((weakref)); |
| 6719 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6720 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6721 | Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias) |
| 6722 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 6723 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6724 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6725 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6726 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 6727 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 6728 | // good to have a way to specify "these attributes must appear as a group", |
| 6729 | // for these. Additionally, it would be good to have a way to specify "these |
| 6730 | // 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] | 6731 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 6732 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6733 | if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6734 | // FIXME: This emits a different error message than |
| 6735 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6736 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6737 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6738 | } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6739 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6740 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6741 | } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6742 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6743 | D->setInvalidDecl(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6744 | } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 6745 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 6746 | D->setInvalidDecl(); |
Yaxun Liu | aa24601 | 2018-06-12 23:58:59 +0000 | [diff] [blame] | 6747 | } else if (!D->hasAttr<CUDAGlobalAttr>()) { |
| 6748 | if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 6749 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6750 | << A << ExpectedKernelFunction; |
| 6751 | D->setInvalidDecl(); |
| 6752 | } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 6753 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6754 | << A << ExpectedKernelFunction; |
| 6755 | D->setInvalidDecl(); |
| 6756 | } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6757 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6758 | << A << ExpectedKernelFunction; |
| 6759 | D->setInvalidDecl(); |
| 6760 | } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6761 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6762 | << A << ExpectedKernelFunction; |
| 6763 | D->setInvalidDecl(); |
| 6764 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6765 | } |
| 6766 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6767 | } |
| 6768 | |
Hiroshi Inoue | 939d932 | 2017-06-30 05:40:31 +0000 | [diff] [blame] | 6769 | // Helper for delayed processing TransparentUnion attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6770 | void Sema::ProcessDeclAttributeDelayed(Decl *D, |
| 6771 | const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6772 | for (const ParsedAttr &AL : AttrList) |
| 6773 | if (AL.getKind() == ParsedAttr::AT_TransparentUnion) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6774 | handleTransparentUnionAttr(*this, D, AL); |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 6775 | break; |
| 6776 | } |
| 6777 | } |
| 6778 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6779 | // Annotation attributes are the only attributes allowed after an access |
| 6780 | // specifier. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6781 | bool Sema::ProcessAccessDeclAttributeList( |
| 6782 | AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6783 | for (const ParsedAttr &AL : AttrList) { |
| 6784 | if (AL.getKind() == ParsedAttr::AT_Annotate) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6785 | ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6786 | } else { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6787 | Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6788 | return true; |
| 6789 | } |
| 6790 | } |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6791 | return false; |
| 6792 | } |
| 6793 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6794 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 6795 | /// contains any decl attributes that we should warn about. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6796 | static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6797 | for (const ParsedAttr &AL : A) { |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6798 | // Only warn if the attribute is an unignored, non-type attribute. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6799 | if (AL.isUsedAsTypeAttr() || AL.isInvalid()) |
| 6800 | continue; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6801 | if (AL.getKind() == ParsedAttr::IgnoredAttribute) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6802 | continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6803 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 6804 | if (AL.getKind() == ParsedAttr::UnknownAttribute) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6805 | S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6806 | << AL << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6807 | } else { |
Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 6808 | S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL |
| 6809 | << AL.getRange(); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6810 | } |
| 6811 | } |
| 6812 | } |
| 6813 | |
| 6814 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 6815 | /// used to build a declaration, complain about any decl attributes |
| 6816 | /// which might be lying around on it. |
| 6817 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6818 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes()); |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6819 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 6820 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 6821 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 6822 | } |
| 6823 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6824 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6825 | /// \#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] | 6826 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 6827 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6828 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6829 | NamedDecl *NewD = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6830 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6831 | FunctionDecl *NewFD; |
| 6832 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6833 | // FIXME: Mangling? |
| 6834 | // FIXME: Is the qualifier info correct? |
| 6835 | // FIXME: Is the DeclContext correct? |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6836 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 6837 | Loc, Loc, DeclarationName(II), |
| 6838 | FD->getType(), FD->getTypeSourceInfo(), |
| 6839 | SC_None, false/*isInlineSpecified*/, |
| 6840 | FD->hasPrototype(), |
| 6841 | false/*isConstexprSpecified*/); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6842 | NewD = NewFD; |
| 6843 | |
| 6844 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6845 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6846 | |
| 6847 | // Fake up parameter variables; they are declared as if this were |
| 6848 | // a typedef. |
| 6849 | QualType FDTy = FD->getType(); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6850 | if (const auto *FT = FDTy->getAs<FunctionProtoType>()) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6851 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6852 | for (const auto &AI : FT->param_types()) { |
| 6853 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6854 | Param->setScopeInfo(0, Params.size()); |
| 6855 | Params.push_back(Param); |
| 6856 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6857 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6858 | } |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6859 | } else if (auto *VD = dyn_cast<VarDecl>(ND)) { |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6860 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6861 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6862 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6863 | VD->getStorageClass()); |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6864 | if (VD->getQualifier()) |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame] | 6865 | cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc()); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6866 | } |
| 6867 | return NewD; |
| 6868 | } |
| 6869 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6870 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6871 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6872 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6873 | if (W.getUsed()) return; // only do this once |
| 6874 | W.setUsed(true); |
| 6875 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 6876 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6877 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6878 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 6879 | W.getLocation())); |
| 6880 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6881 | WeakTopLevelDecl.push_back(NewD); |
| 6882 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 6883 | // to insert Decl at TU scope, sorry. |
| 6884 | DeclContext *SavedContext = CurContext; |
| 6885 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 6886 | NewD->setDeclContext(CurContext); |
| 6887 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6888 | PushOnScopeChains(NewD, S); |
| 6889 | CurContext = SavedContext; |
| 6890 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6891 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6892 | } |
| 6893 | } |
| 6894 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6895 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 6896 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 6897 | // have to do this. |
| 6898 | LoadExternalWeakUndeclaredIdentifiers(); |
| 6899 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6900 | NamedDecl *ND = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6901 | if (auto *VD = dyn_cast<VarDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6902 | if (VD->isExternC()) |
| 6903 | ND = VD; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6904 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6905 | if (FD->isExternC()) |
| 6906 | ND = FD; |
| 6907 | if (ND) { |
| 6908 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 6909 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6910 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 6911 | WeakInfo W = I->second; |
| 6912 | DeclApplyPragmaWeak(S, ND, W); |
| 6913 | WeakUndeclaredIdentifiers[Id] = W; |
| 6914 | } |
| 6915 | } |
| 6916 | } |
| 6917 | } |
| 6918 | } |
| 6919 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6920 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 6921 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 6922 | /// 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] | 6923 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6924 | // Apply decl attributes from the DeclSpec if present. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6925 | if (!PD.getDeclSpec().getAttributes().empty()) |
| 6926 | ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes()); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6927 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6928 | // Walk the declarator structure, applying decl attributes that were in a type |
| 6929 | // position to the decl itself. This handles cases like: |
| 6930 | // int *__attr__(x)** D; |
| 6931 | // when X is a decl attribute. |
| 6932 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6933 | ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(), |
| 6934 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6935 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6936 | // Finally, apply any attributes on the decl itself. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6937 | ProcessDeclAttributeList(S, D, PD.getAttributes()); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 6938 | |
| 6939 | // Apply additional attributes specified by '#pragma clang attribute'. |
| 6940 | AddPragmaAttributes(S, D); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6941 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6942 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6943 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6944 | /// If so, it'll still be annotated with an attribute that makes it |
| 6945 | /// illegal to actually use. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6946 | static bool isForbiddenTypeAllowed(Sema &S, Decl *D, |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6947 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6948 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6949 | // Private ivars are always okay. Unfortunately, people don't |
| 6950 | // always properly make their ivars private, even in system headers. |
| 6951 | // Plus we need to make fields okay, too. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6952 | if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) && |
| 6953 | !isa<FunctionDecl>(D)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6954 | return false; |
| 6955 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6956 | // Silently accept unsupported uses of __weak in both user and system |
| 6957 | // declarations when it's been disabled, for ease of integration with |
| 6958 | // -fno-objc-arc files. We do have to take some care against attempts |
| 6959 | // to define such things; for now, we've only done that for ivars |
| 6960 | // and properties. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6961 | if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6962 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 6963 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 6964 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 6965 | return true; |
| 6966 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6967 | } |
| 6968 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6969 | // Allow all sorts of things in system headers. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6970 | if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6971 | // Currently, all the failures dealt with this way are due to ARC |
| 6972 | // restrictions. |
| 6973 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 6974 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | /// Handle a delayed forbidden-type diagnostic. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6981 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD, |
| 6982 | Decl *D) { |
| 6983 | auto Reason = UnavailableAttr::IR_None; |
| 6984 | if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) { |
| 6985 | assert(Reason && "didn't set reason?"); |
| 6986 | D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6987 | return; |
| 6988 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6989 | if (S.getLangOpts().ObjCAutoRefCount) |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6990 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 6991 | // FIXME: we may want to suppress diagnostics for all |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6992 | // kind of forbidden type messages on unavailable functions. |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6993 | if (FD->hasAttr<UnavailableAttr>() && |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 6994 | DD.getForbiddenTypeDiagnostic() == |
| 6995 | diag::err_arc_array_param_no_ownership) { |
| 6996 | DD.Triggered = true; |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6997 | return; |
| 6998 | } |
| 6999 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7000 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7001 | S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic()) |
| 7002 | << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument(); |
| 7003 | DD.Triggered = true; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7004 | } |
| 7005 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7006 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 7007 | const Decl *D) { |
| 7008 | // Check each AvailabilityAttr to find the one for this platform. |
| 7009 | for (const auto *A : D->attrs()) { |
| 7010 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 7011 | // FIXME: this is copied from CheckAvailability. We should try to |
| 7012 | // de-duplicate. |
| 7013 | |
| 7014 | // Check if this is an App Extension "platform", and if so chop off |
| 7015 | // the suffix for matching with the actual platform. |
| 7016 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 7017 | StringRef RealizedPlatform = ActualPlatform; |
| 7018 | if (Context.getLangOpts().AppExt) { |
| 7019 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 7020 | if (suffix != StringRef::npos) |
| 7021 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 7022 | } |
| 7023 | |
| 7024 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 7025 | |
| 7026 | // Match the platform name. |
| 7027 | if (RealizedPlatform == TargetPlatform) |
| 7028 | return Avail; |
| 7029 | } |
| 7030 | } |
| 7031 | return nullptr; |
| 7032 | } |
| 7033 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7034 | /// The diagnostic we should emit for \c D, and the declaration that |
| 7035 | /// originated it, or \c AR_Available. |
| 7036 | /// |
| 7037 | /// \param D The declaration to check. |
| 7038 | /// \param Message If non-null, this will be populated with the message from |
| 7039 | /// the availability attribute that is selected. |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7040 | /// \param ClassReceiver If we're checking the the method of a class message |
| 7041 | /// send, the class. Otherwise nullptr. |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7042 | static std::pair<AvailabilityResult, const NamedDecl *> |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7043 | ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D, |
| 7044 | std::string *Message, |
| 7045 | ObjCInterfaceDecl *ClassReceiver) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7046 | AvailabilityResult Result = D->getAvailability(Message); |
| 7047 | |
| 7048 | // For typedefs, if the typedef declaration appears available look |
| 7049 | // to the underlying type to see if it is more restrictive. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7050 | while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7051 | if (Result == AR_Available) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7052 | if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7053 | D = TT->getDecl(); |
| 7054 | Result = D->getAvailability(Message); |
| 7055 | continue; |
| 7056 | } |
| 7057 | } |
| 7058 | break; |
| 7059 | } |
| 7060 | |
| 7061 | // Forward class declarations get their attributes from their definition. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7062 | if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7063 | if (IDecl->getDefinition()) { |
| 7064 | D = IDecl->getDefinition(); |
| 7065 | Result = D->getAvailability(Message); |
| 7066 | } |
| 7067 | } |
| 7068 | |
| 7069 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 7070 | if (Result == AR_Available) { |
| 7071 | const DeclContext *DC = ECD->getDeclContext(); |
| 7072 | if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) { |
| 7073 | Result = TheEnumDecl->getAvailability(Message); |
| 7074 | D = TheEnumDecl; |
| 7075 | } |
| 7076 | } |
| 7077 | |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7078 | // For +new, infer availability from -init. |
| 7079 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7080 | if (S.NSAPIObj && ClassReceiver) { |
| 7081 | ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod( |
| 7082 | S.NSAPIObj->getInitSelector()); |
| 7083 | if (Init && Result == AR_Available && MD->isClassMethod() && |
| 7084 | MD->getSelector() == S.NSAPIObj->getNewSelector() && |
| 7085 | MD->definedInNSObject(S.getASTContext())) { |
| 7086 | Result = Init->getAvailability(Message); |
| 7087 | D = Init; |
| 7088 | } |
| 7089 | } |
| 7090 | } |
| 7091 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7092 | return {Result, D}; |
| 7093 | } |
| 7094 | |
| 7095 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7096 | /// whether we should emit a diagnostic for \c K and \c DeclVersion in |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7097 | /// the context of \c Ctx. For example, we should emit an unavailable diagnostic |
| 7098 | /// in a deprecated context, but not the other way around. |
| 7099 | static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 7100 | VersionTuple DeclVersion, |
| 7101 | Decl *Ctx) { |
| 7102 | assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 7103 | |
| 7104 | // Checks if we should emit the availability diagnostic in the context of C. |
| 7105 | auto CheckContext = [&](const Decl *C) { |
| 7106 | if (K == AR_NotYetIntroduced) { |
| 7107 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 7108 | if (AA->getIntroduced() >= DeclVersion) |
| 7109 | return true; |
| 7110 | } else if (K == AR_Deprecated) |
| 7111 | if (C->isDeprecated()) |
| 7112 | return true; |
| 7113 | |
| 7114 | if (C->isUnavailable()) |
| 7115 | return true; |
| 7116 | return false; |
| 7117 | }; |
| 7118 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7119 | do { |
| 7120 | if (CheckContext(Ctx)) |
| 7121 | return false; |
| 7122 | |
| 7123 | // An implementation implicitly has the availability of the interface. |
Steven Wu | 3bb4aa5 | 2018-04-16 23:34:18 +0000 | [diff] [blame] | 7124 | // Unless it is "+load" method. |
| 7125 | if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx)) |
| 7126 | if (MethodD->isClassMethod() && |
| 7127 | MethodD->getSelector().getAsString() == "load") |
| 7128 | return true; |
| 7129 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7130 | if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7131 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 7132 | if (CheckContext(Interface)) |
| 7133 | return false; |
| 7134 | } |
| 7135 | // A category implicitly has the availability of the interface. |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7136 | else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7137 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 7138 | if (CheckContext(Interface)) |
| 7139 | return false; |
| 7140 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 7141 | |
| 7142 | return true; |
| 7143 | } |
| 7144 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7145 | static bool |
| 7146 | shouldDiagnoseAvailabilityByDefault(const ASTContext &Context, |
| 7147 | const VersionTuple &DeploymentVersion, |
| 7148 | const VersionTuple &DeclVersion) { |
| 7149 | const auto &Triple = Context.getTargetInfo().getTriple(); |
| 7150 | VersionTuple ForceAvailabilityFromVersion; |
| 7151 | switch (Triple.getOS()) { |
| 7152 | case llvm::Triple::IOS: |
| 7153 | case llvm::Triple::TvOS: |
| 7154 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11); |
| 7155 | break; |
| 7156 | case llvm::Triple::WatchOS: |
| 7157 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4); |
| 7158 | break; |
| 7159 | case llvm::Triple::Darwin: |
| 7160 | case llvm::Triple::MacOSX: |
| 7161 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13); |
| 7162 | break; |
| 7163 | default: |
| 7164 | // New targets should always warn about availability. |
| 7165 | return Triple.getVendor() == llvm::Triple::Apple; |
| 7166 | } |
| 7167 | return DeploymentVersion >= ForceAvailabilityFromVersion || |
| 7168 | DeclVersion >= ForceAvailabilityFromVersion; |
| 7169 | } |
| 7170 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7171 | static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) { |
| 7172 | for (Decl *Ctx = OrigCtx; Ctx; |
| 7173 | Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) { |
| 7174 | if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx)) |
| 7175 | return cast<NamedDecl>(Ctx); |
| 7176 | if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) { |
| 7177 | if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx)) |
| 7178 | return Imp->getClassInterface(); |
| 7179 | return CD; |
| 7180 | } |
| 7181 | } |
| 7182 | |
| 7183 | return dyn_cast<NamedDecl>(OrigCtx); |
| 7184 | } |
| 7185 | |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7186 | namespace { |
| 7187 | |
| 7188 | struct AttributeInsertion { |
| 7189 | StringRef Prefix; |
| 7190 | SourceLocation Loc; |
| 7191 | StringRef Suffix; |
| 7192 | |
| 7193 | static AttributeInsertion createInsertionAfter(const NamedDecl *D) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7194 | return {" ", D->getEndLoc(), ""}; |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7195 | } |
| 7196 | static AttributeInsertion createInsertionAfter(SourceLocation Loc) { |
| 7197 | return {" ", Loc, ""}; |
| 7198 | } |
| 7199 | static AttributeInsertion createInsertionBefore(const NamedDecl *D) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7200 | return {"", D->getBeginLoc(), "\n"}; |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7201 | } |
| 7202 | }; |
| 7203 | |
| 7204 | } // end anonymous namespace |
| 7205 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7206 | /// Tries to parse a string as ObjC method name. |
| 7207 | /// |
| 7208 | /// \param Name The string to parse. Expected to originate from availability |
| 7209 | /// attribute argument. |
| 7210 | /// \param SlotNames The vector that will be populated with slot names. In case |
| 7211 | /// of unsuccessful parsing can contain invalid data. |
| 7212 | /// \returns A number of method parameters if parsing was successful, None |
| 7213 | /// otherwise. |
| 7214 | static Optional<unsigned> |
| 7215 | tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames, |
| 7216 | const LangOptions &LangOpts) { |
| 7217 | // Accept replacements starting with - or + as valid ObjC method names. |
| 7218 | if (!Name.empty() && (Name.front() == '-' || Name.front() == '+')) |
| 7219 | Name = Name.drop_front(1); |
| 7220 | if (Name.empty()) |
| 7221 | return None; |
| 7222 | Name.split(SlotNames, ':'); |
| 7223 | unsigned NumParams; |
| 7224 | if (Name.back() == ':') { |
| 7225 | // Remove an empty string at the end that doesn't represent any slot. |
| 7226 | SlotNames.pop_back(); |
| 7227 | NumParams = SlotNames.size(); |
| 7228 | } else { |
| 7229 | if (SlotNames.size() != 1) |
| 7230 | // Not a valid method name, just a colon-separated string. |
| 7231 | return None; |
| 7232 | NumParams = 0; |
| 7233 | } |
| 7234 | // Verify all slot names are valid. |
| 7235 | bool AllowDollar = LangOpts.DollarIdents; |
| 7236 | for (StringRef S : SlotNames) { |
| 7237 | if (S.empty()) |
| 7238 | continue; |
| 7239 | if (!isValidIdentifier(S, AllowDollar)) |
| 7240 | return None; |
| 7241 | } |
| 7242 | return NumParams; |
| 7243 | } |
| 7244 | |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7245 | /// Returns a source location in which it's appropriate to insert a new |
| 7246 | /// attribute for the given declaration \D. |
| 7247 | static Optional<AttributeInsertion> |
| 7248 | createAttributeInsertion(const NamedDecl *D, const SourceManager &SM, |
| 7249 | const LangOptions &LangOpts) { |
| 7250 | if (isa<ObjCPropertyDecl>(D)) |
| 7251 | return AttributeInsertion::createInsertionAfter(D); |
| 7252 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7253 | if (MD->hasBody()) |
| 7254 | return None; |
| 7255 | return AttributeInsertion::createInsertionAfter(D); |
| 7256 | } |
| 7257 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
| 7258 | SourceLocation Loc = |
| 7259 | Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts); |
| 7260 | if (Loc.isInvalid()) |
| 7261 | return None; |
| 7262 | // Insert after the 'struct'/whatever keyword. |
| 7263 | return AttributeInsertion::createInsertionAfter(Loc); |
| 7264 | } |
| 7265 | return AttributeInsertion::createInsertionBefore(D); |
| 7266 | } |
| 7267 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7268 | /// Actually emit an availability diagnostic for a reference to an unavailable |
| 7269 | /// decl. |
| 7270 | /// |
| 7271 | /// \param Ctx The context that the reference occurred in |
| 7272 | /// \param ReferringDecl The exact declaration that was referenced. |
| 7273 | /// \param OffendingDecl A related decl to \c ReferringDecl that has an |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7274 | /// availability attribute corresponding to \c K attached to it. Note that this |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7275 | /// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and |
| 7276 | /// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl |
| 7277 | /// and OffendingDecl is the EnumDecl. |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7278 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7279 | Decl *Ctx, const NamedDecl *ReferringDecl, |
| 7280 | const NamedDecl *OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7281 | StringRef Message, |
| 7282 | ArrayRef<SourceLocation> Locs, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7283 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7284 | const ObjCPropertyDecl *ObjCProperty, |
| 7285 | bool ObjCPropertyAccess) { |
| 7286 | // Diagnostics for deprecated or unavailable. |
| 7287 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7288 | unsigned diag_available_here = diag::note_availability_specified_here; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7289 | SourceLocation NoteLocation = OffendingDecl->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7290 | |
| 7291 | // Matches 'diag::note_property_attribute' options. |
| 7292 | unsigned property_note_select; |
| 7293 | |
| 7294 | // Matches diag::note_availability_specified_here. |
| 7295 | unsigned available_here_select_kind; |
| 7296 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7297 | VersionTuple DeclVersion; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7298 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7299 | DeclVersion = AA->getIntroduced(); |
| 7300 | |
| 7301 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx)) |
| 7302 | return; |
| 7303 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7304 | SourceLocation Loc = Locs.front(); |
| 7305 | |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7306 | // The declaration can have multiple availability attributes, we are looking |
| 7307 | // at one of them. |
| 7308 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl); |
| 7309 | if (A && A->isInherited()) { |
| 7310 | for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl; |
| 7311 | Redecl = Redecl->getPreviousDecl()) { |
| 7312 | const AvailabilityAttr *AForRedecl = |
| 7313 | getAttrForPlatform(S.Context, Redecl); |
| 7314 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 7315 | // If D is a declaration with inherited attributes, the note should |
| 7316 | // point to the declaration with actual attributes. |
| 7317 | NoteLocation = Redecl->getLocation(); |
| 7318 | break; |
| 7319 | } |
| 7320 | } |
| 7321 | } |
| 7322 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7323 | switch (K) { |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7324 | case AR_NotYetIntroduced: { |
| 7325 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 7326 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 7327 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 7328 | // later. |
| 7329 | const AvailabilityAttr *AA = |
| 7330 | getAttrForPlatform(S.getASTContext(), OffendingDecl); |
| 7331 | VersionTuple Introduced = AA->getIntroduced(); |
| 7332 | |
| 7333 | bool UseNewWarning = shouldDiagnoseAvailabilityByDefault( |
| 7334 | S.Context, S.Context.getTargetInfo().getPlatformMinVersion(), |
| 7335 | Introduced); |
| 7336 | unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new |
| 7337 | : diag::warn_unguarded_availability; |
| 7338 | |
| 7339 | S.Diag(Loc, Warning) |
| 7340 | << OffendingDecl |
| 7341 | << AvailabilityAttr::getPrettyPlatformName( |
| 7342 | S.getASTContext().getTargetInfo().getPlatformName()) |
| 7343 | << Introduced.getAsString(); |
| 7344 | |
| 7345 | S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here) |
| 7346 | << OffendingDecl << /* partial */ 3; |
| 7347 | |
| 7348 | if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) { |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7349 | if (const auto *TD = dyn_cast<TagDecl>(Enclosing)) |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7350 | if (TD->getDeclName().isEmpty()) { |
| 7351 | S.Diag(TD->getLocation(), |
| 7352 | diag::note_decl_unguarded_availability_silence) |
| 7353 | << /*Anonymous*/ 1 << TD->getKindName(); |
| 7354 | return; |
| 7355 | } |
| 7356 | auto FixitNoteDiag = |
| 7357 | S.Diag(Enclosing->getLocation(), |
| 7358 | diag::note_decl_unguarded_availability_silence) |
| 7359 | << /*Named*/ 0 << Enclosing; |
| 7360 | // Don't offer a fixit for declarations with availability attributes. |
| 7361 | if (Enclosing->hasAttr<AvailabilityAttr>()) |
| 7362 | return; |
| 7363 | if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE")) |
| 7364 | return; |
| 7365 | Optional<AttributeInsertion> Insertion = createAttributeInsertion( |
| 7366 | Enclosing, S.getSourceManager(), S.getLangOpts()); |
| 7367 | if (!Insertion) |
| 7368 | return; |
| 7369 | std::string PlatformName = |
| 7370 | AvailabilityAttr::getPlatformNameSourceSpelling( |
| 7371 | S.getASTContext().getTargetInfo().getPlatformName()) |
| 7372 | .lower(); |
| 7373 | std::string Introduced = |
| 7374 | OffendingDecl->getVersionIntroduced().getAsString(); |
| 7375 | FixitNoteDiag << FixItHint::CreateInsertion( |
| 7376 | Insertion->Loc, |
| 7377 | (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName + |
| 7378 | "(" + Introduced + "))" + Insertion->Suffix) |
| 7379 | .str()); |
| 7380 | } |
| 7381 | return; |
| 7382 | } |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7383 | case AR_Deprecated: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7384 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 7385 | : diag::warn_property_method_deprecated; |
| 7386 | diag_message = diag::warn_deprecated_message; |
| 7387 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 7388 | property_note_select = /* deprecated */ 0; |
| 7389 | available_here_select_kind = /* deprecated */ 2; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7390 | if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 7391 | NoteLocation = AL->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7392 | break; |
| 7393 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7394 | case AR_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7395 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 7396 | : diag::err_property_method_unavailable; |
| 7397 | diag_message = diag::err_unavailable_message; |
| 7398 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 7399 | property_note_select = /* unavailable */ 1; |
| 7400 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7401 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7402 | if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) { |
| 7403 | if (AL->isImplicit() && AL->getImplicitReason()) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7404 | // Most of these failures are due to extra restrictions in ARC; |
| 7405 | // reflect that in the primary diagnostic when applicable. |
| 7406 | auto flagARCError = [&] { |
| 7407 | if (S.getLangOpts().ObjCAutoRefCount && |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7408 | S.getSourceManager().isInSystemHeader( |
| 7409 | OffendingDecl->getLocation())) |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7410 | diag = diag::err_unavailable_in_arc; |
| 7411 | }; |
| 7412 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7413 | switch (AL->getImplicitReason()) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7414 | case UnavailableAttr::IR_None: break; |
| 7415 | |
| 7416 | case UnavailableAttr::IR_ARCForbiddenType: |
| 7417 | flagARCError(); |
| 7418 | diag_available_here = diag::note_arc_forbidden_type; |
| 7419 | break; |
| 7420 | |
| 7421 | case UnavailableAttr::IR_ForbiddenWeak: |
| 7422 | if (S.getLangOpts().ObjCWeakRuntime) |
| 7423 | diag_available_here = diag::note_arc_weak_disabled; |
| 7424 | else |
| 7425 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 7426 | break; |
| 7427 | |
| 7428 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 7429 | flagARCError(); |
| 7430 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 7431 | break; |
| 7432 | |
| 7433 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 7434 | flagARCError(); |
| 7435 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 7436 | break; |
| 7437 | |
| 7438 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 7439 | flagARCError(); |
| 7440 | diag_available_here = diag::note_arc_field_with_ownership; |
| 7441 | break; |
| 7442 | } |
| 7443 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7444 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7445 | break; |
| 7446 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7447 | case AR_Available: |
| 7448 | llvm_unreachable("Warning for availability of available declaration?"); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7449 | } |
| 7450 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7451 | SmallVector<FixItHint, 12> FixIts; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7452 | if (K == AR_Deprecated) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7453 | StringRef Replacement; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7454 | if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>()) |
| 7455 | Replacement = AL->getReplacement(); |
| 7456 | if (auto AL = getAttrForPlatform(S.Context, OffendingDecl)) |
| 7457 | Replacement = AL->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7458 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7459 | CharSourceRange UseRange; |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7460 | if (!Replacement.empty()) |
| 7461 | UseRange = |
| 7462 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7463 | if (UseRange.isValid()) { |
| 7464 | if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) { |
| 7465 | Selector Sel = MethodDecl->getSelector(); |
| 7466 | SmallVector<StringRef, 12> SelectorSlotNames; |
| 7467 | Optional<unsigned> NumParams = tryParseObjCMethodName( |
| 7468 | Replacement, SelectorSlotNames, S.getLangOpts()); |
| 7469 | if (NumParams && NumParams.getValue() == Sel.getNumArgs()) { |
| 7470 | assert(SelectorSlotNames.size() == Locs.size()); |
| 7471 | for (unsigned I = 0; I < Locs.size(); ++I) { |
| 7472 | if (!Sel.getNameForSlot(I).empty()) { |
| 7473 | CharSourceRange NameRange = CharSourceRange::getCharRange( |
| 7474 | Locs[I], S.getLocForEndOfToken(Locs[I])); |
| 7475 | FixIts.push_back(FixItHint::CreateReplacement( |
| 7476 | NameRange, SelectorSlotNames[I])); |
| 7477 | } else |
| 7478 | FixIts.push_back( |
| 7479 | FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I])); |
| 7480 | } |
| 7481 | } else |
| 7482 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 7483 | } else |
| 7484 | FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement)); |
| 7485 | } |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7486 | } |
| 7487 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7488 | if (!Message.empty()) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7489 | S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7490 | if (ObjCProperty) |
| 7491 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7492 | << ObjCProperty->getDeclName() << property_note_select; |
| 7493 | } else if (!UnknownObjCClass) { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7494 | S.Diag(Loc, diag) << ReferringDecl << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7495 | if (ObjCProperty) |
| 7496 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7497 | << ObjCProperty->getDeclName() << property_note_select; |
| 7498 | } else { |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7499 | S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7500 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 7501 | } |
| 7502 | |
Erik Pilkington | 8b352c4 | 2017-08-14 19:49:12 +0000 | [diff] [blame] | 7503 | S.Diag(NoteLocation, diag_available_here) |
| 7504 | << OffendingDecl << available_here_select_kind; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7505 | } |
| 7506 | |
| 7507 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 7508 | Decl *Ctx) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7509 | assert(DD.Kind == DelayedDiagnostic::Availability && |
| 7510 | "Expected an availability diagnostic here"); |
| 7511 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7512 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 7513 | DoEmitAvailabilityWarning( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7514 | S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(), |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7515 | DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(), |
| 7516 | DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(), |
| 7517 | DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7518 | } |
| 7519 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7520 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 7521 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7522 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7523 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7524 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7525 | // When delaying diagnostics to run in the context of a parsed |
| 7526 | // declaration, we only want to actually emit anything if parsing |
| 7527 | // succeeds. |
| 7528 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7529 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7530 | // We emit all the active diagnostics in this pool or any of its |
| 7531 | // parents. In general, we'll get one pool for the decl spec |
| 7532 | // and a child pool for each declarator; in a decl group like: |
| 7533 | // deprecated_typedef foo, *bar, baz(); |
| 7534 | // only the declarator pops will be passed decls. This is correct; |
| 7535 | // we really do need to consider delayed diagnostics from the decl spec |
| 7536 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7537 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7538 | do { |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 7539 | bool AnyAccessFailures = false; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7540 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7541 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 7542 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 7543 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7544 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7545 | continue; |
| 7546 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7547 | switch (diag.Kind) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7548 | case DelayedDiagnostic::Availability: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 7549 | // Don't bother giving deprecation/unavailable diagnostics if |
| 7550 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 7551 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7552 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7553 | break; |
| 7554 | |
| 7555 | case DelayedDiagnostic::Access: |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 7556 | // Only produce one access control diagnostic for a structured binding |
| 7557 | // declaration: we don't need to tell the user that all the fields are |
| 7558 | // inaccessible one at a time. |
| 7559 | if (AnyAccessFailures && isa<DecompositionDecl>(decl)) |
| 7560 | continue; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7561 | HandleDelayedAccessCheck(diag, decl); |
Richard Smith | 5c9b3b7 | 2018-09-25 22:12:44 +0000 | [diff] [blame] | 7562 | if (diag.Triggered) |
| 7563 | AnyAccessFailures = true; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7564 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7565 | |
| 7566 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7567 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7568 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7569 | } |
| 7570 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7571 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7572 | } |
| 7573 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7574 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 7575 | /// been delayed in the current context instead of in the given pool. |
| 7576 | /// Essentially, this just moves them to the current pool. |
| 7577 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 7578 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 7579 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 7580 | curPool->steal(pool); |
| 7581 | } |
| 7582 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7583 | static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR, |
| 7584 | const NamedDecl *ReferringDecl, |
| 7585 | const NamedDecl *OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7586 | StringRef Message, |
| 7587 | ArrayRef<SourceLocation> Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7588 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7589 | const ObjCPropertyDecl *ObjCProperty, |
| 7590 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7591 | // Delay if we're currently parsing a declaration. |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7592 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 7593 | S.DelayedDiagnostics.add( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7594 | DelayedDiagnostic::makeAvailability( |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7595 | AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7596 | ObjCProperty, Message, ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7597 | return; |
| 7598 | } |
| 7599 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7600 | Decl *Ctx = cast<Decl>(S.getCurLexicalContext()); |
| 7601 | DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl, |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7602 | Message, Locs, UnknownObjCClass, ObjCProperty, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7603 | ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7604 | } |
Erik Pilkington | 48c7cc9 | 2016-07-29 17:37:38 +0000 | [diff] [blame] | 7605 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7606 | namespace { |
| 7607 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7608 | /// Returns true if the given statement can be a body-like child of \p Parent. |
| 7609 | bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) { |
| 7610 | switch (Parent->getStmtClass()) { |
| 7611 | case Stmt::IfStmtClass: |
| 7612 | return cast<IfStmt>(Parent)->getThen() == S || |
| 7613 | cast<IfStmt>(Parent)->getElse() == S; |
| 7614 | case Stmt::WhileStmtClass: |
| 7615 | return cast<WhileStmt>(Parent)->getBody() == S; |
| 7616 | case Stmt::DoStmtClass: |
| 7617 | return cast<DoStmt>(Parent)->getBody() == S; |
| 7618 | case Stmt::ForStmtClass: |
| 7619 | return cast<ForStmt>(Parent)->getBody() == S; |
| 7620 | case Stmt::CXXForRangeStmtClass: |
| 7621 | return cast<CXXForRangeStmt>(Parent)->getBody() == S; |
| 7622 | case Stmt::ObjCForCollectionStmtClass: |
| 7623 | return cast<ObjCForCollectionStmt>(Parent)->getBody() == S; |
| 7624 | case Stmt::CaseStmtClass: |
| 7625 | case Stmt::DefaultStmtClass: |
| 7626 | return cast<SwitchCase>(Parent)->getSubStmt() == S; |
| 7627 | default: |
| 7628 | return false; |
| 7629 | } |
| 7630 | } |
| 7631 | |
| 7632 | class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> { |
| 7633 | const Stmt *Target; |
| 7634 | |
| 7635 | public: |
| 7636 | bool VisitStmt(Stmt *S) { return S != Target; } |
| 7637 | |
| 7638 | /// Returns true if the given statement is present in the given declaration. |
| 7639 | static bool isContained(const Stmt *Target, const Decl *D) { |
| 7640 | StmtUSEFinder Visitor; |
| 7641 | Visitor.Target = Target; |
| 7642 | return !Visitor.TraverseDecl(const_cast<Decl *>(D)); |
| 7643 | } |
| 7644 | }; |
| 7645 | |
| 7646 | /// Traverses the AST and finds the last statement that used a given |
| 7647 | /// declaration. |
| 7648 | class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> { |
| 7649 | const Decl *D; |
| 7650 | |
| 7651 | public: |
| 7652 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 7653 | if (DRE->getDecl() == D) |
| 7654 | return false; |
| 7655 | return true; |
| 7656 | } |
| 7657 | |
| 7658 | static const Stmt *findLastStmtThatUsesDecl(const Decl *D, |
| 7659 | const CompoundStmt *Scope) { |
| 7660 | LastDeclUSEFinder Visitor; |
| 7661 | Visitor.D = D; |
| 7662 | for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) { |
| 7663 | const Stmt *S = *I; |
| 7664 | if (!Visitor.TraverseStmt(const_cast<Stmt *>(S))) |
| 7665 | return S; |
| 7666 | } |
| 7667 | return nullptr; |
| 7668 | } |
| 7669 | }; |
| 7670 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7671 | /// This class implements -Wunguarded-availability. |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7672 | /// |
| 7673 | /// This is done with a traversal of the AST of a function that makes reference |
| 7674 | /// to a partially available declaration. Whenever we encounter an \c if of the |
| 7675 | /// form: \c if(@available(...)), we use the version from the condition to visit |
| 7676 | /// the then statement. |
| 7677 | class DiagnoseUnguardedAvailability |
| 7678 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 7679 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 7680 | |
| 7681 | Sema &SemaRef; |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7682 | Decl *Ctx; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7683 | |
| 7684 | /// Stack of potentially nested 'if (@available(...))'s. |
| 7685 | SmallVector<VersionTuple, 8> AvailabilityStack; |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7686 | SmallVector<const Stmt *, 16> StmtStack; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7687 | |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7688 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range, |
| 7689 | ObjCInterfaceDecl *ClassReceiver = nullptr); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7690 | |
| 7691 | public: |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7692 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 7693 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 7694 | AvailabilityStack.push_back( |
| 7695 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7696 | } |
| 7697 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 7698 | bool TraverseDecl(Decl *D) { |
| 7699 | // Avoid visiting nested functions to prevent duplicate warnings. |
| 7700 | if (!D || isa<FunctionDecl>(D)) |
| 7701 | return true; |
| 7702 | return Base::TraverseDecl(D); |
| 7703 | } |
| 7704 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7705 | bool TraverseStmt(Stmt *S) { |
| 7706 | if (!S) |
| 7707 | return true; |
| 7708 | StmtStack.push_back(S); |
| 7709 | bool Result = Base::TraverseStmt(S); |
| 7710 | StmtStack.pop_back(); |
| 7711 | return Result; |
| 7712 | } |
| 7713 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7714 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 7715 | |
| 7716 | bool TraverseIfStmt(IfStmt *If); |
| 7717 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 7718 | bool TraverseLambdaExpr(LambdaExpr *E) { return true; } |
| 7719 | |
Erik Pilkington | ba87c62 | 2017-08-18 20:20:56 +0000 | [diff] [blame] | 7720 | // for 'case X:' statements, don't bother looking at the 'X'; it can't lead |
| 7721 | // to any useful diagnostics. |
| 7722 | bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); } |
| 7723 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 7724 | bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) { |
| 7725 | if (PRE->isClassReceiver()) |
| 7726 | DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation()); |
| 7727 | return true; |
| 7728 | } |
| 7729 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7730 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7731 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) { |
| 7732 | ObjCInterfaceDecl *ID = nullptr; |
| 7733 | QualType ReceiverTy = Msg->getClassReceiver(); |
| 7734 | if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType()) |
| 7735 | ID = ReceiverTy->getAsObjCInterfaceType()->getInterface(); |
| 7736 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7737 | DiagnoseDeclAvailability( |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7738 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID); |
| 7739 | } |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7740 | return true; |
| 7741 | } |
| 7742 | |
| 7743 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 7744 | DiagnoseDeclAvailability(DRE->getDecl(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7745 | SourceRange(DRE->getBeginLoc(), DRE->getEndLoc())); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7746 | return true; |
| 7747 | } |
| 7748 | |
| 7749 | bool VisitMemberExpr(MemberExpr *ME) { |
| 7750 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7751 | SourceRange(ME->getBeginLoc(), ME->getEndLoc())); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7752 | return true; |
| 7753 | } |
| 7754 | |
Alex Lorenz | 0a484ba | 2017-05-24 15:15:29 +0000 | [diff] [blame] | 7755 | bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7756 | SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use) |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 7757 | << (!SemaRef.getLangOpts().ObjC); |
Alex Lorenz | 0a484ba | 2017-05-24 15:15:29 +0000 | [diff] [blame] | 7758 | return true; |
| 7759 | } |
| 7760 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7761 | bool VisitTypeLoc(TypeLoc Ty); |
| 7762 | }; |
| 7763 | |
| 7764 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7765 | NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7766 | AvailabilityResult Result; |
| 7767 | const NamedDecl *OffendingDecl; |
| 7768 | std::tie(Result, OffendingDecl) = |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7769 | ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass); |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7770 | if (Result != AR_Available) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7771 | // All other diagnostic kinds have already been handled in |
| 7772 | // DiagnoseAvailabilityOfDecl. |
| 7773 | if (Result != AR_NotYetIntroduced) |
| 7774 | return; |
| 7775 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7776 | const AvailabilityAttr *AA = |
| 7777 | getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7778 | VersionTuple Introduced = AA->getIntroduced(); |
| 7779 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7780 | if (AvailabilityStack.back() >= Introduced) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7781 | return; |
| 7782 | |
| 7783 | // If the context of this function is less available than D, we should not |
| 7784 | // emit a diagnostic. |
| 7785 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx)) |
| 7786 | return; |
| 7787 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7788 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 7789 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 7790 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 7791 | // later. |
| 7792 | unsigned DiagKind = |
| 7793 | shouldDiagnoseAvailabilityByDefault( |
| 7794 | SemaRef.Context, |
| 7795 | SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced) |
| 7796 | ? diag::warn_unguarded_availability_new |
| 7797 | : diag::warn_unguarded_availability; |
| 7798 | |
| 7799 | SemaRef.Diag(Range.getBegin(), DiagKind) |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7800 | << Range << D |
| 7801 | << AvailabilityAttr::getPrettyPlatformName( |
| 7802 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
| 7803 | << Introduced.getAsString(); |
| 7804 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7805 | SemaRef.Diag(OffendingDecl->getLocation(), |
| 7806 | diag::note_availability_specified_here) |
| 7807 | << OffendingDecl << /* partial */ 3; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7808 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7809 | auto FixitDiag = |
| 7810 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 7811 | << Range << D |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 7812 | << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0 |
| 7813 | : /*__builtin_available*/ 1); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7814 | |
| 7815 | // Find the statement which should be enclosed in the if @available check. |
| 7816 | if (StmtStack.empty()) |
| 7817 | return; |
| 7818 | const Stmt *StmtOfUse = StmtStack.back(); |
| 7819 | const CompoundStmt *Scope = nullptr; |
| 7820 | for (const Stmt *S : llvm::reverse(StmtStack)) { |
| 7821 | if (const auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 7822 | Scope = CS; |
| 7823 | break; |
| 7824 | } |
| 7825 | if (isBodyLikeChildStmt(StmtOfUse, S)) { |
| 7826 | // The declaration won't be seen outside of the statement, so we don't |
| 7827 | // have to wrap the uses of any declared variables in if (@available). |
| 7828 | // Therefore we can avoid setting Scope here. |
| 7829 | break; |
| 7830 | } |
| 7831 | StmtOfUse = S; |
| 7832 | } |
| 7833 | const Stmt *LastStmtOfUse = nullptr; |
| 7834 | if (isa<DeclStmt>(StmtOfUse) && Scope) { |
| 7835 | for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) { |
| 7836 | if (StmtUSEFinder::isContained(StmtStack.back(), D)) { |
| 7837 | LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope); |
| 7838 | break; |
| 7839 | } |
| 7840 | } |
| 7841 | } |
| 7842 | |
| 7843 | const SourceManager &SM = SemaRef.getSourceManager(); |
| 7844 | SourceLocation IfInsertionLoc = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7845 | SM.getExpansionLoc(StmtOfUse->getBeginLoc()); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7846 | SourceLocation StmtEndLoc = |
| 7847 | SM.getExpansionRange( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 7848 | (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc()) |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 7849 | .getEnd(); |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7850 | if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc)) |
| 7851 | return; |
| 7852 | |
| 7853 | StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM); |
| 7854 | const char *ExtraIndentation = " "; |
| 7855 | std::string FixItString; |
| 7856 | llvm::raw_string_ostream FixItOS(FixItString); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 7857 | FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available" |
| 7858 | : "__builtin_available") |
Alex Lorenz | e1fb64e | 2017-05-09 15:34:46 +0000 | [diff] [blame] | 7859 | << "(" |
| 7860 | << AvailabilityAttr::getPlatformNameSourceSpelling( |
| 7861 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7862 | << " " << Introduced.getAsString() << ", *)) {\n" |
| 7863 | << Indentation << ExtraIndentation; |
| 7864 | FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str()); |
| 7865 | SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken( |
| 7866 | StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(), |
| 7867 | /*SkipTrailingWhitespaceAndNewLine=*/false); |
| 7868 | if (ElseInsertionLoc.isInvalid()) |
| 7869 | ElseInsertionLoc = |
| 7870 | Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts()); |
| 7871 | FixItOS.str().clear(); |
| 7872 | FixItOS << "\n" |
| 7873 | << Indentation << "} else {\n" |
| 7874 | << Indentation << ExtraIndentation |
| 7875 | << "// Fallback on earlier versions\n" |
| 7876 | << Indentation << "}"; |
| 7877 | FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7878 | } |
| 7879 | } |
| 7880 | |
| 7881 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 7882 | const Type *TyPtr = Ty.getTypePtr(); |
| 7883 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 7884 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 7885 | if (Range.isInvalid()) |
| 7886 | return true; |
| 7887 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7888 | if (const auto *TT = dyn_cast<TagType>(TyPtr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7889 | TagDecl *TD = TT->getDecl(); |
| 7890 | DiagnoseDeclAvailability(TD, Range); |
| 7891 | |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7892 | } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7893 | TypedefNameDecl *D = TD->getDecl(); |
| 7894 | DiagnoseDeclAvailability(D, Range); |
| 7895 | |
| 7896 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 7897 | if (NamedDecl *D = ObjCO->getInterface()) |
| 7898 | DiagnoseDeclAvailability(D, Range); |
| 7899 | } |
| 7900 | |
| 7901 | return true; |
| 7902 | } |
| 7903 | |
| 7904 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 7905 | VersionTuple CondVersion; |
| 7906 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 7907 | CondVersion = E->getVersion(); |
| 7908 | |
| 7909 | // If we're using the '*' case here or if this check is redundant, then we |
| 7910 | // use the enclosing version to check both branches. |
| 7911 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
Alex Lorenz | 98f9fcd | 2017-08-17 14:22:27 +0000 | [diff] [blame] | 7912 | return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7913 | } else { |
| 7914 | // This isn't an availability checking 'if', we can just continue. |
| 7915 | return Base::TraverseIfStmt(If); |
| 7916 | } |
| 7917 | |
| 7918 | AvailabilityStack.push_back(CondVersion); |
| 7919 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 7920 | AvailabilityStack.pop_back(); |
| 7921 | |
| 7922 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 7923 | } |
| 7924 | |
| 7925 | } // end anonymous namespace |
| 7926 | |
| 7927 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 7928 | Stmt *Body = nullptr; |
| 7929 | |
| 7930 | if (auto *FD = D->getAsFunction()) { |
| 7931 | // FIXME: We only examine the pattern decl for availability violations now, |
| 7932 | // but we should also examine instantiated templates. |
| 7933 | if (FD->isTemplateInstantiation()) |
| 7934 | return; |
| 7935 | |
| 7936 | Body = FD->getBody(); |
| 7937 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7938 | Body = MD->getBody(); |
Alex Lorenz | 28559ce | 2017-04-26 14:20:02 +0000 | [diff] [blame] | 7939 | else if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 7940 | Body = BD->getBody(); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7941 | |
| 7942 | assert(Body && "Need a body here!"); |
| 7943 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7944 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7945 | } |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7946 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7947 | void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D, |
| 7948 | ArrayRef<SourceLocation> Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7949 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7950 | bool ObjCPropertyAccess, |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7951 | bool AvoidPartialAvailabilityChecks, |
| 7952 | ObjCInterfaceDecl *ClassReceiver) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7953 | std::string Message; |
| 7954 | AvailabilityResult Result; |
| 7955 | const NamedDecl* OffendingDecl; |
| 7956 | // See if this declaration is unavailable, deprecated, or partial. |
Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 7957 | std::tie(Result, OffendingDecl) = |
| 7958 | ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver); |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7959 | if (Result == AR_Available) |
| 7960 | return; |
| 7961 | |
| 7962 | if (Result == AR_NotYetIntroduced) { |
| 7963 | if (AvoidPartialAvailabilityChecks) |
| 7964 | return; |
| 7965 | |
| 7966 | // We need to know the @available context in the current function to |
| 7967 | // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that |
| 7968 | // when we're done parsing the current function. |
| 7969 | if (getCurFunctionOrMethodDecl()) { |
| 7970 | getEnclosingFunction()->HasPotentialAvailabilityViolations = true; |
| 7971 | return; |
| 7972 | } else if (getCurBlock() || getCurLambda()) { |
| 7973 | getCurFunction()->HasPotentialAvailabilityViolations = true; |
| 7974 | return; |
| 7975 | } |
| 7976 | } |
| 7977 | |
| 7978 | const ObjCPropertyDecl *ObjCPDecl = nullptr; |
Aaron Ballman | a70c6b5 | 2018-02-15 16:20:20 +0000 | [diff] [blame] | 7979 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7980 | if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { |
| 7981 | AvailabilityResult PDeclResult = PD->getAvailability(nullptr); |
| 7982 | if (PDeclResult == Result) |
| 7983 | ObjCPDecl = PD; |
| 7984 | } |
| 7985 | } |
| 7986 | |
Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 7987 | EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs, |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7988 | UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess); |
| 7989 | } |