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> |
| 247 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 248 | const AttributeList &Attr) { |
| 249 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 250 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 251 | << Attr.getName() << A; |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 252 | return true; |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 257 | /// \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] | 258 | /// instance method D. May output an error. |
| 259 | /// |
| 260 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 261 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 262 | const AttributeList &Attr, |
| 263 | unsigned AttrArgNum, |
| 264 | const Expr *IdxExpr, |
| 265 | uint64_t &Idx) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 266 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 267 | |
| 268 | // In C++ the implicit 'this' function parameter also counts. |
| 269 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 270 | bool HP = hasFunctionProto(D); |
| 271 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 272 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 273 | unsigned NumParams = |
| 274 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 275 | |
| 276 | llvm::APSInt IdxInt; |
| 277 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 278 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 279 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 280 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 281 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | |
| 285 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 286 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 287 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 288 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | Idx--; // Convert to zero-based. |
| 292 | if (HasImplicitThisParam) { |
| 293 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 294 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 295 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 296 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | --Idx; |
| 300 | } |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 305 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 306 | /// If not emit an error and return false. If the argument is an identifier it |
| 307 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 308 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 309 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 310 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 311 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 312 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 313 | if (Attr.isArgIdent(ArgNum)) { |
| 314 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 315 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 316 | << Attr.getName() << AANT_ArgumentString |
| 317 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 318 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 319 | Str = Loc->Ident->getName(); |
| 320 | if (ArgLocation) |
| 321 | *ArgLocation = Loc->Loc; |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | // Now check for an actual string literal. |
| 326 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 327 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 328 | if (ArgLocation) |
| 329 | *ArgLocation = ArgExpr->getLocStart(); |
| 330 | |
| 331 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 332 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 333 | << Attr.getName() << AANT_ArgumentString; |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | Str = Literal->getString(); |
| 338 | return true; |
| 339 | } |
| 340 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 341 | /// \brief Applies the given attribute to the Decl without performing any |
| 342 | /// additional semantic checking. |
| 343 | template <typename AttrType> |
| 344 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 345 | const AttributeList &Attr) { |
| 346 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 347 | Attr.getAttributeSpellingListIndex())); |
| 348 | } |
| 349 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 350 | /// \brief Check if the passed-in expression is of type int or bool. |
| 351 | static bool isIntOrBool(Expr *Exp) { |
| 352 | QualType QT = Exp->getType(); |
| 353 | return QT->isBooleanType() || QT->isIntegerType(); |
| 354 | } |
| 355 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 356 | |
| 357 | // Check to see if the type is a smart pointer of some kind. We assume |
| 358 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 359 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 360 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 361 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 362 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 363 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 364 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 365 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 366 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 367 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 368 | return false; |
| 369 | |
| 370 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 373 | /// \brief Check if passed in Decl is a pointer type. |
| 374 | /// Note that this function may produce an error message. |
| 375 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 376 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 377 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 378 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 379 | QualType QT = vd->getType(); |
| 380 | if (QT->isAnyPointerType()) |
| 381 | return true; |
| 382 | |
| 383 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 384 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 385 | // (We don't want to force template instantiation if we can avoid it, |
| 386 | // since that would alter the order in which templates are instantiated.) |
| 387 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 388 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 389 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 390 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 391 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 392 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 393 | |
| 394 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 395 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 399 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 400 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 401 | static const RecordType *getRecordType(QualType QT) { |
| 402 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 403 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 404 | |
| 405 | // Now check if we point to record type. |
| 406 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 407 | return PT->getPointeeType()->getAs<RecordType>(); |
| 408 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 409 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 412 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 413 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 414 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 415 | if (!RT) |
| 416 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 417 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 418 | // 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] | 419 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 420 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 421 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 422 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 423 | // FIXME -- Check the type that the smart pointer points to. |
| 424 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 425 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 426 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 427 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 428 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 429 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 430 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 431 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 432 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 433 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 434 | CXXBasePaths BPaths(false, false); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 435 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 436 | const auto *Type = BS->getType()->getAs<RecordType>(); |
| 437 | return Type->getDecl()->hasAttr<CapabilityAttr>(); |
| 438 | }, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 439 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 440 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 444 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 445 | const auto *TD = Ty->getAs<TypedefType>(); |
| 446 | if (!TD) |
| 447 | return false; |
| 448 | |
| 449 | TypedefNameDecl *TN = TD->getDecl(); |
| 450 | if (!TN) |
| 451 | return false; |
| 452 | |
| 453 | return TN->hasAttr<CapabilityAttr>(); |
| 454 | } |
| 455 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 456 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 457 | if (checkTypedefTypeForCapability(Ty)) |
| 458 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 459 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 460 | if (checkRecordTypeForCapability(S, Ty)) |
| 461 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 462 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 463 | return false; |
| 464 | } |
| 465 | |
| 466 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 467 | // Capability expressions are simple expressions involving the boolean logic |
| 468 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 469 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 470 | // is a capability or not. |
| 471 | |
| 472 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 473 | return typeHasCapability(S, E->getType()); |
| 474 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 475 | return isCapabilityExpr(S, E->getSubExpr()); |
| 476 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 477 | return isCapabilityExpr(S, E->getSubExpr()); |
| 478 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 479 | if (E->getOpcode() == UO_LNot) |
| 480 | return isCapabilityExpr(S, E->getSubExpr()); |
| 481 | return false; |
| 482 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 483 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 484 | return isCapabilityExpr(S, E->getLHS()) && |
| 485 | isCapabilityExpr(S, E->getRHS()); |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 492 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 493 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 494 | /// \param Sidx The attribute argument index to start checking with. |
| 495 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 496 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 497 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 498 | const AttributeList &Attr, |
| 499 | SmallVectorImpl<Expr *> &Args, |
| 500 | int Sidx = 0, |
| 501 | bool ParamIdxOk = false) { |
| 502 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 503 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 504 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 505 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 506 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 507 | Args.push_back(ArgExp); |
| 508 | continue; |
| 509 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 510 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 511 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 512 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 513 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 514 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 515 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 516 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 517 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 518 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 519 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 520 | // We allow constant strings to be used as a placeholder for expressions |
| 521 | // that are not valid C++ syntax, but warn that they are ignored. |
| 522 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 523 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 524 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 525 | continue; |
| 526 | } |
| 527 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 528 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 529 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 530 | // A pointer to member expression of the form &MyClass::mu is treated |
| 531 | // specially -- we need to look at the type of the member. |
| 532 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 533 | if (UOp->getOpcode() == UO_AddrOf) |
| 534 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 535 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 536 | ArgTy = DRE->getDecl()->getType(); |
| 537 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 538 | // 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] | 539 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 540 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 541 | // Now check if we index into a record type function param. |
| 542 | if(!RT && ParamIdxOk) { |
| 543 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 544 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 545 | if(FD && IL) { |
| 546 | unsigned int NumParams = FD->getNumParams(); |
| 547 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 548 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 549 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 550 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 551 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 552 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 553 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 554 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 555 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 559 | // If the type does not have a capability, see if the components of the |
| 560 | // expression have capabilities. This allows for writing C code where the |
| 561 | // capability may be on the type, and the expression is a capability |
| 562 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 563 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 564 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 565 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 566 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 567 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 568 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 571 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 572 | // Attribute Implementations |
| 573 | //===----------------------------------------------------------------------===// |
| 574 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 575 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 576 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 577 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 578 | return; |
| 579 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 580 | D->addAttr(::new (S.Context) |
| 581 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 582 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 585 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 586 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 587 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 588 | SmallVector<Expr*, 1> Args; |
| 589 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 590 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 591 | unsigned Size = Args.size(); |
| 592 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 593 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 594 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 596 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 597 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 600 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 601 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 602 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 603 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 604 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 605 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 606 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 609 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 610 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 611 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 612 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 613 | return; |
| 614 | |
| 615 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 616 | return; |
| 617 | |
| 618 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 619 | S.Context, Arg, |
| 620 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 623 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 624 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 625 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 626 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 627 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 628 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 629 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 630 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 631 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
| 632 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
| 633 | << Attr.getName(); |
| 634 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 635 | } |
| 636 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 637 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 638 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 639 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 640 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 641 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 642 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 645 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 646 | const AttributeList &Attr) { |
| 647 | SmallVector<Expr*, 1> Args; |
| 648 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 649 | return; |
| 650 | |
| 651 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 652 | D->addAttr(::new (S.Context) |
| 653 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 654 | StartArg, Args.size(), |
| 655 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 658 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 659 | const AttributeList &Attr) { |
| 660 | SmallVector<Expr*, 1> Args; |
| 661 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 662 | return; |
| 663 | |
| 664 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 665 | D->addAttr(::new (S.Context) |
| 666 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 667 | StartArg, Args.size(), |
| 668 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 671 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 672 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 673 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 674 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 675 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 676 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 677 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 678 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 679 | } |
| 680 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 681 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 682 | const AttributeList &Attr) { |
| 683 | SmallVector<Expr*, 1> Args; |
| 684 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 685 | return; |
| 686 | |
| 687 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 688 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 689 | D->addAttr(::new (S.Context) |
| 690 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 691 | Attr.getAttributeSpellingListIndex())); |
| 692 | } |
| 693 | |
| 694 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 695 | const AttributeList &Attr) { |
| 696 | SmallVector<Expr*, 1> Args; |
| 697 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 698 | return; |
| 699 | |
| 700 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 701 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 702 | D->addAttr(::new (S.Context) |
| 703 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 704 | StartArg, Size, |
| 705 | Attr.getAttributeSpellingListIndex())); |
| 706 | } |
| 707 | |
| 708 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 709 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 710 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 711 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 712 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 713 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 714 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 715 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 716 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 717 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 718 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 722 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 723 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 724 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 727 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 728 | const AttributeList &Attr) { |
| 729 | SmallVector<Expr*, 2> Args; |
| 730 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 731 | return; |
| 732 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 733 | D->addAttr(::new (S.Context) |
| 734 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 735 | Attr.getArgAsExpr(0), |
| 736 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 737 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 740 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | const AttributeList &Attr) { |
| 742 | SmallVector<Expr*, 2> Args; |
| 743 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 744 | return; |
| 745 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 746 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 747 | Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(), |
| 748 | Args.size(), Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 751 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 752 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 753 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 754 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 755 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 756 | unsigned Size = Args.size(); |
| 757 | if (Size == 0) |
| 758 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 759 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 760 | D->addAttr(::new (S.Context) |
| 761 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 762 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 766 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 767 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 768 | return; |
| 769 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 770 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 771 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 772 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 773 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 774 | if (Size == 0) |
| 775 | return; |
| 776 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 777 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 778 | D->addAttr(::new (S.Context) |
| 779 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 780 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 783 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 784 | Expr *Cond = Attr.getArgAsExpr(0); |
| 785 | if (!Cond->isTypeDependent()) { |
| 786 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 787 | if (Converted.isInvalid()) |
| 788 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 789 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | StringRef Msg; |
| 793 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 794 | return; |
| 795 | |
| 796 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 797 | if (!Cond->isValueDependent() && |
| 798 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 799 | Diags)) { |
| 800 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 801 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 802 | S.Diag(Diags[I].first, Diags[I].second); |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | D->addAttr(::new (S.Context) |
| 807 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 808 | Attr.getAttributeSpellingListIndex())); |
| 809 | } |
| 810 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 811 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 812 | ConsumableAttr::ConsumedState DefaultState; |
| 813 | |
| 814 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 815 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 816 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 817 | DefaultState)) { |
| 818 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 819 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 820 | return; |
| 821 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 822 | } else { |
| 823 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 824 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 825 | return; |
| 826 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 827 | |
| 828 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 829 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 830 | Attr.getAttributeSpellingListIndex())); |
| 831 | } |
| 832 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 833 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 834 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 835 | const AttributeList &Attr) { |
| 836 | ASTContext &CurrContext = S.getASTContext(); |
| 837 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 838 | |
| 839 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 840 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 841 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 842 | RD->getNameAsString(); |
| 843 | |
| 844 | return false; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | return true; |
| 849 | } |
| 850 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 851 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 852 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 853 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 854 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 855 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 856 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 857 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 858 | return; |
| 859 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 860 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 861 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 862 | CallableWhenAttr::ConsumedState CallableState; |
| 863 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 864 | StringRef StateString; |
| 865 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 866 | if (Attr.isArgIdent(ArgIndex)) { |
| 867 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 868 | StateString = Ident->Ident->getName(); |
| 869 | Loc = Ident->Loc; |
| 870 | } else { |
| 871 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 872 | return; |
| 873 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 874 | |
| 875 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 876 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 877 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 878 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 879 | return; |
| 880 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 881 | |
| 882 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 883 | } |
| 884 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 885 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 886 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 887 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 888 | } |
| 889 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 890 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 891 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 892 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 893 | ParamTypestateAttr::ConsumedState ParamState; |
| 894 | |
| 895 | if (Attr.isArgIdent(0)) { |
| 896 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 897 | StringRef StateString = Ident->Ident->getName(); |
| 898 | |
| 899 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 900 | ParamState)) { |
| 901 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 902 | << Attr.getName() << StateString; |
| 903 | return; |
| 904 | } |
| 905 | } else { |
| 906 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 907 | Attr.getName() << AANT_ArgumentIdentifier; |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | // FIXME: This check is currently being done in the analysis. It can be |
| 912 | // enabled here only after the parser propagates attributes at |
| 913 | // template specialization definition, not declaration. |
| 914 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 915 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 916 | // |
| 917 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 918 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 919 | // ReturnType.getAsString(); |
| 920 | // return; |
| 921 | //} |
| 922 | |
| 923 | D->addAttr(::new (S.Context) |
| 924 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 925 | Attr.getAttributeSpellingListIndex())); |
| 926 | } |
| 927 | |
| 928 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 929 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 930 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 931 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 932 | |
| 933 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 934 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 935 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 936 | ReturnState)) { |
| 937 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 938 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 939 | return; |
| 940 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 941 | } else { |
| 942 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 943 | Attr.getName() << AANT_ArgumentIdentifier; |
| 944 | return; |
| 945 | } |
| 946 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 947 | // FIXME: This check is currently being done in the analysis. It can be |
| 948 | // enabled here only after the parser propagates attributes at |
| 949 | // template specialization definition, not declaration. |
| 950 | //QualType ReturnType; |
| 951 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 952 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 953 | // ReturnType = Param->getType(); |
| 954 | // |
| 955 | //} else if (const CXXConstructorDecl *Constructor = |
| 956 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 957 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 958 | // |
| 959 | //} else { |
| 960 | // |
| 961 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 962 | //} |
| 963 | // |
| 964 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 965 | // |
| 966 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 967 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 968 | // ReturnType.getAsString(); |
| 969 | // return; |
| 970 | //} |
| 971 | |
| 972 | D->addAttr(::new (S.Context) |
| 973 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 974 | Attr.getAttributeSpellingListIndex())); |
| 975 | } |
| 976 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 977 | |
| 978 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 979 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 980 | return; |
| 981 | |
| 982 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 983 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 984 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 985 | StringRef Param = Ident->Ident->getName(); |
| 986 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 987 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 988 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 991 | } else { |
| 992 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 993 | Attr.getName() << AANT_ArgumentIdentifier; |
| 994 | return; |
| 995 | } |
| 996 | |
| 997 | D->addAttr(::new (S.Context) |
| 998 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 999 | Attr.getAttributeSpellingListIndex())); |
| 1000 | } |
| 1001 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1002 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1003 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1004 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1005 | return; |
| 1006 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1007 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1008 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1009 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1010 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1011 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1012 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1013 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1014 | return; |
| 1015 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1016 | } else { |
| 1017 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1018 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1023 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1024 | Attr.getAttributeSpellingListIndex())); |
| 1025 | } |
| 1026 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1027 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1028 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1029 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1030 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1033 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1034 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1035 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1036 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1037 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1038 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1039 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1040 | if (!FD->getType()->isDependentType() && |
| 1041 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1042 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1043 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1044 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1045 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1046 | FD->addAttr(::new (S.Context) |
| 1047 | PackedAttr(Attr.getRange(), S.Context, |
| 1048 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1049 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1050 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1053 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1054 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1055 | // variables or properties of Objective-C classes. The outlet must also |
| 1056 | // have an object reference type. |
| 1057 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1058 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1059 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1060 | << Attr.getName() << VD->getType() << 0; |
| 1061 | return false; |
| 1062 | } |
| 1063 | } |
| 1064 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1065 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1066 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1067 | << Attr.getName() << PD->getType() << 1; |
| 1068 | return false; |
| 1069 | } |
| 1070 | } |
| 1071 | else { |
| 1072 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1073 | return false; |
| 1074 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1075 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
| 1078 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1079 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1080 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1081 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1082 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1083 | D->addAttr(::new (S.Context) |
| 1084 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1085 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1088 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1089 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1090 | |
| 1091 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1092 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1093 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1094 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1095 | return; |
| 1096 | } |
| 1097 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1098 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1099 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1100 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1101 | ParsedType PT; |
| 1102 | |
| 1103 | if (Attr.hasParsedType()) |
| 1104 | PT = Attr.getTypeArg(); |
| 1105 | else { |
| 1106 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1107 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1108 | if (!PT) { |
| 1109 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1110 | return; |
| 1111 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1112 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1113 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1114 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1115 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1116 | if (!QTLoc) |
| 1117 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1118 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1119 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1120 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1121 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1122 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1123 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1124 | S.Diag(Attr.getLoc(), |
| 1125 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1126 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1127 | return; |
| 1128 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1129 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1130 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1131 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1132 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1135 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1136 | if (RefOkay) { |
| 1137 | if (T->isReferenceType()) |
| 1138 | return true; |
| 1139 | } else { |
| 1140 | T = T.getNonReferenceType(); |
| 1141 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1142 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1143 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1144 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1145 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1146 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1147 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1148 | for (const auto *I : UD->fields()) { |
| 1149 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1150 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1151 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1159 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1160 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1161 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1162 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1163 | if (!S.isValidPointerAttrType(T)) { |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1164 | S.Diag(Attr.getLoc(), isReturnValue |
| 1165 | ? diag::warn_attribute_return_pointers_only |
| 1166 | : diag::warn_attribute_pointers_only) |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1167 | << Attr.getName() << AttrParmRange << TypeRange; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1168 | return false; |
| 1169 | } |
| 1170 | return true; |
| 1171 | } |
| 1172 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1173 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1174 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1175 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1176 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1177 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1178 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1179 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1182 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1183 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1184 | Ex->getSourceRange(), |
| 1185 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1186 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1187 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1188 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1189 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1190 | |
| 1191 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1192 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1193 | // check if the attribute came from a macro expansion or a template |
| 1194 | // instantiation. |
| 1195 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
| 1196 | S.ActiveTemplateInstantiations.empty()) { |
| 1197 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1198 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1199 | I != E && !AnyPointers; ++I) { |
| 1200 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1201 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1202 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1203 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1204 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1205 | if (!AnyPointers) |
| 1206 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1207 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1208 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1209 | unsigned *Start = NonNullArgs.data(); |
| 1210 | unsigned Size = NonNullArgs.size(); |
| 1211 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1212 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1213 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1214 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1217 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1218 | const AttributeList &Attr) { |
| 1219 | if (Attr.getNumArgs() > 0) { |
| 1220 | if (D->getFunctionType()) { |
| 1221 | handleNonNullAttr(S, D, Attr); |
| 1222 | } else { |
| 1223 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1224 | << D->getSourceRange(); |
| 1225 | } |
| 1226 | return; |
| 1227 | } |
| 1228 | |
| 1229 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1230 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1231 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1232 | return; |
| 1233 | |
| 1234 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1235 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1236 | Attr.getAttributeSpellingListIndex())); |
| 1237 | } |
| 1238 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1239 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1240 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1241 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1242 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1243 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1244 | /* isReturnValue */ true)) |
| 1245 | return; |
| 1246 | |
| 1247 | D->addAttr(::new (S.Context) |
| 1248 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1249 | Attr.getAttributeSpellingListIndex())); |
| 1250 | } |
| 1251 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1252 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1253 | const AttributeList &Attr) { |
| 1254 | Expr *E = Attr.getArgAsExpr(0), |
| 1255 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1256 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1257 | Attr.getAttributeSpellingListIndex()); |
| 1258 | } |
| 1259 | |
| 1260 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1261 | Expr *OE, unsigned SpellingListIndex) { |
| 1262 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1263 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1264 | |
| 1265 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1266 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1267 | |
| 1268 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1269 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1270 | << &TmpAttr << AttrRange << SR; |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | if (!E->isValueDependent()) { |
| 1275 | llvm::APSInt I(64); |
| 1276 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1277 | if (OE) |
| 1278 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1279 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1280 | << E->getSourceRange(); |
| 1281 | else |
| 1282 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1283 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1284 | << E->getSourceRange(); |
| 1285 | return; |
| 1286 | } |
| 1287 | |
| 1288 | if (!I.isPowerOf2()) { |
| 1289 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1290 | << E->getSourceRange(); |
| 1291 | return; |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | if (OE) { |
| 1296 | if (!OE->isValueDependent()) { |
| 1297 | llvm::APSInt I(64); |
| 1298 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1299 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1300 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1301 | << OE->getSourceRange(); |
| 1302 | return; |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | D->addAttr(::new (Context) |
| 1308 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1309 | } |
| 1310 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1311 | /// Normalize the attribute, __foo__ becomes foo. |
| 1312 | /// Returns true if normalization was applied. |
| 1313 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1314 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1315 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1316 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1317 | return true; |
| 1318 | } |
| 1319 | return false; |
| 1320 | } |
| 1321 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1322 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1323 | // This attribute must be applied to a function declaration. The first |
| 1324 | // argument to the attribute must be an identifier, the name of the resource, |
| 1325 | // for example: malloc. The following arguments must be argument indexes, the |
| 1326 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1327 | // 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] | 1328 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1329 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1330 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1331 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1332 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1333 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1334 | return; |
| 1335 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1336 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1337 | // Figure out our Kind. |
| 1338 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1339 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1340 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1341 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1342 | // Check arguments. |
| 1343 | switch (K) { |
| 1344 | case OwnershipAttr::Takes: |
| 1345 | case OwnershipAttr::Holds: |
| 1346 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1347 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1348 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1349 | return; |
| 1350 | } |
| 1351 | break; |
| 1352 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1353 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1354 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1355 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1356 | return; |
| 1357 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1358 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1361 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1362 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1363 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1364 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1365 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1366 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1367 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1368 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1369 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1370 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1371 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1372 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1373 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1374 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1375 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1376 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1377 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1378 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1379 | case OwnershipAttr::Takes: |
| 1380 | case OwnershipAttr::Holds: |
| 1381 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1382 | Err = 0; |
| 1383 | break; |
| 1384 | case OwnershipAttr::Returns: |
| 1385 | if (!T->isIntegerType()) |
| 1386 | Err = 1; |
| 1387 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1388 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1389 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1390 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1391 | << Ex->getSourceRange(); |
| 1392 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1393 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1394 | |
| 1395 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1396 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1397 | // Cannot have two ownership attributes of different kinds for the same |
| 1398 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1399 | if (I->getOwnKind() != K && I->args_end() != |
| 1400 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1401 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1402 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1403 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1404 | } else if (K == OwnershipAttr::Returns && |
| 1405 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1406 | // A returns attribute conflicts with any other returns attribute using |
| 1407 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1408 | // argument indexes are 0-based. |
| 1409 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1410 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1411 | << *(I->args_begin()) + 1; |
| 1412 | if (I->args_size()) |
| 1413 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1414 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1415 | return; |
| 1416 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1419 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | unsigned* start = OwnershipArgs.data(); |
| 1423 | unsigned size = OwnershipArgs.size(); |
| 1424 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1425 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1426 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1427 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1428 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1431 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1432 | // Check the attribute arguments. |
| 1433 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1434 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1435 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1436 | return; |
| 1437 | } |
| 1438 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1439 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1440 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1441 | // gcc rejects |
| 1442 | // class c { |
| 1443 | // static int a __attribute__((weakref ("v2"))); |
| 1444 | // static int b() __attribute__((weakref ("f3"))); |
| 1445 | // }; |
| 1446 | // and ignores the attributes of |
| 1447 | // void f(void) { |
| 1448 | // static int a __attribute__((weakref ("v2"))); |
| 1449 | // } |
| 1450 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1451 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1452 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1453 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1454 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1455 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | // The GCC manual says |
| 1459 | // |
| 1460 | // At present, a declaration to which `weakref' is attached can only |
| 1461 | // be `static'. |
| 1462 | // |
| 1463 | // It also says |
| 1464 | // |
| 1465 | // Without a TARGET, |
| 1466 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1467 | // equivalent to `weak'. |
| 1468 | // |
| 1469 | // gcc 4.4.1 will accept |
| 1470 | // int a7 __attribute__((weakref)); |
| 1471 | // as |
| 1472 | // int a7 __attribute__((weak)); |
| 1473 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1474 | // it if this behaviour is actually used. |
| 1475 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1476 | // GCC rejects |
| 1477 | // static ((alias ("y"), weakref)). |
| 1478 | // Should we? How to check that weakref is before or after alias? |
| 1479 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1480 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1481 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1482 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1483 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1484 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1485 | // GCC will accept anything as the argument of weakref. Should we |
| 1486 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1487 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1488 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1489 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1490 | D->addAttr(::new (S.Context) |
| 1491 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1492 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1495 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1496 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1497 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1498 | return; |
| 1499 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1500 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1501 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1502 | return; |
| 1503 | } |
| 1504 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1505 | // Aliases should be on declarations, not definitions. |
| 1506 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1507 | if (FD->isThisDeclarationADefinition()) { |
| 1508 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD; |
| 1509 | return; |
| 1510 | } |
| 1511 | } else { |
| 1512 | const auto *VD = cast<VarDecl>(D); |
| 1513 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
| 1514 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD; |
| 1515 | return; |
| 1516 | } |
| 1517 | } |
| 1518 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1519 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1520 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1521 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1522 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1525 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1526 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1527 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1528 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1529 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1530 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1534 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1535 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1536 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1537 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1538 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1541 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1542 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1543 | StringRef Model; |
| 1544 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1545 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1546 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1547 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1548 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1549 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1550 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1551 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1552 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1556 | D->addAttr(::new (S.Context) |
| 1557 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1558 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1561 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1562 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1563 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1564 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1565 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1566 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1569 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1570 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1573 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1574 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1575 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1576 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1577 | return; |
| 1578 | } |
| 1579 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1580 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1581 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1584 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1585 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (S.CheckNoReturnAttr(attr)) return; |
| 1588 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1589 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1590 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1591 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1592 | return; |
| 1593 | } |
| 1594 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1595 | D->addAttr(::new (S.Context) |
| 1596 | NoReturnAttr(attr.getRange(), S.Context, |
| 1597 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1601 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1602 | attr.setInvalid(); |
| 1603 | return true; |
| 1604 | } |
| 1605 | |
| 1606 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1609 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1610 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1611 | |
| 1612 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1613 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 1614 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1615 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1616 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1617 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1618 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1619 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1620 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1621 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1622 | return; |
| 1623 | } |
| 1624 | } |
| 1625 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1626 | D->addAttr(::new (S.Context) |
| 1627 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1628 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1631 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1632 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1633 | /* |
| 1634 | Returning a Vector Class in Registers |
| 1635 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1636 | According to the PPU ABI specifications, a class with a single member of |
| 1637 | vector type is returned in memory when used as the return value of a function. |
| 1638 | This results in inefficient code when implementing vector classes. To return |
| 1639 | the value in a single vector register, add the vecreturn attribute to the |
| 1640 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1641 | |
| 1642 | Example: |
| 1643 | |
| 1644 | struct Vector |
| 1645 | { |
| 1646 | __vector float xyzw; |
| 1647 | } __attribute__((vecreturn)); |
| 1648 | |
| 1649 | Vector Add(Vector lhs, Vector rhs) |
| 1650 | { |
| 1651 | Vector result; |
| 1652 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1653 | return result; // This will be returned in a register |
| 1654 | } |
| 1655 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1656 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1657 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | } |
| 1660 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1661 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1662 | int count = 0; |
| 1663 | |
| 1664 | if (!isa<CXXRecordDecl>(record)) { |
| 1665 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1666 | return; |
| 1667 | } |
| 1668 | |
| 1669 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1670 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1671 | return; |
| 1672 | } |
| 1673 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1674 | for (const auto *I : record->fields()) { |
| 1675 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1676 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1677 | return; |
| 1678 | } |
| 1679 | count++; |
| 1680 | } |
| 1681 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1682 | D->addAttr(::new (S.Context) |
| 1683 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1684 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1687 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1688 | const AttributeList &Attr) { |
| 1689 | if (isa<ParmVarDecl>(D)) { |
| 1690 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1691 | // parameter of a function declaration or lambda. |
| 1692 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1693 | S.Diag(Attr.getLoc(), |
| 1694 | diag::err_carries_dependency_param_not_function_decl); |
| 1695 | return; |
| 1696 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1697 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1698 | |
| 1699 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1700 | Attr.getRange(), S.Context, |
| 1701 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1704 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1705 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1706 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1707 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1708 | return; |
| 1709 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1710 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1711 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1712 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1713 | return; |
| 1714 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1715 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1716 | D->addAttr(::new (S.Context) |
| 1717 | UsedAttr(Attr.getRange(), S.Context, |
| 1718 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1721 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1722 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1723 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1724 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1725 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1726 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1727 | D->addAttr(::new (S.Context) |
| 1728 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1729 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1732 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1733 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1734 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1735 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1736 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1737 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1738 | D->addAttr(::new (S.Context) |
| 1739 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1740 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1743 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1744 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1745 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1746 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1747 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1748 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1749 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1750 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1751 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1752 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1755 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1756 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1757 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 1758 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 1759 | << Attr.getName() << Attr.getRange(); |
| 1760 | return; |
| 1761 | } |
| 1762 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1763 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1764 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1765 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1768 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1769 | IdentifierInfo *Platform, |
| 1770 | VersionTuple Introduced, |
| 1771 | VersionTuple Deprecated, |
| 1772 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1773 | StringRef PlatformName |
| 1774 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1775 | if (PlatformName.empty()) |
| 1776 | PlatformName = Platform->getName(); |
| 1777 | |
| 1778 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1779 | // of these steps are needed). |
| 1780 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1781 | !(Introduced <= Deprecated)) { |
| 1782 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1783 | << 1 << PlatformName << Deprecated.getAsString() |
| 1784 | << 0 << Introduced.getAsString(); |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
| 1788 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1789 | !(Introduced <= Obsoleted)) { |
| 1790 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1791 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1792 | << 0 << Introduced.getAsString(); |
| 1793 | return true; |
| 1794 | } |
| 1795 | |
| 1796 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1797 | !(Deprecated <= Obsoleted)) { |
| 1798 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1799 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1800 | << 1 << Deprecated.getAsString(); |
| 1801 | return true; |
| 1802 | } |
| 1803 | |
| 1804 | return false; |
| 1805 | } |
| 1806 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1807 | /// \brief Check whether the two versions match. |
| 1808 | /// |
| 1809 | /// If either version tuple is empty, then they are assumed to match. If |
| 1810 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1811 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1812 | bool BeforeIsOkay) { |
| 1813 | if (X.empty() || Y.empty()) |
| 1814 | return true; |
| 1815 | |
| 1816 | if (X == Y) |
| 1817 | return true; |
| 1818 | |
| 1819 | if (BeforeIsOkay && X < Y) |
| 1820 | return true; |
| 1821 | |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1825 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1826 | IdentifierInfo *Platform, |
| 1827 | VersionTuple Introduced, |
| 1828 | VersionTuple Deprecated, |
| 1829 | VersionTuple Obsoleted, |
| 1830 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1831 | StringRef Message, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1832 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1833 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1834 | VersionTuple MergedIntroduced = Introduced; |
| 1835 | VersionTuple MergedDeprecated = Deprecated; |
| 1836 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1837 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1838 | bool OverrideOrImpl = false; |
| 1839 | switch (AMK) { |
| 1840 | case AMK_None: |
| 1841 | case AMK_Redeclaration: |
| 1842 | OverrideOrImpl = false; |
| 1843 | break; |
| 1844 | |
| 1845 | case AMK_Override: |
| 1846 | case AMK_ProtocolImplementation: |
| 1847 | OverrideOrImpl = true; |
| 1848 | break; |
| 1849 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1850 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1851 | if (D->hasAttrs()) { |
| 1852 | AttrVec &Attrs = D->getAttrs(); |
| 1853 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1854 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1855 | if (!OldAA) { |
| 1856 | ++i; |
| 1857 | continue; |
| 1858 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1859 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1860 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1861 | if (OldPlatform != Platform) { |
| 1862 | ++i; |
| 1863 | continue; |
| 1864 | } |
| 1865 | |
| 1866 | FoundAny = true; |
| 1867 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1868 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1869 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1870 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1871 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1872 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 1873 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 1874 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1875 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1876 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 1877 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1878 | int Which = -1; |
| 1879 | VersionTuple FirstVersion; |
| 1880 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1881 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1882 | Which = 0; |
| 1883 | FirstVersion = OldIntroduced; |
| 1884 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1885 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1886 | Which = 1; |
| 1887 | FirstVersion = Deprecated; |
| 1888 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1889 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1890 | Which = 2; |
| 1891 | FirstVersion = Obsoleted; |
| 1892 | SecondVersion = OldObsoleted; |
| 1893 | } |
| 1894 | |
| 1895 | if (Which == -1) { |
| 1896 | Diag(OldAA->getLocation(), |
| 1897 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1898 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1899 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1900 | } else { |
| 1901 | Diag(OldAA->getLocation(), |
| 1902 | diag::warn_mismatched_availability_override) |
| 1903 | << Which |
| 1904 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1905 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 1906 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1907 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1908 | if (AMK == AMK_Override) |
| 1909 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1910 | else |
| 1911 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1912 | } else { |
| 1913 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1914 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1915 | } |
| 1916 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1917 | Attrs.erase(Attrs.begin() + i); |
| 1918 | --e; |
| 1919 | continue; |
| 1920 | } |
| 1921 | |
| 1922 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1923 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1924 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1925 | |
| 1926 | if (MergedIntroduced2.empty()) |
| 1927 | MergedIntroduced2 = OldIntroduced; |
| 1928 | if (MergedDeprecated2.empty()) |
| 1929 | MergedDeprecated2 = OldDeprecated; |
| 1930 | if (MergedObsoleted2.empty()) |
| 1931 | MergedObsoleted2 = OldObsoleted; |
| 1932 | |
| 1933 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1934 | MergedIntroduced2, MergedDeprecated2, |
| 1935 | MergedObsoleted2)) { |
| 1936 | Attrs.erase(Attrs.begin() + i); |
| 1937 | --e; |
| 1938 | continue; |
| 1939 | } |
| 1940 | |
| 1941 | MergedIntroduced = MergedIntroduced2; |
| 1942 | MergedDeprecated = MergedDeprecated2; |
| 1943 | MergedObsoleted = MergedObsoleted2; |
| 1944 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1945 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | if (FoundAny && |
| 1949 | MergedIntroduced == Introduced && |
| 1950 | MergedDeprecated == Deprecated && |
| 1951 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1952 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1954 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1955 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1956 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1957 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1958 | !OverrideOrImpl) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1959 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1960 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1961 | Obsoleted, IsUnavailable, Message, |
| 1962 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1963 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1964 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1967 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1968 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1969 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1970 | return; |
| 1971 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1972 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1973 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1974 | IdentifierInfo *II = Platform->Ident; |
| 1975 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1976 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1977 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1978 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1979 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1980 | if (!ND) { |
| 1981 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1982 | return; |
| 1983 | } |
| 1984 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1985 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1986 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1987 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1988 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1989 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1990 | if (const StringLiteral *SE = |
| 1991 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1992 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1993 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1994 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1995 | Introduced.Version, |
| 1996 | Deprecated.Version, |
| 1997 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1998 | IsUnavailable, Str, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1999 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2000 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2001 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2002 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2005 | template <class T> |
| 2006 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2007 | typename T::VisibilityType value, |
| 2008 | unsigned attrSpellingListIndex) { |
| 2009 | T *existingAttr = D->getAttr<T>(); |
| 2010 | if (existingAttr) { |
| 2011 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2012 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2013 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2014 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2015 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2016 | D->dropAttr<T>(); |
| 2017 | } |
| 2018 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2019 | } |
| 2020 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2021 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2022 | VisibilityAttr::VisibilityType Vis, |
| 2023 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2024 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2025 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2028 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2029 | TypeVisibilityAttr::VisibilityType Vis, |
| 2030 | unsigned AttrSpellingListIndex) { |
| 2031 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2032 | AttrSpellingListIndex); |
| 2033 | } |
| 2034 | |
| 2035 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2036 | bool isTypeVisibility) { |
| 2037 | // Visibility attributes don't mean anything on a typedef. |
| 2038 | if (isa<TypedefNameDecl>(D)) { |
| 2039 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2040 | << Attr.getName(); |
| 2041 | return; |
| 2042 | } |
| 2043 | |
| 2044 | // 'type_visibility' can only go on a type or namespace. |
| 2045 | if (isTypeVisibility && |
| 2046 | !(isa<TagDecl>(D) || |
| 2047 | isa<ObjCInterfaceDecl>(D) || |
| 2048 | isa<NamespaceDecl>(D))) { |
| 2049 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2050 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2051 | return; |
| 2052 | } |
| 2053 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2054 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2055 | StringRef TypeStr; |
| 2056 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2057 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2058 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2059 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2060 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2061 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2062 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2063 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2064 | return; |
| 2065 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2066 | |
| 2067 | // Complain about attempts to use protected visibility on targets |
| 2068 | // (like Darwin) that don't support it. |
| 2069 | if (type == VisibilityAttr::Protected && |
| 2070 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2071 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2072 | type = VisibilityAttr::Default; |
| 2073 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2074 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2075 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2076 | clang::Attr *newAttr; |
| 2077 | if (isTypeVisibility) { |
| 2078 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2079 | (TypeVisibilityAttr::VisibilityType) type, |
| 2080 | Index); |
| 2081 | } else { |
| 2082 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2083 | } |
| 2084 | if (newAttr) |
| 2085 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2088 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2089 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2090 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2091 | if (!Attr.isArgIdent(0)) { |
| 2092 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2093 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2094 | return; |
| 2095 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2096 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2097 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2098 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2099 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2100 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2101 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2102 | return; |
| 2103 | } |
| 2104 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2105 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2106 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2107 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2108 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2109 | // Ignore the attribute. |
| 2110 | return; |
| 2111 | } |
| 2112 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2113 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2114 | S.Context, F, |
| 2115 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2118 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2119 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2120 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2121 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2122 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2123 | return; |
| 2124 | } |
| 2125 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2126 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2127 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2128 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2129 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2130 | return; |
| 2131 | } |
| 2132 | } |
| 2133 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2134 | // It is okay to include this attribute on properties, e.g.: |
| 2135 | // |
| 2136 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2137 | // |
| 2138 | // In this case it follows tradition and suppresses an error in the above |
| 2139 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2140 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2141 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2142 | D->addAttr(::new (S.Context) |
| 2143 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2144 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2147 | static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2148 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2149 | QualType T = TD->getUnderlyingType(); |
| 2150 | if (!T->isObjCObjectPointerType()) { |
| 2151 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2152 | return; |
| 2153 | } |
| 2154 | } else { |
| 2155 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2156 | return; |
| 2157 | } |
| 2158 | D->addAttr(::new (S.Context) |
| 2159 | ObjCIndependentClassAttr(Attr.getRange(), S.Context, |
| 2160 | Attr.getAttributeSpellingListIndex())); |
| 2161 | } |
| 2162 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2163 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2164 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2165 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2166 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2167 | return; |
| 2168 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2169 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2170 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2171 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2172 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2173 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2174 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2175 | return; |
| 2176 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2177 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2178 | D->addAttr(::new (S.Context) |
| 2179 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2180 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2183 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2184 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2185 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2186 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2187 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2188 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2189 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2190 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2191 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2192 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2193 | return; |
| 2194 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2195 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2196 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2197 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2198 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2199 | return; |
| 2200 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2201 | |
| 2202 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2205 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2206 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2207 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2208 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2209 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2210 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2211 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2212 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2213 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2214 | return; |
| 2215 | } |
| 2216 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2217 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2218 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2219 | // FIXME: This error message could be improved, it would be nice |
| 2220 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2221 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2222 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2223 | return; |
| 2224 | } |
| 2225 | } |
| 2226 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2227 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2228 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2229 | if (isa<FunctionNoProtoType>(FT)) { |
| 2230 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2231 | return; |
| 2232 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2234 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2235 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2236 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2237 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2238 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2239 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2240 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2241 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2242 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2243 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2244 | if (!BD->isVariadic()) { |
| 2245 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2246 | return; |
| 2247 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2248 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2249 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2250 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2251 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2252 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2253 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2254 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2255 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2256 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2257 | return; |
| 2258 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2259 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2260 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2261 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2262 | return; |
| 2263 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2264 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2265 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2266 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2267 | return; |
| 2268 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2269 | D->addAttr(::new (S.Context) |
| 2270 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2271 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2274 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2275 | if (D->getFunctionType() && |
| 2276 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2277 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2278 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2279 | return; |
| 2280 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2281 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2282 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2283 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2284 | << Attr.getName() << 1; |
| 2285 | return; |
| 2286 | } |
| 2287 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2288 | D->addAttr(::new (S.Context) |
| 2289 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2290 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2293 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2294 | // weak_import only applies to variable & function declarations. |
| 2295 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2296 | if (!D->canBeWeakImported(isDef)) { |
| 2297 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2298 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2299 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2300 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2301 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2302 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2303 | // Nothing to warn about here. |
| 2304 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2305 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2306 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2307 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2308 | return; |
| 2309 | } |
| 2310 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2311 | D->addAttr(::new (S.Context) |
| 2312 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2313 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2316 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2317 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2318 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2319 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2320 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2321 | for (unsigned i = 0; i < 3; ++i) { |
| 2322 | const Expr *E = Attr.getArgAsExpr(i); |
| 2323 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2324 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2325 | if (WGSize[i] == 0) { |
| 2326 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2327 | << Attr.getName() << E->getSourceRange(); |
| 2328 | return; |
| 2329 | } |
| 2330 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2331 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2332 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2333 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2334 | Existing->getYDim() == WGSize[1] && |
| 2335 | Existing->getZDim() == WGSize[2])) |
| 2336 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2337 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2338 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2339 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2340 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2343 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2344 | if (!Attr.hasParsedType()) { |
| 2345 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2346 | << Attr.getName() << 1; |
| 2347 | return; |
| 2348 | } |
| 2349 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2350 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2351 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2352 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2353 | |
| 2354 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2355 | (ParmType->isBooleanType() || |
| 2356 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2357 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2358 | << ParmType; |
| 2359 | return; |
| 2360 | } |
| 2361 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2362 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2363 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2364 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2365 | return; |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2370 | ParmTSI, |
| 2371 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2374 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2375 | StringRef Name, |
| 2376 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2377 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2378 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2379 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2380 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2381 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2382 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2383 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2384 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2385 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2388 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2389 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2390 | if (!Error.empty()) { |
| 2391 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2392 | return false; |
| 2393 | } |
| 2394 | return true; |
| 2395 | } |
| 2396 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2397 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2398 | // Make sure that there is a string literal as the sections's single |
| 2399 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2400 | StringRef Str; |
| 2401 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2402 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2403 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2405 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2406 | return; |
| 2407 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2408 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2409 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2410 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2411 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2412 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2413 | return; |
| 2414 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2416 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2417 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2418 | if (NewAttr) |
| 2419 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2422 | // Check for things we'd like to warn about, no errors or validation for now. |
| 2423 | // TODO: Validation should use a backend target library that specifies |
| 2424 | // the allowable subtarget features and cpus. We could use something like a |
| 2425 | // TargetCodeGenInfo hook here to do validation. |
| 2426 | void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 2427 | for (auto Str : {"tune=", "fpmath="}) |
| 2428 | if (AttrStr.find(Str) != StringRef::npos) |
| 2429 | Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str; |
| 2430 | } |
| 2431 | |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2432 | static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2433 | StringRef Str; |
| 2434 | SourceLocation LiteralLoc; |
| 2435 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
| 2436 | return; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2437 | S.checkTargetAttr(LiteralLoc, Str); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2438 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2439 | TargetAttr *NewAttr = |
| 2440 | ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index); |
| 2441 | D->addAttr(NewAttr); |
| 2442 | } |
| 2443 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2444 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2445 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2446 | VarDecl *VD = cast<VarDecl>(D); |
| 2447 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2448 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2449 | return; |
| 2450 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2451 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2452 | Expr *E = Attr.getArgAsExpr(0); |
| 2453 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2454 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2455 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2456 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2457 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2458 | // will warn the user. |
| 2459 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2460 | if (DRE->hasQualifier()) |
| 2461 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2462 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2463 | NI = DRE->getNameInfo(); |
| 2464 | if (!FD) { |
| 2465 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2466 | << NI.getName(); |
| 2467 | return; |
| 2468 | } |
| 2469 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2470 | if (ULE->hasExplicitTemplateArgs()) |
| 2471 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2472 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2473 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2474 | if (!FD) { |
| 2475 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2476 | << NI.getName(); |
| 2477 | if (ULE->getType() == S.Context.OverloadTy) |
| 2478 | S.NoteAllOverloadCandidates(ULE); |
| 2479 | return; |
| 2480 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2481 | } else { |
| 2482 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2483 | return; |
| 2484 | } |
| 2485 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2486 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2487 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2488 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2489 | return; |
| 2490 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2491 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2492 | // We're currently more strict than GCC about what function types we accept. |
| 2493 | // If this ever proves to be a problem it should be easy to fix. |
| 2494 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2495 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2496 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2497 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2498 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2499 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2500 | return; |
| 2501 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2502 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2503 | D->addAttr(::new (S.Context) |
| 2504 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2505 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2508 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2509 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2510 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2511 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2512 | uint64_t Idx; |
| 2513 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2514 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2515 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2516 | // Make sure the format string is really a string. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2517 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2518 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2519 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 2520 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2521 | !isCFStringType(Ty, S.Context) && |
| 2522 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2523 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2524 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2525 | << "a string type" << IdxExpr->getSourceRange() |
| 2526 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2527 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2528 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2529 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2530 | if (!isNSStringType(Ty, S.Context) && |
| 2531 | !isCFStringType(Ty, S.Context) && |
| 2532 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2533 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2534 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2535 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 2536 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2537 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2540 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2541 | // because that has corrected for the implicit this parameter, and is zero- |
| 2542 | // based. The attribute expects what the user wrote explicitly. |
| 2543 | llvm::APSInt Val; |
| 2544 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2545 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2546 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2547 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2548 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2551 | enum FormatAttrKind { |
| 2552 | CFStringFormat, |
| 2553 | NSStringFormat, |
| 2554 | StrftimeFormat, |
| 2555 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2556 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2557 | InvalidFormat |
| 2558 | }; |
| 2559 | |
| 2560 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2561 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2562 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2563 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2564 | // Check for formats that get handled specially. |
| 2565 | .Case("NSString", NSStringFormat) |
| 2566 | .Case("CFString", CFStringFormat) |
| 2567 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2568 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2569 | // Otherwise, check for supported formats. |
| 2570 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2571 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2572 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 2573 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 2574 | .Case("os_trace", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2575 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2576 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2577 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2580 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2581 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2582 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2583 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2584 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2585 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2586 | return; |
| 2587 | } |
| 2588 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2589 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2590 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2591 | Attr.setInvalid(); |
| 2592 | return; |
| 2593 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2594 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2595 | if (S.Context.getAsArrayType(T)) |
| 2596 | T = S.Context.getBaseElementType(T); |
| 2597 | if (!T->getAs<RecordType>()) { |
| 2598 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2599 | Attr.setInvalid(); |
| 2600 | return; |
| 2601 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2602 | |
| 2603 | Expr *E = Attr.getArgAsExpr(0); |
| 2604 | uint32_t prioritynum; |
| 2605 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2606 | Attr.setInvalid(); |
| 2607 | return; |
| 2608 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2609 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2610 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2611 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2612 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2613 | Attr.setInvalid(); |
| 2614 | return; |
| 2615 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2616 | D->addAttr(::new (S.Context) |
| 2617 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2618 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2621 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2622 | IdentifierInfo *Format, int FormatIdx, |
| 2623 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2624 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2625 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2626 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 2627 | if (F->getType() == Format && |
| 2628 | F->getFormatIdx() == FormatIdx && |
| 2629 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2630 | // If we don't have a valid location for this attribute, adopt the |
| 2631 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2632 | if (F->getLocation().isInvalid()) |
| 2633 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2634 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2638 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2639 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2642 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2643 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2644 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2645 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2646 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2647 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2648 | return; |
| 2649 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2650 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2651 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2652 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2653 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2654 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2655 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2656 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2657 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2658 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 2659 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2660 | // If we've modified the string name, we need a new identifier for it. |
| 2661 | II = &S.Context.Idents.get(Format); |
| 2662 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2663 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2664 | // Check for supported formats. |
| 2665 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2666 | |
| 2667 | if (Kind == IgnoredFormat) |
| 2668 | return; |
| 2669 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2670 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2671 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2672 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2673 | return; |
| 2674 | } |
| 2675 | |
| 2676 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2677 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2678 | uint32_t Idx; |
| 2679 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2680 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2681 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2682 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2683 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2684 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2685 | return; |
| 2686 | } |
| 2687 | |
| 2688 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2689 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2690 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2691 | if (HasImplicitThisParam) { |
| 2692 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2693 | S.Diag(Attr.getLoc(), |
| 2694 | diag::err_format_attribute_implicit_this_format_string) |
| 2695 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2696 | return; |
| 2697 | } |
| 2698 | ArgIdx--; |
| 2699 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2701 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2702 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2703 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2704 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2705 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2706 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2707 | << "a CFString" << IdxExpr->getSourceRange() |
| 2708 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2709 | return; |
| 2710 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2711 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2712 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2713 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2714 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2715 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2716 | << "an NSString" << IdxExpr->getSourceRange() |
| 2717 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2718 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2719 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2720 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2721 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2722 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2723 | << "a string type" << IdxExpr->getSourceRange() |
| 2724 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2725 | return; |
| 2726 | } |
| 2727 | |
| 2728 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2729 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2730 | uint32_t FirstArg; |
| 2731 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2732 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2733 | |
| 2734 | // check if the function is variadic if the 3rd argument non-zero |
| 2735 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2736 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2737 | ++NumArgs; // +1 for ... |
| 2738 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2739 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
| 2742 | } |
| 2743 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2744 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2745 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2746 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2747 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2748 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2749 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2750 | return; |
| 2751 | } |
| 2752 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2753 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2754 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2755 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2756 | return; |
| 2757 | } |
| 2758 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2759 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2760 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2761 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2762 | if (NewAttr) |
| 2763 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2766 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2767 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2768 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2769 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2770 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2771 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2772 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2773 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2774 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2775 | |
| 2776 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2777 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2778 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2779 | return; |
| 2780 | } |
| 2781 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2782 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2783 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2784 | diag::warn_transparent_union_attribute_not_definition); |
| 2785 | return; |
| 2786 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2787 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2788 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2789 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2790 | if (Field == FieldEnd) { |
| 2791 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2792 | return; |
| 2793 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2794 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2795 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2796 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2797 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2798 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2799 | diag::warn_transparent_union_attribute_floating) |
| 2800 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2801 | return; |
| 2802 | } |
| 2803 | |
| 2804 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2805 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2806 | for (; Field != FieldEnd; ++Field) { |
| 2807 | QualType FieldType = Field->getType(); |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2808 | // FIXME: this isn't fully correct; we also need to test whether the |
| 2809 | // members of the union would all have the same calling convention as the |
| 2810 | // first member of the union. Checking just the size and alignment isn't |
| 2811 | // sufficient (consider structs passed on the stack instead of in registers |
| 2812 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2813 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2814 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2815 | // Warn if we drop the attribute. |
| 2816 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2817 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2818 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2819 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2820 | diag::warn_transparent_union_attribute_field_size_align) |
| 2821 | << isSize << Field->getDeclName() << FieldBits; |
| 2822 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2823 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2824 | diag::note_transparent_union_first_field_size_align) |
| 2825 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2826 | return; |
| 2827 | } |
| 2828 | } |
| 2829 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2830 | RD->addAttr(::new (S.Context) |
| 2831 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2832 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2835 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2836 | // Make sure that there is a string literal as the annotation's single |
| 2837 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2838 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2839 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2840 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2841 | |
| 2842 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2843 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 2844 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2845 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2846 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2847 | |
| 2848 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2849 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2850 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 2853 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 2854 | const AttributeList &Attr) { |
| 2855 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 2856 | Attr.getAttributeSpellingListIndex()); |
| 2857 | } |
| 2858 | |
| 2859 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 2860 | unsigned SpellingListIndex) { |
| 2861 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 2862 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2863 | |
| 2864 | QualType T; |
| 2865 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 2866 | T = TD->getUnderlyingType(); |
| 2867 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2868 | T = VD->getType(); |
| 2869 | else |
| 2870 | llvm_unreachable("Unknown decl type for align_value"); |
| 2871 | |
| 2872 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 2873 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 2874 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 2875 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 2876 | return; |
| 2877 | } |
| 2878 | |
| 2879 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 2880 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 2881 | ExprResult ICE |
| 2882 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2883 | diag::err_align_value_attribute_argument_not_int, |
| 2884 | /*AllowFold*/ false); |
| 2885 | if (ICE.isInvalid()) |
| 2886 | return; |
| 2887 | |
| 2888 | if (!Alignment.isPowerOf2()) { |
| 2889 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 2890 | << E->getSourceRange(); |
| 2891 | return; |
| 2892 | } |
| 2893 | |
| 2894 | D->addAttr(::new (Context) |
| 2895 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 2896 | SpellingListIndex)); |
| 2897 | return; |
| 2898 | } |
| 2899 | |
| 2900 | // Save dependent expressions in the AST to be instantiated. |
| 2901 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
| 2902 | return; |
| 2903 | } |
| 2904 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2905 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2906 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2907 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2908 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2909 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2910 | return; |
| 2911 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2912 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2913 | if (Attr.getNumArgs() == 0) { |
| 2914 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2915 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2916 | return; |
| 2917 | } |
| 2918 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2919 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2920 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2921 | S.Diag(Attr.getEllipsisLoc(), |
| 2922 | diag::err_pack_expansion_without_parameter_packs); |
| 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2927 | return; |
| 2928 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 2929 | if (E->isValueDependent()) { |
| 2930 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 2931 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 2932 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 2933 | << E->getSourceRange(); |
| 2934 | return; |
| 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2939 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2940 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2944 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2945 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2946 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2947 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2948 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2949 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2950 | // C++11 [dcl.align]p1: |
| 2951 | // An alignment-specifier may be applied to a variable or to a class |
| 2952 | // data member, but it shall not be applied to a bit-field, a function |
| 2953 | // parameter, the formal parameter of a catch clause, or a variable |
| 2954 | // declared with the register storage class specifier. An |
| 2955 | // alignment-specifier may also be applied to the declaration of a class |
| 2956 | // or enumeration type. |
| 2957 | // C11 6.7.5/2: |
| 2958 | // An alignment attribute shall not be specified in a declaration of |
| 2959 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2960 | // object declared with the register storage-class specifier. |
| 2961 | int DiagKind = -1; |
| 2962 | if (isa<ParmVarDecl>(D)) { |
| 2963 | DiagKind = 0; |
| 2964 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2965 | if (VD->getStorageClass() == SC_Register) |
| 2966 | DiagKind = 1; |
| 2967 | if (VD->isExceptionVariable()) |
| 2968 | DiagKind = 2; |
| 2969 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2970 | if (FD->isBitField()) |
| 2971 | DiagKind = 3; |
| 2972 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2973 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2974 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2975 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2976 | return; |
| 2977 | } |
| 2978 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2979 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2980 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2981 | return; |
| 2982 | } |
| 2983 | } |
| 2984 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2985 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2986 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2987 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2988 | AA->setPackExpansion(IsPackExpansion); |
| 2989 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2990 | return; |
| 2991 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2992 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2993 | // FIXME: Cache the number on the Attr object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 2994 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2995 | ExprResult ICE |
| 2996 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2997 | diag::err_aligned_attribute_argument_not_int, |
| 2998 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2999 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3000 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3001 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3002 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3003 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3004 | // C++11 [dcl.align]p2: |
| 3005 | // -- if the constant expression evaluates to zero, the alignment |
| 3006 | // specifier shall have no effect |
| 3007 | // C11 6.7.5p6: |
| 3008 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3009 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3010 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3011 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3012 | << E->getSourceRange(); |
| 3013 | return; |
| 3014 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3015 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3016 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3017 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3018 | unsigned MaxValidAlignment = |
| 3019 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3020 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3021 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3022 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3023 | << E->getSourceRange(); |
| 3024 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3025 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3026 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3027 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3028 | unsigned MaxTLSAlign = |
| 3029 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3030 | .getQuantity(); |
| 3031 | auto *VD = dyn_cast<VarDecl>(D); |
| 3032 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3033 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3034 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3035 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3036 | return; |
| 3037 | } |
| 3038 | } |
| 3039 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3040 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3041 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3042 | AA->setPackExpansion(IsPackExpansion); |
| 3043 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3046 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3047 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3048 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3049 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3050 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3051 | SpellingListIndex); |
| 3052 | AA->setPackExpansion(IsPackExpansion); |
| 3053 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3054 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3055 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3056 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3057 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3058 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3059 | QualType UnderlyingTy, DiagTy; |
| 3060 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 3061 | UnderlyingTy = DiagTy = VD->getType(); |
| 3062 | } else { |
| 3063 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3064 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3065 | UnderlyingTy = ED->getIntegerType(); |
| 3066 | } |
| 3067 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3068 | return; |
| 3069 | |
| 3070 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3071 | // The combined effect of all alignment attributes in a declaration shall |
| 3072 | // not specify an alignment that is less strict than the alignment that |
| 3073 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3074 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3075 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3076 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3077 | if (I->isAlignmentDependent()) |
| 3078 | return; |
| 3079 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3080 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3081 | Align = std::max(Align, I->getAlignment(Context)); |
| 3082 | } |
| 3083 | |
| 3084 | if (AlignasAttr && Align) { |
| 3085 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3086 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3087 | if (NaturalAlign > RequestedAlign) |
| 3088 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3089 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3093 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3094 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3095 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3096 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3097 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3098 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3099 | // have to wait until the record is defined to catch any mismatches. |
| 3100 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3101 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3102 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3103 | // The unspecified model never matches what a definition could need. |
| 3104 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3105 | return false; |
| 3106 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3107 | if (BestCase) { |
| 3108 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3109 | return false; |
| 3110 | } else { |
| 3111 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3112 | return false; |
| 3113 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3114 | |
| 3115 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3116 | << 0 /*definition*/; |
| 3117 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3118 | << RD->getNameAsString(); |
| 3119 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3122 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3123 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3124 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3125 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3126 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3127 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3128 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3129 | // This attribute isn't documented, but glibc uses it. It changes |
| 3130 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3131 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3132 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3133 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3134 | return; |
| 3135 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3136 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3137 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3138 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3139 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3140 | normalizeName(Str); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3141 | |
| 3142 | unsigned DestWidth = 0; |
| 3143 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3144 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3145 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3146 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3147 | switch (Str[0]) { |
| 3148 | case 'Q': DestWidth = 8; break; |
| 3149 | case 'H': DestWidth = 16; break; |
| 3150 | case 'S': DestWidth = 32; break; |
| 3151 | case 'D': DestWidth = 64; break; |
| 3152 | case 'X': DestWidth = 96; break; |
| 3153 | case 'T': DestWidth = 128; break; |
| 3154 | } |
| 3155 | if (Str[1] == 'F') { |
| 3156 | IntegerMode = false; |
| 3157 | } else if (Str[1] == 'C') { |
| 3158 | IntegerMode = false; |
| 3159 | ComplexMode = true; |
| 3160 | } else if (Str[1] != 'I') { |
| 3161 | DestWidth = 0; |
| 3162 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3163 | break; |
| 3164 | case 4: |
| 3165 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3166 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3167 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3168 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3169 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3170 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3171 | break; |
| 3172 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3173 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3174 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3175 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3176 | case 11: |
| 3177 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3178 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3179 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3183 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3184 | OldTy = TD->getUnderlyingType(); |
| 3185 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3186 | OldTy = VD->getType(); |
| 3187 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3188 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 3189 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3190 | return; |
| 3191 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3192 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3193 | // Base type can also be a vector type (see PR17453). |
| 3194 | // Distinguish between base type and base element type. |
| 3195 | QualType OldElemTy = OldTy; |
| 3196 | if (const VectorType *VT = OldTy->getAs<VectorType>()) |
| 3197 | OldElemTy = VT->getElementType(); |
| 3198 | |
| 3199 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3200 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3201 | else if (IntegerMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3202 | if (!OldElemTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3203 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3204 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3205 | if (!OldElemTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3206 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3207 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3208 | if (!OldElemTy->isFloatingType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3209 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3210 | } |
| 3211 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3212 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3213 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3214 | // FIXME: Make sure floating-point mappings are accurate |
| 3215 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3216 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3217 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3218 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3221 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3222 | |
| 3223 | if (IntegerMode) |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3224 | NewElemTy = S.Context.getIntTypeForBitwidth( |
| 3225 | DestWidth, OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3226 | else |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3227 | NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3228 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3229 | if (NewElemTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3230 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3231 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3234 | if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3235 | NewElemTy = S.Context.getComplexType(NewElemTy); |
| 3236 | } |
| 3237 | |
| 3238 | QualType NewTy = NewElemTy; |
| 3239 | if (const VectorType *OldVT = OldTy->getAs<VectorType>()) { |
| 3240 | // Complex machine mode does not support base vector types. |
| 3241 | if (ComplexMode) { |
| 3242 | S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type); |
| 3243 | return; |
| 3244 | } |
| 3245 | unsigned NumElements = S.Context.getTypeSize(OldElemTy) * |
| 3246 | OldVT->getNumElements() / |
| 3247 | S.Context.getTypeSize(NewElemTy); |
| 3248 | NewTy = |
| 3249 | S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
| 3250 | } |
| 3251 | |
| 3252 | if (NewTy.isNull()) { |
| 3253 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3254 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3258 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3259 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3260 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3261 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3262 | |
| 3263 | D->addAttr(::new (S.Context) |
| 3264 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3265 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3266 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3267 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3268 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3269 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3270 | if (!VD->hasGlobalStorage()) |
| 3271 | S.Diag(Attr.getLoc(), |
| 3272 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3273 | << Attr.getName(); |
| 3274 | } else if (!isFunctionOrMethod(D)) { |
| 3275 | S.Diag(Attr.getLoc(), |
| 3276 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3277 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3278 | return; |
| 3279 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3280 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3281 | D->addAttr(::new (S.Context) |
| 3282 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3283 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3286 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3287 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3288 | unsigned AttrSpellingListIndex) { |
| 3289 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3290 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3291 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3292 | return nullptr; |
| 3293 | } |
| 3294 | |
| 3295 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3296 | return nullptr; |
| 3297 | |
| 3298 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3299 | AttrSpellingListIndex); |
| 3300 | } |
| 3301 | |
| 3302 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 3303 | unsigned AttrSpellingListIndex) { |
| 3304 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 3305 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 3306 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3307 | return nullptr; |
| 3308 | } |
| 3309 | |
| 3310 | if (D->hasAttr<MinSizeAttr>()) |
| 3311 | return nullptr; |
| 3312 | |
| 3313 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 3314 | } |
| 3315 | |
| 3316 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 3317 | unsigned AttrSpellingListIndex) { |
| 3318 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 3319 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 3320 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3321 | D->dropAttr<AlwaysInlineAttr>(); |
| 3322 | } |
| 3323 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 3324 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 3325 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3326 | D->dropAttr<MinSizeAttr>(); |
| 3327 | } |
| 3328 | |
| 3329 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 3330 | return nullptr; |
| 3331 | |
| 3332 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 3333 | AttrSpellingListIndex); |
| 3334 | } |
| 3335 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3336 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 3337 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3338 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 3339 | D, Attr.getRange(), Attr.getName(), |
| 3340 | Attr.getAttributeSpellingListIndex())) |
| 3341 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3344 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3345 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 3346 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3347 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3350 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 3351 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3352 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 3353 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3354 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3357 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3358 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3359 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3360 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 3361 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3362 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3363 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 3364 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3365 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3366 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3367 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3368 | D->addAttr(::new (S.Context) |
| 3369 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 3370 | Attr.getAttributeSpellingListIndex())); |
Artem Belevich | c3fa25d | 2015-09-22 17:22:51 +0000 | [diff] [blame] | 3371 | |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3374 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3375 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3376 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3377 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3378 | return; |
| 3379 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3380 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3381 | D->addAttr(::new (S.Context) |
| 3382 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3383 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3386 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3387 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3388 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3389 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3390 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3391 | CallingConv CC; |
Richard Smith | eec7cb1 | 2015-05-28 23:38:53 +0000 | [diff] [blame] | 3392 | if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3393 | return; |
| 3394 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3395 | if (!isa<ObjCMethodDecl>(D)) { |
| 3396 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3397 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3398 | return; |
| 3399 | } |
| 3400 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3401 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3402 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3403 | D->addAttr(::new (S.Context) |
| 3404 | FastCallAttr(Attr.getRange(), S.Context, |
| 3405 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3406 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3407 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3408 | D->addAttr(::new (S.Context) |
| 3409 | StdCallAttr(Attr.getRange(), S.Context, |
| 3410 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3411 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3412 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3413 | D->addAttr(::new (S.Context) |
| 3414 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3415 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3416 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3417 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3418 | D->addAttr(::new (S.Context) |
| 3419 | CDeclAttr(Attr.getRange(), S.Context, |
| 3420 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3421 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3422 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3423 | D->addAttr(::new (S.Context) |
| 3424 | PascalAttr(Attr.getRange(), S.Context, |
| 3425 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3426 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3427 | case AttributeList::AT_VectorCall: |
| 3428 | D->addAttr(::new (S.Context) |
| 3429 | VectorCallAttr(Attr.getRange(), S.Context, |
| 3430 | Attr.getAttributeSpellingListIndex())); |
| 3431 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3432 | case AttributeList::AT_MSABI: |
| 3433 | D->addAttr(::new (S.Context) |
| 3434 | MSABIAttr(Attr.getRange(), S.Context, |
| 3435 | Attr.getAttributeSpellingListIndex())); |
| 3436 | return; |
| 3437 | case AttributeList::AT_SysVABI: |
| 3438 | D->addAttr(::new (S.Context) |
| 3439 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3440 | Attr.getAttributeSpellingListIndex())); |
| 3441 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3442 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3443 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3444 | switch (CC) { |
| 3445 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3446 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3447 | break; |
| 3448 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3449 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3450 | break; |
| 3451 | default: |
| 3452 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3455 | D->addAttr(::new (S.Context) |
| 3456 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3457 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3458 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3459 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3460 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3461 | D->addAttr(::new (S.Context) |
| 3462 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3463 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3464 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3465 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3466 | default: |
| 3467 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3471 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3472 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3473 | if (attr.isInvalid()) |
| 3474 | return true; |
| 3475 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3476 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3477 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3478 | attr.setInvalid(); |
| 3479 | return true; |
| 3480 | } |
| 3481 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3482 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3483 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3484 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3485 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3486 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3487 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3488 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3489 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3490 | case AttributeList::AT_MSABI: |
| 3491 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3492 | CC_X86_64Win64; |
| 3493 | break; |
| 3494 | case AttributeList::AT_SysVABI: |
| 3495 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3496 | CC_C; |
| 3497 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3498 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3499 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3500 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3501 | attr.setInvalid(); |
| 3502 | return true; |
| 3503 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3504 | if (StrRef == "aapcs") { |
| 3505 | CC = CC_AAPCS; |
| 3506 | break; |
| 3507 | } else if (StrRef == "aapcs-vfp") { |
| 3508 | CC = CC_AAPCS_VFP; |
| 3509 | break; |
| 3510 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3511 | |
| 3512 | attr.setInvalid(); |
| 3513 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3514 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3515 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3516 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3517 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3520 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3521 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3522 | if (A != TargetInfo::CCCR_OK) { |
| 3523 | if (A == TargetInfo::CCCR_Warning) |
| 3524 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3525 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3526 | // This convention is not valid for the target. Use the default function or |
| 3527 | // method calling convention. |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3528 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3529 | if (FD) |
| 3530 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3531 | TargetInfo::CCMT_NonMember; |
| 3532 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3535 | return false; |
| 3536 | } |
| 3537 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3538 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3539 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3540 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3541 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3542 | return true; |
| 3543 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3544 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3545 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3546 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3547 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3548 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3549 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3550 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3551 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3552 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3553 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3556 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3557 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3558 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3559 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3560 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3563 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3564 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3565 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3566 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3567 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3568 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3571 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 3574 | // Checks whether an argument of launch_bounds attribute is acceptable |
| 3575 | // May output an error. |
| 3576 | static bool checkLaunchBoundsArgument(Sema &S, Expr *E, |
| 3577 | const CUDALaunchBoundsAttr &Attr, |
| 3578 | const unsigned Idx) { |
| 3579 | |
| 3580 | if (S.DiagnoseUnexpandedParameterPack(E)) |
| 3581 | return false; |
| 3582 | |
| 3583 | // Accept template arguments for now as they depend on something else. |
| 3584 | // We'll get to check them when they eventually get instantiated. |
| 3585 | if (E->isValueDependent()) |
| 3586 | return true; |
| 3587 | |
| 3588 | llvm::APSInt I(64); |
| 3589 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 3590 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 3591 | << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
| 3592 | return false; |
| 3593 | } |
| 3594 | // Make sure we can fit it in 32 bits. |
| 3595 | if (!I.isIntN(32)) { |
| 3596 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 3597 | << 32 << /* Unsigned */ 1; |
| 3598 | return false; |
| 3599 | } |
| 3600 | if (I < 0) |
| 3601 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 3602 | << &Attr << Idx << E->getSourceRange(); |
| 3603 | |
| 3604 | return true; |
| 3605 | } |
| 3606 | |
| 3607 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 3608 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 3609 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 3610 | SpellingListIndex); |
| 3611 | |
| 3612 | if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3613 | return; |
| 3614 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 3615 | if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1)) |
| 3616 | return; |
| 3617 | |
| 3618 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 3619 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 3620 | } |
| 3621 | |
| 3622 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3623 | const AttributeList &Attr) { |
| 3624 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 3625 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 3626 | return; |
| 3627 | |
| 3628 | S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3629 | Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr, |
| 3630 | Attr.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3633 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3634 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3635 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3636 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3637 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3638 | return; |
| 3639 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3640 | |
| 3641 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3642 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3643 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3644 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3645 | |
| 3646 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3647 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3648 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3649 | return; |
| 3650 | } |
| 3651 | |
| 3652 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3653 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3654 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3655 | return; |
| 3656 | |
| 3657 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3658 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3659 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3660 | return; |
| 3661 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3662 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3663 | if (IsPointer) { |
| 3664 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3665 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3666 | if (!BufferTy->isPointerType()) { |
| 3667 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3668 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3672 | D->addAttr(::new (S.Context) |
| 3673 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3674 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3675 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3679 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3680 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3681 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3682 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3683 | return; |
| 3684 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3685 | |
| 3686 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3687 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3688 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3689 | if (!isa<VarDecl>(D)) { |
| 3690 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3691 | << Attr.getName() << ExpectedVariable; |
| 3692 | return; |
| 3693 | } |
| 3694 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3695 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3696 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3697 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3698 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3699 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3700 | D->addAttr(::new (S.Context) |
| 3701 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3702 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3703 | Attr.getLayoutCompatible(), |
| 3704 | Attr.getMustBeNull(), |
| 3705 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3708 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3709 | // Checker-specific attribute handlers. |
| 3710 | //===----------------------------------------------------------------------===// |
| 3711 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3712 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3713 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3714 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3717 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3718 | return type->isDependentType() || |
| 3719 | type->isObjCObjectPointerType() || |
| 3720 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3721 | } |
| 3722 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3723 | return type->isDependentType() || |
| 3724 | type->isPointerType() || |
| 3725 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3728 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3729 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3730 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3731 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3732 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3733 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3734 | cf = false; |
| 3735 | } else { |
| 3736 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3737 | cf = true; |
| 3738 | } |
| 3739 | |
| 3740 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3741 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3742 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3743 | return; |
| 3744 | } |
| 3745 | |
| 3746 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3747 | param->addAttr(::new (S.Context) |
| 3748 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3749 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3750 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3751 | param->addAttr(::new (S.Context) |
| 3752 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3753 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3756 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3757 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3758 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3759 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3760 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3761 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3762 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3763 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3764 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3765 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3766 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3767 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3768 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3769 | returnType = FD->getReturnType(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 3770 | else if (auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 3771 | returnType = Param->getType()->getPointeeType(); |
| 3772 | if (returnType.isNull()) { |
| 3773 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 3774 | << Attr.getName() << /*pointer-to-CF*/2 |
| 3775 | << Attr.getRange(); |
| 3776 | return; |
| 3777 | } |
| 3778 | } else { |
| 3779 | AttributeDeclKind ExpectedDeclKind; |
| 3780 | switch (Attr.getKind()) { |
| 3781 | default: llvm_unreachable("invalid ownership attribute"); |
| 3782 | case AttributeList::AT_NSReturnsRetained: |
| 3783 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3784 | case AttributeList::AT_NSReturnsNotRetained: |
| 3785 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 3786 | break; |
| 3787 | |
| 3788 | case AttributeList::AT_CFReturnsRetained: |
| 3789 | case AttributeList::AT_CFReturnsNotRetained: |
| 3790 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 3791 | break; |
| 3792 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3793 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 3794 | << Attr.getRange() << Attr.getName() << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3795 | return; |
| 3796 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3797 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3798 | bool typeOK; |
| 3799 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3800 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3801 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3802 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3803 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3804 | cf = false; |
| 3805 | break; |
| 3806 | |
| 3807 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3808 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3809 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3810 | cf = false; |
| 3811 | break; |
| 3812 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3813 | case AttributeList::AT_CFReturnsRetained: |
| 3814 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3815 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3816 | cf = true; |
| 3817 | break; |
| 3818 | } |
| 3819 | |
| 3820 | if (!typeOK) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 3821 | if (isa<ParmVarDecl>(D)) { |
| 3822 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 3823 | << Attr.getName() << /*pointer-to-CF*/2 |
| 3824 | << Attr.getRange(); |
| 3825 | } else { |
| 3826 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 3827 | enum : unsigned { |
| 3828 | Function, |
| 3829 | Method, |
| 3830 | Property |
| 3831 | } SubjectKind = Function; |
| 3832 | if (isa<ObjCMethodDecl>(D)) |
| 3833 | SubjectKind = Method; |
| 3834 | else if (isa<ObjCPropertyDecl>(D)) |
| 3835 | SubjectKind = Property; |
| 3836 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 3837 | << Attr.getName() << SubjectKind << cf |
| 3838 | << Attr.getRange(); |
| 3839 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3840 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3841 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3842 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3843 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3844 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3845 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3846 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3847 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 3848 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3849 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3850 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3851 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 3852 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3853 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3854 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3855 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 3856 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3857 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3858 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3859 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 3860 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3861 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3862 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3863 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 3864 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3865 | return; |
| 3866 | }; |
| 3867 | } |
| 3868 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3869 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3870 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3871 | const int EP_ObjCMethod = 1; |
| 3872 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3873 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3874 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3875 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3876 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3877 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3878 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3879 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3880 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3881 | if (!resultType->isReferenceType() && |
| 3882 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3883 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3884 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3885 | << attr.getName() |
| 3886 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3887 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3888 | |
| 3889 | // Drop the attribute. |
| 3890 | return; |
| 3891 | } |
| 3892 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3893 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 3894 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3897 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3898 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3899 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3900 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3901 | DeclContext *DC = method->getDeclContext(); |
| 3902 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3903 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3904 | << attr.getName() << 0; |
| 3905 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3906 | return; |
| 3907 | } |
| 3908 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3909 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3910 | << attr.getName() << 1; |
| 3911 | return; |
| 3912 | } |
| 3913 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3914 | method->addAttr(::new (S.Context) |
| 3915 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3916 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3919 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3920 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3921 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3922 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3923 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3924 | D->addAttr(::new (S.Context) |
| 3925 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3926 | Attr.getAttributeSpellingListIndex())); |
| 3927 | } |
| 3928 | |
| 3929 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3930 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3931 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3932 | return; |
| 3933 | |
| 3934 | D->addAttr(::new (S.Context) |
| 3935 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3936 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3939 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3940 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3941 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3942 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3943 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3944 | 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] | 3945 | return; |
| 3946 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 3947 | |
| 3948 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 3949 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 3950 | if (!Parm->Ident->isStr("id")) { |
| 3951 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 3952 | << Attr.getName(); |
| 3953 | return; |
| 3954 | } |
| 3955 | |
| 3956 | // Only allow 'cv void *'. |
| 3957 | QualType T = TD->getUnderlyingType(); |
| 3958 | if (!T->isVoidPointerType()) { |
| 3959 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 3960 | return; |
| 3961 | } |
| 3962 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3963 | |
| 3964 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3965 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3966 | Attr.getAttributeSpellingListIndex())); |
| 3967 | } |
| 3968 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3969 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3970 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3971 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 3972 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3973 | if (!Parm) { |
| 3974 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3975 | return; |
| 3976 | } |
| 3977 | |
| 3978 | D->addAttr(::new (S.Context) |
| 3979 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3980 | Attr.getAttributeSpellingListIndex())); |
| 3981 | } |
| 3982 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3983 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3984 | const AttributeList &Attr) { |
| 3985 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3986 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3987 | if (!RelatedClass) { |
| 3988 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3989 | return; |
| 3990 | } |
| 3991 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3992 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3993 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3994 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3995 | D->addAttr(::new (S.Context) |
| 3996 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3997 | ClassMethod, InstanceMethod, |
| 3998 | Attr.getAttributeSpellingListIndex())); |
| 3999 | } |
| 4000 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4001 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 4002 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4003 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4004 | if (ObjCCategoryDecl *CatDecl = |
| 4005 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4006 | IFace = CatDecl->getClassInterface(); |
| 4007 | else |
| 4008 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4009 | |
| 4010 | if (!IFace) |
| 4011 | return; |
| 4012 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4013 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4014 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4015 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 4016 | Attr.getAttributeSpellingListIndex())); |
| 4017 | } |
| 4018 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4019 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 4020 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4021 | StringRef MetaDataName; |
| 4022 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 4023 | return; |
| 4024 | D->addAttr(::new (S.Context) |
| 4025 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 4026 | MetaDataName, |
| 4027 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4030 | // when a user wants to use objc_boxable with a union or struct |
| 4031 | // but she doesn't have access to the declaration (legacy/third-party code) |
| 4032 | // then she can 'enable' this feature via trick with a typedef |
| 4033 | // e.g.: |
| 4034 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
| 4035 | static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4036 | bool notify = false; |
| 4037 | |
| 4038 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4039 | if (RD && RD->getDefinition()) { |
| 4040 | RD = RD->getDefinition(); |
| 4041 | notify = true; |
| 4042 | } |
| 4043 | |
| 4044 | if (RD) { |
| 4045 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 4046 | ObjCBoxableAttr(Attr.getRange(), S.Context, |
| 4047 | Attr.getAttributeSpellingListIndex()); |
| 4048 | RD->addAttr(BoxableAttr); |
| 4049 | if (notify) { |
| 4050 | // we need to notify ASTReader/ASTWriter about |
| 4051 | // modification of existing declaration |
| 4052 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 4053 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4058 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4059 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4060 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4061 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4062 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4063 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4066 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4067 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4068 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4069 | QualType type = vd->getType(); |
| 4070 | |
| 4071 | if (!type->isDependentType() && |
| 4072 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4073 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4074 | << type; |
| 4075 | return; |
| 4076 | } |
| 4077 | |
| 4078 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4079 | |
| 4080 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4081 | // going to infer. |
| 4082 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4083 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4084 | |
| 4085 | switch (lifetime) { |
| 4086 | case Qualifiers::OCL_None: |
| 4087 | assert(type->isDependentType() && |
| 4088 | "didn't infer lifetime for non-dependent type?"); |
| 4089 | break; |
| 4090 | |
| 4091 | case Qualifiers::OCL_Weak: // meaningful |
| 4092 | case Qualifiers::OCL_Strong: // meaningful |
| 4093 | break; |
| 4094 | |
| 4095 | case Qualifiers::OCL_ExplicitNone: |
| 4096 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4097 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4098 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4099 | break; |
| 4100 | } |
| 4101 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4102 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4103 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4104 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4107 | //===----------------------------------------------------------------------===// |
| 4108 | // Microsoft specific attribute handlers. |
| 4109 | //===----------------------------------------------------------------------===// |
| 4110 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4111 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 4112 | if (!S.LangOpts.CPlusPlus) { |
| 4113 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4114 | << Attr.getName() << AttributeLangSupport::C; |
| 4115 | return; |
| 4116 | } |
| 4117 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 4118 | if (!isa<CXXRecordDecl>(D)) { |
| 4119 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4120 | << Attr.getName() << ExpectedClass; |
| 4121 | return; |
| 4122 | } |
| 4123 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4124 | StringRef StrRef; |
| 4125 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4126 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4127 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4128 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4129 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4130 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4131 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4132 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4133 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4134 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4135 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4136 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4137 | return; |
| 4138 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4139 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4140 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4141 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4142 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4143 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4144 | return; |
| 4145 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4146 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4147 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4148 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4149 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4150 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4151 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4152 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4153 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4156 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4157 | if (!S.LangOpts.CPlusPlus) { |
| 4158 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4159 | << Attr.getName() << AttributeLangSupport::C; |
| 4160 | return; |
| 4161 | } |
| 4162 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4163 | D, Attr.getRange(), /*BestCase=*/true, |
| 4164 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4165 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
| 4166 | if (IA) |
| 4167 | D->addAttr(IA); |
| 4168 | } |
| 4169 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 4170 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 4171 | const AttributeList &Attr) { |
| 4172 | VarDecl *VD = cast<VarDecl>(D); |
| 4173 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 4174 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 4175 | return; |
| 4176 | } |
| 4177 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 4178 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 4179 | return; |
| 4180 | } |
| 4181 | if (VD->hasLocalStorage()) { |
| 4182 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 4183 | return; |
| 4184 | } |
| 4185 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 4186 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4187 | } |
| 4188 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4189 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 4190 | const AttributeList &Attr) { |
| 4191 | // Check the attribute arguments. |
| 4192 | if (Attr.getNumArgs() > 1) { |
| 4193 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4194 | << Attr.getName() << 1; |
| 4195 | return; |
| 4196 | } |
| 4197 | |
| 4198 | StringRef Str; |
| 4199 | SourceLocation ArgLoc; |
| 4200 | |
| 4201 | if (Attr.getNumArgs() == 0) |
| 4202 | Str = ""; |
| 4203 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4204 | return; |
| 4205 | |
| 4206 | ARMInterruptAttr::InterruptType Kind; |
| 4207 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4208 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4209 | << Attr.getName() << Str << ArgLoc; |
| 4210 | return; |
| 4211 | } |
| 4212 | |
| 4213 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 4214 | D->addAttr(::new (S.Context) |
| 4215 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 4216 | } |
| 4217 | |
| 4218 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 4219 | const AttributeList &Attr) { |
| 4220 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4221 | return; |
| 4222 | |
| 4223 | if (!Attr.isArgExpr(0)) { |
| 4224 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 4225 | << AANT_ArgumentIntegerConstant; |
| 4226 | return; |
| 4227 | } |
| 4228 | |
| 4229 | // FIXME: Check for decl - it should be void ()(void). |
| 4230 | |
| 4231 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4232 | llvm::APSInt NumParams(32); |
| 4233 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 4234 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4235 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4236 | << NumParamsExpr->getSourceRange(); |
| 4237 | return; |
| 4238 | } |
| 4239 | |
| 4240 | unsigned Num = NumParams.getLimitedValue(255); |
| 4241 | if ((Num & 1) || Num > 30) { |
| 4242 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 4243 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 4244 | << NumParamsExpr->getSourceRange(); |
| 4245 | return; |
| 4246 | } |
| 4247 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4248 | D->addAttr(::new (S.Context) |
| 4249 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 4250 | Attr.getAttributeSpellingListIndex())); |
| 4251 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4255 | // Dispatch the interrupt attribute based on the current target. |
| 4256 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 4257 | handleMSP430InterruptAttr(S, D, Attr); |
| 4258 | else |
| 4259 | handleARMInterruptAttr(S, D, Attr); |
| 4260 | } |
| 4261 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4262 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 4263 | const AttributeList &Attr) { |
| 4264 | uint32_t NumRegs; |
| 4265 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4266 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4267 | return; |
| 4268 | |
| 4269 | D->addAttr(::new (S.Context) |
| 4270 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, |
| 4271 | NumRegs, |
| 4272 | Attr.getAttributeSpellingListIndex())); |
| 4273 | } |
| 4274 | |
| 4275 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 4276 | const AttributeList &Attr) { |
| 4277 | uint32_t NumRegs; |
| 4278 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4279 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4280 | return; |
| 4281 | |
| 4282 | D->addAttr(::new (S.Context) |
| 4283 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, |
| 4284 | NumRegs, |
| 4285 | Attr.getAttributeSpellingListIndex())); |
| 4286 | } |
| 4287 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4288 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 4289 | const AttributeList& Attr) { |
| 4290 | // If we try to apply it to a function pointer, don't warn, but don't |
| 4291 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 4292 | // special about calling a force_align_arg_pointer function. |
| 4293 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 4294 | if (VD && VD->getType()->isFunctionPointerType()) |
| 4295 | return; |
| 4296 | // Also don't warn on function pointer typedefs. |
| 4297 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 4298 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 4299 | TD->getUnderlyingType()->isFunctionType())) |
| 4300 | return; |
| 4301 | // Attribute can only be applied to function types. |
| 4302 | if (!isa<FunctionDecl>(D)) { |
| 4303 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4304 | << Attr.getName() << /* function */0; |
| 4305 | return; |
| 4306 | } |
| 4307 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4308 | D->addAttr(::new (S.Context) |
| 4309 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 4310 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 4314 | unsigned AttrSpellingListIndex) { |
| 4315 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4316 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4317 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4321 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4322 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4323 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4326 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 4327 | unsigned AttrSpellingListIndex) { |
| 4328 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4329 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4330 | D->dropAttr<DLLImportAttr>(); |
| 4331 | } |
| 4332 | |
| 4333 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4334 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4335 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4336 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4339 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 4340 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 4341 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4342 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 4343 | << A.getName(); |
| 4344 | return; |
| 4345 | } |
| 4346 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 4347 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4348 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 4349 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4350 | // MinGW doesn't allow dllimport on inline functions. |
| 4351 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 4352 | << A.getName(); |
| 4353 | return; |
| 4354 | } |
| 4355 | } |
| 4356 | |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 4357 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 4358 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4359 | MD->getParent()->isLambda()) { |
| 4360 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName(); |
| 4361 | return; |
| 4362 | } |
| 4363 | } |
| 4364 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4365 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 4366 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 4367 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 4368 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4369 | if (NewAttr) |
| 4370 | D->addAttr(NewAttr); |
| 4371 | } |
| 4372 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4373 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4374 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4375 | unsigned AttrSpellingListIndex, |
| 4376 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 4377 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 4378 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4379 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4380 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 4381 | << 1 /*previous declaration*/; |
| 4382 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 4383 | D->dropAttr<MSInheritanceAttr>(); |
| 4384 | } |
| 4385 | |
| 4386 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 4387 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4388 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 4389 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4390 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4391 | } |
| 4392 | } else { |
| 4393 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 4394 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 4395 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4396 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4397 | } |
| 4398 | if (RD->getDescribedClassTemplate()) { |
| 4399 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 4400 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4401 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4406 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4409 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4410 | // The capability attributes take a single string parameter for the name of |
| 4411 | // the capability they represent. The lockable attribute does not take any |
| 4412 | // parameters. However, semantically, both attributes represent the same |
| 4413 | // concept, and so they use the same semantic attribute. Eventually, the |
| 4414 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4415 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 4416 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4417 | // literal will be considered a "mutex." |
| 4418 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4419 | SourceLocation LiteralLoc; |
| 4420 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 4421 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 4422 | return; |
| 4423 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4424 | // Currently, there are only two names allowed for a capability: role and |
| 4425 | // mutex (case insensitive). Diagnose other capability names. |
| 4426 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 4427 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 4428 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4429 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 4430 | Attr.getAttributeSpellingListIndex())); |
| 4431 | } |
| 4432 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4433 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 4434 | const AttributeList &Attr) { |
| 4435 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 4436 | Attr.getArgAsExpr(0), |
| 4437 | Attr.getAttributeSpellingListIndex())); |
| 4438 | } |
| 4439 | |
| 4440 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 4441 | const AttributeList &Attr) { |
| 4442 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4443 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4444 | return; |
| 4445 | |
| 4446 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 4447 | S.Context, |
| 4448 | Args.data(), Args.size(), |
| 4449 | Attr.getAttributeSpellingListIndex())); |
| 4450 | } |
| 4451 | |
| 4452 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 4453 | const AttributeList &Attr) { |
| 4454 | SmallVector<Expr*, 2> Args; |
| 4455 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 4456 | return; |
| 4457 | |
| 4458 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 4459 | S.Context, |
| 4460 | Attr.getArgAsExpr(0), |
| 4461 | Args.data(), |
| 4462 | Args.size(), |
| 4463 | Attr.getAttributeSpellingListIndex())); |
| 4464 | } |
| 4465 | |
| 4466 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 4467 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4468 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4469 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 4470 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4471 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4472 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 4473 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 4474 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4477 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 4478 | const AttributeList &Attr) { |
| 4479 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4480 | return; |
| 4481 | |
| 4482 | // check that all arguments are lockable objects |
| 4483 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 4484 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4485 | if (Args.empty()) |
| 4486 | return; |
| 4487 | |
| 4488 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 4489 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 4490 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 4491 | |
| 4492 | D->addAttr(RCA); |
| 4493 | } |
| 4494 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4495 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4496 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 4497 | if (NSD->isAnonymousNamespace()) { |
| 4498 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 4499 | // Do not want to attach the attribute to the namespace because that will |
| 4500 | // cause confusing diagnostic reports for uses of declarations within the |
| 4501 | // namespace. |
| 4502 | return; |
| 4503 | } |
| 4504 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 4505 | |
| 4506 | if (!S.getLangOpts().CPlusPlus14) |
| 4507 | if (Attr.isCXX11Attribute() && |
| 4508 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Saleem Abdulrasool | b47d606 | 2015-02-18 04:33:26 +0000 | [diff] [blame] | 4509 | S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 4510 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4511 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
| 4512 | } |
| 4513 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4514 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4515 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4516 | return; |
| 4517 | |
| 4518 | std::vector<std::string> Sanitizers; |
| 4519 | |
| 4520 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 4521 | StringRef SanitizerName; |
| 4522 | SourceLocation LiteralLoc; |
| 4523 | |
| 4524 | if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc)) |
| 4525 | return; |
| 4526 | |
| 4527 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 4528 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
| 4529 | |
| 4530 | Sanitizers.push_back(SanitizerName); |
| 4531 | } |
| 4532 | |
| 4533 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 4534 | Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 4535 | Attr.getAttributeSpellingListIndex())); |
| 4536 | } |
| 4537 | |
| 4538 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 4539 | const AttributeList &Attr) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 4540 | StringRef AttrName = Attr.getName()->getName(); |
| 4541 | normalizeName(AttrName); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4542 | std::string SanitizerName = |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 4543 | llvm::StringSwitch<std::string>(AttrName) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4544 | .Case("no_address_safety_analysis", "address") |
| 4545 | .Case("no_sanitize_address", "address") |
| 4546 | .Case("no_sanitize_thread", "thread") |
Peter Collingbourne | 9441094 | 2015-05-15 20:11:18 +0000 | [diff] [blame] | 4547 | .Case("no_sanitize_memory", "memory"); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 4548 | D->addAttr(::new (S.Context) |
| 4549 | NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, |
| 4550 | Attr.getAttributeSpellingListIndex())); |
| 4551 | } |
| 4552 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4553 | /// Handles semantic checking for features that are common to all attributes, |
| 4554 | /// such as checking whether a parameter was properly specified, or the correct |
| 4555 | /// number of arguments were passed, etc. |
| 4556 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4557 | const AttributeList &Attr) { |
| 4558 | // Several attributes carry different semantics than the parsing requires, so |
| 4559 | // those are opted out of the common handling. |
| 4560 | // |
| 4561 | // We also bail on unknown and ignored attributes because those are handled |
| 4562 | // as part of the target-specific handling logic. |
| 4563 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4564 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4565 | return false; |
| 4566 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4567 | // Check whether the attribute requires specific language extensions to be |
| 4568 | // enabled. |
| 4569 | if (!Attr.diagnoseLangOpts(S)) |
| 4570 | return true; |
| 4571 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 4572 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 4573 | // If there are no optional arguments, then checking for the argument count |
| 4574 | // is trivial. |
| 4575 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4576 | return true; |
| 4577 | } else { |
| 4578 | // There are optional arguments, so checking is slightly more involved. |
| 4579 | if (Attr.getMinArgs() && |
| 4580 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 4581 | return true; |
| 4582 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 4583 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 4584 | return true; |
| 4585 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4586 | |
| 4587 | // Check whether the attribute appertains to the given subject. |
| 4588 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 4589 | return true; |
| 4590 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4591 | return false; |
| 4592 | } |
| 4593 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4594 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4595 | // Top Level Sema Entry Points |
| 4596 | //===----------------------------------------------------------------------===// |
| 4597 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4598 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4599 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4600 | /// silently ignore it if a GNU attribute. |
| 4601 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4602 | const AttributeList &Attr, |
| 4603 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4604 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4605 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4606 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4607 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4608 | // instead. |
| 4609 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4610 | return; |
| 4611 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4612 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 4613 | // which do not apply to the current target architecture are treated as |
| 4614 | // though they were unknown attributes. |
| 4615 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 4616 | !Attr.existsInTarget(S.Context.getTargetInfo())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4617 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 4618 | ? diag::warn_unhandled_ms_attribute_ignored |
| 4619 | : diag::warn_unknown_attribute_ignored) |
| 4620 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4621 | return; |
| 4622 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4623 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4624 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4625 | return; |
| 4626 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4627 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4628 | default: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4629 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4630 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4631 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4632 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4633 | handleInterruptAttr(S, D, Attr); |
| 4634 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4635 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4636 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 4637 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4638 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4639 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4640 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4641 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4642 | case AttributeList::AT_Mips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4643 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); |
| 4644 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4645 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4646 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 4647 | break; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4648 | case AttributeList::AT_AMDGPUNumVGPR: |
| 4649 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 4650 | break; |
| 4651 | case AttributeList::AT_AMDGPUNumSGPR: |
| 4652 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 4653 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 4654 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4655 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 4656 | break; |
| 4657 | case AttributeList::AT_IBOutlet: |
| 4658 | handleIBOutlet(S, D, Attr); |
| 4659 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4660 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4661 | handleIBOutletCollection(S, D, Attr); |
| 4662 | break; |
| 4663 | case AttributeList::AT_Alias: |
| 4664 | handleAliasAttr(S, D, Attr); |
| 4665 | break; |
| 4666 | case AttributeList::AT_Aligned: |
| 4667 | handleAlignedAttr(S, D, Attr); |
| 4668 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 4669 | case AttributeList::AT_AlignValue: |
| 4670 | handleAlignValueAttr(S, D, Attr); |
| 4671 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4672 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4673 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4674 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4675 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4676 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 4677 | break; |
| 4678 | case AttributeList::AT_TLSModel: |
| 4679 | handleTLSModelAttr(S, D, Attr); |
| 4680 | break; |
| 4681 | case AttributeList::AT_Annotate: |
| 4682 | handleAnnotateAttr(S, D, Attr); |
| 4683 | break; |
| 4684 | case AttributeList::AT_Availability: |
| 4685 | handleAvailabilityAttr(S, D, Attr); |
| 4686 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4687 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4688 | handleDependencyAttr(S, scope, D, Attr); |
| 4689 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4690 | case AttributeList::AT_Common: |
| 4691 | handleCommonAttr(S, D, Attr); |
| 4692 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4693 | case AttributeList::AT_CUDAConstant: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4694 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); |
| 4695 | break; |
| 4696 | case AttributeList::AT_Constructor: |
| 4697 | handleConstructorAttr(S, D, Attr); |
| 4698 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4699 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4700 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 4701 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4702 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4703 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4704 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4705 | case AttributeList::AT_Destructor: |
| 4706 | handleDestructorAttr(S, D, Attr); |
| 4707 | break; |
| 4708 | case AttributeList::AT_EnableIf: |
| 4709 | handleEnableIfAttr(S, D, Attr); |
| 4710 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4711 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4712 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4713 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4714 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4715 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4716 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4717 | case AttributeList::AT_OptimizeNone: |
| 4718 | handleOptimizeNoneAttr(S, D, Attr); |
| 4719 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 4720 | case AttributeList::AT_FlagEnum: |
| 4721 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 4722 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 4723 | case AttributeList::AT_Flatten: |
| 4724 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 4725 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4726 | case AttributeList::AT_Format: |
| 4727 | handleFormatAttr(S, D, Attr); |
| 4728 | break; |
| 4729 | case AttributeList::AT_FormatArg: |
| 4730 | handleFormatArgAttr(S, D, Attr); |
| 4731 | break; |
| 4732 | case AttributeList::AT_CUDAGlobal: |
| 4733 | handleGlobalAttr(S, D, Attr); |
| 4734 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_CUDADevice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4736 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); |
| 4737 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4738 | case AttributeList::AT_CUDAHost: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4739 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); |
| 4740 | break; |
| 4741 | case AttributeList::AT_GNUInline: |
| 4742 | handleGNUInlineAttr(S, D, Attr); |
| 4743 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4744 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4745 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4746 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 4747 | case AttributeList::AT_Restrict: |
| 4748 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4749 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4750 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4751 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 4752 | break; |
| 4753 | case AttributeList::AT_Mode: |
| 4754 | handleModeAttr(S, D, Attr); |
| 4755 | break; |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 4756 | case AttributeList::AT_NoAlias: |
| 4757 | handleSimpleAttribute<NoAliasAttr>(S, D, Attr); |
| 4758 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4759 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4760 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 4761 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 4762 | case AttributeList::AT_NoSplitStack: |
| 4763 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 4764 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 4765 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4766 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 4767 | handleNonNullAttrParameter(S, PVD, Attr); |
| 4768 | else |
| 4769 | handleNonNullAttr(S, D, Attr); |
| 4770 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 4771 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4772 | handleReturnsNonNullAttr(S, D, Attr); |
| 4773 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4774 | case AttributeList::AT_AssumeAligned: |
| 4775 | handleAssumeAlignedAttr(S, D, Attr); |
| 4776 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4777 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4778 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 4779 | break; |
| 4780 | case AttributeList::AT_Ownership: |
| 4781 | handleOwnershipAttr(S, D, Attr); |
| 4782 | break; |
| 4783 | case AttributeList::AT_Cold: |
| 4784 | handleColdAttr(S, D, Attr); |
| 4785 | break; |
| 4786 | case AttributeList::AT_Hot: |
| 4787 | handleHotAttr(S, D, Attr); |
| 4788 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4789 | case AttributeList::AT_Naked: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4790 | handleSimpleAttribute<NakedAttr>(S, D, Attr); |
| 4791 | break; |
| 4792 | case AttributeList::AT_NoReturn: |
| 4793 | handleNoReturnAttr(S, D, Attr); |
| 4794 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4795 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4796 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 4797 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4798 | case AttributeList::AT_CUDAShared: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4799 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); |
| 4800 | break; |
| 4801 | case AttributeList::AT_VecReturn: |
| 4802 | handleVecReturnAttr(S, D, Attr); |
| 4803 | break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4804 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4805 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4806 | handleObjCOwnershipAttr(S, D, Attr); |
| 4807 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4808 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4809 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 4810 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4811 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4812 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4813 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 4814 | break; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4815 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4816 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4817 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 4818 | break; |
| 4819 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4820 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4821 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 4822 | break; |
| 4823 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4824 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4825 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 4826 | break; |
| 4827 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4828 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4829 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 4830 | break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4831 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4832 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4833 | handleObjCDesignatedInitializer(S, D, Attr); |
| 4834 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4835 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_ObjCRuntimeName: |
| 4837 | handleObjCRuntimeName(S, D, Attr); |
| 4838 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4839 | |
| 4840 | case AttributeList::AT_ObjCBoxable: |
| 4841 | handleObjCBoxable(S, D, Attr); |
| 4842 | break; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4843 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4844 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4845 | handleCFAuditedTransferAttr(S, D, Attr); |
| 4846 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4847 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4848 | handleCFUnknownTransferAttr(S, D, Attr); |
| 4849 | break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4850 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4851 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4852 | case AttributeList::AT_NSConsumed: |
| 4853 | handleNSConsumedAttr(S, D, Attr); |
| 4854 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4855 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4856 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 4857 | break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4858 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4859 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4860 | case AttributeList::AT_NSReturnsNotRetained: |
| 4861 | case AttributeList::AT_CFReturnsNotRetained: |
| 4862 | case AttributeList::AT_NSReturnsRetained: |
| 4863 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4864 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 4865 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4866 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4867 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 4868 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4869 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4870 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 4871 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4872 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4873 | handleVecTypeHint(S, D, Attr); |
| 4874 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4875 | |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4876 | case AttributeList::AT_InitPriority: |
| 4877 | handleInitPriorityAttr(S, D, Attr); |
| 4878 | break; |
| 4879 | |
| 4880 | case AttributeList::AT_Packed: |
| 4881 | handlePackedAttr(S, D, Attr); |
| 4882 | break; |
| 4883 | case AttributeList::AT_Section: |
| 4884 | handleSectionAttr(S, D, Attr); |
| 4885 | break; |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 4886 | case AttributeList::AT_Target: |
| 4887 | handleTargetAttr(S, D, Attr); |
| 4888 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4889 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4890 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4891 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4892 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 4893 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 4894 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4895 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4896 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 4897 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4898 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 4899 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4900 | break; |
| 4901 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4902 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 4903 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 4904 | case AttributeList::AT_Unused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4905 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); |
| 4906 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4907 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4908 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 4909 | break; |
| 4910 | case AttributeList::AT_Used: |
| 4911 | handleUsedAttr(S, D, Attr); |
| 4912 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4913 | case AttributeList::AT_Visibility: |
| 4914 | handleVisibilityAttr(S, D, Attr, false); |
| 4915 | break; |
| 4916 | case AttributeList::AT_TypeVisibility: |
| 4917 | handleVisibilityAttr(S, D, Attr, true); |
| 4918 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4919 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4920 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 4921 | break; |
| 4922 | case AttributeList::AT_WarnUnusedResult: |
| 4923 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4924 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4925 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4926 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 4927 | break; |
| 4928 | case AttributeList::AT_WeakRef: |
| 4929 | handleWeakRefAttr(S, D, Attr); |
| 4930 | break; |
| 4931 | case AttributeList::AT_WeakImport: |
| 4932 | handleWeakImportAttr(S, D, Attr); |
| 4933 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4934 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4935 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4936 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4937 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4938 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 4939 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4940 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4941 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4942 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4943 | case AttributeList::AT_ObjCNSObject: |
| 4944 | handleObjCNSObject(S, D, Attr); |
| 4945 | break; |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4946 | case AttributeList::AT_ObjCIndependentClass: |
| 4947 | handleObjCIndependentClass(S, D, Attr); |
| 4948 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4949 | case AttributeList::AT_Blocks: |
| 4950 | handleBlocksAttr(S, D, Attr); |
| 4951 | break; |
| 4952 | case AttributeList::AT_Sentinel: |
| 4953 | handleSentinelAttr(S, D, Attr); |
| 4954 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4955 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4956 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 4957 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4958 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4959 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 4960 | break; |
| 4961 | case AttributeList::AT_Cleanup: |
| 4962 | handleCleanupAttr(S, D, Attr); |
| 4963 | break; |
| 4964 | case AttributeList::AT_NoDebug: |
| 4965 | handleNoDebugAttr(S, D, Attr); |
| 4966 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 4967 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4968 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 4969 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4970 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4971 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 4972 | break; |
| 4973 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 4974 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 4975 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4976 | case AttributeList::AT_StdCall: |
| 4977 | case AttributeList::AT_CDecl: |
| 4978 | case AttributeList::AT_FastCall: |
| 4979 | case AttributeList::AT_ThisCall: |
| 4980 | case AttributeList::AT_Pascal: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4981 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4982 | case AttributeList::AT_MSABI: |
| 4983 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4984 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4985 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4986 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4987 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4988 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4989 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 4990 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4991 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4992 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); |
| 4993 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4994 | |
| 4995 | // Microsoft attributes: |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 4996 | case AttributeList::AT_MSNoVTable: |
| 4997 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 4998 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 4999 | case AttributeList::AT_MSStruct: |
| 5000 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 5001 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5002 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5003 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5004 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 5005 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5006 | handleMSInheritanceAttr(S, D, Attr); |
| 5007 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5008 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5009 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 5010 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5011 | case AttributeList::AT_Thread: |
| 5012 | handleDeclspecThreadAttr(S, D, Attr); |
| 5013 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5014 | |
| 5015 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 5016 | case AttributeList::AT_AssertExclusiveLock: |
| 5017 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 5018 | break; |
| 5019 | case AttributeList::AT_AssertSharedLock: |
| 5020 | handleAssertSharedLockAttr(S, D, Attr); |
| 5021 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5022 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5023 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 5024 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5025 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5026 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5027 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5028 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5029 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 5030 | break; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5031 | case AttributeList::AT_NoSanitize: |
| 5032 | handleNoSanitizeAttr(S, D, Attr); |
| 5033 | break; |
| 5034 | case AttributeList::AT_NoSanitizeSpecific: |
| 5035 | handleNoSanitizeSpecificAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 5036 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5037 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5038 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 5039 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5040 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5041 | handleGuardedByAttr(S, D, Attr); |
| 5042 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5043 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5044 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5045 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5046 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5047 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5048 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5049 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5050 | handleLockReturnedAttr(S, D, Attr); |
| 5051 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5052 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5053 | handleLocksExcludedAttr(S, D, Attr); |
| 5054 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5055 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5056 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5057 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5058 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5059 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5060 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5061 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5062 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5063 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5064 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5065 | // Capability analysis attributes. |
| 5066 | case AttributeList::AT_Capability: |
| 5067 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5068 | handleCapabilityAttr(S, D, Attr); |
| 5069 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5070 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5071 | handleRequiresCapabilityAttr(S, D, Attr); |
| 5072 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5073 | |
| 5074 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5075 | handleAssertCapabilityAttr(S, D, Attr); |
| 5076 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5077 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5078 | handleAcquireCapabilityAttr(S, D, Attr); |
| 5079 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5080 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5081 | handleReleaseCapabilityAttr(S, D, Attr); |
| 5082 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5083 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5084 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 5085 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5086 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 5087 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 5088 | case AttributeList::AT_Consumable: |
| 5089 | handleConsumableAttr(S, D, Attr); |
| 5090 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 5091 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5092 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 5093 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 5094 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5095 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 5096 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 5097 | case AttributeList::AT_CallableWhen: |
| 5098 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 5099 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 5100 | case AttributeList::AT_ParamTypestate: |
| 5101 | handleParamTypestateAttr(S, D, Attr); |
| 5102 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 5103 | case AttributeList::AT_ReturnTypestate: |
| 5104 | handleReturnTypestateAttr(S, D, Attr); |
| 5105 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 5106 | case AttributeList::AT_SetTypestate: |
| 5107 | handleSetTypestateAttr(S, D, Attr); |
| 5108 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 5109 | case AttributeList::AT_TestTypestate: |
| 5110 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 5111 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 5112 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5113 | // Type safety attributes. |
| 5114 | case AttributeList::AT_ArgumentWithTypeTag: |
| 5115 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 5116 | break; |
| 5117 | case AttributeList::AT_TypeTagForDatatype: |
| 5118 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 5119 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5120 | } |
| 5121 | } |
| 5122 | |
| 5123 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5124 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5125 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5126 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5127 | bool IncludeCXX11Attributes) { |
| 5128 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5129 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5130 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5131 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5132 | // GCC accepts |
| 5133 | // static int a9 __attribute__((weakref)); |
| 5134 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5135 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 5136 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 5137 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5138 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5139 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5140 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5141 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 5142 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 5143 | // good to have a way to specify "these attributes must appear as a group", |
| 5144 | // for these. Additionally, it would be good to have a way to specify "these |
| 5145 | // 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] | 5146 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 5147 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5148 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 5149 | // FIXME: This emits a different error message than |
| 5150 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5151 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5152 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 5153 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5154 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5155 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 5156 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5157 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5158 | D->setInvalidDecl(); |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 5159 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 5160 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5161 | << A << ExpectedKernelFunction; |
| 5162 | D->setInvalidDecl(); |
| 5163 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 5164 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5165 | << A << ExpectedKernelFunction; |
| 5166 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5167 | } |
| 5168 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5171 | // Annotation attributes are the only attributes allowed after an access |
| 5172 | // specifier. |
| 5173 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5174 | const AttributeList *AttrList) { |
| 5175 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5176 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 5177 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5178 | } else { |
| 5179 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5180 | return true; |
| 5181 | } |
| 5182 | } |
| 5183 | |
| 5184 | return false; |
| 5185 | } |
| 5186 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5187 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5188 | /// contains any decl attributes that we should warn about. |
| 5189 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5190 | for ( ; A; A = A->getNext()) { |
| 5191 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5192 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5193 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5194 | |
| 5195 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5196 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5197 | << A->getName() << A->getRange(); |
| 5198 | } else { |
| 5199 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5200 | << A->getName() << A->getRange(); |
| 5201 | } |
| 5202 | } |
| 5203 | } |
| 5204 | |
| 5205 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5206 | /// used to build a declaration, complain about any decl attributes |
| 5207 | /// which might be lying around on it. |
| 5208 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5209 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5210 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5211 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5212 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5213 | } |
| 5214 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5215 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5216 | /// \#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] | 5217 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5218 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5219 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5220 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5221 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5222 | FunctionDecl *NewFD; |
| 5223 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5224 | // FIXME: Mangling? |
| 5225 | // FIXME: Is the qualifier info correct? |
| 5226 | // FIXME: Is the DeclContext correct? |
| 5227 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5228 | Loc, Loc, DeclarationName(II), |
| 5229 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5230 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5231 | FD->hasPrototype(), |
| 5232 | false/*isConstexprSpecified*/); |
| 5233 | NewD = NewFD; |
| 5234 | |
| 5235 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5236 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5237 | |
| 5238 | // Fake up parameter variables; they are declared as if this were |
| 5239 | // a typedef. |
| 5240 | QualType FDTy = FD->getType(); |
| 5241 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5242 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5243 | for (const auto &AI : FT->param_types()) { |
| 5244 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5245 | Param->setScopeInfo(0, Params.size()); |
| 5246 | Params.push_back(Param); |
| 5247 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5248 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5249 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5250 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5251 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5252 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5253 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5254 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5255 | if (VD->getQualifier()) { |
| 5256 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5257 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5258 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5259 | } |
| 5260 | return NewD; |
| 5261 | } |
| 5262 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5263 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5264 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5265 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5266 | if (W.getUsed()) return; // only do this once |
| 5267 | W.setUsed(true); |
| 5268 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5269 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5270 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5271 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 5272 | W.getLocation())); |
| 5273 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5274 | WeakTopLevelDecl.push_back(NewD); |
| 5275 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5276 | // to insert Decl at TU scope, sorry. |
| 5277 | DeclContext *SavedContext = CurContext; |
| 5278 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 5279 | NewD->setDeclContext(CurContext); |
| 5280 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5281 | PushOnScopeChains(NewD, S); |
| 5282 | CurContext = SavedContext; |
| 5283 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5284 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5288 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5289 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5290 | // have to do this. |
| 5291 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5292 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5293 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5294 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5295 | if (VD->isExternC()) |
| 5296 | ND = VD; |
| 5297 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5298 | if (FD->isExternC()) |
| 5299 | ND = FD; |
| 5300 | if (ND) { |
| 5301 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 5302 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5303 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5304 | WeakInfo W = I->second; |
| 5305 | DeclApplyPragmaWeak(S, ND, W); |
| 5306 | WeakUndeclaredIdentifiers[Id] = W; |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | } |
| 5311 | } |
| 5312 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5313 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5314 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5315 | /// 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] | 5316 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5317 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5318 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5319 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5320 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5321 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5322 | // position to the decl itself. This handles cases like: |
| 5323 | // int *__attr__(x)** D; |
| 5324 | // when X is a decl attribute. |
| 5325 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5326 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5327 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5328 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5329 | // Finally, apply any attributes on the decl itself. |
| 5330 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5331 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5332 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5333 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5334 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5335 | /// If so, it'll still be annotated with an attribute that makes it |
| 5336 | /// illegal to actually use. |
| 5337 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl, |
| 5338 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame^] | 5339 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5340 | // Private ivars are always okay. Unfortunately, people don't |
| 5341 | // always properly make their ivars private, even in system headers. |
| 5342 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5343 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5344 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5345 | return false; |
| 5346 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame^] | 5347 | // Silently accept unsupported uses of __weak in both user and system |
| 5348 | // declarations when it's been disabled, for ease of integration with |
| 5349 | // -fno-objc-arc files. We do have to take some care against attempts |
| 5350 | // to define such things; for now, we've only done that for ivars |
| 5351 | // and properties. |
| 5352 | if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) { |
| 5353 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 5354 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 5355 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 5356 | return true; |
| 5357 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame^] | 5360 | // Allow all sorts of things in system headers. |
| 5361 | if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) { |
| 5362 | // Currently, all the failures dealt with this way are due to ARC |
| 5363 | // restrictions. |
| 5364 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 5365 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | /// Handle a delayed forbidden-type diagnostic. |
| 5372 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5373 | Decl *decl) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame^] | 5374 | auto reason = UnavailableAttr::IR_None; |
| 5375 | if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) { |
| 5376 | assert(reason && "didn't set reason?"); |
| 5377 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason, |
| 5378 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5379 | return; |
| 5380 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5381 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5382 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5383 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5384 | // kind of forbidden type messages on unavailable functions. |
| 5385 | if (FD->hasAttr<UnavailableAttr>() && |
| 5386 | diag.getForbiddenTypeDiagnostic() == |
| 5387 | diag::err_arc_array_param_no_ownership) { |
| 5388 | diag.Triggered = true; |
| 5389 | return; |
| 5390 | } |
| 5391 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5392 | |
| 5393 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5394 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5395 | diag.Triggered = true; |
| 5396 | } |
| 5397 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5398 | |
| 5399 | static bool isDeclDeprecated(Decl *D) { |
| 5400 | do { |
| 5401 | if (D->isDeprecated()) |
| 5402 | return true; |
| 5403 | // A category implicitly has the availability of the interface. |
| 5404 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5405 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 5406 | return Interface->isDeprecated(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5407 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5408 | return false; |
| 5409 | } |
| 5410 | |
| 5411 | static bool isDeclUnavailable(Decl *D) { |
| 5412 | do { |
| 5413 | if (D->isUnavailable()) |
| 5414 | return true; |
| 5415 | // A category implicitly has the availability of the interface. |
| 5416 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5417 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 5418 | return Interface->isUnavailable(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5419 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5420 | return false; |
| 5421 | } |
| 5422 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5423 | static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5424 | Decl *Ctx, const NamedDecl *D, |
| 5425 | StringRef Message, SourceLocation Loc, |
| 5426 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5427 | const ObjCPropertyDecl *ObjCProperty, |
| 5428 | bool ObjCPropertyAccess) { |
| 5429 | // Diagnostics for deprecated or unavailable. |
| 5430 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5431 | unsigned diag_available_here = diag::note_availability_specified_here; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5432 | |
| 5433 | // Matches 'diag::note_property_attribute' options. |
| 5434 | unsigned property_note_select; |
| 5435 | |
| 5436 | // Matches diag::note_availability_specified_here. |
| 5437 | unsigned available_here_select_kind; |
| 5438 | |
| 5439 | // Don't warn if our current context is deprecated or unavailable. |
| 5440 | switch (K) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5441 | case Sema::AD_Deprecation: |
Jordan Rose | d17c03e | 2015-04-30 17:20:35 +0000 | [diff] [blame] | 5442 | if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx)) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5443 | return; |
| 5444 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 5445 | : diag::warn_property_method_deprecated; |
| 5446 | diag_message = diag::warn_deprecated_message; |
| 5447 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 5448 | property_note_select = /* deprecated */ 0; |
| 5449 | available_here_select_kind = /* deprecated */ 2; |
| 5450 | break; |
| 5451 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5452 | case Sema::AD_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5453 | if (isDeclUnavailable(Ctx)) |
| 5454 | return; |
| 5455 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 5456 | : diag::err_property_method_unavailable; |
| 5457 | diag_message = diag::err_unavailable_message; |
| 5458 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 5459 | property_note_select = /* unavailable */ 1; |
| 5460 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5461 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame^] | 5462 | if (auto attr = D->getAttr<UnavailableAttr>()) { |
| 5463 | if (attr->isImplicit() && attr->getImplicitReason()) { |
| 5464 | // Most of these failures are due to extra restrictions in ARC; |
| 5465 | // reflect that in the primary diagnostic when applicable. |
| 5466 | auto flagARCError = [&] { |
| 5467 | if (S.getLangOpts().ObjCAutoRefCount && |
| 5468 | S.getSourceManager().isInSystemHeader(D->getLocation())) |
| 5469 | diag = diag::err_unavailable_in_arc; |
| 5470 | }; |
| 5471 | |
| 5472 | switch (attr->getImplicitReason()) { |
| 5473 | case UnavailableAttr::IR_None: break; |
| 5474 | |
| 5475 | case UnavailableAttr::IR_ARCForbiddenType: |
| 5476 | flagARCError(); |
| 5477 | diag_available_here = diag::note_arc_forbidden_type; |
| 5478 | break; |
| 5479 | |
| 5480 | case UnavailableAttr::IR_ForbiddenWeak: |
| 5481 | if (S.getLangOpts().ObjCWeakRuntime) |
| 5482 | diag_available_here = diag::note_arc_weak_disabled; |
| 5483 | else |
| 5484 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 5485 | break; |
| 5486 | |
| 5487 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 5488 | flagARCError(); |
| 5489 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 5490 | break; |
| 5491 | |
| 5492 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 5493 | flagARCError(); |
| 5494 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 5495 | break; |
| 5496 | |
| 5497 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 5498 | flagARCError(); |
| 5499 | diag_available_here = diag::note_arc_field_with_ownership; |
| 5500 | break; |
| 5501 | } |
| 5502 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5503 | } |
| 5504 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5505 | break; |
| 5506 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5507 | case Sema::AD_Partial: |
| 5508 | diag = diag::warn_partial_availability; |
| 5509 | diag_message = diag::warn_partial_message; |
| 5510 | diag_fwdclass_message = diag::warn_partial_fwdclass_message; |
| 5511 | property_note_select = /* partial */ 2; |
| 5512 | available_here_select_kind = /* partial */ 3; |
| 5513 | break; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5516 | if (!Message.empty()) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5517 | S.Diag(Loc, diag_message) << D << Message; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5518 | if (ObjCProperty) |
| 5519 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 5520 | << ObjCProperty->getDeclName() << property_note_select; |
| 5521 | } else if (!UnknownObjCClass) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5522 | S.Diag(Loc, diag) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5523 | if (ObjCProperty) |
| 5524 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 5525 | << ObjCProperty->getDeclName() << property_note_select; |
| 5526 | } else { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5527 | S.Diag(Loc, diag_fwdclass_message) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5528 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5529 | } |
| 5530 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 5531 | S.Diag(D->getLocation(), diag_available_here) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5532 | << D << available_here_select_kind; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5533 | if (K == Sema::AD_Partial) |
| 5534 | S.Diag(Loc, diag::note_partial_availability_silence) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 5538 | Decl *Ctx) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5539 | assert(DD.Kind == DelayedDiagnostic::Deprecation || |
| 5540 | DD.Kind == DelayedDiagnostic::Unavailable); |
| 5541 | Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation |
| 5542 | ? Sema::AD_Deprecation |
| 5543 | : Sema::AD_Unavailable; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5544 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5545 | DoEmitAvailabilityWarning( |
| 5546 | S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc, |
| 5547 | DD.getUnknownObjCClass(), DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5550 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5551 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5552 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5553 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5554 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5555 | // When delaying diagnostics to run in the context of a parsed |
| 5556 | // declaration, we only want to actually emit anything if parsing |
| 5557 | // succeeds. |
| 5558 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5559 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5560 | // We emit all the active diagnostics in this pool or any of its |
| 5561 | // parents. In general, we'll get one pool for the decl spec |
| 5562 | // and a child pool for each declarator; in a decl group like: |
| 5563 | // deprecated_typedef foo, *bar, baz(); |
| 5564 | // only the declarator pops will be passed decls. This is correct; |
| 5565 | // we really do need to consider delayed diagnostics from the decl spec |
| 5566 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5567 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5568 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5569 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5570 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5571 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5572 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5573 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5574 | continue; |
| 5575 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5576 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5577 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5578 | case DelayedDiagnostic::Unavailable: |
| 5579 | // Don't bother giving deprecation/unavailable diagnostics if |
| 5580 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5581 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5582 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5583 | break; |
| 5584 | |
| 5585 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5586 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5587 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5588 | |
| 5589 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5590 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5591 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5592 | } |
| 5593 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5594 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5597 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5598 | /// been delayed in the current context instead of in the given pool. |
| 5599 | /// Essentially, this just moves them to the current pool. |
| 5600 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5601 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5602 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5603 | curPool->steal(pool); |
| 5604 | } |
| 5605 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5606 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 5607 | NamedDecl *D, StringRef Message, |
| 5608 | SourceLocation Loc, |
| 5609 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame] | 5610 | const ObjCPropertyDecl *ObjCProperty, |
| 5611 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5612 | // Delay if we're currently parsing a declaration. |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5613 | if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) { |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 5614 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability( |
| 5615 | AD, Loc, D, UnknownObjCClass, ObjCProperty, Message, |
| 5616 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5617 | return; |
| 5618 | } |
| 5619 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5620 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5621 | DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass, |
| 5622 | ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5623 | } |