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 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTMutationListener.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 24 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 29 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 35 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 36 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 37 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 38 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 39 | C, |
| 40 | Cpp, |
| 41 | ObjC |
| 42 | }; |
| 43 | } |
| 44 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | // Helper functions |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 49 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 50 | /// type (function or function-typed variable) or an Objective-C |
| 51 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 52 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 53 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 54 | } |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 55 | /// \brief Return true if the given decl has function type (function or |
| 56 | /// function-typed variable) or an Objective-C method or a block. |
| 57 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 58 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 59 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 60 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 61 | /// Return true if the given decl has a declarator that should have |
| 62 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 63 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 64 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 65 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 66 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 69 | /// hasFunctionProto - Return true if the given decl has a argument |
| 70 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 71 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 73 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 74 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 75 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 78 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 79 | /// 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] | 80 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 81 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 82 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 83 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 84 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 85 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 86 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 89 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 90 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 91 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 92 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 93 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 94 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 95 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 98 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 99 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 100 | return FD->getParamDecl(Idx)->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 101 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 102 | return MD->parameters()[Idx]->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 103 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 104 | return BD->getParamDecl(Idx)->getSourceRange(); |
| 105 | return SourceRange(); |
| 106 | } |
| 107 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 109 | if (const FunctionType *FnTy = D->getFunctionType()) |
Aaron Ballman | 6288d06 | 2014-07-11 16:31:29 +0000 | [diff] [blame] | 110 | return cast<FunctionType>(FnTy)->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 111 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 114 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 115 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 116 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 117 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 118 | return MD->getReturnTypeSourceRange(); |
| 119 | return SourceRange(); |
| 120 | } |
| 121 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 122 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 123 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 124 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 125 | return proto->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 126 | } |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 127 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 128 | return BD->isVariadic(); |
| 129 | |
| 130 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 133 | static bool isInstanceMethod(const Decl *D) { |
| 134 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 135 | return MethodDecl->isInstance(); |
| 136 | return false; |
| 137 | } |
| 138 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 139 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 140 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 141 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 142 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 143 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 144 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 145 | if (!Cls) |
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 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 150 | // FIXME: Should we walk the chain of classes? |
| 151 | return ClsName == &Ctx.Idents.get("NSString") || |
| 152 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 153 | } |
| 154 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 155 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 156 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 157 | if (!PT) |
| 158 | return false; |
| 159 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 160 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 161 | if (!RT) |
| 162 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 164 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 165 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 166 | return false; |
| 167 | |
| 168 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 169 | } |
| 170 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 171 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 172 | // FIXME: Include the type in the argument list. |
| 173 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 174 | } |
| 175 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 176 | template <typename Compare> |
| 177 | static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr, |
| 178 | unsigned Num, unsigned Diag, |
| 179 | Compare Comp) { |
| 180 | if (Comp(getNumAttributeArgs(Attr), Num)) { |
| 181 | S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 188 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 189 | /// output an error. |
| 190 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 191 | unsigned Num) { |
| 192 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 193 | diag::err_attribute_wrong_number_arguments, |
| 194 | std::not_equal_to<unsigned>()); |
| 195 | } |
| 196 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 197 | /// \brief Check if the attribute has at least as many args as Num. May |
| 198 | /// output an error. |
| 199 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 200 | unsigned Num) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 201 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 202 | diag::err_attribute_too_few_arguments, |
| 203 | std::less<unsigned>()); |
| 204 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 205 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 206 | /// \brief Check if the attribute has at most as many args as Num. May |
| 207 | /// output an error. |
| 208 | static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr, |
| 209 | unsigned Num) { |
| 210 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 211 | diag::err_attribute_too_many_arguments, |
| 212 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 215 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 216 | /// expression and return success or failure. May output an error. |
| 217 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 218 | const Expr *Expr, uint32_t &Val, |
| 219 | unsigned Idx = UINT_MAX) { |
| 220 | llvm::APSInt I(32); |
| 221 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 222 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 223 | if (Idx != UINT_MAX) |
| 224 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 225 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 226 | << Expr->getSourceRange(); |
| 227 | else |
| 228 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 229 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 230 | << Expr->getSourceRange(); |
| 231 | return false; |
| 232 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 233 | |
| 234 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 235 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 236 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 240 | Val = (uint32_t)I.getZExtValue(); |
| 241 | return true; |
| 242 | } |
| 243 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 244 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 245 | /// declaration. Returns true if diagnosed. |
| 246 | template <typename AttrTy> |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 247 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range, |
| 248 | IdentifierInfo *Ident) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 249 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 250 | S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident |
| 251 | << A; |
| 252 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 258 | /// \brief Check if IdxExpr is a valid parameter index for a function or |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 259 | /// instance method D. May output an error. |
| 260 | /// |
| 261 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 262 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 263 | const AttributeList &Attr, |
| 264 | unsigned AttrArgNum, |
| 265 | const Expr *IdxExpr, |
| 266 | uint64_t &Idx) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 267 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 268 | |
| 269 | // In C++ the implicit 'this' function parameter also counts. |
| 270 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 271 | bool HP = hasFunctionProto(D); |
| 272 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 273 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 274 | unsigned NumParams = |
| 275 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 276 | |
| 277 | llvm::APSInt IdxInt; |
| 278 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 279 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 280 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 281 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 282 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
| 286 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 287 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 288 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 289 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | Idx--; // Convert to zero-based. |
| 293 | if (HasImplicitThisParam) { |
| 294 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 295 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 296 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 297 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 298 | return false; |
| 299 | } |
| 300 | --Idx; |
| 301 | } |
| 302 | |
| 303 | return true; |
| 304 | } |
| 305 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 306 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 307 | /// If not emit an error and return false. If the argument is an identifier it |
| 308 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 309 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 310 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 311 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 312 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 313 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 314 | if (Attr.isArgIdent(ArgNum)) { |
| 315 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 316 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 317 | << Attr.getName() << AANT_ArgumentString |
| 318 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 319 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 320 | Str = Loc->Ident->getName(); |
| 321 | if (ArgLocation) |
| 322 | *ArgLocation = Loc->Loc; |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | // Now check for an actual string literal. |
| 327 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 328 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 329 | if (ArgLocation) |
| 330 | *ArgLocation = ArgExpr->getLocStart(); |
| 331 | |
| 332 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 333 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 334 | << Attr.getName() << AANT_ArgumentString; |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | Str = Literal->getString(); |
| 339 | return true; |
| 340 | } |
| 341 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 342 | /// \brief Applies the given attribute to the Decl without performing any |
| 343 | /// additional semantic checking. |
| 344 | template <typename AttrType> |
| 345 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 346 | const AttributeList &Attr) { |
| 347 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 348 | Attr.getAttributeSpellingListIndex())); |
| 349 | } |
| 350 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 351 | /// \brief Check if the passed-in expression is of type int or bool. |
| 352 | static bool isIntOrBool(Expr *Exp) { |
| 353 | QualType QT = Exp->getType(); |
| 354 | return QT->isBooleanType() || QT->isIntegerType(); |
| 355 | } |
| 356 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 357 | |
| 358 | // Check to see if the type is a smart pointer of some kind. We assume |
| 359 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 360 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 361 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 362 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 363 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 364 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 365 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 366 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 367 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 368 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 369 | return false; |
| 370 | |
| 371 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 374 | /// \brief Check if passed in Decl is a pointer type. |
| 375 | /// Note that this function may produce an error message. |
| 376 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 377 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 378 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 379 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 380 | QualType QT = vd->getType(); |
| 381 | if (QT->isAnyPointerType()) |
| 382 | return true; |
| 383 | |
| 384 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 385 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 386 | // (We don't want to force template instantiation if we can avoid it, |
| 387 | // since that would alter the order in which templates are instantiated.) |
| 388 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 389 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 390 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 391 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 392 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 393 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 394 | |
| 395 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 396 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 400 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 401 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 402 | static const RecordType *getRecordType(QualType QT) { |
| 403 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 404 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 405 | |
| 406 | // Now check if we point to record type. |
| 407 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 408 | return PT->getPointeeType()->getAs<RecordType>(); |
| 409 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 410 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 413 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 414 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 415 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 416 | if (!RT) |
| 417 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 418 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 419 | // 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] | 420 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 421 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 422 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 423 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 424 | // FIXME -- Check the type that the smart pointer points to. |
| 425 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 426 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 427 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 428 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 429 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 430 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 431 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 432 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 433 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 434 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 435 | CXXBasePaths BPaths(false, false); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 436 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 437 | const auto *Type = BS->getType()->getAs<RecordType>(); |
| 438 | return Type->getDecl()->hasAttr<CapabilityAttr>(); |
| 439 | }, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 440 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 441 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 445 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 446 | const auto *TD = Ty->getAs<TypedefType>(); |
| 447 | if (!TD) |
| 448 | return false; |
| 449 | |
| 450 | TypedefNameDecl *TN = TD->getDecl(); |
| 451 | if (!TN) |
| 452 | return false; |
| 453 | |
| 454 | return TN->hasAttr<CapabilityAttr>(); |
| 455 | } |
| 456 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 457 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 458 | if (checkTypedefTypeForCapability(Ty)) |
| 459 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 460 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 461 | if (checkRecordTypeForCapability(S, Ty)) |
| 462 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 463 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | |
| 467 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 468 | // Capability expressions are simple expressions involving the boolean logic |
| 469 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 470 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 471 | // is a capability or not. |
| 472 | |
| 473 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 474 | return typeHasCapability(S, E->getType()); |
| 475 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 476 | return isCapabilityExpr(S, E->getSubExpr()); |
| 477 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 478 | return isCapabilityExpr(S, E->getSubExpr()); |
| 479 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 480 | if (E->getOpcode() == UO_LNot) |
| 481 | return isCapabilityExpr(S, E->getSubExpr()); |
| 482 | return false; |
| 483 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 484 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 485 | return isCapabilityExpr(S, E->getLHS()) && |
| 486 | isCapabilityExpr(S, E->getRHS()); |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 493 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 494 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 495 | /// \param Sidx The attribute argument index to start checking with. |
| 496 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 497 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 498 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 499 | const AttributeList &Attr, |
| 500 | SmallVectorImpl<Expr *> &Args, |
| 501 | int Sidx = 0, |
| 502 | bool ParamIdxOk = false) { |
| 503 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 504 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 505 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 506 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 507 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 508 | Args.push_back(ArgExp); |
| 509 | continue; |
| 510 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 511 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 512 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 513 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 514 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 515 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 516 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 517 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 518 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 519 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 520 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 521 | // We allow constant strings to be used as a placeholder for expressions |
| 522 | // that are not valid C++ syntax, but warn that they are ignored. |
| 523 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 524 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 525 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 526 | continue; |
| 527 | } |
| 528 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 529 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 530 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 531 | // A pointer to member expression of the form &MyClass::mu is treated |
| 532 | // specially -- we need to look at the type of the member. |
| 533 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 534 | if (UOp->getOpcode() == UO_AddrOf) |
| 535 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 536 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 537 | ArgTy = DRE->getDecl()->getType(); |
| 538 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 539 | // 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] | 540 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 541 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 542 | // Now check if we index into a record type function param. |
| 543 | if(!RT && ParamIdxOk) { |
| 544 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 545 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 546 | if(FD && IL) { |
| 547 | unsigned int NumParams = FD->getNumParams(); |
| 548 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 549 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 550 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 551 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 552 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 553 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 554 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 555 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 556 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 560 | // If the type does not have a capability, see if the components of the |
| 561 | // expression have capabilities. This allows for writing C code where the |
| 562 | // capability may be on the type, and the expression is a capability |
| 563 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 564 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 565 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 566 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 567 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 568 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 569 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 572 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 573 | // Attribute Implementations |
| 574 | //===----------------------------------------------------------------------===// |
| 575 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 576 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 577 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 578 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 579 | return; |
| 580 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 581 | D->addAttr(::new (S.Context) |
| 582 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 583 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 586 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 587 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 588 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 589 | SmallVector<Expr*, 1> Args; |
| 590 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 591 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 592 | unsigned Size = Args.size(); |
| 593 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 594 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 595 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 596 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 597 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 598 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 601 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 602 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 603 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 604 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 605 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 606 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 607 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 610 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 611 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 612 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 613 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 614 | return; |
| 615 | |
| 616 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 617 | return; |
| 618 | |
| 619 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 620 | S.Context, Arg, |
| 621 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 624 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 625 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 626 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 627 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 628 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 629 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 630 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 631 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 632 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
| 633 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
| 634 | << Attr.getName(); |
| 635 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 636 | } |
| 637 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 638 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 639 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 640 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 641 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 642 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 643 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 646 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 647 | const AttributeList &Attr) { |
| 648 | SmallVector<Expr*, 1> Args; |
| 649 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 650 | return; |
| 651 | |
| 652 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 653 | D->addAttr(::new (S.Context) |
| 654 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 655 | StartArg, Args.size(), |
| 656 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 659 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 660 | const AttributeList &Attr) { |
| 661 | SmallVector<Expr*, 1> Args; |
| 662 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 663 | return; |
| 664 | |
| 665 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 666 | D->addAttr(::new (S.Context) |
| 667 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 668 | StartArg, Args.size(), |
| 669 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 672 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 673 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 674 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 675 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 676 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 677 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 678 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 679 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 680 | } |
| 681 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 682 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 683 | const AttributeList &Attr) { |
| 684 | SmallVector<Expr*, 1> Args; |
| 685 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 686 | return; |
| 687 | |
| 688 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 689 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 690 | D->addAttr(::new (S.Context) |
| 691 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 692 | Attr.getAttributeSpellingListIndex())); |
| 693 | } |
| 694 | |
| 695 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 696 | const AttributeList &Attr) { |
| 697 | SmallVector<Expr*, 1> Args; |
| 698 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 699 | return; |
| 700 | |
| 701 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 702 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 703 | D->addAttr(::new (S.Context) |
| 704 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 705 | StartArg, Size, |
| 706 | Attr.getAttributeSpellingListIndex())); |
| 707 | } |
| 708 | |
| 709 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 710 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 711 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 712 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 713 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 714 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 715 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 716 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 717 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 718 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 719 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 723 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 724 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 725 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 728 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 729 | const AttributeList &Attr) { |
| 730 | SmallVector<Expr*, 2> Args; |
| 731 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 732 | return; |
| 733 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 734 | D->addAttr(::new (S.Context) |
| 735 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 736 | Attr.getArgAsExpr(0), |
| 737 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 738 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 741 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 742 | const AttributeList &Attr) { |
| 743 | SmallVector<Expr*, 2> Args; |
| 744 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 745 | return; |
| 746 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 747 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 748 | Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(), |
| 749 | Args.size(), Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 752 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 753 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 754 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 755 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 756 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 757 | unsigned Size = Args.size(); |
| 758 | if (Size == 0) |
| 759 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 760 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 761 | D->addAttr(::new (S.Context) |
| 762 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 763 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 767 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 768 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 769 | return; |
| 770 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 771 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 772 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 773 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 774 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 775 | if (Size == 0) |
| 776 | return; |
| 777 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 778 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 779 | D->addAttr(::new (S.Context) |
| 780 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 781 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 784 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 785 | Expr *Cond = Attr.getArgAsExpr(0); |
| 786 | if (!Cond->isTypeDependent()) { |
| 787 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 788 | if (Converted.isInvalid()) |
| 789 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 790 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | StringRef Msg; |
| 794 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 795 | return; |
| 796 | |
| 797 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 798 | if (!Cond->isValueDependent() && |
| 799 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 800 | Diags)) { |
| 801 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 802 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 803 | S.Diag(Diags[I].first, Diags[I].second); |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | D->addAttr(::new (S.Context) |
| 808 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 809 | Attr.getAttributeSpellingListIndex())); |
| 810 | } |
| 811 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 812 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, |
| 813 | const AttributeList &Attr) { |
| 814 | if (D->hasAttr<PassObjectSizeAttr>()) { |
| 815 | S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter) |
| 816 | << Attr.getName(); |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | Expr *E = Attr.getArgAsExpr(0); |
| 821 | uint32_t Type; |
| 822 | if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1)) |
| 823 | return; |
| 824 | |
| 825 | // pass_object_size's argument is passed in as the second argument of |
| 826 | // __builtin_object_size. So, it has the same constraints as that second |
| 827 | // argument; namely, it must be in the range [0, 3]. |
| 828 | if (Type > 3) { |
| 829 | S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range) |
| 830 | << Attr.getName() << 0 << 3 << E->getSourceRange(); |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | // pass_object_size is only supported on constant pointer parameters; as a |
| 835 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 836 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 837 | // definition, so we defer the constness check until later. |
| 838 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
| 839 | S.Diag(D->getLocStart(), diag::err_attribute_pointers_only) |
| 840 | << Attr.getName() << 1; |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | D->addAttr(::new (S.Context) |
| 845 | PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type, |
| 846 | Attr.getAttributeSpellingListIndex())); |
| 847 | } |
| 848 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 849 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 850 | ConsumableAttr::ConsumedState DefaultState; |
| 851 | |
| 852 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 853 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 854 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 855 | DefaultState)) { |
| 856 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 857 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 858 | return; |
| 859 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 860 | } else { |
| 861 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 862 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 863 | return; |
| 864 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 865 | |
| 866 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 867 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 868 | Attr.getAttributeSpellingListIndex())); |
| 869 | } |
| 870 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 871 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 872 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 873 | const AttributeList &Attr) { |
| 874 | ASTContext &CurrContext = S.getASTContext(); |
| 875 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 876 | |
| 877 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 878 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 879 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 880 | RD->getNameAsString(); |
| 881 | |
| 882 | return false; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | return true; |
| 887 | } |
| 888 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 889 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 890 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 891 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 892 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 893 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 894 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 895 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 896 | return; |
| 897 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 898 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 899 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 900 | CallableWhenAttr::ConsumedState CallableState; |
| 901 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 902 | StringRef StateString; |
| 903 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 904 | if (Attr.isArgIdent(ArgIndex)) { |
| 905 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 906 | StateString = Ident->Ident->getName(); |
| 907 | Loc = Ident->Loc; |
| 908 | } else { |
| 909 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 910 | return; |
| 911 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 912 | |
| 913 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 914 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 915 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 916 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 917 | return; |
| 918 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 919 | |
| 920 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 921 | } |
| 922 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 923 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 924 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 925 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 926 | } |
| 927 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 928 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 929 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 930 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 931 | ParamTypestateAttr::ConsumedState ParamState; |
| 932 | |
| 933 | if (Attr.isArgIdent(0)) { |
| 934 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 935 | StringRef StateString = Ident->Ident->getName(); |
| 936 | |
| 937 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 938 | ParamState)) { |
| 939 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 940 | << Attr.getName() << StateString; |
| 941 | return; |
| 942 | } |
| 943 | } else { |
| 944 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 945 | Attr.getName() << AANT_ArgumentIdentifier; |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | // FIXME: This check is currently being done in the analysis. It can be |
| 950 | // enabled here only after the parser propagates attributes at |
| 951 | // template specialization definition, not declaration. |
| 952 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 953 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 954 | // |
| 955 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 956 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 957 | // ReturnType.getAsString(); |
| 958 | // return; |
| 959 | //} |
| 960 | |
| 961 | D->addAttr(::new (S.Context) |
| 962 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 963 | Attr.getAttributeSpellingListIndex())); |
| 964 | } |
| 965 | |
| 966 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 967 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 968 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 969 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 970 | |
| 971 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 972 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 973 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 974 | ReturnState)) { |
| 975 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 976 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 977 | return; |
| 978 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 979 | } else { |
| 980 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 981 | Attr.getName() << AANT_ArgumentIdentifier; |
| 982 | return; |
| 983 | } |
| 984 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 985 | // FIXME: This check is currently being done in the analysis. It can be |
| 986 | // enabled here only after the parser propagates attributes at |
| 987 | // template specialization definition, not declaration. |
| 988 | //QualType ReturnType; |
| 989 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 990 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 991 | // ReturnType = Param->getType(); |
| 992 | // |
| 993 | //} else if (const CXXConstructorDecl *Constructor = |
| 994 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 995 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 996 | // |
| 997 | //} else { |
| 998 | // |
| 999 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1000 | //} |
| 1001 | // |
| 1002 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1003 | // |
| 1004 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1005 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1006 | // ReturnType.getAsString(); |
| 1007 | // return; |
| 1008 | //} |
| 1009 | |
| 1010 | D->addAttr(::new (S.Context) |
| 1011 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1012 | Attr.getAttributeSpellingListIndex())); |
| 1013 | } |
| 1014 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1015 | |
| 1016 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1017 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1018 | return; |
| 1019 | |
| 1020 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1021 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1022 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1023 | StringRef Param = Ident->Ident->getName(); |
| 1024 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1025 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1026 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1027 | return; |
| 1028 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1031 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | D->addAttr(::new (S.Context) |
| 1036 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1037 | Attr.getAttributeSpellingListIndex())); |
| 1038 | } |
| 1039 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1040 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1041 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1042 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1043 | return; |
| 1044 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1045 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1046 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1047 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1048 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1049 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1050 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1051 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1052 | return; |
| 1053 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1054 | } else { |
| 1055 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1056 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1061 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1062 | Attr.getAttributeSpellingListIndex())); |
| 1063 | } |
| 1064 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1065 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1066 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1067 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1068 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1071 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1072 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1073 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1074 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1075 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame^] | 1076 | // Report warning about changed offset in the newer compiler versions. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1077 | if (!FD->getType()->isDependentType() && |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame^] | 1078 | !FD->getType()->isIncompleteType() && FD->isBitField() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1079 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame^] | 1080 | S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield); |
| 1081 | |
| 1082 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1083 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1084 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1085 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1088 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1089 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1090 | // variables or properties of Objective-C classes. The outlet must also |
| 1091 | // have an object reference type. |
| 1092 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1093 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1094 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1095 | << Attr.getName() << VD->getType() << 0; |
| 1096 | return false; |
| 1097 | } |
| 1098 | } |
| 1099 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1100 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1101 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1102 | << Attr.getName() << PD->getType() << 1; |
| 1103 | return false; |
| 1104 | } |
| 1105 | } |
| 1106 | else { |
| 1107 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1108 | return false; |
| 1109 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1110 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1111 | return true; |
| 1112 | } |
| 1113 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1114 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1115 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1116 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1117 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1118 | D->addAttr(::new (S.Context) |
| 1119 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1120 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1123 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1124 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1125 | |
| 1126 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1127 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1128 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1129 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | } |
| 1132 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1133 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1134 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1135 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1136 | ParsedType PT; |
| 1137 | |
| 1138 | if (Attr.hasParsedType()) |
| 1139 | PT = Attr.getTypeArg(); |
| 1140 | else { |
| 1141 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1142 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1143 | if (!PT) { |
| 1144 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1145 | return; |
| 1146 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1147 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1148 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1149 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1150 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1151 | if (!QTLoc) |
| 1152 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1153 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1154 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1155 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1156 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1157 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1158 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1159 | S.Diag(Attr.getLoc(), |
| 1160 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1161 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1164 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1165 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1166 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1167 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1170 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1171 | if (RefOkay) { |
| 1172 | if (T->isReferenceType()) |
| 1173 | return true; |
| 1174 | } else { |
| 1175 | T = T.getNonReferenceType(); |
| 1176 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1177 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1178 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1179 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1180 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1181 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1182 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1183 | for (const auto *I : UD->fields()) { |
| 1184 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1185 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1186 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1187 | } |
| 1188 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1194 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1195 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1196 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1197 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1198 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1199 | if (isReturnValue) |
| 1200 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1201 | << Attr.getName() << AttrParmRange << TypeRange; |
| 1202 | else |
| 1203 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1204 | << Attr.getName() << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1205 | return false; |
| 1206 | } |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1210 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1211 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1212 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1213 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1214 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1215 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1216 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1219 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1220 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1221 | Ex->getSourceRange(), |
| 1222 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1223 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1224 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1225 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1226 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1227 | |
| 1228 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1229 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1230 | // check if the attribute came from a macro expansion or a template |
| 1231 | // instantiation. |
| 1232 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
| 1233 | S.ActiveTemplateInstantiations.empty()) { |
| 1234 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1235 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1236 | I != E && !AnyPointers; ++I) { |
| 1237 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1238 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1239 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1240 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1241 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1242 | if (!AnyPointers) |
| 1243 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1244 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1245 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1246 | unsigned *Start = NonNullArgs.data(); |
| 1247 | unsigned Size = NonNullArgs.size(); |
| 1248 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1249 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1250 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1251 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1254 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1255 | const AttributeList &Attr) { |
| 1256 | if (Attr.getNumArgs() > 0) { |
| 1257 | if (D->getFunctionType()) { |
| 1258 | handleNonNullAttr(S, D, Attr); |
| 1259 | } else { |
| 1260 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1261 | << D->getSourceRange(); |
| 1262 | } |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1267 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1268 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1269 | return; |
| 1270 | |
| 1271 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1272 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1273 | Attr.getAttributeSpellingListIndex())); |
| 1274 | } |
| 1275 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1276 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1277 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1278 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1279 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1280 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1281 | /* isReturnValue */ true)) |
| 1282 | return; |
| 1283 | |
| 1284 | D->addAttr(::new (S.Context) |
| 1285 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1286 | Attr.getAttributeSpellingListIndex())); |
| 1287 | } |
| 1288 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1289 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1290 | const AttributeList &Attr) { |
| 1291 | Expr *E = Attr.getArgAsExpr(0), |
| 1292 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1293 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1294 | Attr.getAttributeSpellingListIndex()); |
| 1295 | } |
| 1296 | |
| 1297 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1298 | Expr *OE, unsigned SpellingListIndex) { |
| 1299 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1300 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1301 | |
| 1302 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1303 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1304 | |
| 1305 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1306 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1307 | << &TmpAttr << AttrRange << SR; |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | if (!E->isValueDependent()) { |
| 1312 | llvm::APSInt I(64); |
| 1313 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1314 | if (OE) |
| 1315 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1316 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1317 | << E->getSourceRange(); |
| 1318 | else |
| 1319 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1320 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1321 | << E->getSourceRange(); |
| 1322 | return; |
| 1323 | } |
| 1324 | |
| 1325 | if (!I.isPowerOf2()) { |
| 1326 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1327 | << E->getSourceRange(); |
| 1328 | return; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (OE) { |
| 1333 | if (!OE->isValueDependent()) { |
| 1334 | llvm::APSInt I(64); |
| 1335 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1336 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1337 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1338 | << OE->getSourceRange(); |
| 1339 | return; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | D->addAttr(::new (Context) |
| 1345 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1346 | } |
| 1347 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1348 | /// Normalize the attribute, __foo__ becomes foo. |
| 1349 | /// Returns true if normalization was applied. |
| 1350 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1351 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1352 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1353 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1354 | return true; |
| 1355 | } |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1359 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1360 | // This attribute must be applied to a function declaration. The first |
| 1361 | // argument to the attribute must be an identifier, the name of the resource, |
| 1362 | // for example: malloc. The following arguments must be argument indexes, the |
| 1363 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1364 | // 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] | 1365 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1366 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1367 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1368 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1369 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1370 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1371 | return; |
| 1372 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1373 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1374 | // Figure out our Kind. |
| 1375 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1376 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1377 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1378 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1379 | // Check arguments. |
| 1380 | switch (K) { |
| 1381 | case OwnershipAttr::Takes: |
| 1382 | case OwnershipAttr::Holds: |
| 1383 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1384 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1385 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1386 | return; |
| 1387 | } |
| 1388 | break; |
| 1389 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1390 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1391 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1392 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1393 | return; |
| 1394 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1395 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1398 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1399 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1400 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1401 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1402 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1403 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1404 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1405 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1406 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1407 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1408 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1409 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1410 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1411 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1412 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1413 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1414 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1415 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1416 | case OwnershipAttr::Takes: |
| 1417 | case OwnershipAttr::Holds: |
| 1418 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1419 | Err = 0; |
| 1420 | break; |
| 1421 | case OwnershipAttr::Returns: |
| 1422 | if (!T->isIntegerType()) |
| 1423 | Err = 1; |
| 1424 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1425 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1426 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1427 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1428 | << Ex->getSourceRange(); |
| 1429 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1430 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1433 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1434 | // Cannot have two ownership attributes of different kinds for the same |
| 1435 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1436 | if (I->getOwnKind() != K && I->args_end() != |
| 1437 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1438 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1439 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1440 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1441 | } else if (K == OwnershipAttr::Returns && |
| 1442 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1443 | // A returns attribute conflicts with any other returns attribute using |
| 1444 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1445 | // argument indexes are 0-based. |
| 1446 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1447 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1448 | << *(I->args_begin()) + 1; |
| 1449 | if (I->args_size()) |
| 1450 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1451 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1452 | return; |
| 1453 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1454 | } |
| 1455 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1456 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | unsigned* start = OwnershipArgs.data(); |
| 1460 | unsigned size = OwnershipArgs.size(); |
| 1461 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1462 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1463 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1464 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1465 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1468 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1469 | // Check the attribute arguments. |
| 1470 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1471 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1472 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1473 | return; |
| 1474 | } |
| 1475 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1476 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1477 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1478 | // gcc rejects |
| 1479 | // class c { |
| 1480 | // static int a __attribute__((weakref ("v2"))); |
| 1481 | // static int b() __attribute__((weakref ("f3"))); |
| 1482 | // }; |
| 1483 | // and ignores the attributes of |
| 1484 | // void f(void) { |
| 1485 | // static int a __attribute__((weakref ("v2"))); |
| 1486 | // } |
| 1487 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1488 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1489 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1490 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1491 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1492 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | // The GCC manual says |
| 1496 | // |
| 1497 | // At present, a declaration to which `weakref' is attached can only |
| 1498 | // be `static'. |
| 1499 | // |
| 1500 | // It also says |
| 1501 | // |
| 1502 | // Without a TARGET, |
| 1503 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1504 | // equivalent to `weak'. |
| 1505 | // |
| 1506 | // gcc 4.4.1 will accept |
| 1507 | // int a7 __attribute__((weakref)); |
| 1508 | // as |
| 1509 | // int a7 __attribute__((weak)); |
| 1510 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1511 | // it if this behaviour is actually used. |
| 1512 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1513 | // GCC rejects |
| 1514 | // static ((alias ("y"), weakref)). |
| 1515 | // Should we? How to check that weakref is before or after alias? |
| 1516 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1517 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1518 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1519 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1520 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1521 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1522 | // GCC will accept anything as the argument of weakref. Should we |
| 1523 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1524 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1525 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1526 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1527 | D->addAttr(::new (S.Context) |
| 1528 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1529 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1532 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1533 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1534 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1535 | return; |
| 1536 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1537 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1538 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1539 | return; |
| 1540 | } |
| 1541 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1542 | // Aliases should be on declarations, not definitions. |
| 1543 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1544 | if (FD->isThisDeclarationADefinition()) { |
| 1545 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD; |
| 1546 | return; |
| 1547 | } |
| 1548 | } else { |
| 1549 | const auto *VD = cast<VarDecl>(D); |
| 1550 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
| 1551 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD; |
| 1552 | return; |
| 1553 | } |
| 1554 | } |
| 1555 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1556 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1557 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1558 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1559 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1562 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1563 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1564 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1565 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1566 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1567 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1571 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1572 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1573 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1574 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1575 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1578 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1579 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1580 | StringRef Model; |
| 1581 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1582 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1583 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1584 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1585 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1586 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1587 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1588 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1589 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1590 | return; |
| 1591 | } |
| 1592 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1593 | D->addAttr(::new (S.Context) |
| 1594 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1595 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1598 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1599 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1600 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1601 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1602 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1603 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1606 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1607 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1610 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1611 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1612 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1613 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1614 | return; |
| 1615 | } |
| 1616 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1617 | if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(), |
| 1618 | Attr.getAttributeSpellingListIndex())) |
| 1619 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 1622 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1623 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(), |
| 1624 | Attr.getName())) |
| 1625 | return; |
| 1626 | |
| 1627 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context, |
| 1628 | Attr.getAttributeSpellingListIndex())); |
| 1629 | } |
| 1630 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1631 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1632 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1633 | |
| 1634 | if (S.CheckNoReturnAttr(attr)) return; |
| 1635 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1636 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1637 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1638 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1639 | return; |
| 1640 | } |
| 1641 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1642 | D->addAttr(::new (S.Context) |
| 1643 | NoReturnAttr(attr.getRange(), S.Context, |
| 1644 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1648 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1649 | attr.setInvalid(); |
| 1650 | return true; |
| 1651 | } |
| 1652 | |
| 1653 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1656 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1657 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1658 | |
| 1659 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1660 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 1661 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1662 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1663 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1664 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1665 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1666 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 1667 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1668 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1669 | return; |
| 1670 | } |
| 1671 | } |
| 1672 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1673 | D->addAttr(::new (S.Context) |
| 1674 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1675 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1678 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1679 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1680 | /* |
| 1681 | Returning a Vector Class in Registers |
| 1682 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1683 | According to the PPU ABI specifications, a class with a single member of |
| 1684 | vector type is returned in memory when used as the return value of a function. |
| 1685 | This results in inefficient code when implementing vector classes. To return |
| 1686 | the value in a single vector register, add the vecreturn attribute to the |
| 1687 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1688 | |
| 1689 | Example: |
| 1690 | |
| 1691 | struct Vector |
| 1692 | { |
| 1693 | __vector float xyzw; |
| 1694 | } __attribute__((vecreturn)); |
| 1695 | |
| 1696 | Vector Add(Vector lhs, Vector rhs) |
| 1697 | { |
| 1698 | Vector result; |
| 1699 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1700 | return result; // This will be returned in a register |
| 1701 | } |
| 1702 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1703 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1704 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1705 | return; |
| 1706 | } |
| 1707 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1708 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1709 | int count = 0; |
| 1710 | |
| 1711 | if (!isa<CXXRecordDecl>(record)) { |
| 1712 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1713 | return; |
| 1714 | } |
| 1715 | |
| 1716 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1717 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1718 | return; |
| 1719 | } |
| 1720 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1721 | for (const auto *I : record->fields()) { |
| 1722 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1723 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1724 | return; |
| 1725 | } |
| 1726 | count++; |
| 1727 | } |
| 1728 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1729 | D->addAttr(::new (S.Context) |
| 1730 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1731 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1734 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1735 | const AttributeList &Attr) { |
| 1736 | if (isa<ParmVarDecl>(D)) { |
| 1737 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1738 | // parameter of a function declaration or lambda. |
| 1739 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1740 | S.Diag(Attr.getLoc(), |
| 1741 | diag::err_carries_dependency_param_not_function_decl); |
| 1742 | return; |
| 1743 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1744 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1745 | |
| 1746 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1747 | Attr.getRange(), S.Context, |
| 1748 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1751 | static void handleNotTailCalledAttr(Sema &S, Decl *D, |
| 1752 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1753 | if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(), |
| 1754 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | |
| 1757 | D->addAttr(::new (S.Context) NotTailCalledAttr( |
| 1758 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1759 | } |
| 1760 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 1761 | static void handleDisableTailCallsAttr(Sema &S, Decl *D, |
| 1762 | const AttributeList &Attr) { |
| 1763 | if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(), |
| 1764 | Attr.getName())) |
| 1765 | return; |
| 1766 | |
| 1767 | D->addAttr(::new (S.Context) DisableTailCallsAttr( |
| 1768 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1769 | } |
| 1770 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1771 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1772 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1773 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1774 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1775 | return; |
| 1776 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1777 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1778 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1779 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1782 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1783 | D->addAttr(::new (S.Context) |
| 1784 | UsedAttr(Attr.getRange(), S.Context, |
| 1785 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1788 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1789 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1790 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1791 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1792 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1793 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1794 | D->addAttr(::new (S.Context) |
| 1795 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1796 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1799 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1800 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1801 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1802 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1803 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1804 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1805 | D->addAttr(::new (S.Context) |
| 1806 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1807 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1810 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1811 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1812 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1813 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1814 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1815 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1816 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1817 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1818 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1819 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1822 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1823 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1824 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 1825 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 1826 | << Attr.getName() << Attr.getRange(); |
| 1827 | return; |
| 1828 | } |
| 1829 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1830 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1831 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1832 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1835 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1836 | IdentifierInfo *Platform, |
| 1837 | VersionTuple Introduced, |
| 1838 | VersionTuple Deprecated, |
| 1839 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1840 | StringRef PlatformName |
| 1841 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1842 | if (PlatformName.empty()) |
| 1843 | PlatformName = Platform->getName(); |
| 1844 | |
| 1845 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1846 | // of these steps are needed). |
| 1847 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1848 | !(Introduced <= Deprecated)) { |
| 1849 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1850 | << 1 << PlatformName << Deprecated.getAsString() |
| 1851 | << 0 << Introduced.getAsString(); |
| 1852 | return true; |
| 1853 | } |
| 1854 | |
| 1855 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1856 | !(Introduced <= Obsoleted)) { |
| 1857 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1858 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1859 | << 0 << Introduced.getAsString(); |
| 1860 | return true; |
| 1861 | } |
| 1862 | |
| 1863 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1864 | !(Deprecated <= Obsoleted)) { |
| 1865 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1866 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1867 | << 1 << Deprecated.getAsString(); |
| 1868 | return true; |
| 1869 | } |
| 1870 | |
| 1871 | return false; |
| 1872 | } |
| 1873 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1874 | /// \brief Check whether the two versions match. |
| 1875 | /// |
| 1876 | /// If either version tuple is empty, then they are assumed to match. If |
| 1877 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1878 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1879 | bool BeforeIsOkay) { |
| 1880 | if (X.empty() || Y.empty()) |
| 1881 | return true; |
| 1882 | |
| 1883 | if (X == Y) |
| 1884 | return true; |
| 1885 | |
| 1886 | if (BeforeIsOkay && X < Y) |
| 1887 | return true; |
| 1888 | |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1892 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1893 | IdentifierInfo *Platform, |
| 1894 | VersionTuple Introduced, |
| 1895 | VersionTuple Deprecated, |
| 1896 | VersionTuple Obsoleted, |
| 1897 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1898 | StringRef Message, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1899 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1900 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1901 | VersionTuple MergedIntroduced = Introduced; |
| 1902 | VersionTuple MergedDeprecated = Deprecated; |
| 1903 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1904 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1905 | bool OverrideOrImpl = false; |
| 1906 | switch (AMK) { |
| 1907 | case AMK_None: |
| 1908 | case AMK_Redeclaration: |
| 1909 | OverrideOrImpl = false; |
| 1910 | break; |
| 1911 | |
| 1912 | case AMK_Override: |
| 1913 | case AMK_ProtocolImplementation: |
| 1914 | OverrideOrImpl = true; |
| 1915 | break; |
| 1916 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1917 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1918 | if (D->hasAttrs()) { |
| 1919 | AttrVec &Attrs = D->getAttrs(); |
| 1920 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1921 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1922 | if (!OldAA) { |
| 1923 | ++i; |
| 1924 | continue; |
| 1925 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1926 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1927 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1928 | if (OldPlatform != Platform) { |
| 1929 | ++i; |
| 1930 | continue; |
| 1931 | } |
| 1932 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 1933 | // If there is an existing availability attribute for this platform that |
| 1934 | // is explicit and the new one is implicit use the explicit one and |
| 1935 | // discard the new implicit attribute. |
| 1936 | if (OldAA->getRange().isValid() && Range.isInvalid()) { |
| 1937 | return nullptr; |
| 1938 | } |
| 1939 | |
| 1940 | // If there is an existing attribute for this platform that is implicit |
| 1941 | // and the new attribute is explicit then erase the old one and |
| 1942 | // continue processing the attributes. |
| 1943 | if (Range.isValid() && OldAA->getRange().isInvalid()) { |
| 1944 | Attrs.erase(Attrs.begin() + i); |
| 1945 | --e; |
| 1946 | continue; |
| 1947 | } |
| 1948 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1949 | FoundAny = true; |
| 1950 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1951 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1952 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1953 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1954 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1955 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 1956 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 1957 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1958 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1959 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 1960 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1961 | int Which = -1; |
| 1962 | VersionTuple FirstVersion; |
| 1963 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1964 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1965 | Which = 0; |
| 1966 | FirstVersion = OldIntroduced; |
| 1967 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1968 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1969 | Which = 1; |
| 1970 | FirstVersion = Deprecated; |
| 1971 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1972 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1973 | Which = 2; |
| 1974 | FirstVersion = Obsoleted; |
| 1975 | SecondVersion = OldObsoleted; |
| 1976 | } |
| 1977 | |
| 1978 | if (Which == -1) { |
| 1979 | Diag(OldAA->getLocation(), |
| 1980 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1981 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1982 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1983 | } else { |
| 1984 | Diag(OldAA->getLocation(), |
| 1985 | diag::warn_mismatched_availability_override) |
| 1986 | << Which |
| 1987 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1988 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 1989 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1990 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1991 | if (AMK == AMK_Override) |
| 1992 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1993 | else |
| 1994 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1995 | } else { |
| 1996 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1997 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1998 | } |
| 1999 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2000 | Attrs.erase(Attrs.begin() + i); |
| 2001 | --e; |
| 2002 | continue; |
| 2003 | } |
| 2004 | |
| 2005 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2006 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2007 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2008 | |
| 2009 | if (MergedIntroduced2.empty()) |
| 2010 | MergedIntroduced2 = OldIntroduced; |
| 2011 | if (MergedDeprecated2.empty()) |
| 2012 | MergedDeprecated2 = OldDeprecated; |
| 2013 | if (MergedObsoleted2.empty()) |
| 2014 | MergedObsoleted2 = OldObsoleted; |
| 2015 | |
| 2016 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2017 | MergedIntroduced2, MergedDeprecated2, |
| 2018 | MergedObsoleted2)) { |
| 2019 | Attrs.erase(Attrs.begin() + i); |
| 2020 | --e; |
| 2021 | continue; |
| 2022 | } |
| 2023 | |
| 2024 | MergedIntroduced = MergedIntroduced2; |
| 2025 | MergedDeprecated = MergedDeprecated2; |
| 2026 | MergedObsoleted = MergedObsoleted2; |
| 2027 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2028 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | if (FoundAny && |
| 2032 | MergedIntroduced == Introduced && |
| 2033 | MergedDeprecated == Deprecated && |
| 2034 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2035 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2036 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2037 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2038 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2039 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2040 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2041 | !OverrideOrImpl) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2042 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2043 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2044 | Obsoleted, IsUnavailable, Message, |
| 2045 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2046 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2047 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2050 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2051 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2052 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2053 | return; |
| 2054 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2055 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2056 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2057 | IdentifierInfo *II = Platform->Ident; |
| 2058 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2059 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2060 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2061 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2062 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2063 | if (!ND) { |
| 2064 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2065 | return; |
| 2066 | } |
| 2067 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2068 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2069 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2070 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2071 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2072 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2073 | if (const StringLiteral *SE = |
| 2074 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2075 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2076 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2077 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2078 | Introduced.Version, |
| 2079 | Deprecated.Version, |
| 2080 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2081 | IsUnavailable, Str, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2082 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2083 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2084 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2085 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2086 | |
| 2087 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2088 | // matches before the start of the watchOS platform. |
| 2089 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2090 | IdentifierInfo *NewII = nullptr; |
| 2091 | if (II->getName() == "ios") |
| 2092 | NewII = &S.Context.Idents.get("watchos"); |
| 2093 | else if (II->getName() == "ios_app_extension") |
| 2094 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2095 | |
| 2096 | if (NewII) { |
| 2097 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2098 | if (Version.empty()) |
| 2099 | return Version; |
| 2100 | auto Major = Version.getMajor(); |
| 2101 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2102 | if (NewMajor >= 2) { |
| 2103 | if (Version.getMinor().hasValue()) { |
| 2104 | if (Version.getSubminor().hasValue()) |
| 2105 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2106 | Version.getSubminor().getValue()); |
| 2107 | else |
| 2108 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | return VersionTuple(2, 0); |
| 2113 | }; |
| 2114 | |
| 2115 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2116 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2117 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2118 | |
| 2119 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
| 2120 | SourceRange(), |
| 2121 | NewII, |
| 2122 | NewIntroduced, |
| 2123 | NewDeprecated, |
| 2124 | NewObsoleted, |
| 2125 | IsUnavailable, Str, |
| 2126 | Sema::AMK_None, |
| 2127 | Index); |
| 2128 | if (NewAttr) |
| 2129 | D->addAttr(NewAttr); |
| 2130 | } |
| 2131 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2132 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2133 | // matches before the start of the tvOS platform. |
| 2134 | IdentifierInfo *NewII = nullptr; |
| 2135 | if (II->getName() == "ios") |
| 2136 | NewII = &S.Context.Idents.get("tvos"); |
| 2137 | else if (II->getName() == "ios_app_extension") |
| 2138 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2139 | |
| 2140 | if (NewII) { |
| 2141 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
| 2142 | SourceRange(), |
| 2143 | NewII, |
| 2144 | Introduced.Version, |
| 2145 | Deprecated.Version, |
| 2146 | Obsoleted.Version, |
| 2147 | IsUnavailable, Str, |
| 2148 | Sema::AMK_None, |
| 2149 | Index); |
| 2150 | if (NewAttr) |
| 2151 | D->addAttr(NewAttr); |
| 2152 | } |
| 2153 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2156 | template <class T> |
| 2157 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2158 | typename T::VisibilityType value, |
| 2159 | unsigned attrSpellingListIndex) { |
| 2160 | T *existingAttr = D->getAttr<T>(); |
| 2161 | if (existingAttr) { |
| 2162 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2163 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2164 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2165 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2166 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2167 | D->dropAttr<T>(); |
| 2168 | } |
| 2169 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2170 | } |
| 2171 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2172 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2173 | VisibilityAttr::VisibilityType Vis, |
| 2174 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2175 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2176 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2179 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2180 | TypeVisibilityAttr::VisibilityType Vis, |
| 2181 | unsigned AttrSpellingListIndex) { |
| 2182 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2183 | AttrSpellingListIndex); |
| 2184 | } |
| 2185 | |
| 2186 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2187 | bool isTypeVisibility) { |
| 2188 | // Visibility attributes don't mean anything on a typedef. |
| 2189 | if (isa<TypedefNameDecl>(D)) { |
| 2190 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2191 | << Attr.getName(); |
| 2192 | return; |
| 2193 | } |
| 2194 | |
| 2195 | // 'type_visibility' can only go on a type or namespace. |
| 2196 | if (isTypeVisibility && |
| 2197 | !(isa<TagDecl>(D) || |
| 2198 | isa<ObjCInterfaceDecl>(D) || |
| 2199 | isa<NamespaceDecl>(D))) { |
| 2200 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2201 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2202 | return; |
| 2203 | } |
| 2204 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2205 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2206 | StringRef TypeStr; |
| 2207 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2208 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2209 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2210 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2211 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2212 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2213 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2214 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2215 | return; |
| 2216 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2217 | |
| 2218 | // Complain about attempts to use protected visibility on targets |
| 2219 | // (like Darwin) that don't support it. |
| 2220 | if (type == VisibilityAttr::Protected && |
| 2221 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2222 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2223 | type = VisibilityAttr::Default; |
| 2224 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2225 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2226 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2227 | clang::Attr *newAttr; |
| 2228 | if (isTypeVisibility) { |
| 2229 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2230 | (TypeVisibilityAttr::VisibilityType) type, |
| 2231 | Index); |
| 2232 | } else { |
| 2233 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2234 | } |
| 2235 | if (newAttr) |
| 2236 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2239 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2240 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2241 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2242 | if (!Attr.isArgIdent(0)) { |
| 2243 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2244 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2245 | return; |
| 2246 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2247 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2248 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2249 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2250 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2251 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2252 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2253 | return; |
| 2254 | } |
| 2255 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2256 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2257 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2258 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2259 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2260 | // Ignore the attribute. |
| 2261 | return; |
| 2262 | } |
| 2263 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2264 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2265 | S.Context, F, |
| 2266 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2269 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2270 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2271 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2272 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2273 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2274 | return; |
| 2275 | } |
| 2276 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2277 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2278 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2279 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2280 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2281 | return; |
| 2282 | } |
| 2283 | } |
| 2284 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2285 | // It is okay to include this attribute on properties, e.g.: |
| 2286 | // |
| 2287 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2288 | // |
| 2289 | // In this case it follows tradition and suppresses an error in the above |
| 2290 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2291 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2292 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2293 | D->addAttr(::new (S.Context) |
| 2294 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2295 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2298 | static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2299 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2300 | QualType T = TD->getUnderlyingType(); |
| 2301 | if (!T->isObjCObjectPointerType()) { |
| 2302 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2303 | return; |
| 2304 | } |
| 2305 | } else { |
| 2306 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2307 | return; |
| 2308 | } |
| 2309 | D->addAttr(::new (S.Context) |
| 2310 | ObjCIndependentClassAttr(Attr.getRange(), S.Context, |
| 2311 | Attr.getAttributeSpellingListIndex())); |
| 2312 | } |
| 2313 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2314 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2315 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2316 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2317 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2318 | return; |
| 2319 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2320 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2321 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2322 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2323 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2324 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2325 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2326 | return; |
| 2327 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2328 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2329 | D->addAttr(::new (S.Context) |
| 2330 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2331 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2334 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2335 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2336 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2337 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2338 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2339 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2340 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2341 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2342 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2343 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2344 | return; |
| 2345 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2346 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2347 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2348 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2349 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2352 | |
| 2353 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2356 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2357 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2358 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2359 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2360 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2361 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2362 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2363 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2364 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2365 | return; |
| 2366 | } |
| 2367 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2368 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2369 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2370 | // FIXME: This error message could be improved, it would be nice |
| 2371 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2372 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2373 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2374 | return; |
| 2375 | } |
| 2376 | } |
| 2377 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2378 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2379 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2380 | if (isa<FunctionNoProtoType>(FT)) { |
| 2381 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2382 | return; |
| 2383 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2384 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2385 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2386 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2387 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2388 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2389 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2390 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2391 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2392 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2393 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2394 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2395 | if (!BD->isVariadic()) { |
| 2396 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2397 | return; |
| 2398 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2399 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2400 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2401 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2402 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2403 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2404 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2405 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2406 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2407 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2408 | return; |
| 2409 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2410 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2411 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2412 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2413 | return; |
| 2414 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2415 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2416 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2417 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2418 | return; |
| 2419 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2420 | D->addAttr(::new (S.Context) |
| 2421 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2422 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2425 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2426 | if (D->getFunctionType() && |
| 2427 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2428 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2429 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2430 | return; |
| 2431 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2432 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2433 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2434 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2435 | << Attr.getName() << 1; |
| 2436 | return; |
| 2437 | } |
| 2438 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2439 | D->addAttr(::new (S.Context) |
| 2440 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2441 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2444 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2445 | // weak_import only applies to variable & function declarations. |
| 2446 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2447 | if (!D->canBeWeakImported(isDef)) { |
| 2448 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2449 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2450 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2451 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2452 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2453 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2454 | // Nothing to warn about here. |
| 2455 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2456 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2457 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2458 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2459 | return; |
| 2460 | } |
| 2461 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2462 | D->addAttr(::new (S.Context) |
| 2463 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2464 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2467 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2468 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2469 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2470 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2471 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2472 | for (unsigned i = 0; i < 3; ++i) { |
| 2473 | const Expr *E = Attr.getArgAsExpr(i); |
| 2474 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2475 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2476 | if (WGSize[i] == 0) { |
| 2477 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2478 | << Attr.getName() << E->getSourceRange(); |
| 2479 | return; |
| 2480 | } |
| 2481 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2482 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2483 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2484 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2485 | Existing->getYDim() == WGSize[1] && |
| 2486 | Existing->getZDim() == WGSize[2])) |
| 2487 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2488 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2489 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2490 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2491 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2494 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2495 | if (!Attr.hasParsedType()) { |
| 2496 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2497 | << Attr.getName() << 1; |
| 2498 | return; |
| 2499 | } |
| 2500 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2501 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2502 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2503 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2504 | |
| 2505 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2506 | (ParmType->isBooleanType() || |
| 2507 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2508 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2509 | << ParmType; |
| 2510 | return; |
| 2511 | } |
| 2512 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2513 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2514 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2515 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2516 | return; |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2521 | ParmTSI, |
| 2522 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2525 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2526 | StringRef Name, |
| 2527 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2528 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2529 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2530 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2531 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2532 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2533 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2534 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2535 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2536 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2539 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2540 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2541 | if (!Error.empty()) { |
| 2542 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2543 | return false; |
| 2544 | } |
| 2545 | return true; |
| 2546 | } |
| 2547 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2548 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2549 | // Make sure that there is a string literal as the sections's single |
| 2550 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2551 | StringRef Str; |
| 2552 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2553 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2554 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2555 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2556 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2557 | return; |
| 2558 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2559 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2560 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2561 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2562 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2563 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2564 | return; |
| 2565 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2567 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2568 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2569 | if (NewAttr) |
| 2570 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2573 | // Check for things we'd like to warn about, no errors or validation for now. |
| 2574 | // TODO: Validation should use a backend target library that specifies |
| 2575 | // the allowable subtarget features and cpus. We could use something like a |
| 2576 | // TargetCodeGenInfo hook here to do validation. |
| 2577 | void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 2578 | for (auto Str : {"tune=", "fpmath="}) |
| 2579 | if (AttrStr.find(Str) != StringRef::npos) |
| 2580 | Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str; |
| 2581 | } |
| 2582 | |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2583 | static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2584 | StringRef Str; |
| 2585 | SourceLocation LiteralLoc; |
| 2586 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
| 2587 | return; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2588 | S.checkTargetAttr(LiteralLoc, Str); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2589 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2590 | TargetAttr *NewAttr = |
| 2591 | ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index); |
| 2592 | D->addAttr(NewAttr); |
| 2593 | } |
| 2594 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2595 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2596 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2597 | VarDecl *VD = cast<VarDecl>(D); |
| 2598 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2599 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2600 | return; |
| 2601 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2602 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2603 | Expr *E = Attr.getArgAsExpr(0); |
| 2604 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2605 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2606 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2607 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2608 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2609 | // will warn the user. |
| 2610 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2611 | if (DRE->hasQualifier()) |
| 2612 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2613 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2614 | NI = DRE->getNameInfo(); |
| 2615 | if (!FD) { |
| 2616 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2617 | << NI.getName(); |
| 2618 | return; |
| 2619 | } |
| 2620 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2621 | if (ULE->hasExplicitTemplateArgs()) |
| 2622 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2623 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2624 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2625 | if (!FD) { |
| 2626 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2627 | << NI.getName(); |
| 2628 | if (ULE->getType() == S.Context.OverloadTy) |
| 2629 | S.NoteAllOverloadCandidates(ULE); |
| 2630 | return; |
| 2631 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2632 | } else { |
| 2633 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2634 | return; |
| 2635 | } |
| 2636 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2637 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2638 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2639 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2640 | return; |
| 2641 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2642 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2643 | // We're currently more strict than GCC about what function types we accept. |
| 2644 | // If this ever proves to be a problem it should be easy to fix. |
| 2645 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2646 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2647 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2648 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2649 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2650 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2651 | return; |
| 2652 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2653 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2654 | D->addAttr(::new (S.Context) |
| 2655 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2656 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2659 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2660 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2661 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2662 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2663 | uint64_t Idx; |
| 2664 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2665 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2666 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2667 | // Make sure the format string is really a string. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2668 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2669 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2670 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 2671 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2672 | !isCFStringType(Ty, S.Context) && |
| 2673 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2674 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2675 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2676 | << "a string type" << IdxExpr->getSourceRange() |
| 2677 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2678 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2679 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2680 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2681 | if (!isNSStringType(Ty, S.Context) && |
| 2682 | !isCFStringType(Ty, S.Context) && |
| 2683 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2684 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2685 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2686 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 2687 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2688 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2691 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2692 | // because that has corrected for the implicit this parameter, and is zero- |
| 2693 | // based. The attribute expects what the user wrote explicitly. |
| 2694 | llvm::APSInt Val; |
| 2695 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2696 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2697 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2698 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2699 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2702 | enum FormatAttrKind { |
| 2703 | CFStringFormat, |
| 2704 | NSStringFormat, |
| 2705 | StrftimeFormat, |
| 2706 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2707 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2708 | InvalidFormat |
| 2709 | }; |
| 2710 | |
| 2711 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2712 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2713 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2714 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2715 | // Check for formats that get handled specially. |
| 2716 | .Case("NSString", NSStringFormat) |
| 2717 | .Case("CFString", CFStringFormat) |
| 2718 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2719 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2720 | // Otherwise, check for supported formats. |
| 2721 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2722 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2723 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 2724 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 2725 | .Case("os_trace", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2726 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2727 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2728 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2731 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2732 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2733 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2734 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2735 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2736 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2737 | return; |
| 2738 | } |
| 2739 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2740 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2741 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2742 | Attr.setInvalid(); |
| 2743 | return; |
| 2744 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2745 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2746 | if (S.Context.getAsArrayType(T)) |
| 2747 | T = S.Context.getBaseElementType(T); |
| 2748 | if (!T->getAs<RecordType>()) { |
| 2749 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2750 | Attr.setInvalid(); |
| 2751 | return; |
| 2752 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2753 | |
| 2754 | Expr *E = Attr.getArgAsExpr(0); |
| 2755 | uint32_t prioritynum; |
| 2756 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2757 | Attr.setInvalid(); |
| 2758 | return; |
| 2759 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2760 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2761 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2762 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2763 | << E->getSourceRange() << Attr.getName() << 101 << 65535; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2764 | Attr.setInvalid(); |
| 2765 | return; |
| 2766 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2767 | D->addAttr(::new (S.Context) |
| 2768 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2769 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2772 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2773 | IdentifierInfo *Format, int FormatIdx, |
| 2774 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2775 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2776 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2777 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 2778 | if (F->getType() == Format && |
| 2779 | F->getFormatIdx() == FormatIdx && |
| 2780 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2781 | // If we don't have a valid location for this attribute, adopt the |
| 2782 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2783 | if (F->getLocation().isInvalid()) |
| 2784 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2785 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2789 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2790 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2793 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2794 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2795 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2796 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2797 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2798 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2799 | return; |
| 2800 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2801 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2802 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2803 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2804 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2805 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2806 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2807 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2808 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2809 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 2810 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2811 | // If we've modified the string name, we need a new identifier for it. |
| 2812 | II = &S.Context.Idents.get(Format); |
| 2813 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2814 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2815 | // Check for supported formats. |
| 2816 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2817 | |
| 2818 | if (Kind == IgnoredFormat) |
| 2819 | return; |
| 2820 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2821 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2822 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2823 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2824 | return; |
| 2825 | } |
| 2826 | |
| 2827 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2828 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2829 | uint32_t Idx; |
| 2830 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2831 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2832 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2833 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2834 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2835 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2836 | return; |
| 2837 | } |
| 2838 | |
| 2839 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2840 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2841 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2842 | if (HasImplicitThisParam) { |
| 2843 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2844 | S.Diag(Attr.getLoc(), |
| 2845 | diag::err_format_attribute_implicit_this_format_string) |
| 2846 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2847 | return; |
| 2848 | } |
| 2849 | ArgIdx--; |
| 2850 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2852 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2853 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2854 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2855 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2856 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2857 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2858 | << "a CFString" << IdxExpr->getSourceRange() |
| 2859 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2860 | return; |
| 2861 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2862 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2863 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2864 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2865 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2866 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2867 | << "an NSString" << IdxExpr->getSourceRange() |
| 2868 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2869 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2870 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2871 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2872 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2873 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2874 | << "a string type" << IdxExpr->getSourceRange() |
| 2875 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2876 | return; |
| 2877 | } |
| 2878 | |
| 2879 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2880 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2881 | uint32_t FirstArg; |
| 2882 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2883 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2884 | |
| 2885 | // check if the function is variadic if the 3rd argument non-zero |
| 2886 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2887 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2888 | ++NumArgs; // +1 for ... |
| 2889 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2890 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2891 | return; |
| 2892 | } |
| 2893 | } |
| 2894 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2895 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2896 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2897 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2898 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2899 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2900 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2901 | return; |
| 2902 | } |
| 2903 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2904 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2905 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2906 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2907 | return; |
| 2908 | } |
| 2909 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2910 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2911 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2912 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2913 | if (NewAttr) |
| 2914 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2917 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2918 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2919 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2920 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2921 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2922 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2923 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2924 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2925 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2926 | |
| 2927 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2928 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2929 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2930 | return; |
| 2931 | } |
| 2932 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2933 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2934 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2935 | diag::warn_transparent_union_attribute_not_definition); |
| 2936 | return; |
| 2937 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2938 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2939 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2940 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2941 | if (Field == FieldEnd) { |
| 2942 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2943 | return; |
| 2944 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2945 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2946 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2947 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2948 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2949 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2950 | diag::warn_transparent_union_attribute_floating) |
| 2951 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2952 | return; |
| 2953 | } |
| 2954 | |
| 2955 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2956 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2957 | for (; Field != FieldEnd; ++Field) { |
| 2958 | QualType FieldType = Field->getType(); |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2959 | // FIXME: this isn't fully correct; we also need to test whether the |
| 2960 | // members of the union would all have the same calling convention as the |
| 2961 | // first member of the union. Checking just the size and alignment isn't |
| 2962 | // sufficient (consider structs passed on the stack instead of in registers |
| 2963 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2964 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2965 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2966 | // Warn if we drop the attribute. |
| 2967 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2968 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2969 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2970 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2971 | diag::warn_transparent_union_attribute_field_size_align) |
| 2972 | << isSize << Field->getDeclName() << FieldBits; |
| 2973 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2974 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2975 | diag::note_transparent_union_first_field_size_align) |
| 2976 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2977 | return; |
| 2978 | } |
| 2979 | } |
| 2980 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2981 | RD->addAttr(::new (S.Context) |
| 2982 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2983 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2986 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2987 | // Make sure that there is a string literal as the annotation's single |
| 2988 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2989 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2990 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2991 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2992 | |
| 2993 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2994 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 2995 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2996 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2997 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2998 | |
| 2999 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3000 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3001 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3004 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 3005 | const AttributeList &Attr) { |
| 3006 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3007 | Attr.getAttributeSpellingListIndex()); |
| 3008 | } |
| 3009 | |
| 3010 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3011 | unsigned SpellingListIndex) { |
| 3012 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3013 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3014 | |
| 3015 | QualType T; |
| 3016 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3017 | T = TD->getUnderlyingType(); |
| 3018 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3019 | T = VD->getType(); |
| 3020 | else |
| 3021 | llvm_unreachable("Unknown decl type for align_value"); |
| 3022 | |
| 3023 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3024 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3025 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3026 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3027 | return; |
| 3028 | } |
| 3029 | |
| 3030 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3031 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3032 | ExprResult ICE |
| 3033 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3034 | diag::err_align_value_attribute_argument_not_int, |
| 3035 | /*AllowFold*/ false); |
| 3036 | if (ICE.isInvalid()) |
| 3037 | return; |
| 3038 | |
| 3039 | if (!Alignment.isPowerOf2()) { |
| 3040 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3041 | << E->getSourceRange(); |
| 3042 | return; |
| 3043 | } |
| 3044 | |
| 3045 | D->addAttr(::new (Context) |
| 3046 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3047 | SpellingListIndex)); |
| 3048 | return; |
| 3049 | } |
| 3050 | |
| 3051 | // Save dependent expressions in the AST to be instantiated. |
| 3052 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
| 3053 | return; |
| 3054 | } |
| 3055 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3056 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3057 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3058 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3059 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3060 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3061 | return; |
| 3062 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3063 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3064 | if (Attr.getNumArgs() == 0) { |
| 3065 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3066 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3067 | return; |
| 3068 | } |
| 3069 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3070 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3071 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3072 | S.Diag(Attr.getEllipsisLoc(), |
| 3073 | diag::err_pack_expansion_without_parameter_packs); |
| 3074 | return; |
| 3075 | } |
| 3076 | |
| 3077 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3078 | return; |
| 3079 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 3080 | if (E->isValueDependent()) { |
| 3081 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3082 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3083 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 3084 | << E->getSourceRange(); |
| 3085 | return; |
| 3086 | } |
| 3087 | } |
| 3088 | } |
| 3089 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3090 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3091 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3095 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3096 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3097 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3098 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3099 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3100 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3101 | // C++11 [dcl.align]p1: |
| 3102 | // An alignment-specifier may be applied to a variable or to a class |
| 3103 | // data member, but it shall not be applied to a bit-field, a function |
| 3104 | // parameter, the formal parameter of a catch clause, or a variable |
| 3105 | // declared with the register storage class specifier. An |
| 3106 | // alignment-specifier may also be applied to the declaration of a class |
| 3107 | // or enumeration type. |
| 3108 | // C11 6.7.5/2: |
| 3109 | // An alignment attribute shall not be specified in a declaration of |
| 3110 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3111 | // object declared with the register storage-class specifier. |
| 3112 | int DiagKind = -1; |
| 3113 | if (isa<ParmVarDecl>(D)) { |
| 3114 | DiagKind = 0; |
| 3115 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3116 | if (VD->getStorageClass() == SC_Register) |
| 3117 | DiagKind = 1; |
| 3118 | if (VD->isExceptionVariable()) |
| 3119 | DiagKind = 2; |
| 3120 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3121 | if (FD->isBitField()) |
| 3122 | DiagKind = 3; |
| 3123 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3124 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3125 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3126 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3127 | return; |
| 3128 | } |
| 3129 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3130 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3131 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3132 | return; |
| 3133 | } |
| 3134 | } |
| 3135 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3136 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3137 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3138 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3139 | AA->setPackExpansion(IsPackExpansion); |
| 3140 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3141 | return; |
| 3142 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3143 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3144 | // FIXME: Cache the number on the Attr object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3145 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3146 | ExprResult ICE |
| 3147 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3148 | diag::err_aligned_attribute_argument_not_int, |
| 3149 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3150 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3151 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3152 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3153 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3154 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3155 | // C++11 [dcl.align]p2: |
| 3156 | // -- if the constant expression evaluates to zero, the alignment |
| 3157 | // specifier shall have no effect |
| 3158 | // C11 6.7.5p6: |
| 3159 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3160 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3161 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3162 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3163 | << E->getSourceRange(); |
| 3164 | return; |
| 3165 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3166 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3167 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3168 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3169 | unsigned MaxValidAlignment = |
| 3170 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3171 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3172 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3173 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3174 | << E->getSourceRange(); |
| 3175 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3176 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3177 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3178 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3179 | unsigned MaxTLSAlign = |
| 3180 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3181 | .getQuantity(); |
| 3182 | auto *VD = dyn_cast<VarDecl>(D); |
| 3183 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3184 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3185 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3186 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3187 | return; |
| 3188 | } |
| 3189 | } |
| 3190 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3191 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3192 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3193 | AA->setPackExpansion(IsPackExpansion); |
| 3194 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3197 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3198 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3199 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3200 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3201 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3202 | SpellingListIndex); |
| 3203 | AA->setPackExpansion(IsPackExpansion); |
| 3204 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3205 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3206 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3207 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3208 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3209 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3210 | QualType UnderlyingTy, DiagTy; |
| 3211 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 3212 | UnderlyingTy = DiagTy = VD->getType(); |
| 3213 | } else { |
| 3214 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3215 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3216 | UnderlyingTy = ED->getIntegerType(); |
| 3217 | } |
| 3218 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3219 | return; |
| 3220 | |
| 3221 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3222 | // The combined effect of all alignment attributes in a declaration shall |
| 3223 | // not specify an alignment that is less strict than the alignment that |
| 3224 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3225 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3226 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3227 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3228 | if (I->isAlignmentDependent()) |
| 3229 | return; |
| 3230 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3231 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3232 | Align = std::max(Align, I->getAlignment(Context)); |
| 3233 | } |
| 3234 | |
| 3235 | if (AlignasAttr && Align) { |
| 3236 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3237 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3238 | if (NaturalAlign > RequestedAlign) |
| 3239 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3240 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3244 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3245 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3246 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3247 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3248 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3249 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3250 | // have to wait until the record is defined to catch any mismatches. |
| 3251 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3252 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3253 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3254 | // The unspecified model never matches what a definition could need. |
| 3255 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3256 | return false; |
| 3257 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3258 | if (BestCase) { |
| 3259 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3260 | return false; |
| 3261 | } else { |
| 3262 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3263 | return false; |
| 3264 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3265 | |
| 3266 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3267 | << 0 /*definition*/; |
| 3268 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3269 | << RD->getNameAsString(); |
| 3270 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3273 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3274 | /// attribute. |
| 3275 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3276 | bool &IntegerMode, bool &ComplexMode) { |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3277 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3278 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3279 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3280 | case 'Q': |
| 3281 | DestWidth = 8; |
| 3282 | break; |
| 3283 | case 'H': |
| 3284 | DestWidth = 16; |
| 3285 | break; |
| 3286 | case 'S': |
| 3287 | DestWidth = 32; |
| 3288 | break; |
| 3289 | case 'D': |
| 3290 | DestWidth = 64; |
| 3291 | break; |
| 3292 | case 'X': |
| 3293 | DestWidth = 96; |
| 3294 | break; |
| 3295 | case 'T': |
| 3296 | DestWidth = 128; |
| 3297 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3298 | } |
| 3299 | if (Str[1] == 'F') { |
| 3300 | IntegerMode = false; |
| 3301 | } else if (Str[1] == 'C') { |
| 3302 | IntegerMode = false; |
| 3303 | ComplexMode = true; |
| 3304 | } else if (Str[1] != 'I') { |
| 3305 | DestWidth = 0; |
| 3306 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3307 | break; |
| 3308 | case 4: |
| 3309 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3310 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3311 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3312 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3313 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3314 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3315 | break; |
| 3316 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3317 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3318 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3319 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3320 | case 11: |
| 3321 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3322 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3323 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3324 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3328 | /// type. |
| 3329 | /// |
| 3330 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3331 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3332 | /// HImode, not an intermediate pointer. |
| 3333 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3334 | // This attribute isn't documented, but glibc uses it. It changes |
| 3335 | // the width of an int or unsigned int to the specified size. |
| 3336 | if (!Attr.isArgIdent(0)) { |
| 3337 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3338 | << AANT_ArgumentIdentifier; |
| 3339 | return; |
| 3340 | } |
| 3341 | |
| 3342 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3343 | StringRef Str = Name->getName(); |
| 3344 | |
| 3345 | normalizeName(Str); |
| 3346 | |
| 3347 | unsigned DestWidth = 0; |
| 3348 | bool IntegerMode = true; |
| 3349 | bool ComplexMode = false; |
| 3350 | llvm::APInt VectorSize(64, 0); |
| 3351 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3352 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3353 | size_t StrSize = Str.size(); |
| 3354 | size_t VectorStringLength = 0; |
| 3355 | while ((VectorStringLength + 1) < StrSize && |
| 3356 | isdigit(Str[VectorStringLength + 1])) |
| 3357 | ++VectorStringLength; |
| 3358 | if (VectorStringLength && |
| 3359 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3360 | VectorSize.isPowerOf2()) { |
| 3361 | parseModeAttrArg(S, Str.substr(VectorStringLength + 1), DestWidth, |
| 3362 | IntegerMode, ComplexMode); |
| 3363 | S.Diag(Attr.getLoc(), diag::warn_vector_mode_deprecated); |
| 3364 | } else { |
| 3365 | VectorSize = 0; |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | if (!VectorSize) |
| 3370 | parseModeAttrArg(S, Str, DestWidth, IntegerMode, ComplexMode); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3371 | |
| 3372 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3373 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3374 | OldTy = TD->getUnderlyingType(); |
| 3375 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3376 | OldTy = VD->getType(); |
| 3377 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3378 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 3379 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3380 | return; |
| 3381 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3382 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3383 | // Base type can also be a vector type (see PR17453). |
| 3384 | // Distinguish between base type and base element type. |
| 3385 | QualType OldElemTy = OldTy; |
| 3386 | if (const VectorType *VT = OldTy->getAs<VectorType>()) |
| 3387 | OldElemTy = VT->getElementType(); |
| 3388 | |
| 3389 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3390 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3391 | else if (IntegerMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3392 | if (!OldElemTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3393 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3394 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3395 | if (!OldElemTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3396 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3397 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3398 | if (!OldElemTy->isFloatingType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3399 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3400 | } |
| 3401 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3402 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3403 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3404 | // FIXME: Make sure floating-point mappings are accurate |
| 3405 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3406 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3407 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3408 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3411 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3412 | |
| 3413 | if (IntegerMode) |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3414 | NewElemTy = S.Context.getIntTypeForBitwidth( |
| 3415 | DestWidth, OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3416 | else |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3417 | NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3418 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3419 | if (NewElemTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3420 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3421 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3424 | if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3425 | NewElemTy = S.Context.getComplexType(NewElemTy); |
| 3426 | } |
| 3427 | |
| 3428 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3429 | if (VectorSize.getBoolValue()) { |
| 3430 | NewTy = S.Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3431 | VectorType::GenericVector); |
| 3432 | } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3433 | // Complex machine mode does not support base vector types. |
| 3434 | if (ComplexMode) { |
| 3435 | S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type); |
| 3436 | return; |
| 3437 | } |
| 3438 | unsigned NumElements = S.Context.getTypeSize(OldElemTy) * |
| 3439 | OldVT->getNumElements() / |
| 3440 | S.Context.getTypeSize(NewElemTy); |
| 3441 | NewTy = |
| 3442 | S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
| 3443 | } |
| 3444 | |
| 3445 | if (NewTy.isNull()) { |
| 3446 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3447 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3451 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3452 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3453 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3454 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3455 | |
| 3456 | D->addAttr(::new (S.Context) |
| 3457 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3458 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3459 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3460 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3461 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3462 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3463 | if (!VD->hasGlobalStorage()) |
| 3464 | S.Diag(Attr.getLoc(), |
| 3465 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3466 | << Attr.getName(); |
| 3467 | } else if (!isFunctionOrMethod(D)) { |
| 3468 | S.Diag(Attr.getLoc(), |
| 3469 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3470 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3471 | return; |
| 3472 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3473 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3474 | D->addAttr(::new (S.Context) |
| 3475 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3476 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3479 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3480 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3481 | unsigned AttrSpellingListIndex) { |
| 3482 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3483 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3484 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3485 | return nullptr; |
| 3486 | } |
| 3487 | |
| 3488 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3489 | return nullptr; |
| 3490 | |
| 3491 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3492 | AttrSpellingListIndex); |
| 3493 | } |
| 3494 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3495 | CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range, |
| 3496 | IdentifierInfo *Ident, |
| 3497 | unsigned AttrSpellingListIndex) { |
| 3498 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident)) |
| 3499 | return nullptr; |
| 3500 | |
| 3501 | return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex); |
| 3502 | } |
| 3503 | |
| 3504 | InternalLinkageAttr * |
| 3505 | Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range, |
| 3506 | IdentifierInfo *Ident, |
| 3507 | unsigned AttrSpellingListIndex) { |
| 3508 | if (auto VD = dyn_cast<VarDecl>(D)) { |
| 3509 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 3510 | // ImplicitParm or VarTemplateSpecialization). |
| 3511 | if (VD->getKind() != Decl::Var) { |
| 3512 | Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type) |
| 3513 | << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 3514 | : ExpectedVariableOrFunction); |
| 3515 | return nullptr; |
| 3516 | } |
| 3517 | // Attribute does not apply to non-static local variables. |
| 3518 | if (VD->hasLocalStorage()) { |
| 3519 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 3520 | return nullptr; |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident)) |
| 3525 | return nullptr; |
| 3526 | |
| 3527 | return ::new (Context) |
| 3528 | InternalLinkageAttr(Range, Context, AttrSpellingListIndex); |
| 3529 | } |
| 3530 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3531 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 3532 | unsigned AttrSpellingListIndex) { |
| 3533 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 3534 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 3535 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3536 | return nullptr; |
| 3537 | } |
| 3538 | |
| 3539 | if (D->hasAttr<MinSizeAttr>()) |
| 3540 | return nullptr; |
| 3541 | |
| 3542 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 3543 | } |
| 3544 | |
| 3545 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 3546 | unsigned AttrSpellingListIndex) { |
| 3547 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 3548 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 3549 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3550 | D->dropAttr<AlwaysInlineAttr>(); |
| 3551 | } |
| 3552 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 3553 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 3554 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3555 | D->dropAttr<MinSizeAttr>(); |
| 3556 | } |
| 3557 | |
| 3558 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 3559 | return nullptr; |
| 3560 | |
| 3561 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 3562 | AttrSpellingListIndex); |
| 3563 | } |
| 3564 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3565 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 3566 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3567 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(), |
| 3568 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 3569 | return; |
| 3570 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3571 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 3572 | D, Attr.getRange(), Attr.getName(), |
| 3573 | Attr.getAttributeSpellingListIndex())) |
| 3574 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3577 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3578 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 3579 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3580 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3583 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 3584 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3585 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 3586 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3587 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3590 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3591 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3592 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3593 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 3594 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3595 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3596 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 3597 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3598 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3599 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3600 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3601 | D->addAttr(::new (S.Context) |
| 3602 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 3603 | Attr.getAttributeSpellingListIndex())); |
Artem Belevich | c3fa25d | 2015-09-22 17:22:51 +0000 | [diff] [blame] | 3604 | |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3607 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3608 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3609 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3610 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3611 | return; |
| 3612 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3613 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3614 | D->addAttr(::new (S.Context) |
| 3615 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3616 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3619 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3620 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3621 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3622 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3623 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3624 | CallingConv CC; |
Richard Smith | eec7cb1 | 2015-05-28 23:38:53 +0000 | [diff] [blame] | 3625 | if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3626 | return; |
| 3627 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3628 | if (!isa<ObjCMethodDecl>(D)) { |
| 3629 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3630 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3631 | return; |
| 3632 | } |
| 3633 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3634 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3635 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3636 | D->addAttr(::new (S.Context) |
| 3637 | FastCallAttr(Attr.getRange(), S.Context, |
| 3638 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3639 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3640 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3641 | D->addAttr(::new (S.Context) |
| 3642 | StdCallAttr(Attr.getRange(), S.Context, |
| 3643 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3644 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3645 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3646 | D->addAttr(::new (S.Context) |
| 3647 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3648 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3649 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3650 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3651 | D->addAttr(::new (S.Context) |
| 3652 | CDeclAttr(Attr.getRange(), S.Context, |
| 3653 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3654 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3655 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3656 | D->addAttr(::new (S.Context) |
| 3657 | PascalAttr(Attr.getRange(), S.Context, |
| 3658 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3659 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3660 | case AttributeList::AT_VectorCall: |
| 3661 | D->addAttr(::new (S.Context) |
| 3662 | VectorCallAttr(Attr.getRange(), S.Context, |
| 3663 | Attr.getAttributeSpellingListIndex())); |
| 3664 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3665 | case AttributeList::AT_MSABI: |
| 3666 | D->addAttr(::new (S.Context) |
| 3667 | MSABIAttr(Attr.getRange(), S.Context, |
| 3668 | Attr.getAttributeSpellingListIndex())); |
| 3669 | return; |
| 3670 | case AttributeList::AT_SysVABI: |
| 3671 | D->addAttr(::new (S.Context) |
| 3672 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3673 | Attr.getAttributeSpellingListIndex())); |
| 3674 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3675 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3676 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3677 | switch (CC) { |
| 3678 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3679 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3680 | break; |
| 3681 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3682 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3683 | break; |
| 3684 | default: |
| 3685 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3688 | D->addAttr(::new (S.Context) |
| 3689 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3690 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3691 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3692 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3693 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3694 | D->addAttr(::new (S.Context) |
| 3695 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3696 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3697 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3698 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3699 | default: |
| 3700 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3704 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3705 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3706 | if (attr.isInvalid()) |
| 3707 | return true; |
| 3708 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3709 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3710 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3711 | attr.setInvalid(); |
| 3712 | return true; |
| 3713 | } |
| 3714 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3715 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3716 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3717 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3718 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3719 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3720 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3721 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3722 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3723 | case AttributeList::AT_MSABI: |
| 3724 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3725 | CC_X86_64Win64; |
| 3726 | break; |
| 3727 | case AttributeList::AT_SysVABI: |
| 3728 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3729 | CC_C; |
| 3730 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3731 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3732 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3733 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3734 | attr.setInvalid(); |
| 3735 | return true; |
| 3736 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3737 | if (StrRef == "aapcs") { |
| 3738 | CC = CC_AAPCS; |
| 3739 | break; |
| 3740 | } else if (StrRef == "aapcs-vfp") { |
| 3741 | CC = CC_AAPCS_VFP; |
| 3742 | break; |
| 3743 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3744 | |
| 3745 | attr.setInvalid(); |
| 3746 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3747 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3748 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3749 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3750 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3753 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3754 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3755 | if (A != TargetInfo::CCCR_OK) { |
| 3756 | if (A == TargetInfo::CCCR_Warning) |
| 3757 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3758 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3759 | // This convention is not valid for the target. Use the default function or |
| 3760 | // method calling convention. |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3761 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3762 | if (FD) |
| 3763 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3764 | TargetInfo::CCMT_NonMember; |
| 3765 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3768 | return false; |
| 3769 | } |
| 3770 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3771 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3772 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3773 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3774 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3775 | return true; |
| 3776 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3777 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3778 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3779 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3780 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3781 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3782 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3783 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3784 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3785 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3786 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3789 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3790 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3791 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3792 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3793 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3796 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3797 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3798 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3799 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3800 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3801 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3804 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 3807 | // Checks whether an argument of launch_bounds attribute is acceptable |
| 3808 | // May output an error. |
| 3809 | static bool checkLaunchBoundsArgument(Sema &S, Expr *E, |
| 3810 | const CUDALaunchBoundsAttr &Attr, |
| 3811 | const unsigned Idx) { |
| 3812 | |
| 3813 | if (S.DiagnoseUnexpandedParameterPack(E)) |
| 3814 | return false; |
| 3815 | |
| 3816 | // Accept template arguments for now as they depend on something else. |
| 3817 | // We'll get to check them when they eventually get instantiated. |
| 3818 | if (E->isValueDependent()) |
| 3819 | return true; |
| 3820 | |
| 3821 | llvm::APSInt I(64); |
| 3822 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 3823 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 3824 | << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
| 3825 | return false; |
| 3826 | } |
| 3827 | // Make sure we can fit it in 32 bits. |
| 3828 | if (!I.isIntN(32)) { |
| 3829 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 3830 | << 32 << /* Unsigned */ 1; |
| 3831 | return false; |
| 3832 | } |
| 3833 | if (I < 0) |
| 3834 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 3835 | << &Attr << Idx << E->getSourceRange(); |
| 3836 | |
| 3837 | return true; |
| 3838 | } |
| 3839 | |
| 3840 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 3841 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 3842 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 3843 | SpellingListIndex); |
| 3844 | |
| 3845 | if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3846 | return; |
| 3847 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 3848 | if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1)) |
| 3849 | return; |
| 3850 | |
| 3851 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 3852 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 3853 | } |
| 3854 | |
| 3855 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3856 | const AttributeList &Attr) { |
| 3857 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 3858 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 3859 | return; |
| 3860 | |
| 3861 | S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3862 | Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr, |
| 3863 | Attr.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3866 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3867 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3868 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3869 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3870 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3871 | return; |
| 3872 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3873 | |
| 3874 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3875 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3876 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3877 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3878 | |
| 3879 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3880 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3881 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3882 | return; |
| 3883 | } |
| 3884 | |
| 3885 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3886 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3887 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3888 | return; |
| 3889 | |
| 3890 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3891 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3892 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3893 | return; |
| 3894 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3895 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3896 | if (IsPointer) { |
| 3897 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3898 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3899 | if (!BufferTy->isPointerType()) { |
| 3900 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3901 | << Attr.getName() << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3902 | } |
| 3903 | } |
| 3904 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3905 | D->addAttr(::new (S.Context) |
| 3906 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3907 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3908 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3912 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3913 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3914 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3915 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3916 | return; |
| 3917 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3918 | |
| 3919 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3920 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3921 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3922 | if (!isa<VarDecl>(D)) { |
| 3923 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3924 | << Attr.getName() << ExpectedVariable; |
| 3925 | return; |
| 3926 | } |
| 3927 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3928 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3929 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3930 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3931 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3932 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3933 | D->addAttr(::new (S.Context) |
| 3934 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3935 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3936 | Attr.getLayoutCompatible(), |
| 3937 | Attr.getMustBeNull(), |
| 3938 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3941 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3942 | // Checker-specific attribute handlers. |
| 3943 | //===----------------------------------------------------------------------===// |
| 3944 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3945 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3946 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3947 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3950 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3951 | return type->isDependentType() || |
| 3952 | type->isObjCObjectPointerType() || |
| 3953 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3954 | } |
| 3955 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3956 | return type->isDependentType() || |
| 3957 | type->isPointerType() || |
| 3958 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3961 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3962 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3963 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3964 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3965 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3966 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3967 | cf = false; |
| 3968 | } else { |
| 3969 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3970 | cf = true; |
| 3971 | } |
| 3972 | |
| 3973 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3974 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3975 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3976 | return; |
| 3977 | } |
| 3978 | |
| 3979 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3980 | param->addAttr(::new (S.Context) |
| 3981 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3982 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3983 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3984 | param->addAttr(::new (S.Context) |
| 3985 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3986 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3989 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3990 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3991 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3992 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3993 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3994 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3995 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3996 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3997 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3998 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3999 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4000 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4001 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4002 | returnType = FD->getReturnType(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4003 | else if (auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4004 | returnType = Param->getType()->getPointeeType(); |
| 4005 | if (returnType.isNull()) { |
| 4006 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4007 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4008 | << Attr.getRange(); |
| 4009 | return; |
| 4010 | } |
| 4011 | } else { |
| 4012 | AttributeDeclKind ExpectedDeclKind; |
| 4013 | switch (Attr.getKind()) { |
| 4014 | default: llvm_unreachable("invalid ownership attribute"); |
| 4015 | case AttributeList::AT_NSReturnsRetained: |
| 4016 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4017 | case AttributeList::AT_NSReturnsNotRetained: |
| 4018 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4019 | break; |
| 4020 | |
| 4021 | case AttributeList::AT_CFReturnsRetained: |
| 4022 | case AttributeList::AT_CFReturnsNotRetained: |
| 4023 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4024 | break; |
| 4025 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4026 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4027 | << Attr.getRange() << Attr.getName() << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4028 | return; |
| 4029 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4030 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4031 | bool typeOK; |
| 4032 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4033 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4034 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4035 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4036 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4037 | cf = false; |
| 4038 | break; |
| 4039 | |
| 4040 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4041 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4042 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4043 | cf = false; |
| 4044 | break; |
| 4045 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4046 | case AttributeList::AT_CFReturnsRetained: |
| 4047 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4048 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4049 | cf = true; |
| 4050 | break; |
| 4051 | } |
| 4052 | |
| 4053 | if (!typeOK) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4054 | if (isa<ParmVarDecl>(D)) { |
| 4055 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4056 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4057 | << Attr.getRange(); |
| 4058 | } else { |
| 4059 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4060 | enum : unsigned { |
| 4061 | Function, |
| 4062 | Method, |
| 4063 | Property |
| 4064 | } SubjectKind = Function; |
| 4065 | if (isa<ObjCMethodDecl>(D)) |
| 4066 | SubjectKind = Method; |
| 4067 | else if (isa<ObjCPropertyDecl>(D)) |
| 4068 | SubjectKind = Property; |
| 4069 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4070 | << Attr.getName() << SubjectKind << cf |
| 4071 | << Attr.getRange(); |
| 4072 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4073 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4074 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4075 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4076 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4077 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4078 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4079 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4080 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 4081 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4082 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4083 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4084 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 4085 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4086 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4087 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4088 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 4089 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4090 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4091 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4092 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 4093 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4094 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4095 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4096 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 4097 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4098 | return; |
| 4099 | }; |
| 4100 | } |
| 4101 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4102 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4103 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4104 | const int EP_ObjCMethod = 1; |
| 4105 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4106 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4107 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4108 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4109 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4110 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4111 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4112 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4113 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4114 | if (!resultType->isReferenceType() && |
| 4115 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4116 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4117 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4118 | << attr.getName() |
| 4119 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4120 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4121 | |
| 4122 | // Drop the attribute. |
| 4123 | return; |
| 4124 | } |
| 4125 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4126 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 4127 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4130 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4131 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4132 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4133 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4134 | DeclContext *DC = method->getDeclContext(); |
| 4135 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4136 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4137 | << attr.getName() << 0; |
| 4138 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4139 | return; |
| 4140 | } |
| 4141 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4142 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4143 | << attr.getName() << 1; |
| 4144 | return; |
| 4145 | } |
| 4146 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4147 | method->addAttr(::new (S.Context) |
| 4148 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4149 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4152 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 4153 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4154 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(), |
| 4155 | Attr.getName())) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4156 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4157 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4158 | D->addAttr(::new (S.Context) |
| 4159 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 4160 | Attr.getAttributeSpellingListIndex())); |
| 4161 | } |
| 4162 | |
| 4163 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 4164 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4165 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(), |
| 4166 | Attr.getName())) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4167 | return; |
| 4168 | |
| 4169 | D->addAttr(::new (S.Context) |
| 4170 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 4171 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4174 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4175 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4176 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4177 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4178 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4179 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4180 | return; |
| 4181 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4182 | |
| 4183 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 4184 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4185 | if (!Parm->Ident->isStr("id")) { |
| 4186 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 4187 | << Attr.getName(); |
| 4188 | return; |
| 4189 | } |
| 4190 | |
| 4191 | // Only allow 'cv void *'. |
| 4192 | QualType T = TD->getUnderlyingType(); |
| 4193 | if (!T->isVoidPointerType()) { |
| 4194 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 4195 | return; |
| 4196 | } |
| 4197 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4198 | |
| 4199 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4200 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4201 | Attr.getAttributeSpellingListIndex())); |
| 4202 | } |
| 4203 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4204 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 4205 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4206 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 4207 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4208 | if (!Parm) { |
| 4209 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4210 | return; |
| 4211 | } |
| 4212 | |
| 4213 | D->addAttr(::new (S.Context) |
| 4214 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 4215 | Attr.getAttributeSpellingListIndex())); |
| 4216 | } |
| 4217 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4218 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4219 | const AttributeList &Attr) { |
| 4220 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4221 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4222 | if (!RelatedClass) { |
| 4223 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4224 | return; |
| 4225 | } |
| 4226 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4227 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4228 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4229 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4230 | D->addAttr(::new (S.Context) |
| 4231 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 4232 | ClassMethod, InstanceMethod, |
| 4233 | Attr.getAttributeSpellingListIndex())); |
| 4234 | } |
| 4235 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4236 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 4237 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4238 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4239 | if (ObjCCategoryDecl *CatDecl = |
| 4240 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4241 | IFace = CatDecl->getClassInterface(); |
| 4242 | else |
| 4243 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4244 | |
| 4245 | if (!IFace) |
| 4246 | return; |
| 4247 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4248 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4249 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4250 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 4251 | Attr.getAttributeSpellingListIndex())); |
| 4252 | } |
| 4253 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4254 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 4255 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4256 | StringRef MetaDataName; |
| 4257 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 4258 | return; |
| 4259 | D->addAttr(::new (S.Context) |
| 4260 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 4261 | MetaDataName, |
| 4262 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4265 | // when a user wants to use objc_boxable with a union or struct |
| 4266 | // but she doesn't have access to the declaration (legacy/third-party code) |
| 4267 | // then she can 'enable' this feature via trick with a typedef |
| 4268 | // e.g.: |
| 4269 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
| 4270 | static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4271 | bool notify = false; |
| 4272 | |
| 4273 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4274 | if (RD && RD->getDefinition()) { |
| 4275 | RD = RD->getDefinition(); |
| 4276 | notify = true; |
| 4277 | } |
| 4278 | |
| 4279 | if (RD) { |
| 4280 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 4281 | ObjCBoxableAttr(Attr.getRange(), S.Context, |
| 4282 | Attr.getAttributeSpellingListIndex()); |
| 4283 | RD->addAttr(BoxableAttr); |
| 4284 | if (notify) { |
| 4285 | // we need to notify ASTReader/ASTWriter about |
| 4286 | // modification of existing declaration |
| 4287 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 4288 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 4289 | } |
| 4290 | } |
| 4291 | } |
| 4292 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4293 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4294 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4295 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4296 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4297 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4298 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4301 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4302 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4303 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4304 | QualType type = vd->getType(); |
| 4305 | |
| 4306 | if (!type->isDependentType() && |
| 4307 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4308 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4309 | << type; |
| 4310 | return; |
| 4311 | } |
| 4312 | |
| 4313 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4314 | |
| 4315 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4316 | // going to infer. |
| 4317 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4318 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4319 | |
| 4320 | switch (lifetime) { |
| 4321 | case Qualifiers::OCL_None: |
| 4322 | assert(type->isDependentType() && |
| 4323 | "didn't infer lifetime for non-dependent type?"); |
| 4324 | break; |
| 4325 | |
| 4326 | case Qualifiers::OCL_Weak: // meaningful |
| 4327 | case Qualifiers::OCL_Strong: // meaningful |
| 4328 | break; |
| 4329 | |
| 4330 | case Qualifiers::OCL_ExplicitNone: |
| 4331 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4332 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4333 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4334 | break; |
| 4335 | } |
| 4336 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4337 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4338 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4339 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4342 | //===----------------------------------------------------------------------===// |
| 4343 | // Microsoft specific attribute handlers. |
| 4344 | //===----------------------------------------------------------------------===// |
| 4345 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4346 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 4347 | if (!S.LangOpts.CPlusPlus) { |
| 4348 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4349 | << Attr.getName() << AttributeLangSupport::C; |
| 4350 | return; |
| 4351 | } |
| 4352 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 4353 | if (!isa<CXXRecordDecl>(D)) { |
| 4354 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4355 | << Attr.getName() << ExpectedClass; |
| 4356 | return; |
| 4357 | } |
| 4358 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4359 | StringRef StrRef; |
| 4360 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4361 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4362 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4363 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4364 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4365 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4366 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4367 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4368 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4369 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4370 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4371 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4372 | return; |
| 4373 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4374 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4375 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4376 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4377 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4378 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4379 | return; |
| 4380 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4381 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4382 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4383 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4384 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4385 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4386 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4387 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4388 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4391 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4392 | if (!S.LangOpts.CPlusPlus) { |
| 4393 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4394 | << Attr.getName() << AttributeLangSupport::C; |
| 4395 | return; |
| 4396 | } |
| 4397 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4398 | D, Attr.getRange(), /*BestCase=*/true, |
| 4399 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4400 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
| 4401 | if (IA) |
| 4402 | D->addAttr(IA); |
| 4403 | } |
| 4404 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 4405 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 4406 | const AttributeList &Attr) { |
| 4407 | VarDecl *VD = cast<VarDecl>(D); |
| 4408 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 4409 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 4410 | return; |
| 4411 | } |
| 4412 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 4413 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 4414 | return; |
| 4415 | } |
| 4416 | if (VD->hasLocalStorage()) { |
| 4417 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 4418 | return; |
| 4419 | } |
| 4420 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 4421 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4422 | } |
| 4423 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4424 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 4425 | const AttributeList &Attr) { |
| 4426 | // Check the attribute arguments. |
| 4427 | if (Attr.getNumArgs() > 1) { |
| 4428 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4429 | << Attr.getName() << 1; |
| 4430 | return; |
| 4431 | } |
| 4432 | |
| 4433 | StringRef Str; |
| 4434 | SourceLocation ArgLoc; |
| 4435 | |
| 4436 | if (Attr.getNumArgs() == 0) |
| 4437 | Str = ""; |
| 4438 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4439 | return; |
| 4440 | |
| 4441 | ARMInterruptAttr::InterruptType Kind; |
| 4442 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4443 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4444 | << Attr.getName() << Str << ArgLoc; |
| 4445 | return; |
| 4446 | } |
| 4447 | |
| 4448 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 4449 | D->addAttr(::new (S.Context) |
| 4450 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 4451 | } |
| 4452 | |
| 4453 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 4454 | const AttributeList &Attr) { |
| 4455 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4456 | return; |
| 4457 | |
| 4458 | if (!Attr.isArgExpr(0)) { |
| 4459 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 4460 | << AANT_ArgumentIntegerConstant; |
| 4461 | return; |
| 4462 | } |
| 4463 | |
| 4464 | // FIXME: Check for decl - it should be void ()(void). |
| 4465 | |
| 4466 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4467 | llvm::APSInt NumParams(32); |
| 4468 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 4469 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4470 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4471 | << NumParamsExpr->getSourceRange(); |
| 4472 | return; |
| 4473 | } |
| 4474 | |
| 4475 | unsigned Num = NumParams.getLimitedValue(255); |
| 4476 | if ((Num & 1) || Num > 30) { |
| 4477 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 4478 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 4479 | << NumParamsExpr->getSourceRange(); |
| 4480 | return; |
| 4481 | } |
| 4482 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4483 | D->addAttr(::new (S.Context) |
| 4484 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 4485 | Attr.getAttributeSpellingListIndex())); |
| 4486 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4489 | static void handleMipsInterruptAttr(Sema &S, Decl *D, |
| 4490 | const AttributeList &Attr) { |
| 4491 | // Only one optional argument permitted. |
| 4492 | if (Attr.getNumArgs() > 1) { |
| 4493 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4494 | << Attr.getName() << 1; |
| 4495 | return; |
| 4496 | } |
| 4497 | |
| 4498 | StringRef Str; |
| 4499 | SourceLocation ArgLoc; |
| 4500 | |
| 4501 | if (Attr.getNumArgs() == 0) |
| 4502 | Str = ""; |
| 4503 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4504 | return; |
| 4505 | |
| 4506 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 4507 | // a) Must be a function. |
| 4508 | // b) Must have no parameters. |
| 4509 | // c) Must have the 'void' return type. |
| 4510 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 4511 | // lacks the 'eret' instruction. |
| 4512 | // e) The attribute itself must either have no argument or one of the |
| 4513 | // valid interrupt types, see [MipsInterruptDocs]. |
| 4514 | |
| 4515 | if (!isFunctionOrMethod(D)) { |
| 4516 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 4517 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 4518 | return; |
| 4519 | } |
| 4520 | |
| 4521 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 4522 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 4523 | << 0; |
| 4524 | return; |
| 4525 | } |
| 4526 | |
| 4527 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 4528 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 4529 | << 1; |
| 4530 | return; |
| 4531 | } |
| 4532 | |
| 4533 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(), |
| 4534 | Attr.getName())) |
| 4535 | return; |
| 4536 | |
| 4537 | MipsInterruptAttr::InterruptType Kind; |
| 4538 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4539 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4540 | << Attr.getName() << "'" + std::string(Str) + "'"; |
| 4541 | return; |
| 4542 | } |
| 4543 | |
| 4544 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
| 4545 | Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex())); |
| 4546 | } |
| 4547 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4548 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4549 | // Dispatch the interrupt attribute based on the current target. |
| 4550 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 4551 | handleMSP430InterruptAttr(S, D, Attr); |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4552 | else if (S.Context.getTargetInfo().getTriple().getArch() == |
| 4553 | llvm::Triple::mipsel || |
| 4554 | S.Context.getTargetInfo().getTriple().getArch() == |
| 4555 | llvm::Triple::mips) |
| 4556 | handleMipsInterruptAttr(S, D, Attr); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4557 | else |
| 4558 | handleARMInterruptAttr(S, D, Attr); |
| 4559 | } |
| 4560 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4561 | static void handleMips16Attribute(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4562 | if (checkAttrMutualExclusion<MipsInterruptAttr>(S, D, Attr.getRange(), |
| 4563 | Attr.getName())) |
| 4564 | return; |
| 4565 | |
| 4566 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); |
| 4567 | } |
| 4568 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4569 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 4570 | const AttributeList &Attr) { |
| 4571 | uint32_t NumRegs; |
| 4572 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4573 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4574 | return; |
| 4575 | |
| 4576 | D->addAttr(::new (S.Context) |
| 4577 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, |
| 4578 | NumRegs, |
| 4579 | Attr.getAttributeSpellingListIndex())); |
| 4580 | } |
| 4581 | |
| 4582 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 4583 | const AttributeList &Attr) { |
| 4584 | uint32_t NumRegs; |
| 4585 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4586 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4587 | return; |
| 4588 | |
| 4589 | D->addAttr(::new (S.Context) |
| 4590 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, |
| 4591 | NumRegs, |
| 4592 | Attr.getAttributeSpellingListIndex())); |
| 4593 | } |
| 4594 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4595 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 4596 | const AttributeList& Attr) { |
| 4597 | // If we try to apply it to a function pointer, don't warn, but don't |
| 4598 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 4599 | // special about calling a force_align_arg_pointer function. |
| 4600 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 4601 | if (VD && VD->getType()->isFunctionPointerType()) |
| 4602 | return; |
| 4603 | // Also don't warn on function pointer typedefs. |
| 4604 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 4605 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 4606 | TD->getUnderlyingType()->isFunctionType())) |
| 4607 | return; |
| 4608 | // Attribute can only be applied to function types. |
| 4609 | if (!isa<FunctionDecl>(D)) { |
| 4610 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4611 | << Attr.getName() << /* function */0; |
| 4612 | return; |
| 4613 | } |
| 4614 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4615 | D->addAttr(::new (S.Context) |
| 4616 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 4617 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 4621 | unsigned AttrSpellingListIndex) { |
| 4622 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4623 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4624 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4628 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4629 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4630 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4633 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 4634 | unsigned AttrSpellingListIndex) { |
| 4635 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4636 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4637 | D->dropAttr<DLLImportAttr>(); |
| 4638 | } |
| 4639 | |
| 4640 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4641 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4642 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4643 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4646 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 4647 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 4648 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4649 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 4650 | << A.getName(); |
| 4651 | return; |
| 4652 | } |
| 4653 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 4654 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4655 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 4656 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4657 | // MinGW doesn't allow dllimport on inline functions. |
| 4658 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 4659 | << A.getName(); |
| 4660 | return; |
| 4661 | } |
| 4662 | } |
| 4663 | |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 4664 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 4665 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4666 | MD->getParent()->isLambda()) { |
| 4667 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName(); |
| 4668 | return; |
| 4669 | } |
| 4670 | } |
| 4671 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4672 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 4673 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 4674 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 4675 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4676 | if (NewAttr) |
| 4677 | D->addAttr(NewAttr); |
| 4678 | } |
| 4679 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4680 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4681 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4682 | unsigned AttrSpellingListIndex, |
| 4683 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 4684 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 4685 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4686 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4687 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 4688 | << 1 /*previous declaration*/; |
| 4689 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 4690 | D->dropAttr<MSInheritanceAttr>(); |
| 4691 | } |
| 4692 | |
| 4693 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 4694 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4695 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 4696 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4697 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4698 | } |
| 4699 | } else { |
| 4700 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 4701 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 4702 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4703 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4704 | } |
| 4705 | if (RD->getDescribedClassTemplate()) { |
| 4706 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 4707 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4708 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4713 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4716 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4717 | // The capability attributes take a single string parameter for the name of |
| 4718 | // the capability they represent. The lockable attribute does not take any |
| 4719 | // parameters. However, semantically, both attributes represent the same |
| 4720 | // concept, and so they use the same semantic attribute. Eventually, the |
| 4721 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4722 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 4723 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4724 | // literal will be considered a "mutex." |
| 4725 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4726 | SourceLocation LiteralLoc; |
| 4727 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 4728 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 4729 | return; |
| 4730 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4731 | // Currently, there are only two names allowed for a capability: role and |
| 4732 | // mutex (case insensitive). Diagnose other capability names. |
| 4733 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 4734 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 4735 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4736 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 4737 | Attr.getAttributeSpellingListIndex())); |
| 4738 | } |
| 4739 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4740 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 4741 | const AttributeList &Attr) { |
| 4742 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 4743 | Attr.getArgAsExpr(0), |
| 4744 | Attr.getAttributeSpellingListIndex())); |
| 4745 | } |
| 4746 | |
| 4747 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 4748 | const AttributeList &Attr) { |
| 4749 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4750 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4751 | return; |
| 4752 | |
| 4753 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 4754 | S.Context, |
| 4755 | Args.data(), Args.size(), |
| 4756 | Attr.getAttributeSpellingListIndex())); |
| 4757 | } |
| 4758 | |
| 4759 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 4760 | const AttributeList &Attr) { |
| 4761 | SmallVector<Expr*, 2> Args; |
| 4762 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 4763 | return; |
| 4764 | |
| 4765 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 4766 | S.Context, |
| 4767 | Attr.getArgAsExpr(0), |
| 4768 | Args.data(), |
| 4769 | Args.size(), |
| 4770 | Attr.getAttributeSpellingListIndex())); |
| 4771 | } |
| 4772 | |
| 4773 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 4774 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4775 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4776 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 4777 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4778 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4779 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 4780 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 4781 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4784 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 4785 | const AttributeList &Attr) { |
| 4786 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4787 | return; |
| 4788 | |
| 4789 | // check that all arguments are lockable objects |
| 4790 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 4791 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4792 | if (Args.empty()) |
| 4793 | return; |
| 4794 | |
| 4795 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 4796 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 4797 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 4798 | |
| 4799 | D->addAttr(RCA); |
| 4800 | } |
| 4801 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4802 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4803 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 4804 | if (NSD->isAnonymousNamespace()) { |
| 4805 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 4806 | // Do not want to attach the attribute to the namespace because that will |
| 4807 | // cause confusing diagnostic reports for uses of declarations within the |
| 4808 | // namespace. |
| 4809 | return; |
| 4810 | } |
| 4811 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 4812 | |
| 4813 | if (!S.getLangOpts().CPlusPlus14) |
| 4814 | if (Attr.isCXX11Attribute() && |
| 4815 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Saleem Abdulrasool | b47d606 | 2015-02-18 04:33:26 +0000 | [diff] [blame] | 4816 | S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 4817 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4818 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
| 4819 | } |
| 4820 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4821 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4822 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4823 | return; |
| 4824 | |
| 4825 | std::vector<std::string> Sanitizers; |
| 4826 | |
| 4827 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 4828 | StringRef SanitizerName; |
| 4829 | SourceLocation LiteralLoc; |
| 4830 | |
| 4831 | if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc)) |
| 4832 | return; |
| 4833 | |
| 4834 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 4835 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
| 4836 | |
| 4837 | Sanitizers.push_back(SanitizerName); |
| 4838 | } |
| 4839 | |
| 4840 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 4841 | Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 4842 | Attr.getAttributeSpellingListIndex())); |
| 4843 | } |
| 4844 | |
| 4845 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 4846 | const AttributeList &Attr) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 4847 | StringRef AttrName = Attr.getName()->getName(); |
| 4848 | normalizeName(AttrName); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4849 | std::string SanitizerName = |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 4850 | llvm::StringSwitch<std::string>(AttrName) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4851 | .Case("no_address_safety_analysis", "address") |
| 4852 | .Case("no_sanitize_address", "address") |
| 4853 | .Case("no_sanitize_thread", "thread") |
Peter Collingbourne | 9441094 | 2015-05-15 20:11:18 +0000 | [diff] [blame] | 4854 | .Case("no_sanitize_memory", "memory"); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4855 | D->addAttr(::new (S.Context) |
| 4856 | NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, |
| 4857 | Attr.getAttributeSpellingListIndex())); |
| 4858 | } |
| 4859 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4860 | static void handleInternalLinkageAttr(Sema &S, Decl *D, |
| 4861 | const AttributeList &Attr) { |
| 4862 | if (InternalLinkageAttr *Internal = |
| 4863 | S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(), |
| 4864 | Attr.getAttributeSpellingListIndex())) |
| 4865 | D->addAttr(Internal); |
| 4866 | } |
| 4867 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4868 | /// Handles semantic checking for features that are common to all attributes, |
| 4869 | /// such as checking whether a parameter was properly specified, or the correct |
| 4870 | /// number of arguments were passed, etc. |
| 4871 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4872 | const AttributeList &Attr) { |
| 4873 | // Several attributes carry different semantics than the parsing requires, so |
| 4874 | // those are opted out of the common handling. |
| 4875 | // |
| 4876 | // We also bail on unknown and ignored attributes because those are handled |
| 4877 | // as part of the target-specific handling logic. |
| 4878 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4879 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4880 | return false; |
| 4881 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4882 | // Check whether the attribute requires specific language extensions to be |
| 4883 | // enabled. |
| 4884 | if (!Attr.diagnoseLangOpts(S)) |
| 4885 | return true; |
| 4886 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 4887 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 4888 | // If there are no optional arguments, then checking for the argument count |
| 4889 | // is trivial. |
| 4890 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4891 | return true; |
| 4892 | } else { |
| 4893 | // There are optional arguments, so checking is slightly more involved. |
| 4894 | if (Attr.getMinArgs() && |
| 4895 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 4896 | return true; |
| 4897 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 4898 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 4899 | return true; |
| 4900 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4901 | |
| 4902 | // Check whether the attribute appertains to the given subject. |
| 4903 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 4904 | return true; |
| 4905 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4906 | return false; |
| 4907 | } |
| 4908 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4909 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4910 | // Top Level Sema Entry Points |
| 4911 | //===----------------------------------------------------------------------===// |
| 4912 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4913 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4914 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4915 | /// silently ignore it if a GNU attribute. |
| 4916 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4917 | const AttributeList &Attr, |
| 4918 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4919 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4920 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4921 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4922 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4923 | // instead. |
| 4924 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4925 | return; |
| 4926 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4927 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 4928 | // which do not apply to the current target architecture are treated as |
| 4929 | // though they were unknown attributes. |
| 4930 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 4931 | !Attr.existsInTarget(S.Context.getTargetInfo())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4932 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 4933 | ? diag::warn_unhandled_ms_attribute_ignored |
| 4934 | : diag::warn_unknown_attribute_ignored) |
| 4935 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4936 | return; |
| 4937 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4938 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4939 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4940 | return; |
| 4941 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4942 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4943 | default: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4944 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4945 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4946 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4947 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4948 | handleInterruptAttr(S, D, Attr); |
| 4949 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4950 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4951 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 4952 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4953 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4954 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4955 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4956 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4957 | case AttributeList::AT_Mips16: |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4958 | handleMips16Attribute(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4959 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4960 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4961 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 4962 | break; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4963 | case AttributeList::AT_AMDGPUNumVGPR: |
| 4964 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 4965 | break; |
| 4966 | case AttributeList::AT_AMDGPUNumSGPR: |
| 4967 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 4968 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 4969 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4970 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 4971 | break; |
| 4972 | case AttributeList::AT_IBOutlet: |
| 4973 | handleIBOutlet(S, D, Attr); |
| 4974 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4975 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4976 | handleIBOutletCollection(S, D, Attr); |
| 4977 | break; |
| 4978 | case AttributeList::AT_Alias: |
| 4979 | handleAliasAttr(S, D, Attr); |
| 4980 | break; |
| 4981 | case AttributeList::AT_Aligned: |
| 4982 | handleAlignedAttr(S, D, Attr); |
| 4983 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 4984 | case AttributeList::AT_AlignValue: |
| 4985 | handleAlignValueAttr(S, D, Attr); |
| 4986 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4987 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4988 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4989 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4990 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4991 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 4992 | break; |
| 4993 | case AttributeList::AT_TLSModel: |
| 4994 | handleTLSModelAttr(S, D, Attr); |
| 4995 | break; |
| 4996 | case AttributeList::AT_Annotate: |
| 4997 | handleAnnotateAttr(S, D, Attr); |
| 4998 | break; |
| 4999 | case AttributeList::AT_Availability: |
| 5000 | handleAvailabilityAttr(S, D, Attr); |
| 5001 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5002 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5003 | handleDependencyAttr(S, scope, D, Attr); |
| 5004 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5005 | case AttributeList::AT_Common: |
| 5006 | handleCommonAttr(S, D, Attr); |
| 5007 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5008 | case AttributeList::AT_CUDAConstant: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5009 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); |
| 5010 | break; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5011 | case AttributeList::AT_PassObjectSize: |
| 5012 | handlePassObjectSizeAttr(S, D, Attr); |
| 5013 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5014 | case AttributeList::AT_Constructor: |
| 5015 | handleConstructorAttr(S, D, Attr); |
| 5016 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5017 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5018 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 5019 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5020 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5021 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5022 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5023 | case AttributeList::AT_Destructor: |
| 5024 | handleDestructorAttr(S, D, Attr); |
| 5025 | break; |
| 5026 | case AttributeList::AT_EnableIf: |
| 5027 | handleEnableIfAttr(S, D, Attr); |
| 5028 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5029 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5030 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5031 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5032 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 5033 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5034 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5035 | case AttributeList::AT_OptimizeNone: |
| 5036 | handleOptimizeNoneAttr(S, D, Attr); |
| 5037 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 5038 | case AttributeList::AT_FlagEnum: |
| 5039 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 5040 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 5041 | case AttributeList::AT_Flatten: |
| 5042 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 5043 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5044 | case AttributeList::AT_Format: |
| 5045 | handleFormatAttr(S, D, Attr); |
| 5046 | break; |
| 5047 | case AttributeList::AT_FormatArg: |
| 5048 | handleFormatArgAttr(S, D, Attr); |
| 5049 | break; |
| 5050 | case AttributeList::AT_CUDAGlobal: |
| 5051 | handleGlobalAttr(S, D, Attr); |
| 5052 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 5053 | case AttributeList::AT_CUDADevice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5054 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); |
| 5055 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5056 | case AttributeList::AT_CUDAHost: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5057 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); |
| 5058 | break; |
| 5059 | case AttributeList::AT_GNUInline: |
| 5060 | handleGNUInlineAttr(S, D, Attr); |
| 5061 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5062 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5063 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 5064 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 5065 | case AttributeList::AT_Restrict: |
| 5066 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5067 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5068 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5069 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 5070 | break; |
| 5071 | case AttributeList::AT_Mode: |
| 5072 | handleModeAttr(S, D, Attr); |
| 5073 | break; |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 5074 | case AttributeList::AT_NoAlias: |
| 5075 | handleSimpleAttribute<NoAliasAttr>(S, D, Attr); |
| 5076 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5077 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5078 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 5079 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 5080 | case AttributeList::AT_NoSplitStack: |
| 5081 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 5082 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 5083 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5084 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 5085 | handleNonNullAttrParameter(S, PVD, Attr); |
| 5086 | else |
| 5087 | handleNonNullAttr(S, D, Attr); |
| 5088 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 5089 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5090 | handleReturnsNonNullAttr(S, D, Attr); |
| 5091 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 5092 | case AttributeList::AT_AssumeAligned: |
| 5093 | handleAssumeAlignedAttr(S, D, Attr); |
| 5094 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5095 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5096 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 5097 | break; |
| 5098 | case AttributeList::AT_Ownership: |
| 5099 | handleOwnershipAttr(S, D, Attr); |
| 5100 | break; |
| 5101 | case AttributeList::AT_Cold: |
| 5102 | handleColdAttr(S, D, Attr); |
| 5103 | break; |
| 5104 | case AttributeList::AT_Hot: |
| 5105 | handleHotAttr(S, D, Attr); |
| 5106 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5107 | case AttributeList::AT_Naked: |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 5108 | handleNakedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5109 | break; |
| 5110 | case AttributeList::AT_NoReturn: |
| 5111 | handleNoReturnAttr(S, D, Attr); |
| 5112 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 5113 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5114 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 5115 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5116 | case AttributeList::AT_CUDAShared: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5117 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); |
| 5118 | break; |
| 5119 | case AttributeList::AT_VecReturn: |
| 5120 | handleVecReturnAttr(S, D, Attr); |
| 5121 | break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5122 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5123 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5124 | handleObjCOwnershipAttr(S, D, Attr); |
| 5125 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5126 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5127 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 5128 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5129 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5130 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5131 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 5132 | break; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 5133 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5134 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5135 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 5136 | break; |
| 5137 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5138 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5139 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 5140 | break; |
| 5141 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5142 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5143 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 5144 | break; |
| 5145 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5146 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5147 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 5148 | break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 5149 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5150 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5151 | handleObjCDesignatedInitializer(S, D, Attr); |
| 5152 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5153 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5154 | case AttributeList::AT_ObjCRuntimeName: |
| 5155 | handleObjCRuntimeName(S, D, Attr); |
| 5156 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5157 | |
| 5158 | case AttributeList::AT_ObjCBoxable: |
| 5159 | handleObjCBoxable(S, D, Attr); |
| 5160 | break; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5161 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5162 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5163 | handleCFAuditedTransferAttr(S, D, Attr); |
| 5164 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5165 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5166 | handleCFUnknownTransferAttr(S, D, Attr); |
| 5167 | break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 5168 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5169 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5170 | case AttributeList::AT_NSConsumed: |
| 5171 | handleNSConsumedAttr(S, D, Attr); |
| 5172 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5173 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5174 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 5175 | break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 5176 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5177 | case AttributeList::AT_NSReturnsAutoreleased: |
| 5178 | case AttributeList::AT_NSReturnsNotRetained: |
| 5179 | case AttributeList::AT_CFReturnsNotRetained: |
| 5180 | case AttributeList::AT_NSReturnsRetained: |
| 5181 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5182 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 5183 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 5184 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5185 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 5186 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5187 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5188 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 5189 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 5190 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5191 | handleVecTypeHint(S, D, Attr); |
| 5192 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 5193 | |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5194 | case AttributeList::AT_InitPriority: |
| 5195 | handleInitPriorityAttr(S, D, Attr); |
| 5196 | break; |
| 5197 | |
| 5198 | case AttributeList::AT_Packed: |
| 5199 | handlePackedAttr(S, D, Attr); |
| 5200 | break; |
| 5201 | case AttributeList::AT_Section: |
| 5202 | handleSectionAttr(S, D, Attr); |
| 5203 | break; |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 5204 | case AttributeList::AT_Target: |
| 5205 | handleTargetAttr(S, D, Attr); |
| 5206 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5207 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 5208 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5209 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5210 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 5211 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 5212 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5213 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5214 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 5215 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 5216 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 5217 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 5218 | break; |
| 5219 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5220 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 5221 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 5222 | case AttributeList::AT_Unused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5223 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); |
| 5224 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5225 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5226 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 5227 | break; |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 5228 | case AttributeList::AT_NotTailCalled: |
| 5229 | handleNotTailCalledAttr(S, D, Attr); |
| 5230 | break; |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 5231 | case AttributeList::AT_DisableTailCalls: |
| 5232 | handleDisableTailCallsAttr(S, D, Attr); |
| 5233 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5234 | case AttributeList::AT_Used: |
| 5235 | handleUsedAttr(S, D, Attr); |
| 5236 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 5237 | case AttributeList::AT_Visibility: |
| 5238 | handleVisibilityAttr(S, D, Attr, false); |
| 5239 | break; |
| 5240 | case AttributeList::AT_TypeVisibility: |
| 5241 | handleVisibilityAttr(S, D, Attr, true); |
| 5242 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 5243 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5244 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 5245 | break; |
| 5246 | case AttributeList::AT_WarnUnusedResult: |
| 5247 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 5248 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 5249 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5250 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 5251 | break; |
| 5252 | case AttributeList::AT_WeakRef: |
| 5253 | handleWeakRefAttr(S, D, Attr); |
| 5254 | break; |
| 5255 | case AttributeList::AT_WeakImport: |
| 5256 | handleWeakImportAttr(S, D, Attr); |
| 5257 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5258 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5259 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5260 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5261 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5262 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 5263 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5264 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5265 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 5266 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5267 | case AttributeList::AT_ObjCNSObject: |
| 5268 | handleObjCNSObject(S, D, Attr); |
| 5269 | break; |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 5270 | case AttributeList::AT_ObjCIndependentClass: |
| 5271 | handleObjCIndependentClass(S, D, Attr); |
| 5272 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5273 | case AttributeList::AT_Blocks: |
| 5274 | handleBlocksAttr(S, D, Attr); |
| 5275 | break; |
| 5276 | case AttributeList::AT_Sentinel: |
| 5277 | handleSentinelAttr(S, D, Attr); |
| 5278 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 5279 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5280 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 5281 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5282 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5283 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 5284 | break; |
| 5285 | case AttributeList::AT_Cleanup: |
| 5286 | handleCleanupAttr(S, D, Attr); |
| 5287 | break; |
| 5288 | case AttributeList::AT_NoDebug: |
| 5289 | handleNoDebugAttr(S, D, Attr); |
| 5290 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 5291 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5292 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 5293 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5294 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5295 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 5296 | break; |
| 5297 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 5298 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 5299 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5300 | case AttributeList::AT_StdCall: |
| 5301 | case AttributeList::AT_CDecl: |
| 5302 | case AttributeList::AT_FastCall: |
| 5303 | case AttributeList::AT_ThisCall: |
| 5304 | case AttributeList::AT_Pascal: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5305 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 5306 | case AttributeList::AT_MSABI: |
| 5307 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5308 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 5309 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5310 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 5311 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5312 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5313 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 5314 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 5315 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5316 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); |
| 5317 | break; |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5318 | case AttributeList::AT_InternalLinkage: |
| 5319 | handleInternalLinkageAttr(S, D, Attr); |
| 5320 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 5321 | |
| 5322 | // Microsoft attributes: |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 5323 | case AttributeList::AT_MSNoVTable: |
| 5324 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 5325 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 5326 | case AttributeList::AT_MSStruct: |
| 5327 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 5328 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5329 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5330 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5331 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 5332 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5333 | handleMSInheritanceAttr(S, D, Attr); |
| 5334 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5335 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5336 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 5337 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5338 | case AttributeList::AT_Thread: |
| 5339 | handleDeclspecThreadAttr(S, D, Attr); |
| 5340 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5341 | |
| 5342 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 5343 | case AttributeList::AT_AssertExclusiveLock: |
| 5344 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 5345 | break; |
| 5346 | case AttributeList::AT_AssertSharedLock: |
| 5347 | handleAssertSharedLockAttr(S, D, Attr); |
| 5348 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5349 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5350 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 5351 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5352 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5353 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5354 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5355 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5356 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 5357 | break; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5358 | case AttributeList::AT_NoSanitize: |
| 5359 | handleNoSanitizeAttr(S, D, Attr); |
| 5360 | break; |
| 5361 | case AttributeList::AT_NoSanitizeSpecific: |
| 5362 | handleNoSanitizeSpecificAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 5363 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5364 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5365 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 5366 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5367 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5368 | handleGuardedByAttr(S, D, Attr); |
| 5369 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5370 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5371 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5372 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5373 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5374 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5375 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5376 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5377 | handleLockReturnedAttr(S, D, Attr); |
| 5378 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5379 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5380 | handleLocksExcludedAttr(S, D, Attr); |
| 5381 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5382 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5383 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5384 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5385 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5386 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5387 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5388 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5389 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5390 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5391 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5392 | // Capability analysis attributes. |
| 5393 | case AttributeList::AT_Capability: |
| 5394 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5395 | handleCapabilityAttr(S, D, Attr); |
| 5396 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5397 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5398 | handleRequiresCapabilityAttr(S, D, Attr); |
| 5399 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5400 | |
| 5401 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5402 | handleAssertCapabilityAttr(S, D, Attr); |
| 5403 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5404 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5405 | handleAcquireCapabilityAttr(S, D, Attr); |
| 5406 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5407 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5408 | handleReleaseCapabilityAttr(S, D, Attr); |
| 5409 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5410 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5411 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 5412 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5413 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 5414 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 5415 | case AttributeList::AT_Consumable: |
| 5416 | handleConsumableAttr(S, D, Attr); |
| 5417 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 5418 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5419 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 5420 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 5421 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5422 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 5423 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 5424 | case AttributeList::AT_CallableWhen: |
| 5425 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 5426 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 5427 | case AttributeList::AT_ParamTypestate: |
| 5428 | handleParamTypestateAttr(S, D, Attr); |
| 5429 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 5430 | case AttributeList::AT_ReturnTypestate: |
| 5431 | handleReturnTypestateAttr(S, D, Attr); |
| 5432 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 5433 | case AttributeList::AT_SetTypestate: |
| 5434 | handleSetTypestateAttr(S, D, Attr); |
| 5435 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 5436 | case AttributeList::AT_TestTypestate: |
| 5437 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 5438 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 5439 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5440 | // Type safety attributes. |
| 5441 | case AttributeList::AT_ArgumentWithTypeTag: |
| 5442 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 5443 | break; |
| 5444 | case AttributeList::AT_TypeTagForDatatype: |
| 5445 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 5446 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5451 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5452 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5453 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5454 | bool IncludeCXX11Attributes) { |
| 5455 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5456 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5457 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5458 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5459 | // GCC accepts |
| 5460 | // static int a9 __attribute__((weakref)); |
| 5461 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5462 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 5463 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 5464 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5465 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5466 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5467 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5468 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 5469 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 5470 | // good to have a way to specify "these attributes must appear as a group", |
| 5471 | // for these. Additionally, it would be good to have a way to specify "these |
| 5472 | // 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] | 5473 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 5474 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5475 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 5476 | // FIXME: This emits a different error message than |
| 5477 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5478 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5479 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 5480 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5481 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5482 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 5483 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5484 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5485 | D->setInvalidDecl(); |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 5486 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 5487 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5488 | << A << ExpectedKernelFunction; |
| 5489 | D->setInvalidDecl(); |
| 5490 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 5491 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5492 | << A << ExpectedKernelFunction; |
| 5493 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5494 | } |
| 5495 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5496 | } |
| 5497 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5498 | // Annotation attributes are the only attributes allowed after an access |
| 5499 | // specifier. |
| 5500 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5501 | const AttributeList *AttrList) { |
| 5502 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5503 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 5504 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5505 | } else { |
| 5506 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5507 | return true; |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | return false; |
| 5512 | } |
| 5513 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5514 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5515 | /// contains any decl attributes that we should warn about. |
| 5516 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5517 | for ( ; A; A = A->getNext()) { |
| 5518 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5519 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5520 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5521 | |
| 5522 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5523 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5524 | << A->getName() << A->getRange(); |
| 5525 | } else { |
| 5526 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5527 | << A->getName() << A->getRange(); |
| 5528 | } |
| 5529 | } |
| 5530 | } |
| 5531 | |
| 5532 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5533 | /// used to build a declaration, complain about any decl attributes |
| 5534 | /// which might be lying around on it. |
| 5535 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5536 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5537 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5538 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5539 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5540 | } |
| 5541 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5542 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5543 | /// \#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] | 5544 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5545 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5546 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5547 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5548 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5549 | // FIXME: Mangling? |
| 5550 | // FIXME: Is the qualifier info correct? |
| 5551 | // FIXME: Is the DeclContext correct? |
Alexander Musman | f97c893 | 2015-11-26 09:34:30 +0000 | [diff] [blame] | 5552 | |
| 5553 | LookupResult Previous(*this, II, Loc, LookupOrdinaryName); |
| 5554 | LookupParsedName(Previous, TUScope, nullptr, true); |
| 5555 | |
| 5556 | auto NewFD = FunctionDecl::Create( |
| 5557 | FD->getASTContext(), FD->getDeclContext(), Loc, Loc, |
| 5558 | DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None, |
| 5559 | false /*isInlineSpecified*/, FD->hasPrototype(), |
| 5560 | false /*isConstexprSpecified*/); |
| 5561 | |
| 5562 | CheckFunctionDeclaration(TUScope, NewFD, Previous, |
| 5563 | false /*IsExplicitSpecialization*/); |
| 5564 | |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5565 | NewD = NewFD; |
| 5566 | |
| 5567 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5568 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5569 | |
| 5570 | // Fake up parameter variables; they are declared as if this were |
| 5571 | // a typedef. |
| 5572 | QualType FDTy = FD->getType(); |
| 5573 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5574 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5575 | for (const auto &AI : FT->param_types()) { |
| 5576 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5577 | Param->setScopeInfo(0, Params.size()); |
| 5578 | Params.push_back(Param); |
| 5579 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5580 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5581 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5582 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5583 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5584 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5585 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5586 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5587 | if (VD->getQualifier()) { |
| 5588 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5589 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5590 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5591 | } |
| 5592 | return NewD; |
| 5593 | } |
| 5594 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5595 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5596 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5597 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5598 | if (W.getUsed()) return; // only do this once |
| 5599 | W.setUsed(true); |
| 5600 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5601 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5602 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5603 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 5604 | W.getLocation())); |
| 5605 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5606 | WeakTopLevelDecl.push_back(NewD); |
| 5607 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5608 | // to insert Decl at TU scope, sorry. |
| 5609 | DeclContext *SavedContext = CurContext; |
| 5610 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 5611 | NewD->setDeclContext(CurContext); |
| 5612 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5613 | PushOnScopeChains(NewD, S); |
| 5614 | CurContext = SavedContext; |
| 5615 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5616 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5617 | } |
| 5618 | } |
| 5619 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5620 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5621 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5622 | // have to do this. |
| 5623 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5624 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5625 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5626 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5627 | if (VD->isExternC()) |
| 5628 | ND = VD; |
| 5629 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5630 | if (FD->isExternC()) |
| 5631 | ND = FD; |
| 5632 | if (ND) { |
| 5633 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 5634 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5635 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5636 | WeakInfo W = I->second; |
| 5637 | DeclApplyPragmaWeak(S, ND, W); |
| 5638 | WeakUndeclaredIdentifiers[Id] = W; |
| 5639 | } |
| 5640 | } |
| 5641 | } |
| 5642 | } |
| 5643 | } |
| 5644 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5645 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5646 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5647 | /// 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] | 5648 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5649 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5650 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5651 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5652 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5653 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5654 | // position to the decl itself. This handles cases like: |
| 5655 | // int *__attr__(x)** D; |
| 5656 | // when X is a decl attribute. |
| 5657 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5658 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5659 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5660 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5661 | // Finally, apply any attributes on the decl itself. |
| 5662 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5663 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5664 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5665 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5666 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5667 | /// If so, it'll still be annotated with an attribute that makes it |
| 5668 | /// illegal to actually use. |
| 5669 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl, |
| 5670 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 5671 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5672 | // Private ivars are always okay. Unfortunately, people don't |
| 5673 | // always properly make their ivars private, even in system headers. |
| 5674 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5675 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5676 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5677 | return false; |
| 5678 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 5679 | // Silently accept unsupported uses of __weak in both user and system |
| 5680 | // declarations when it's been disabled, for ease of integration with |
| 5681 | // -fno-objc-arc files. We do have to take some care against attempts |
| 5682 | // to define such things; for now, we've only done that for ivars |
| 5683 | // and properties. |
| 5684 | if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) { |
| 5685 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 5686 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 5687 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 5688 | return true; |
| 5689 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 5692 | // Allow all sorts of things in system headers. |
| 5693 | if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) { |
| 5694 | // Currently, all the failures dealt with this way are due to ARC |
| 5695 | // restrictions. |
| 5696 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 5697 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5701 | } |
| 5702 | |
| 5703 | /// Handle a delayed forbidden-type diagnostic. |
| 5704 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5705 | Decl *decl) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 5706 | auto reason = UnavailableAttr::IR_None; |
| 5707 | if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) { |
| 5708 | assert(reason && "didn't set reason?"); |
| 5709 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason, |
| 5710 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5711 | return; |
| 5712 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5713 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5714 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5715 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5716 | // kind of forbidden type messages on unavailable functions. |
| 5717 | if (FD->hasAttr<UnavailableAttr>() && |
| 5718 | diag.getForbiddenTypeDiagnostic() == |
| 5719 | diag::err_arc_array_param_no_ownership) { |
| 5720 | diag.Triggered = true; |
| 5721 | return; |
| 5722 | } |
| 5723 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5724 | |
| 5725 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5726 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5727 | diag.Triggered = true; |
| 5728 | } |
| 5729 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5730 | |
| 5731 | static bool isDeclDeprecated(Decl *D) { |
| 5732 | do { |
| 5733 | if (D->isDeprecated()) |
| 5734 | return true; |
| 5735 | // A category implicitly has the availability of the interface. |
| 5736 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5737 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 5738 | return Interface->isDeprecated(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5739 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5740 | return false; |
| 5741 | } |
| 5742 | |
| 5743 | static bool isDeclUnavailable(Decl *D) { |
| 5744 | do { |
| 5745 | if (D->isUnavailable()) |
| 5746 | return true; |
| 5747 | // A category implicitly has the availability of the interface. |
| 5748 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5749 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 5750 | return Interface->isUnavailable(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5751 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5752 | return false; |
| 5753 | } |
| 5754 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5755 | static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5756 | Decl *Ctx, const NamedDecl *D, |
| 5757 | StringRef Message, SourceLocation Loc, |
| 5758 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5759 | const ObjCPropertyDecl *ObjCProperty, |
| 5760 | bool ObjCPropertyAccess) { |
| 5761 | // Diagnostics for deprecated or unavailable. |
| 5762 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5763 | unsigned diag_available_here = diag::note_availability_specified_here; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5764 | |
| 5765 | // Matches 'diag::note_property_attribute' options. |
| 5766 | unsigned property_note_select; |
| 5767 | |
| 5768 | // Matches diag::note_availability_specified_here. |
| 5769 | unsigned available_here_select_kind; |
| 5770 | |
| 5771 | // Don't warn if our current context is deprecated or unavailable. |
| 5772 | switch (K) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5773 | case Sema::AD_Deprecation: |
Jordan Rose | d17c03e | 2015-04-30 17:20:35 +0000 | [diff] [blame] | 5774 | if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx)) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5775 | return; |
| 5776 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 5777 | : diag::warn_property_method_deprecated; |
| 5778 | diag_message = diag::warn_deprecated_message; |
| 5779 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 5780 | property_note_select = /* deprecated */ 0; |
| 5781 | available_here_select_kind = /* deprecated */ 2; |
| 5782 | break; |
| 5783 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5784 | case Sema::AD_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5785 | if (isDeclUnavailable(Ctx)) |
| 5786 | return; |
| 5787 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 5788 | : diag::err_property_method_unavailable; |
| 5789 | diag_message = diag::err_unavailable_message; |
| 5790 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 5791 | property_note_select = /* unavailable */ 1; |
| 5792 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5793 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 5794 | if (auto attr = D->getAttr<UnavailableAttr>()) { |
| 5795 | if (attr->isImplicit() && attr->getImplicitReason()) { |
| 5796 | // Most of these failures are due to extra restrictions in ARC; |
| 5797 | // reflect that in the primary diagnostic when applicable. |
| 5798 | auto flagARCError = [&] { |
| 5799 | if (S.getLangOpts().ObjCAutoRefCount && |
| 5800 | S.getSourceManager().isInSystemHeader(D->getLocation())) |
| 5801 | diag = diag::err_unavailable_in_arc; |
| 5802 | }; |
| 5803 | |
| 5804 | switch (attr->getImplicitReason()) { |
| 5805 | case UnavailableAttr::IR_None: break; |
| 5806 | |
| 5807 | case UnavailableAttr::IR_ARCForbiddenType: |
| 5808 | flagARCError(); |
| 5809 | diag_available_here = diag::note_arc_forbidden_type; |
| 5810 | break; |
| 5811 | |
| 5812 | case UnavailableAttr::IR_ForbiddenWeak: |
| 5813 | if (S.getLangOpts().ObjCWeakRuntime) |
| 5814 | diag_available_here = diag::note_arc_weak_disabled; |
| 5815 | else |
| 5816 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 5817 | break; |
| 5818 | |
| 5819 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 5820 | flagARCError(); |
| 5821 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 5822 | break; |
| 5823 | |
| 5824 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 5825 | flagARCError(); |
| 5826 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 5827 | break; |
| 5828 | |
| 5829 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 5830 | flagARCError(); |
| 5831 | diag_available_here = diag::note_arc_field_with_ownership; |
| 5832 | break; |
| 5833 | } |
| 5834 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5835 | } |
| 5836 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5837 | break; |
| 5838 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5839 | case Sema::AD_Partial: |
| 5840 | diag = diag::warn_partial_availability; |
| 5841 | diag_message = diag::warn_partial_message; |
| 5842 | diag_fwdclass_message = diag::warn_partial_fwdclass_message; |
| 5843 | property_note_select = /* partial */ 2; |
| 5844 | available_here_select_kind = /* partial */ 3; |
| 5845 | break; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5848 | if (!Message.empty()) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5849 | S.Diag(Loc, diag_message) << D << Message; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5850 | if (ObjCProperty) |
| 5851 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 5852 | << ObjCProperty->getDeclName() << property_note_select; |
| 5853 | } else if (!UnknownObjCClass) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5854 | S.Diag(Loc, diag) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5855 | if (ObjCProperty) |
| 5856 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 5857 | << ObjCProperty->getDeclName() << property_note_select; |
| 5858 | } else { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5859 | S.Diag(Loc, diag_fwdclass_message) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5860 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5861 | } |
| 5862 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5863 | S.Diag(D->getLocation(), diag_available_here) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5864 | << D << available_here_select_kind; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5865 | if (K == Sema::AD_Partial) |
| 5866 | S.Diag(Loc, diag::note_partial_availability_silence) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 5870 | Decl *Ctx) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5871 | assert(DD.Kind == DelayedDiagnostic::Deprecation || |
| 5872 | DD.Kind == DelayedDiagnostic::Unavailable); |
| 5873 | Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation |
| 5874 | ? Sema::AD_Deprecation |
| 5875 | : Sema::AD_Unavailable; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5876 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5877 | DoEmitAvailabilityWarning( |
| 5878 | S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc, |
| 5879 | DD.getUnknownObjCClass(), DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5882 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5883 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5884 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5885 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5886 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5887 | // When delaying diagnostics to run in the context of a parsed |
| 5888 | // declaration, we only want to actually emit anything if parsing |
| 5889 | // succeeds. |
| 5890 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5891 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5892 | // We emit all the active diagnostics in this pool or any of its |
| 5893 | // parents. In general, we'll get one pool for the decl spec |
| 5894 | // and a child pool for each declarator; in a decl group like: |
| 5895 | // deprecated_typedef foo, *bar, baz(); |
| 5896 | // only the declarator pops will be passed decls. This is correct; |
| 5897 | // we really do need to consider delayed diagnostics from the decl spec |
| 5898 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5899 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5900 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5901 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5902 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5903 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5904 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5905 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5906 | continue; |
| 5907 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5908 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5909 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5910 | case DelayedDiagnostic::Unavailable: |
| 5911 | // Don't bother giving deprecation/unavailable diagnostics if |
| 5912 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5913 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5914 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5915 | break; |
| 5916 | |
| 5917 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5918 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5919 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5920 | |
| 5921 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5922 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5923 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5924 | } |
| 5925 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5926 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5927 | } |
| 5928 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5929 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5930 | /// been delayed in the current context instead of in the given pool. |
| 5931 | /// Essentially, this just moves them to the current pool. |
| 5932 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5933 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5934 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5935 | curPool->steal(pool); |
| 5936 | } |
| 5937 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5938 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 5939 | NamedDecl *D, StringRef Message, |
| 5940 | SourceLocation Loc, |
| 5941 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame] | 5942 | const ObjCPropertyDecl *ObjCProperty, |
| 5943 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5944 | // Delay if we're currently parsing a declaration. |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5945 | if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) { |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 5946 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability( |
| 5947 | AD, Loc, D, UnknownObjCClass, ObjCProperty, Message, |
| 5948 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5949 | return; |
| 5950 | } |
| 5951 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5952 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5953 | DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass, |
| 5954 | ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5955 | } |