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" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 34 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 35 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 36 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 37 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 38 | C, |
| 39 | Cpp, |
| 40 | ObjC |
| 41 | }; |
| 42 | } |
| 43 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | // Helper functions |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 48 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 49 | /// type (function or function-typed variable) or an Objective-C |
| 50 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 51 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 52 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 53 | } |
| 54 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 55 | /// Return true if the given decl has a declarator that should have |
| 56 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 58 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 59 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 60 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 63 | /// hasFunctionProto - Return true if the given decl has a argument |
| 64 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 65 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 66 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 67 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 68 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 69 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 72 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 73 | /// 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] | 74 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 75 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 76 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 77 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 79 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 80 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 83 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 84 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 85 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 86 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 87 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 88 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 89 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 92 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 93 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 94 | return FD->getParamDecl(Idx)->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 95 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 96 | return MD->parameters()[Idx]->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 97 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 98 | return BD->getParamDecl(Idx)->getSourceRange(); |
| 99 | return SourceRange(); |
| 100 | } |
| 101 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 102 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 103 | if (const FunctionType *FnTy = D->getFunctionType()) |
Aaron Ballman | 6288d06 | 2014-07-11 16:31:29 +0000 | [diff] [blame] | 104 | return cast<FunctionType>(FnTy)->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 105 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 108 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 109 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 110 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 111 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 112 | return MD->getReturnTypeSourceRange(); |
| 113 | return SourceRange(); |
| 114 | } |
| 115 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 116 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 117 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 118 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 119 | return proto->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 120 | } |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 121 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 122 | return BD->isVariadic(); |
| 123 | |
| 124 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 127 | static bool isInstanceMethod(const Decl *D) { |
| 128 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 129 | return MethodDecl->isInstance(); |
| 130 | return false; |
| 131 | } |
| 132 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 133 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 134 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 135 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 136 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 137 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 138 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 139 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 140 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 141 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 142 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 144 | // FIXME: Should we walk the chain of classes? |
| 145 | return ClsName == &Ctx.Idents.get("NSString") || |
| 146 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 147 | } |
| 148 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 149 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 150 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 151 | if (!PT) |
| 152 | return false; |
| 153 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 154 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 155 | if (!RT) |
| 156 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 158 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 159 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 160 | return false; |
| 161 | |
| 162 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 163 | } |
| 164 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 165 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 166 | // FIXME: Include the type in the argument list. |
| 167 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 168 | } |
| 169 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 170 | template <typename Compare> |
| 171 | static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr, |
| 172 | unsigned Num, unsigned Diag, |
| 173 | Compare Comp) { |
| 174 | if (Comp(getNumAttributeArgs(Attr), Num)) { |
| 175 | S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 182 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 183 | /// output an error. |
| 184 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 185 | unsigned Num) { |
| 186 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 187 | diag::err_attribute_wrong_number_arguments, |
| 188 | std::not_equal_to<unsigned>()); |
| 189 | } |
| 190 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 191 | /// \brief Check if the attribute has at least as many args as Num. May |
| 192 | /// output an error. |
| 193 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 194 | unsigned Num) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 195 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 196 | diag::err_attribute_too_few_arguments, |
| 197 | std::less<unsigned>()); |
| 198 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 199 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 200 | /// \brief Check if the attribute has at most as many args as Num. May |
| 201 | /// output an error. |
| 202 | static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr, |
| 203 | unsigned Num) { |
| 204 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 205 | diag::err_attribute_too_many_arguments, |
| 206 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 209 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 210 | /// expression and return success or failure. May output an error. |
| 211 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 212 | const Expr *Expr, uint32_t &Val, |
| 213 | unsigned Idx = UINT_MAX) { |
| 214 | llvm::APSInt I(32); |
| 215 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 216 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 217 | if (Idx != UINT_MAX) |
| 218 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 219 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 220 | << Expr->getSourceRange(); |
| 221 | else |
| 222 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 223 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 224 | << Expr->getSourceRange(); |
| 225 | return false; |
| 226 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 227 | |
| 228 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 229 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 230 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 234 | Val = (uint32_t)I.getZExtValue(); |
| 235 | return true; |
| 236 | } |
| 237 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 238 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 239 | /// declaration. Returns true if diagnosed. |
| 240 | template <typename AttrTy> |
| 241 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 242 | const AttributeList &Attr) { |
| 243 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 244 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 245 | << Attr.getName() << A; |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 251 | /// \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] | 252 | /// instance method D. May output an error. |
| 253 | /// |
| 254 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 255 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 256 | const AttributeList &Attr, |
| 257 | unsigned AttrArgNum, |
| 258 | const Expr *IdxExpr, |
| 259 | uint64_t &Idx) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 260 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 261 | |
| 262 | // In C++ the implicit 'this' function parameter also counts. |
| 263 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 264 | bool HP = hasFunctionProto(D); |
| 265 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 266 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 267 | unsigned NumParams = |
| 268 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 269 | |
| 270 | llvm::APSInt IdxInt; |
| 271 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 272 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 273 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 274 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 275 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | |
| 279 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 280 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 281 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 282 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | Idx--; // Convert to zero-based. |
| 286 | if (HasImplicitThisParam) { |
| 287 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 288 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 289 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 290 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | --Idx; |
| 294 | } |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 299 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 300 | /// If not emit an error and return false. If the argument is an identifier it |
| 301 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 302 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 303 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 304 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 305 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 306 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 307 | if (Attr.isArgIdent(ArgNum)) { |
| 308 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 309 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 310 | << Attr.getName() << AANT_ArgumentString |
| 311 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 312 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 313 | Str = Loc->Ident->getName(); |
| 314 | if (ArgLocation) |
| 315 | *ArgLocation = Loc->Loc; |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | // Now check for an actual string literal. |
| 320 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 321 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 322 | if (ArgLocation) |
| 323 | *ArgLocation = ArgExpr->getLocStart(); |
| 324 | |
| 325 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 326 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 327 | << Attr.getName() << AANT_ArgumentString; |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | Str = Literal->getString(); |
| 332 | return true; |
| 333 | } |
| 334 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 335 | /// \brief Applies the given attribute to the Decl without performing any |
| 336 | /// additional semantic checking. |
| 337 | template <typename AttrType> |
| 338 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 339 | const AttributeList &Attr) { |
| 340 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 341 | Attr.getAttributeSpellingListIndex())); |
| 342 | } |
| 343 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 344 | /// \brief Check if the passed-in expression is of type int or bool. |
| 345 | static bool isIntOrBool(Expr *Exp) { |
| 346 | QualType QT = Exp->getType(); |
| 347 | return QT->isBooleanType() || QT->isIntegerType(); |
| 348 | } |
| 349 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 350 | |
| 351 | // Check to see if the type is a smart pointer of some kind. We assume |
| 352 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 353 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 354 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 355 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 356 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 357 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 358 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 359 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 360 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 361 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 362 | return false; |
| 363 | |
| 364 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 367 | /// \brief Check if passed in Decl is a pointer type. |
| 368 | /// Note that this function may produce an error message. |
| 369 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 370 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 371 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 372 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 373 | QualType QT = vd->getType(); |
| 374 | if (QT->isAnyPointerType()) |
| 375 | return true; |
| 376 | |
| 377 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 378 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 379 | // (We don't want to force template instantiation if we can avoid it, |
| 380 | // since that would alter the order in which templates are instantiated.) |
| 381 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 382 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 383 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 384 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 385 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 386 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 387 | |
| 388 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 389 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 393 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 394 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 395 | static const RecordType *getRecordType(QualType QT) { |
| 396 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 397 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 398 | |
| 399 | // Now check if we point to record type. |
| 400 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 401 | return PT->getPointeeType()->getAs<RecordType>(); |
| 402 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 403 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 406 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 407 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 408 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 409 | if (!RT) |
| 410 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 411 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 412 | // 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] | 413 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 414 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 415 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 416 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 417 | // FIXME -- Check the type that the smart pointer points to. |
| 418 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 419 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 420 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 421 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 422 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 423 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 424 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 425 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 426 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 427 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 428 | CXXBasePaths BPaths(false, false); |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 429 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P, |
| 430 | void *) { |
| 431 | return BS->getType()->getAs<RecordType>() |
| 432 | ->getDecl()->hasAttr<CapabilityAttr>(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 433 | }, nullptr, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 434 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 435 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 439 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 440 | const auto *TD = Ty->getAs<TypedefType>(); |
| 441 | if (!TD) |
| 442 | return false; |
| 443 | |
| 444 | TypedefNameDecl *TN = TD->getDecl(); |
| 445 | if (!TN) |
| 446 | return false; |
| 447 | |
| 448 | return TN->hasAttr<CapabilityAttr>(); |
| 449 | } |
| 450 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 451 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 452 | if (checkTypedefTypeForCapability(Ty)) |
| 453 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 454 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 455 | if (checkRecordTypeForCapability(S, Ty)) |
| 456 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 457 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 458 | return false; |
| 459 | } |
| 460 | |
| 461 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 462 | // Capability expressions are simple expressions involving the boolean logic |
| 463 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 464 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 465 | // is a capability or not. |
| 466 | |
| 467 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 468 | return typeHasCapability(S, E->getType()); |
| 469 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 470 | return isCapabilityExpr(S, E->getSubExpr()); |
| 471 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 472 | return isCapabilityExpr(S, E->getSubExpr()); |
| 473 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 474 | if (E->getOpcode() == UO_LNot) |
| 475 | return isCapabilityExpr(S, E->getSubExpr()); |
| 476 | return false; |
| 477 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 478 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 479 | return isCapabilityExpr(S, E->getLHS()) && |
| 480 | isCapabilityExpr(S, E->getRHS()); |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 487 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 488 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 489 | /// \param Sidx The attribute argument index to start checking with. |
| 490 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 491 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 492 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 493 | const AttributeList &Attr, |
| 494 | SmallVectorImpl<Expr *> &Args, |
| 495 | int Sidx = 0, |
| 496 | bool ParamIdxOk = false) { |
| 497 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 498 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 499 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 500 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 501 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 502 | Args.push_back(ArgExp); |
| 503 | continue; |
| 504 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 505 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 506 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 507 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 508 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 509 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 510 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 511 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 512 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 513 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 514 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 515 | // We allow constant strings to be used as a placeholder for expressions |
| 516 | // that are not valid C++ syntax, but warn that they are ignored. |
| 517 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 518 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 519 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 520 | continue; |
| 521 | } |
| 522 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 523 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 524 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 525 | // A pointer to member expression of the form &MyClass::mu is treated |
| 526 | // specially -- we need to look at the type of the member. |
| 527 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 528 | if (UOp->getOpcode() == UO_AddrOf) |
| 529 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 530 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 531 | ArgTy = DRE->getDecl()->getType(); |
| 532 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 533 | // 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] | 534 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 535 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 536 | // Now check if we index into a record type function param. |
| 537 | if(!RT && ParamIdxOk) { |
| 538 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 539 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 540 | if(FD && IL) { |
| 541 | unsigned int NumParams = FD->getNumParams(); |
| 542 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 543 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 544 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 545 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 546 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 547 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 548 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 549 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 550 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 554 | // If the type does not have a capability, see if the components of the |
| 555 | // expression have capabilities. This allows for writing C code where the |
| 556 | // capability may be on the type, and the expression is a capability |
| 557 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 558 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 559 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 560 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 561 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 562 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 563 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 566 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 567 | // Attribute Implementations |
| 568 | //===----------------------------------------------------------------------===// |
| 569 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 570 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 571 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 572 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 573 | return; |
| 574 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 575 | D->addAttr(::new (S.Context) |
| 576 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 577 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 580 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 581 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 582 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 583 | SmallVector<Expr*, 1> Args; |
| 584 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 585 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 586 | unsigned Size = Args.size(); |
| 587 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 588 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 589 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 590 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 591 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 592 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 596 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 597 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 598 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 599 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 600 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 601 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 604 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 605 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 606 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 607 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 608 | return; |
| 609 | |
| 610 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 611 | return; |
| 612 | |
| 613 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 614 | S.Context, Arg, |
| 615 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 618 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 619 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 620 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 621 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 622 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 623 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 624 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 625 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 626 | if (!QT->isDependentType()) { |
| 627 | const RecordType *RT = getRecordType(QT); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 628 | if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 629 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 630 | << Attr.getName(); |
| 631 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 635 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 636 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 637 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 638 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 639 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 640 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 643 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 644 | const AttributeList &Attr) { |
| 645 | SmallVector<Expr*, 1> Args; |
| 646 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 647 | return; |
| 648 | |
| 649 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 650 | D->addAttr(::new (S.Context) |
| 651 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 652 | StartArg, Args.size(), |
| 653 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 656 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 657 | const AttributeList &Attr) { |
| 658 | SmallVector<Expr*, 1> Args; |
| 659 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 660 | return; |
| 661 | |
| 662 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 663 | D->addAttr(::new (S.Context) |
| 664 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 665 | StartArg, Args.size(), |
| 666 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 669 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 670 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 671 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 672 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 673 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 674 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 675 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 676 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 677 | } |
| 678 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 679 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 680 | const AttributeList &Attr) { |
| 681 | SmallVector<Expr*, 1> Args; |
| 682 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 683 | return; |
| 684 | |
| 685 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 686 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 687 | D->addAttr(::new (S.Context) |
| 688 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 689 | Attr.getAttributeSpellingListIndex())); |
| 690 | } |
| 691 | |
| 692 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 693 | const AttributeList &Attr) { |
| 694 | SmallVector<Expr*, 1> Args; |
| 695 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 696 | return; |
| 697 | |
| 698 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 699 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 700 | D->addAttr(::new (S.Context) |
| 701 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 702 | StartArg, Size, |
| 703 | Attr.getAttributeSpellingListIndex())); |
| 704 | } |
| 705 | |
| 706 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 707 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 708 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 709 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 710 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 711 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 712 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 713 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 714 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 715 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 716 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 720 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 721 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 722 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 725 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 726 | const AttributeList &Attr) { |
| 727 | SmallVector<Expr*, 2> Args; |
| 728 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 729 | return; |
| 730 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 731 | D->addAttr(::new (S.Context) |
| 732 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 733 | Attr.getArgAsExpr(0), |
| 734 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 735 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 738 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 739 | const AttributeList &Attr) { |
| 740 | SmallVector<Expr*, 2> Args; |
| 741 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 742 | return; |
| 743 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 744 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 745 | Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(), |
| 746 | Args.size(), Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 749 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 750 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 751 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 752 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 753 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 754 | unsigned Size = Args.size(); |
| 755 | if (Size == 0) |
| 756 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 757 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 758 | D->addAttr(::new (S.Context) |
| 759 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 760 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 764 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 765 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 766 | return; |
| 767 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 768 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 769 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 770 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 771 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 772 | if (Size == 0) |
| 773 | return; |
| 774 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 775 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 776 | D->addAttr(::new (S.Context) |
| 777 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 778 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 781 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 782 | Expr *Cond = Attr.getArgAsExpr(0); |
| 783 | if (!Cond->isTypeDependent()) { |
| 784 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 785 | if (Converted.isInvalid()) |
| 786 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 787 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | StringRef Msg; |
| 791 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 792 | return; |
| 793 | |
| 794 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 795 | if (!Cond->isValueDependent() && |
| 796 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 797 | Diags)) { |
| 798 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 799 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 800 | S.Diag(Diags[I].first, Diags[I].second); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | D->addAttr(::new (S.Context) |
| 805 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 806 | Attr.getAttributeSpellingListIndex())); |
| 807 | } |
| 808 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 809 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 810 | ConsumableAttr::ConsumedState DefaultState; |
| 811 | |
| 812 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 813 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 814 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 815 | DefaultState)) { |
| 816 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 817 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 818 | return; |
| 819 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 820 | } else { |
| 821 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 822 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 823 | return; |
| 824 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 825 | |
| 826 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 827 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 828 | Attr.getAttributeSpellingListIndex())); |
| 829 | } |
| 830 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 831 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 832 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 833 | const AttributeList &Attr) { |
| 834 | ASTContext &CurrContext = S.getASTContext(); |
| 835 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 836 | |
| 837 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 838 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 839 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 840 | RD->getNameAsString(); |
| 841 | |
| 842 | return false; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return true; |
| 847 | } |
| 848 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 849 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 850 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 851 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 852 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 853 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 854 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 855 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 856 | return; |
| 857 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 858 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 859 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 860 | CallableWhenAttr::ConsumedState CallableState; |
| 861 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 862 | StringRef StateString; |
| 863 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 864 | if (Attr.isArgIdent(ArgIndex)) { |
| 865 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 866 | StateString = Ident->Ident->getName(); |
| 867 | Loc = Ident->Loc; |
| 868 | } else { |
| 869 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 870 | return; |
| 871 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 872 | |
| 873 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 874 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 875 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 876 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 877 | return; |
| 878 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 879 | |
| 880 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 881 | } |
| 882 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 883 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 884 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 885 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 886 | } |
| 887 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 888 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 889 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 890 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 891 | ParamTypestateAttr::ConsumedState ParamState; |
| 892 | |
| 893 | if (Attr.isArgIdent(0)) { |
| 894 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 895 | StringRef StateString = Ident->Ident->getName(); |
| 896 | |
| 897 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 898 | ParamState)) { |
| 899 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 900 | << Attr.getName() << StateString; |
| 901 | return; |
| 902 | } |
| 903 | } else { |
| 904 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 905 | Attr.getName() << AANT_ArgumentIdentifier; |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | // FIXME: This check is currently being done in the analysis. It can be |
| 910 | // enabled here only after the parser propagates attributes at |
| 911 | // template specialization definition, not declaration. |
| 912 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 913 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 914 | // |
| 915 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 916 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 917 | // ReturnType.getAsString(); |
| 918 | // return; |
| 919 | //} |
| 920 | |
| 921 | D->addAttr(::new (S.Context) |
| 922 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 923 | Attr.getAttributeSpellingListIndex())); |
| 924 | } |
| 925 | |
| 926 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 927 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 928 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 929 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 930 | |
| 931 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 932 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 933 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 934 | ReturnState)) { |
| 935 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 936 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 937 | return; |
| 938 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 939 | } else { |
| 940 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 941 | Attr.getName() << AANT_ArgumentIdentifier; |
| 942 | return; |
| 943 | } |
| 944 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 945 | // FIXME: This check is currently being done in the analysis. It can be |
| 946 | // enabled here only after the parser propagates attributes at |
| 947 | // template specialization definition, not declaration. |
| 948 | //QualType ReturnType; |
| 949 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 950 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 951 | // ReturnType = Param->getType(); |
| 952 | // |
| 953 | //} else if (const CXXConstructorDecl *Constructor = |
| 954 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 955 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 956 | // |
| 957 | //} else { |
| 958 | // |
| 959 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 960 | //} |
| 961 | // |
| 962 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 963 | // |
| 964 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 965 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 966 | // ReturnType.getAsString(); |
| 967 | // return; |
| 968 | //} |
| 969 | |
| 970 | D->addAttr(::new (S.Context) |
| 971 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 972 | Attr.getAttributeSpellingListIndex())); |
| 973 | } |
| 974 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 975 | |
| 976 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 977 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 978 | return; |
| 979 | |
| 980 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 981 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 982 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 983 | StringRef Param = Ident->Ident->getName(); |
| 984 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 985 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 986 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 987 | return; |
| 988 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 989 | } else { |
| 990 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 991 | Attr.getName() << AANT_ArgumentIdentifier; |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | D->addAttr(::new (S.Context) |
| 996 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 997 | Attr.getAttributeSpellingListIndex())); |
| 998 | } |
| 999 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1000 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1001 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1002 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1003 | return; |
| 1004 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1005 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1006 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1007 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1008 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1009 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1010 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1011 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1012 | return; |
| 1013 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1014 | } else { |
| 1015 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1016 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1017 | return; |
| 1018 | } |
| 1019 | |
| 1020 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1021 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1022 | Attr.getAttributeSpellingListIndex())); |
| 1023 | } |
| 1024 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1025 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1026 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1027 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1028 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1031 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1032 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1033 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1034 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1035 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1036 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1037 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1038 | if (!FD->getType()->isDependentType() && |
| 1039 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1040 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1041 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1042 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1043 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1044 | FD->addAttr(::new (S.Context) |
| 1045 | PackedAttr(Attr.getRange(), S.Context, |
| 1046 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1047 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1048 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1051 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1052 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1053 | // variables or properties of Objective-C classes. The outlet must also |
| 1054 | // have an object reference type. |
| 1055 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1056 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1057 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1058 | << Attr.getName() << VD->getType() << 0; |
| 1059 | return false; |
| 1060 | } |
| 1061 | } |
| 1062 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1063 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1064 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1065 | << Attr.getName() << PD->getType() << 1; |
| 1066 | return false; |
| 1067 | } |
| 1068 | } |
| 1069 | else { |
| 1070 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1071 | return false; |
| 1072 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1073 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1074 | return true; |
| 1075 | } |
| 1076 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1077 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1078 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1079 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1080 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1081 | D->addAttr(::new (S.Context) |
| 1082 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1083 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1086 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1087 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1088 | |
| 1089 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1090 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1091 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1092 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1093 | return; |
| 1094 | } |
| 1095 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1096 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1097 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1098 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1099 | ParsedType PT; |
| 1100 | |
| 1101 | if (Attr.hasParsedType()) |
| 1102 | PT = Attr.getTypeArg(); |
| 1103 | else { |
| 1104 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1105 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1106 | if (!PT) { |
| 1107 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1108 | return; |
| 1109 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1110 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1111 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1112 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1113 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1114 | if (!QTLoc) |
| 1115 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1116 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1117 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1118 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1119 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1120 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1121 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1122 | S.Diag(Attr.getLoc(), |
| 1123 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1124 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1127 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1128 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1129 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1130 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1133 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1134 | if (RefOkay) { |
| 1135 | if (T->isReferenceType()) |
| 1136 | return true; |
| 1137 | } else { |
| 1138 | T = T.getNonReferenceType(); |
| 1139 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1140 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1141 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1142 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1143 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1144 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1145 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1146 | for (const auto *I : UD->fields()) { |
| 1147 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1148 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1149 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1157 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1158 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1159 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1160 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1161 | if (!S.isValidPointerAttrType(T)) { |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1162 | S.Diag(Attr.getLoc(), isReturnValue |
| 1163 | ? diag::warn_attribute_return_pointers_only |
| 1164 | : diag::warn_attribute_pointers_only) |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1165 | << Attr.getName() << AttrParmRange << TypeRange; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1166 | return false; |
| 1167 | } |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1171 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1172 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1173 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1174 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1175 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1176 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1177 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1178 | |
| 1179 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1180 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1181 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1182 | Ex->getSourceRange(), |
| 1183 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1184 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1185 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1186 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1187 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1188 | |
| 1189 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1190 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1191 | // check if the attribute came from a macro expansion or a template |
| 1192 | // instantiation. |
| 1193 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
| 1194 | S.ActiveTemplateInstantiations.empty()) { |
| 1195 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1196 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1197 | I != E && !AnyPointers; ++I) { |
| 1198 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1199 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1200 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1201 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1202 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1203 | if (!AnyPointers) |
| 1204 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1205 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1206 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1207 | unsigned *Start = NonNullArgs.data(); |
| 1208 | unsigned Size = NonNullArgs.size(); |
| 1209 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1210 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1211 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1212 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1215 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1216 | const AttributeList &Attr) { |
| 1217 | if (Attr.getNumArgs() > 0) { |
| 1218 | if (D->getFunctionType()) { |
| 1219 | handleNonNullAttr(S, D, Attr); |
| 1220 | } else { |
| 1221 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1222 | << D->getSourceRange(); |
| 1223 | } |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1228 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1229 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1230 | return; |
| 1231 | |
| 1232 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1233 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1234 | Attr.getAttributeSpellingListIndex())); |
| 1235 | } |
| 1236 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1237 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1238 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1239 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1240 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1241 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1242 | /* isReturnValue */ true)) |
| 1243 | return; |
| 1244 | |
| 1245 | D->addAttr(::new (S.Context) |
| 1246 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1247 | Attr.getAttributeSpellingListIndex())); |
| 1248 | } |
| 1249 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1250 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1251 | const AttributeList &Attr) { |
| 1252 | Expr *E = Attr.getArgAsExpr(0), |
| 1253 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1254 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1255 | Attr.getAttributeSpellingListIndex()); |
| 1256 | } |
| 1257 | |
| 1258 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1259 | Expr *OE, unsigned SpellingListIndex) { |
| 1260 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1261 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1262 | |
| 1263 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1264 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1265 | |
| 1266 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1267 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1268 | << &TmpAttr << AttrRange << SR; |
| 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | if (!E->isValueDependent()) { |
| 1273 | llvm::APSInt I(64); |
| 1274 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1275 | if (OE) |
| 1276 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1277 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1278 | << E->getSourceRange(); |
| 1279 | else |
| 1280 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1281 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1282 | << E->getSourceRange(); |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | if (!I.isPowerOf2()) { |
| 1287 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1288 | << E->getSourceRange(); |
| 1289 | return; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | if (OE) { |
| 1294 | if (!OE->isValueDependent()) { |
| 1295 | llvm::APSInt I(64); |
| 1296 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1297 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1298 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1299 | << OE->getSourceRange(); |
| 1300 | return; |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | D->addAttr(::new (Context) |
| 1306 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1307 | } |
| 1308 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1309 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1310 | // This attribute must be applied to a function declaration. The first |
| 1311 | // argument to the attribute must be an identifier, the name of the resource, |
| 1312 | // for example: malloc. The following arguments must be argument indexes, the |
| 1313 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1314 | // 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] | 1315 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1316 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1317 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1318 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1319 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1320 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1321 | return; |
| 1322 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1323 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1324 | // Figure out our Kind. |
| 1325 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1326 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1327 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1328 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1329 | // Check arguments. |
| 1330 | switch (K) { |
| 1331 | case OwnershipAttr::Takes: |
| 1332 | case OwnershipAttr::Holds: |
| 1333 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1334 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1335 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1336 | return; |
| 1337 | } |
| 1338 | break; |
| 1339 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1340 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1341 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1342 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1343 | return; |
| 1344 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1345 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1348 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1349 | |
| 1350 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1351 | StringRef ModuleName = Module->getName(); |
| 1352 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1353 | ModuleName.size() > 4) { |
| 1354 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1355 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1356 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1357 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1358 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1359 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1360 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1361 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1362 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1363 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1364 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1365 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1366 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1367 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1368 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1369 | case OwnershipAttr::Takes: |
| 1370 | case OwnershipAttr::Holds: |
| 1371 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1372 | Err = 0; |
| 1373 | break; |
| 1374 | case OwnershipAttr::Returns: |
| 1375 | if (!T->isIntegerType()) |
| 1376 | Err = 1; |
| 1377 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1378 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1379 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1380 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1381 | << Ex->getSourceRange(); |
| 1382 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1383 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1384 | |
| 1385 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1386 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1387 | // Cannot have two ownership attributes of different kinds for the same |
| 1388 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1389 | if (I->getOwnKind() != K && I->args_end() != |
| 1390 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1391 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1392 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1393 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1394 | } else if (K == OwnershipAttr::Returns && |
| 1395 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1396 | // A returns attribute conflicts with any other returns attribute using |
| 1397 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1398 | // argument indexes are 0-based. |
| 1399 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1400 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1401 | << *(I->args_begin()) + 1; |
| 1402 | if (I->args_size()) |
| 1403 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1404 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1405 | return; |
| 1406 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1407 | } |
| 1408 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1409 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | unsigned* start = OwnershipArgs.data(); |
| 1413 | unsigned size = OwnershipArgs.size(); |
| 1414 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1415 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1416 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1417 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1418 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1421 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1422 | // Check the attribute arguments. |
| 1423 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1424 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1425 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1426 | return; |
| 1427 | } |
| 1428 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1429 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1430 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1431 | // gcc rejects |
| 1432 | // class c { |
| 1433 | // static int a __attribute__((weakref ("v2"))); |
| 1434 | // static int b() __attribute__((weakref ("f3"))); |
| 1435 | // }; |
| 1436 | // and ignores the attributes of |
| 1437 | // void f(void) { |
| 1438 | // static int a __attribute__((weakref ("v2"))); |
| 1439 | // } |
| 1440 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1441 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1442 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1443 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1444 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1445 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | // The GCC manual says |
| 1449 | // |
| 1450 | // At present, a declaration to which `weakref' is attached can only |
| 1451 | // be `static'. |
| 1452 | // |
| 1453 | // It also says |
| 1454 | // |
| 1455 | // Without a TARGET, |
| 1456 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1457 | // equivalent to `weak'. |
| 1458 | // |
| 1459 | // gcc 4.4.1 will accept |
| 1460 | // int a7 __attribute__((weakref)); |
| 1461 | // as |
| 1462 | // int a7 __attribute__((weak)); |
| 1463 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1464 | // it if this behaviour is actually used. |
| 1465 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1466 | // GCC rejects |
| 1467 | // static ((alias ("y"), weakref)). |
| 1468 | // Should we? How to check that weakref is before or after alias? |
| 1469 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1470 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1471 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1472 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1473 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1474 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1475 | // GCC will accept anything as the argument of weakref. Should we |
| 1476 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1477 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1478 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1479 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1480 | D->addAttr(::new (S.Context) |
| 1481 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1482 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1485 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1486 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1487 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1488 | return; |
| 1489 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1490 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1491 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1492 | return; |
| 1493 | } |
| 1494 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1495 | // Aliases should be on declarations, not definitions. |
| 1496 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1497 | if (FD->isThisDeclarationADefinition()) { |
| 1498 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD; |
| 1499 | return; |
| 1500 | } |
| 1501 | } else { |
| 1502 | const auto *VD = cast<VarDecl>(D); |
| 1503 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
| 1504 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD; |
| 1505 | return; |
| 1506 | } |
| 1507 | } |
| 1508 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1509 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1510 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1511 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1512 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1515 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1516 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1517 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1518 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1519 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1520 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1524 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1525 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1526 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1527 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1528 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1531 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1532 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1533 | StringRef Model; |
| 1534 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1535 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1536 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1537 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1538 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1539 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1540 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1541 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1542 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1543 | return; |
| 1544 | } |
| 1545 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1546 | D->addAttr(::new (S.Context) |
| 1547 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1548 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1551 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1552 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1553 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1554 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1555 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1556 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1559 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1560 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1563 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1564 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1565 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1566 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1567 | return; |
| 1568 | } |
| 1569 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1570 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1571 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1574 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1575 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1576 | |
| 1577 | if (S.CheckNoReturnAttr(attr)) return; |
| 1578 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1579 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1580 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1581 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1582 | return; |
| 1583 | } |
| 1584 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1585 | D->addAttr(::new (S.Context) |
| 1586 | NoReturnAttr(attr.getRange(), S.Context, |
| 1587 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1591 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1592 | attr.setInvalid(); |
| 1593 | return true; |
| 1594 | } |
| 1595 | |
| 1596 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1599 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1600 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1601 | |
| 1602 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1603 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1604 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1605 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1606 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1607 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1608 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1609 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1610 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1611 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1612 | return; |
| 1613 | } |
| 1614 | } |
| 1615 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1616 | D->addAttr(::new (S.Context) |
| 1617 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1618 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1621 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1622 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1623 | /* |
| 1624 | Returning a Vector Class in Registers |
| 1625 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1626 | According to the PPU ABI specifications, a class with a single member of |
| 1627 | vector type is returned in memory when used as the return value of a function. |
| 1628 | This results in inefficient code when implementing vector classes. To return |
| 1629 | the value in a single vector register, add the vecreturn attribute to the |
| 1630 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1631 | |
| 1632 | Example: |
| 1633 | |
| 1634 | struct Vector |
| 1635 | { |
| 1636 | __vector float xyzw; |
| 1637 | } __attribute__((vecreturn)); |
| 1638 | |
| 1639 | Vector Add(Vector lhs, Vector rhs) |
| 1640 | { |
| 1641 | Vector result; |
| 1642 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1643 | return result; // This will be returned in a register |
| 1644 | } |
| 1645 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1646 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1647 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | } |
| 1650 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1651 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1652 | int count = 0; |
| 1653 | |
| 1654 | if (!isa<CXXRecordDecl>(record)) { |
| 1655 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1656 | return; |
| 1657 | } |
| 1658 | |
| 1659 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1660 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1661 | return; |
| 1662 | } |
| 1663 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1664 | for (const auto *I : record->fields()) { |
| 1665 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1666 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1667 | return; |
| 1668 | } |
| 1669 | count++; |
| 1670 | } |
| 1671 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1672 | D->addAttr(::new (S.Context) |
| 1673 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1674 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1677 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1678 | const AttributeList &Attr) { |
| 1679 | if (isa<ParmVarDecl>(D)) { |
| 1680 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1681 | // parameter of a function declaration or lambda. |
| 1682 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1683 | S.Diag(Attr.getLoc(), |
| 1684 | diag::err_carries_dependency_param_not_function_decl); |
| 1685 | return; |
| 1686 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1687 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1688 | |
| 1689 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1690 | Attr.getRange(), S.Context, |
| 1691 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1694 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1695 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1696 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1697 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1698 | return; |
| 1699 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1700 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1701 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1702 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1703 | return; |
| 1704 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1705 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1706 | D->addAttr(::new (S.Context) |
| 1707 | UsedAttr(Attr.getRange(), S.Context, |
| 1708 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1711 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1712 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1713 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1714 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1715 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1716 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1717 | D->addAttr(::new (S.Context) |
| 1718 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1719 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1722 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1723 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1724 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1725 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1726 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1727 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1728 | D->addAttr(::new (S.Context) |
| 1729 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1730 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1733 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1734 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1735 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1736 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1737 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1738 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1739 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1740 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1741 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1742 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1745 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1746 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1747 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 1748 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 1749 | << Attr.getName() << Attr.getRange(); |
| 1750 | return; |
| 1751 | } |
| 1752 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1753 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1754 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1755 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1758 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1759 | IdentifierInfo *Platform, |
| 1760 | VersionTuple Introduced, |
| 1761 | VersionTuple Deprecated, |
| 1762 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1763 | StringRef PlatformName |
| 1764 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1765 | if (PlatformName.empty()) |
| 1766 | PlatformName = Platform->getName(); |
| 1767 | |
| 1768 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1769 | // of these steps are needed). |
| 1770 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1771 | !(Introduced <= Deprecated)) { |
| 1772 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1773 | << 1 << PlatformName << Deprecated.getAsString() |
| 1774 | << 0 << Introduced.getAsString(); |
| 1775 | return true; |
| 1776 | } |
| 1777 | |
| 1778 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1779 | !(Introduced <= Obsoleted)) { |
| 1780 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1781 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1782 | << 0 << Introduced.getAsString(); |
| 1783 | return true; |
| 1784 | } |
| 1785 | |
| 1786 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1787 | !(Deprecated <= Obsoleted)) { |
| 1788 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1789 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1790 | << 1 << Deprecated.getAsString(); |
| 1791 | return true; |
| 1792 | } |
| 1793 | |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1797 | /// \brief Check whether the two versions match. |
| 1798 | /// |
| 1799 | /// If either version tuple is empty, then they are assumed to match. If |
| 1800 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1801 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1802 | bool BeforeIsOkay) { |
| 1803 | if (X.empty() || Y.empty()) |
| 1804 | return true; |
| 1805 | |
| 1806 | if (X == Y) |
| 1807 | return true; |
| 1808 | |
| 1809 | if (BeforeIsOkay && X < Y) |
| 1810 | return true; |
| 1811 | |
| 1812 | return false; |
| 1813 | } |
| 1814 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1815 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1816 | IdentifierInfo *Platform, |
| 1817 | VersionTuple Introduced, |
| 1818 | VersionTuple Deprecated, |
| 1819 | VersionTuple Obsoleted, |
| 1820 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1821 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1822 | bool Override, |
| 1823 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1824 | VersionTuple MergedIntroduced = Introduced; |
| 1825 | VersionTuple MergedDeprecated = Deprecated; |
| 1826 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1827 | bool FoundAny = false; |
| 1828 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1829 | if (D->hasAttrs()) { |
| 1830 | AttrVec &Attrs = D->getAttrs(); |
| 1831 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1832 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1833 | if (!OldAA) { |
| 1834 | ++i; |
| 1835 | continue; |
| 1836 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1837 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1838 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1839 | if (OldPlatform != Platform) { |
| 1840 | ++i; |
| 1841 | continue; |
| 1842 | } |
| 1843 | |
| 1844 | FoundAny = true; |
| 1845 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1846 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1847 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1848 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1849 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1850 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1851 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1852 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1853 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1854 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1855 | if (Override) { |
| 1856 | int Which = -1; |
| 1857 | VersionTuple FirstVersion; |
| 1858 | VersionTuple SecondVersion; |
| 1859 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1860 | Which = 0; |
| 1861 | FirstVersion = OldIntroduced; |
| 1862 | SecondVersion = Introduced; |
| 1863 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1864 | Which = 1; |
| 1865 | FirstVersion = Deprecated; |
| 1866 | SecondVersion = OldDeprecated; |
| 1867 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1868 | Which = 2; |
| 1869 | FirstVersion = Obsoleted; |
| 1870 | SecondVersion = OldObsoleted; |
| 1871 | } |
| 1872 | |
| 1873 | if (Which == -1) { |
| 1874 | Diag(OldAA->getLocation(), |
| 1875 | diag::warn_mismatched_availability_override_unavail) |
| 1876 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1877 | } else { |
| 1878 | Diag(OldAA->getLocation(), |
| 1879 | diag::warn_mismatched_availability_override) |
| 1880 | << Which |
| 1881 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1882 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1883 | } |
| 1884 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1885 | } else { |
| 1886 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1887 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1888 | } |
| 1889 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1890 | Attrs.erase(Attrs.begin() + i); |
| 1891 | --e; |
| 1892 | continue; |
| 1893 | } |
| 1894 | |
| 1895 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1896 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1897 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1898 | |
| 1899 | if (MergedIntroduced2.empty()) |
| 1900 | MergedIntroduced2 = OldIntroduced; |
| 1901 | if (MergedDeprecated2.empty()) |
| 1902 | MergedDeprecated2 = OldDeprecated; |
| 1903 | if (MergedObsoleted2.empty()) |
| 1904 | MergedObsoleted2 = OldObsoleted; |
| 1905 | |
| 1906 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1907 | MergedIntroduced2, MergedDeprecated2, |
| 1908 | MergedObsoleted2)) { |
| 1909 | Attrs.erase(Attrs.begin() + i); |
| 1910 | --e; |
| 1911 | continue; |
| 1912 | } |
| 1913 | |
| 1914 | MergedIntroduced = MergedIntroduced2; |
| 1915 | MergedDeprecated = MergedDeprecated2; |
| 1916 | MergedObsoleted = MergedObsoleted2; |
| 1917 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1918 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | if (FoundAny && |
| 1922 | MergedIntroduced == Introduced && |
| 1923 | MergedDeprecated == Deprecated && |
| 1924 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1925 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1926 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1927 | // Only create a new attribute if !Override, but we want to do |
| 1928 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1929 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1930 | MergedDeprecated, MergedObsoleted) && |
| 1931 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1932 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1933 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1934 | Obsoleted, IsUnavailable, Message, |
| 1935 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1936 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1937 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1940 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1941 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1942 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1943 | return; |
| 1944 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1945 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1946 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1947 | IdentifierInfo *II = Platform->Ident; |
| 1948 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1949 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1950 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1951 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1952 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1953 | if (!ND) { |
| 1954 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1955 | return; |
| 1956 | } |
| 1957 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1958 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1959 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1960 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1961 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1962 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1963 | if (const StringLiteral *SE = |
| 1964 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1965 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1966 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1967 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1968 | Introduced.Version, |
| 1969 | Deprecated.Version, |
| 1970 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1971 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1972 | /*Override=*/false, |
| 1973 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1974 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1975 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1978 | template <class T> |
| 1979 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1980 | typename T::VisibilityType value, |
| 1981 | unsigned attrSpellingListIndex) { |
| 1982 | T *existingAttr = D->getAttr<T>(); |
| 1983 | if (existingAttr) { |
| 1984 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1985 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1986 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1987 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1988 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1989 | D->dropAttr<T>(); |
| 1990 | } |
| 1991 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1992 | } |
| 1993 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1994 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1995 | VisibilityAttr::VisibilityType Vis, |
| 1996 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1997 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1998 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2001 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2002 | TypeVisibilityAttr::VisibilityType Vis, |
| 2003 | unsigned AttrSpellingListIndex) { |
| 2004 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2005 | AttrSpellingListIndex); |
| 2006 | } |
| 2007 | |
| 2008 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2009 | bool isTypeVisibility) { |
| 2010 | // Visibility attributes don't mean anything on a typedef. |
| 2011 | if (isa<TypedefNameDecl>(D)) { |
| 2012 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2013 | << Attr.getName(); |
| 2014 | return; |
| 2015 | } |
| 2016 | |
| 2017 | // 'type_visibility' can only go on a type or namespace. |
| 2018 | if (isTypeVisibility && |
| 2019 | !(isa<TagDecl>(D) || |
| 2020 | isa<ObjCInterfaceDecl>(D) || |
| 2021 | isa<NamespaceDecl>(D))) { |
| 2022 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2023 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2024 | return; |
| 2025 | } |
| 2026 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2027 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2028 | StringRef TypeStr; |
| 2029 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2030 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2031 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2032 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2033 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2034 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2035 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2036 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2037 | return; |
| 2038 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Complain about attempts to use protected visibility on targets |
| 2041 | // (like Darwin) that don't support it. |
| 2042 | if (type == VisibilityAttr::Protected && |
| 2043 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2044 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2045 | type = VisibilityAttr::Default; |
| 2046 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2047 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2048 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2049 | clang::Attr *newAttr; |
| 2050 | if (isTypeVisibility) { |
| 2051 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2052 | (TypeVisibilityAttr::VisibilityType) type, |
| 2053 | Index); |
| 2054 | } else { |
| 2055 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2056 | } |
| 2057 | if (newAttr) |
| 2058 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2061 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2062 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2063 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2064 | if (!Attr.isArgIdent(0)) { |
| 2065 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2066 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2067 | return; |
| 2068 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2069 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2070 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2071 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2072 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2073 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2074 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2075 | return; |
| 2076 | } |
| 2077 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2078 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2079 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2080 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2081 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2082 | // Ignore the attribute. |
| 2083 | return; |
| 2084 | } |
| 2085 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2086 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2087 | S.Context, F, |
| 2088 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2091 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2092 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2093 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2094 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2095 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2096 | return; |
| 2097 | } |
| 2098 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2099 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2100 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2101 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2102 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2103 | return; |
| 2104 | } |
| 2105 | } |
| 2106 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2107 | // It is okay to include this attribute on properties, e.g.: |
| 2108 | // |
| 2109 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2110 | // |
| 2111 | // In this case it follows tradition and suppresses an error in the above |
| 2112 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2113 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2114 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2115 | D->addAttr(::new (S.Context) |
| 2116 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2117 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2120 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2121 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2122 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2123 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2124 | return; |
| 2125 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2126 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2127 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2128 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2129 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2130 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2131 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2132 | return; |
| 2133 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2134 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2135 | D->addAttr(::new (S.Context) |
| 2136 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2137 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2140 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2141 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2142 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2143 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2144 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2145 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2146 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2147 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2148 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2149 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2150 | return; |
| 2151 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2152 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2153 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2154 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2155 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2156 | return; |
| 2157 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2158 | |
| 2159 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2162 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2163 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2164 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2165 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2166 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2167 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2168 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2169 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2170 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2171 | return; |
| 2172 | } |
| 2173 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2174 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2175 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2176 | // FIXME: This error message could be improved, it would be nice |
| 2177 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2178 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2179 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2180 | return; |
| 2181 | } |
| 2182 | } |
| 2183 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2184 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2185 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2186 | if (isa<FunctionNoProtoType>(FT)) { |
| 2187 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2188 | return; |
| 2189 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2190 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2191 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2192 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2193 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2194 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2195 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2196 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2197 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2198 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2199 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2200 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2201 | if (!BD->isVariadic()) { |
| 2202 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2203 | return; |
| 2204 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2205 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2206 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2207 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2208 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2209 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2210 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2211 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2212 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2213 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2214 | return; |
| 2215 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2216 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2217 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2218 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2219 | return; |
| 2220 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2221 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2222 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2223 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2224 | return; |
| 2225 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2226 | D->addAttr(::new (S.Context) |
| 2227 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2228 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2231 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2232 | if (D->getFunctionType() && |
| 2233 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2234 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2235 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2236 | return; |
| 2237 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2238 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2239 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2240 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2241 | << Attr.getName() << 1; |
| 2242 | return; |
| 2243 | } |
| 2244 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2245 | D->addAttr(::new (S.Context) |
| 2246 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2247 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2250 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2251 | // weak_import only applies to variable & function declarations. |
| 2252 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2253 | if (!D->canBeWeakImported(isDef)) { |
| 2254 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2255 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2256 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2257 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2258 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2259 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2260 | // Nothing to warn about here. |
| 2261 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2262 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2263 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2264 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2265 | return; |
| 2266 | } |
| 2267 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2268 | D->addAttr(::new (S.Context) |
| 2269 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2270 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2273 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2274 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2275 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2276 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2277 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2278 | for (unsigned i = 0; i < 3; ++i) { |
| 2279 | const Expr *E = Attr.getArgAsExpr(i); |
| 2280 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2281 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2282 | if (WGSize[i] == 0) { |
| 2283 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2284 | << Attr.getName() << E->getSourceRange(); |
| 2285 | return; |
| 2286 | } |
| 2287 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2288 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2289 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2290 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2291 | Existing->getYDim() == WGSize[1] && |
| 2292 | Existing->getZDim() == WGSize[2])) |
| 2293 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2294 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2295 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2296 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2297 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2300 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2301 | if (!Attr.hasParsedType()) { |
| 2302 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2303 | << Attr.getName() << 1; |
| 2304 | return; |
| 2305 | } |
| 2306 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2307 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2308 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2309 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2310 | |
| 2311 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2312 | (ParmType->isBooleanType() || |
| 2313 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2314 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2315 | << ParmType; |
| 2316 | return; |
| 2317 | } |
| 2318 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2319 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2320 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2321 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2322 | return; |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2327 | ParmTSI, |
| 2328 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2331 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2332 | StringRef Name, |
| 2333 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2334 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2335 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2336 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2337 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2338 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2339 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2340 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2341 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2342 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2345 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2346 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2347 | if (!Error.empty()) { |
| 2348 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2349 | return false; |
| 2350 | } |
| 2351 | return true; |
| 2352 | } |
| 2353 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2354 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2355 | // Make sure that there is a string literal as the sections's single |
| 2356 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2357 | StringRef Str; |
| 2358 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2359 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2360 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2362 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2363 | return; |
| 2364 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2365 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2366 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2367 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2368 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2369 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2370 | return; |
| 2371 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2372 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2373 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2374 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2375 | if (NewAttr) |
| 2376 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2379 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2380 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2381 | VarDecl *VD = cast<VarDecl>(D); |
| 2382 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2383 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2384 | return; |
| 2385 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2386 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2387 | Expr *E = Attr.getArgAsExpr(0); |
| 2388 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2389 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2390 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2391 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2392 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2393 | // will warn the user. |
| 2394 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2395 | if (DRE->hasQualifier()) |
| 2396 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2397 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2398 | NI = DRE->getNameInfo(); |
| 2399 | if (!FD) { |
| 2400 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2401 | << NI.getName(); |
| 2402 | return; |
| 2403 | } |
| 2404 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2405 | if (ULE->hasExplicitTemplateArgs()) |
| 2406 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2407 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2408 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2409 | if (!FD) { |
| 2410 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2411 | << NI.getName(); |
| 2412 | if (ULE->getType() == S.Context.OverloadTy) |
| 2413 | S.NoteAllOverloadCandidates(ULE); |
| 2414 | return; |
| 2415 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2416 | } else { |
| 2417 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2418 | return; |
| 2419 | } |
| 2420 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2421 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2422 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2423 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2424 | return; |
| 2425 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2426 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2427 | // We're currently more strict than GCC about what function types we accept. |
| 2428 | // If this ever proves to be a problem it should be easy to fix. |
| 2429 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2430 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2431 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2432 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2433 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2434 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2435 | return; |
| 2436 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2437 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2438 | D->addAttr(::new (S.Context) |
| 2439 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2440 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2443 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2444 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2445 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2446 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2447 | uint64_t Idx; |
| 2448 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2449 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2450 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2451 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2452 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2453 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2454 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2455 | if (not_nsstring_type && |
| 2456 | !isCFStringType(Ty, S.Context) && |
| 2457 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2458 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2459 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 2460 | << (not_nsstring_type ? "a string type" : "an NSString") |
| 2461 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2462 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2463 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2464 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2465 | if (!isNSStringType(Ty, S.Context) && |
| 2466 | !isCFStringType(Ty, S.Context) && |
| 2467 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2468 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2469 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 2470 | << (not_nsstring_type ? "string type" : "NSString") |
| 2471 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2472 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2475 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2476 | // because that has corrected for the implicit this parameter, and is zero- |
| 2477 | // based. The attribute expects what the user wrote explicitly. |
| 2478 | llvm::APSInt Val; |
| 2479 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2480 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2481 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2482 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2483 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2486 | enum FormatAttrKind { |
| 2487 | CFStringFormat, |
| 2488 | NSStringFormat, |
| 2489 | StrftimeFormat, |
| 2490 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2491 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2492 | InvalidFormat |
| 2493 | }; |
| 2494 | |
| 2495 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2496 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2497 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2498 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2499 | // Check for formats that get handled specially. |
| 2500 | .Case("NSString", NSStringFormat) |
| 2501 | .Case("CFString", CFStringFormat) |
| 2502 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2503 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2504 | // Otherwise, check for supported formats. |
| 2505 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2506 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2507 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 2508 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 2509 | .Case("os_trace", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2510 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2511 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2512 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2515 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2516 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2517 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2518 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2519 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2520 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2521 | return; |
| 2522 | } |
| 2523 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2524 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2525 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2526 | Attr.setInvalid(); |
| 2527 | return; |
| 2528 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2529 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2530 | if (S.Context.getAsArrayType(T)) |
| 2531 | T = S.Context.getBaseElementType(T); |
| 2532 | if (!T->getAs<RecordType>()) { |
| 2533 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2534 | Attr.setInvalid(); |
| 2535 | return; |
| 2536 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2537 | |
| 2538 | Expr *E = Attr.getArgAsExpr(0); |
| 2539 | uint32_t prioritynum; |
| 2540 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2541 | Attr.setInvalid(); |
| 2542 | return; |
| 2543 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2544 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2545 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2546 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2547 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2548 | Attr.setInvalid(); |
| 2549 | return; |
| 2550 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2551 | D->addAttr(::new (S.Context) |
| 2552 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2553 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2556 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2557 | IdentifierInfo *Format, int FormatIdx, |
| 2558 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2559 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2560 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2561 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 2562 | if (F->getType() == Format && |
| 2563 | F->getFormatIdx() == FormatIdx && |
| 2564 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2565 | // If we don't have a valid location for this attribute, adopt the |
| 2566 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2567 | if (F->getLocation().isInvalid()) |
| 2568 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2569 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2573 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2574 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2577 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2578 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2579 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2580 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2581 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2582 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2583 | return; |
| 2584 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2585 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2586 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2587 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2588 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2589 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2590 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2591 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2592 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2593 | |
| 2594 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2595 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2596 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2597 | // If we've modified the string name, we need a new identifier for it. |
| 2598 | II = &S.Context.Idents.get(Format); |
| 2599 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2600 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2601 | // Check for supported formats. |
| 2602 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2603 | |
| 2604 | if (Kind == IgnoredFormat) |
| 2605 | return; |
| 2606 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2607 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2608 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2609 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2610 | return; |
| 2611 | } |
| 2612 | |
| 2613 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2614 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2615 | uint32_t Idx; |
| 2616 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2617 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2618 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2619 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2620 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2621 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2622 | return; |
| 2623 | } |
| 2624 | |
| 2625 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2626 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2627 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2628 | if (HasImplicitThisParam) { |
| 2629 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2630 | S.Diag(Attr.getLoc(), |
| 2631 | diag::err_format_attribute_implicit_this_format_string) |
| 2632 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2633 | return; |
| 2634 | } |
| 2635 | ArgIdx--; |
| 2636 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2638 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2639 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2640 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2641 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2642 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2643 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2644 | << "a CFString" << IdxExpr->getSourceRange() |
| 2645 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2646 | return; |
| 2647 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2648 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2649 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2650 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2651 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2652 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2653 | << "an NSString" << IdxExpr->getSourceRange() |
| 2654 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2655 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2656 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2657 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2658 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2659 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2660 | << "a string type" << IdxExpr->getSourceRange() |
| 2661 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2662 | return; |
| 2663 | } |
| 2664 | |
| 2665 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2666 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2667 | uint32_t FirstArg; |
| 2668 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2669 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2670 | |
| 2671 | // check if the function is variadic if the 3rd argument non-zero |
| 2672 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2673 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2674 | ++NumArgs; // +1 for ... |
| 2675 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2676 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2677 | return; |
| 2678 | } |
| 2679 | } |
| 2680 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2681 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2682 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2683 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2684 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2685 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2686 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2687 | return; |
| 2688 | } |
| 2689 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2690 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2691 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2692 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2693 | return; |
| 2694 | } |
| 2695 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2696 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2697 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2698 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2699 | if (NewAttr) |
| 2700 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2703 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2704 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2705 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2706 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2707 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2708 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2709 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2710 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2711 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2712 | |
| 2713 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2714 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2715 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2716 | return; |
| 2717 | } |
| 2718 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2719 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2720 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2721 | diag::warn_transparent_union_attribute_not_definition); |
| 2722 | return; |
| 2723 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2724 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2725 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2726 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2727 | if (Field == FieldEnd) { |
| 2728 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2729 | return; |
| 2730 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2731 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2732 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2733 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2734 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2735 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2736 | diag::warn_transparent_union_attribute_floating) |
| 2737 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2738 | return; |
| 2739 | } |
| 2740 | |
| 2741 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2742 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2743 | for (; Field != FieldEnd; ++Field) { |
| 2744 | QualType FieldType = Field->getType(); |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2745 | // FIXME: this isn't fully correct; we also need to test whether the |
| 2746 | // members of the union would all have the same calling convention as the |
| 2747 | // first member of the union. Checking just the size and alignment isn't |
| 2748 | // sufficient (consider structs passed on the stack instead of in registers |
| 2749 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2750 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2751 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2752 | // Warn if we drop the attribute. |
| 2753 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2754 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2755 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2756 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2757 | diag::warn_transparent_union_attribute_field_size_align) |
| 2758 | << isSize << Field->getDeclName() << FieldBits; |
| 2759 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2760 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2761 | diag::note_transparent_union_first_field_size_align) |
| 2762 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2763 | return; |
| 2764 | } |
| 2765 | } |
| 2766 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2767 | RD->addAttr(::new (S.Context) |
| 2768 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2769 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2772 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2773 | // Make sure that there is a string literal as the annotation's single |
| 2774 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2775 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2776 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2777 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2778 | |
| 2779 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2780 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 2781 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2782 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2783 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2784 | |
| 2785 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2786 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2787 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 2790 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 2791 | const AttributeList &Attr) { |
| 2792 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 2793 | Attr.getAttributeSpellingListIndex()); |
| 2794 | } |
| 2795 | |
| 2796 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 2797 | unsigned SpellingListIndex) { |
| 2798 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 2799 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2800 | |
| 2801 | QualType T; |
| 2802 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 2803 | T = TD->getUnderlyingType(); |
| 2804 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2805 | T = VD->getType(); |
| 2806 | else |
| 2807 | llvm_unreachable("Unknown decl type for align_value"); |
| 2808 | |
| 2809 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 2810 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 2811 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 2812 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 2813 | return; |
| 2814 | } |
| 2815 | |
| 2816 | if (!E->isValueDependent()) { |
| 2817 | llvm::APSInt Alignment(32); |
| 2818 | ExprResult ICE |
| 2819 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2820 | diag::err_align_value_attribute_argument_not_int, |
| 2821 | /*AllowFold*/ false); |
| 2822 | if (ICE.isInvalid()) |
| 2823 | return; |
| 2824 | |
| 2825 | if (!Alignment.isPowerOf2()) { |
| 2826 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 2827 | << E->getSourceRange(); |
| 2828 | return; |
| 2829 | } |
| 2830 | |
| 2831 | D->addAttr(::new (Context) |
| 2832 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 2833 | SpellingListIndex)); |
| 2834 | return; |
| 2835 | } |
| 2836 | |
| 2837 | // Save dependent expressions in the AST to be instantiated. |
| 2838 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
| 2839 | return; |
| 2840 | } |
| 2841 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2842 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2843 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2844 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2845 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2846 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2847 | return; |
| 2848 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2849 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2850 | if (Attr.getNumArgs() == 0) { |
| 2851 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2852 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2853 | return; |
| 2854 | } |
| 2855 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2856 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2857 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2858 | S.Diag(Attr.getEllipsisLoc(), |
| 2859 | diag::err_pack_expansion_without_parameter_packs); |
| 2860 | return; |
| 2861 | } |
| 2862 | |
| 2863 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2864 | return; |
| 2865 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame^] | 2866 | if (E->isValueDependent()) { |
| 2867 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 2868 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 2869 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 2870 | << E->getSourceRange(); |
| 2871 | return; |
| 2872 | } |
| 2873 | } |
| 2874 | } |
| 2875 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2876 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2877 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2881 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2882 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2883 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2884 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2885 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2886 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2887 | // C++11 [dcl.align]p1: |
| 2888 | // An alignment-specifier may be applied to a variable or to a class |
| 2889 | // data member, but it shall not be applied to a bit-field, a function |
| 2890 | // parameter, the formal parameter of a catch clause, or a variable |
| 2891 | // declared with the register storage class specifier. An |
| 2892 | // alignment-specifier may also be applied to the declaration of a class |
| 2893 | // or enumeration type. |
| 2894 | // C11 6.7.5/2: |
| 2895 | // An alignment attribute shall not be specified in a declaration of |
| 2896 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2897 | // object declared with the register storage-class specifier. |
| 2898 | int DiagKind = -1; |
| 2899 | if (isa<ParmVarDecl>(D)) { |
| 2900 | DiagKind = 0; |
| 2901 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2902 | if (VD->getStorageClass() == SC_Register) |
| 2903 | DiagKind = 1; |
| 2904 | if (VD->isExceptionVariable()) |
| 2905 | DiagKind = 2; |
| 2906 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2907 | if (FD->isBitField()) |
| 2908 | DiagKind = 3; |
| 2909 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2910 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2911 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2912 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2913 | return; |
| 2914 | } |
| 2915 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2916 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2917 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2918 | return; |
| 2919 | } |
| 2920 | } |
| 2921 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2922 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2923 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2924 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2925 | AA->setPackExpansion(IsPackExpansion); |
| 2926 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2927 | return; |
| 2928 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2929 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2930 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2931 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2932 | ExprResult ICE |
| 2933 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2934 | diag::err_aligned_attribute_argument_not_int, |
| 2935 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2936 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2937 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2938 | |
| 2939 | // C++11 [dcl.align]p2: |
| 2940 | // -- if the constant expression evaluates to zero, the alignment |
| 2941 | // specifier shall have no effect |
| 2942 | // C11 6.7.5p6: |
| 2943 | // An alignment specification of zero has no effect. |
| 2944 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2945 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 2946 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2947 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2948 | return; |
| 2949 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2950 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 2951 | // Alignment calculations can wrap around if it's greater than 2**28. |
| 2952 | unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456; |
| 2953 | if (Alignment.getZExtValue() > MaxValidAlignment) { |
| 2954 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 2955 | << E->getSourceRange(); |
| 2956 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2957 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2958 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2959 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2960 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2961 | AA->setPackExpansion(IsPackExpansion); |
| 2962 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2965 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2966 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2967 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2968 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2969 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2970 | SpellingListIndex); |
| 2971 | AA->setPackExpansion(IsPackExpansion); |
| 2972 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2973 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2974 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2975 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2976 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2977 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 2978 | QualType UnderlyingTy, DiagTy; |
| 2979 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 2980 | UnderlyingTy = DiagTy = VD->getType(); |
| 2981 | } else { |
| 2982 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 2983 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 2984 | UnderlyingTy = ED->getIntegerType(); |
| 2985 | } |
| 2986 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2987 | return; |
| 2988 | |
| 2989 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2990 | // The combined effect of all alignment attributes in a declaration shall |
| 2991 | // not specify an alignment that is less strict than the alignment that |
| 2992 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2993 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2994 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2995 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2996 | if (I->isAlignmentDependent()) |
| 2997 | return; |
| 2998 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2999 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3000 | Align = std::max(Align, I->getAlignment(Context)); |
| 3001 | } |
| 3002 | |
| 3003 | if (AlignasAttr && Align) { |
| 3004 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3005 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3006 | if (NaturalAlign > RequestedAlign) |
| 3007 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3008 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3012 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3013 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3014 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3015 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3016 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3017 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3018 | // have to wait until the record is defined to catch any mismatches. |
| 3019 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3020 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3021 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3022 | // The unspecified model never matches what a definition could need. |
| 3023 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3024 | return false; |
| 3025 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3026 | if (BestCase) { |
| 3027 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3028 | return false; |
| 3029 | } else { |
| 3030 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3031 | return false; |
| 3032 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3033 | |
| 3034 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3035 | << 0 /*definition*/; |
| 3036 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3037 | << RD->getNameAsString(); |
| 3038 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3041 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3042 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3043 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3044 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3045 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3046 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3047 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3048 | // This attribute isn't documented, but glibc uses it. It changes |
| 3049 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3050 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3051 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3052 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3053 | return; |
| 3054 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3055 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3056 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3057 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3058 | |
| 3059 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3060 | if (Str.startswith("__") && Str.endswith("__")) |
| 3061 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3062 | |
| 3063 | unsigned DestWidth = 0; |
| 3064 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3065 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3066 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3067 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3068 | switch (Str[0]) { |
| 3069 | case 'Q': DestWidth = 8; break; |
| 3070 | case 'H': DestWidth = 16; break; |
| 3071 | case 'S': DestWidth = 32; break; |
| 3072 | case 'D': DestWidth = 64; break; |
| 3073 | case 'X': DestWidth = 96; break; |
| 3074 | case 'T': DestWidth = 128; break; |
| 3075 | } |
| 3076 | if (Str[1] == 'F') { |
| 3077 | IntegerMode = false; |
| 3078 | } else if (Str[1] == 'C') { |
| 3079 | IntegerMode = false; |
| 3080 | ComplexMode = true; |
| 3081 | } else if (Str[1] != 'I') { |
| 3082 | DestWidth = 0; |
| 3083 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3084 | break; |
| 3085 | case 4: |
| 3086 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3087 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3088 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3089 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3090 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3091 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3092 | break; |
| 3093 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3094 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3095 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3096 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3097 | case 11: |
| 3098 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3099 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3100 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3104 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3105 | OldTy = TD->getUnderlyingType(); |
| 3106 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3107 | OldTy = VD->getType(); |
| 3108 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3109 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 3110 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3111 | return; |
| 3112 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3113 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3114 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3115 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3116 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3117 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3118 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3119 | } else if (ComplexMode) { |
| 3120 | if (!OldTy->isComplexType()) |
| 3121 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3122 | } else { |
| 3123 | if (!OldTy->isFloatingType()) |
| 3124 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3125 | } |
| 3126 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3127 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3128 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3129 | // FIXME: Make sure floating-point mappings are accurate |
| 3130 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3131 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3132 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3133 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | QualType NewTy; |
| 3137 | |
| 3138 | if (IntegerMode) |
| 3139 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3140 | OldTy->isSignedIntegerType()); |
| 3141 | else |
| 3142 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3143 | |
| 3144 | if (NewTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3145 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3146 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3149 | if (ComplexMode) { |
| 3150 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3154 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3155 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3156 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3157 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3158 | |
| 3159 | D->addAttr(::new (S.Context) |
| 3160 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3161 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3162 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3163 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3164 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3165 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3166 | if (!VD->hasGlobalStorage()) |
| 3167 | S.Diag(Attr.getLoc(), |
| 3168 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3169 | << Attr.getName(); |
| 3170 | } else if (!isFunctionOrMethod(D)) { |
| 3171 | S.Diag(Attr.getLoc(), |
| 3172 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3173 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3174 | return; |
| 3175 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3176 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3177 | D->addAttr(::new (S.Context) |
| 3178 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3179 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3182 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3183 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3184 | unsigned AttrSpellingListIndex) { |
| 3185 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3186 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3187 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3188 | return nullptr; |
| 3189 | } |
| 3190 | |
| 3191 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3192 | return nullptr; |
| 3193 | |
| 3194 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3195 | AttrSpellingListIndex); |
| 3196 | } |
| 3197 | |
| 3198 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 3199 | unsigned AttrSpellingListIndex) { |
| 3200 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 3201 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 3202 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3203 | return nullptr; |
| 3204 | } |
| 3205 | |
| 3206 | if (D->hasAttr<MinSizeAttr>()) |
| 3207 | return nullptr; |
| 3208 | |
| 3209 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 3210 | } |
| 3211 | |
| 3212 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 3213 | unsigned AttrSpellingListIndex) { |
| 3214 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 3215 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 3216 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3217 | D->dropAttr<AlwaysInlineAttr>(); |
| 3218 | } |
| 3219 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 3220 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 3221 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3222 | D->dropAttr<MinSizeAttr>(); |
| 3223 | } |
| 3224 | |
| 3225 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 3226 | return nullptr; |
| 3227 | |
| 3228 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 3229 | AttrSpellingListIndex); |
| 3230 | } |
| 3231 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3232 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 3233 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3234 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 3235 | D, Attr.getRange(), Attr.getName(), |
| 3236 | Attr.getAttributeSpellingListIndex())) |
| 3237 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3240 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3241 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 3242 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3243 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3246 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 3247 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3248 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 3249 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3250 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3253 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3254 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3255 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3256 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 3257 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3258 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3259 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 3260 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3261 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3262 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3263 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3264 | D->addAttr(::new (S.Context) |
| 3265 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 3266 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3269 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3270 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3271 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3272 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3273 | return; |
| 3274 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3275 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3276 | D->addAttr(::new (S.Context) |
| 3277 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3278 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3281 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3282 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3283 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3284 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3285 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3286 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3287 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3288 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3289 | return; |
| 3290 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3291 | if (!isa<ObjCMethodDecl>(D)) { |
| 3292 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3293 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3294 | return; |
| 3295 | } |
| 3296 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3297 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3298 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3299 | D->addAttr(::new (S.Context) |
| 3300 | FastCallAttr(Attr.getRange(), S.Context, |
| 3301 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3302 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3303 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3304 | D->addAttr(::new (S.Context) |
| 3305 | StdCallAttr(Attr.getRange(), S.Context, |
| 3306 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3307 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3308 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3309 | D->addAttr(::new (S.Context) |
| 3310 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3311 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3312 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3313 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3314 | D->addAttr(::new (S.Context) |
| 3315 | CDeclAttr(Attr.getRange(), S.Context, |
| 3316 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3317 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3318 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3319 | D->addAttr(::new (S.Context) |
| 3320 | PascalAttr(Attr.getRange(), S.Context, |
| 3321 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3322 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3323 | case AttributeList::AT_VectorCall: |
| 3324 | D->addAttr(::new (S.Context) |
| 3325 | VectorCallAttr(Attr.getRange(), S.Context, |
| 3326 | Attr.getAttributeSpellingListIndex())); |
| 3327 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3328 | case AttributeList::AT_MSABI: |
| 3329 | D->addAttr(::new (S.Context) |
| 3330 | MSABIAttr(Attr.getRange(), S.Context, |
| 3331 | Attr.getAttributeSpellingListIndex())); |
| 3332 | return; |
| 3333 | case AttributeList::AT_SysVABI: |
| 3334 | D->addAttr(::new (S.Context) |
| 3335 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3336 | Attr.getAttributeSpellingListIndex())); |
| 3337 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3338 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3339 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3340 | switch (CC) { |
| 3341 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3342 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3343 | break; |
| 3344 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3345 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3346 | break; |
| 3347 | default: |
| 3348 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3351 | D->addAttr(::new (S.Context) |
| 3352 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3353 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3354 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3355 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3356 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3357 | D->addAttr(::new (S.Context) |
| 3358 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3359 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3360 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3361 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3362 | default: |
| 3363 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3364 | } |
| 3365 | } |
| 3366 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3367 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3368 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3369 | if (attr.isInvalid()) |
| 3370 | return true; |
| 3371 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3372 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3373 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3374 | attr.setInvalid(); |
| 3375 | return true; |
| 3376 | } |
| 3377 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3378 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3379 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3380 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3381 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3382 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3383 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3384 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3385 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3386 | case AttributeList::AT_MSABI: |
| 3387 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3388 | CC_X86_64Win64; |
| 3389 | break; |
| 3390 | case AttributeList::AT_SysVABI: |
| 3391 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3392 | CC_C; |
| 3393 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3394 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3395 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3396 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3397 | attr.setInvalid(); |
| 3398 | return true; |
| 3399 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3400 | if (StrRef == "aapcs") { |
| 3401 | CC = CC_AAPCS; |
| 3402 | break; |
| 3403 | } else if (StrRef == "aapcs-vfp") { |
| 3404 | CC = CC_AAPCS_VFP; |
| 3405 | break; |
| 3406 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3407 | |
| 3408 | attr.setInvalid(); |
| 3409 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3410 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3411 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3412 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3413 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3416 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3417 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3418 | if (A != TargetInfo::CCCR_OK) { |
| 3419 | if (A == TargetInfo::CCCR_Warning) |
| 3420 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3421 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3422 | // This convention is not valid for the target. Use the default function or |
| 3423 | // method calling convention. |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3424 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3425 | if (FD) |
| 3426 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3427 | TargetInfo::CCMT_NonMember; |
| 3428 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3431 | return false; |
| 3432 | } |
| 3433 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3434 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3435 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3436 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3437 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3438 | return true; |
| 3439 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3440 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3441 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3442 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3443 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3444 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3445 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3446 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3447 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3448 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3449 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3452 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3453 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3454 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3455 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3456 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3459 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3460 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3461 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3462 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3463 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3464 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3467 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3470 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3471 | const AttributeList &Attr) { |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3472 | uint32_t MaxThreads, MinBlocks = 0; |
| 3473 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3474 | return; |
| 3475 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3476 | Attr.getArgAsExpr(1), |
| 3477 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3478 | return; |
| 3479 | |
| 3480 | D->addAttr(::new (S.Context) |
| 3481 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3482 | MaxThreads, MinBlocks, |
| 3483 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3486 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3487 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3488 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3489 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3490 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3491 | return; |
| 3492 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3493 | |
| 3494 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3495 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3496 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3497 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3498 | |
| 3499 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3500 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3501 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3502 | return; |
| 3503 | } |
| 3504 | |
| 3505 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3506 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3507 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3508 | return; |
| 3509 | |
| 3510 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3511 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3512 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3513 | return; |
| 3514 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3515 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3516 | if (IsPointer) { |
| 3517 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3518 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3519 | if (!BufferTy->isPointerType()) { |
| 3520 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3521 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3525 | D->addAttr(::new (S.Context) |
| 3526 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3527 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3528 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3532 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3533 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3534 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3535 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3536 | return; |
| 3537 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3538 | |
| 3539 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3540 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3541 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3542 | if (!isa<VarDecl>(D)) { |
| 3543 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3544 | << Attr.getName() << ExpectedVariable; |
| 3545 | return; |
| 3546 | } |
| 3547 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3548 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3549 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3550 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3551 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3552 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3553 | D->addAttr(::new (S.Context) |
| 3554 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3555 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3556 | Attr.getLayoutCompatible(), |
| 3557 | Attr.getMustBeNull(), |
| 3558 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3561 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3562 | // Checker-specific attribute handlers. |
| 3563 | //===----------------------------------------------------------------------===// |
| 3564 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3565 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3566 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3567 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3570 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3571 | return type->isDependentType() || |
| 3572 | type->isObjCObjectPointerType() || |
| 3573 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3574 | } |
| 3575 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3576 | return type->isDependentType() || |
| 3577 | type->isPointerType() || |
| 3578 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3581 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3582 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3583 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3584 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3585 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3586 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3587 | cf = false; |
| 3588 | } else { |
| 3589 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3590 | cf = true; |
| 3591 | } |
| 3592 | |
| 3593 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3594 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3595 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3596 | return; |
| 3597 | } |
| 3598 | |
| 3599 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3600 | param->addAttr(::new (S.Context) |
| 3601 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3602 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3603 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3604 | param->addAttr(::new (S.Context) |
| 3605 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3606 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3609 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3610 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3611 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3612 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3613 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3614 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3615 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3616 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3617 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3618 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3619 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3620 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3621 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3622 | returnType = FD->getReturnType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3623 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3624 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3625 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3626 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3627 | return; |
| 3628 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3629 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3630 | bool typeOK; |
| 3631 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3632 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3633 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3634 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3635 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3636 | cf = false; |
| 3637 | break; |
| 3638 | |
| 3639 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3640 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3641 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3642 | cf = false; |
| 3643 | break; |
| 3644 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3645 | case AttributeList::AT_CFReturnsRetained: |
| 3646 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3647 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3648 | cf = true; |
| 3649 | break; |
| 3650 | } |
| 3651 | |
| 3652 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3653 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3654 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3655 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3656 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3657 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3658 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3659 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3660 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3661 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3662 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 3663 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3664 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3665 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3666 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 3667 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3668 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3669 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3670 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 3671 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3672 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3673 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3674 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 3675 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3676 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3677 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3678 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 3679 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3680 | return; |
| 3681 | }; |
| 3682 | } |
| 3683 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3684 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3685 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3686 | const int EP_ObjCMethod = 1; |
| 3687 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3688 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3689 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3690 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3691 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3692 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3693 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3694 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3695 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3696 | if (!resultType->isReferenceType() && |
| 3697 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3698 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3699 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3700 | << attr.getName() |
| 3701 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3702 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3703 | |
| 3704 | // Drop the attribute. |
| 3705 | return; |
| 3706 | } |
| 3707 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3708 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 3709 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3712 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3713 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3714 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3715 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3716 | DeclContext *DC = method->getDeclContext(); |
| 3717 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3718 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3719 | << attr.getName() << 0; |
| 3720 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3721 | return; |
| 3722 | } |
| 3723 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3724 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3725 | << attr.getName() << 1; |
| 3726 | return; |
| 3727 | } |
| 3728 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3729 | method->addAttr(::new (S.Context) |
| 3730 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3731 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3734 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3735 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3736 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3737 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3738 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3739 | D->addAttr(::new (S.Context) |
| 3740 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3741 | Attr.getAttributeSpellingListIndex())); |
| 3742 | } |
| 3743 | |
| 3744 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3745 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3746 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3747 | return; |
| 3748 | |
| 3749 | D->addAttr(::new (S.Context) |
| 3750 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3751 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3754 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3755 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3756 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3757 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3758 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3759 | 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] | 3760 | return; |
| 3761 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 3762 | |
| 3763 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 3764 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 3765 | if (!Parm->Ident->isStr("id")) { |
| 3766 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 3767 | << Attr.getName(); |
| 3768 | return; |
| 3769 | } |
| 3770 | |
| 3771 | // Only allow 'cv void *'. |
| 3772 | QualType T = TD->getUnderlyingType(); |
| 3773 | if (!T->isVoidPointerType()) { |
| 3774 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 3775 | return; |
| 3776 | } |
| 3777 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3778 | |
| 3779 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3780 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3781 | Attr.getAttributeSpellingListIndex())); |
| 3782 | } |
| 3783 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3784 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3785 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3786 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 3787 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3788 | if (!Parm) { |
| 3789 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3790 | return; |
| 3791 | } |
| 3792 | |
| 3793 | D->addAttr(::new (S.Context) |
| 3794 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3795 | Attr.getAttributeSpellingListIndex())); |
| 3796 | } |
| 3797 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3798 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3799 | const AttributeList &Attr) { |
| 3800 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3801 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3802 | if (!RelatedClass) { |
| 3803 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3804 | return; |
| 3805 | } |
| 3806 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3807 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3808 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3809 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3810 | D->addAttr(::new (S.Context) |
| 3811 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3812 | ClassMethod, InstanceMethod, |
| 3813 | Attr.getAttributeSpellingListIndex())); |
| 3814 | } |
| 3815 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3816 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3817 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 3818 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 3819 | if (ObjCCategoryDecl *CatDecl = |
| 3820 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 3821 | IFace = CatDecl->getClassInterface(); |
| 3822 | else |
| 3823 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 3824 | |
| 3825 | if (!IFace) |
| 3826 | return; |
| 3827 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3828 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3829 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3830 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3831 | Attr.getAttributeSpellingListIndex())); |
| 3832 | } |
| 3833 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 3834 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 3835 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 3836 | StringRef MetaDataName; |
| 3837 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 3838 | return; |
| 3839 | D->addAttr(::new (S.Context) |
| 3840 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 3841 | MetaDataName, |
| 3842 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3845 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3846 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3847 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3848 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3849 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3850 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3853 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3854 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3855 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3856 | QualType type = vd->getType(); |
| 3857 | |
| 3858 | if (!type->isDependentType() && |
| 3859 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3860 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3861 | << type; |
| 3862 | return; |
| 3863 | } |
| 3864 | |
| 3865 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3866 | |
| 3867 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3868 | // going to infer. |
| 3869 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3870 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3871 | |
| 3872 | switch (lifetime) { |
| 3873 | case Qualifiers::OCL_None: |
| 3874 | assert(type->isDependentType() && |
| 3875 | "didn't infer lifetime for non-dependent type?"); |
| 3876 | break; |
| 3877 | |
| 3878 | case Qualifiers::OCL_Weak: // meaningful |
| 3879 | case Qualifiers::OCL_Strong: // meaningful |
| 3880 | break; |
| 3881 | |
| 3882 | case Qualifiers::OCL_ExplicitNone: |
| 3883 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3884 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3885 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3886 | break; |
| 3887 | } |
| 3888 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3889 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3890 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3891 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3894 | //===----------------------------------------------------------------------===// |
| 3895 | // Microsoft specific attribute handlers. |
| 3896 | //===----------------------------------------------------------------------===// |
| 3897 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3898 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3899 | if (!S.LangOpts.CPlusPlus) { |
| 3900 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3901 | << Attr.getName() << AttributeLangSupport::C; |
| 3902 | return; |
| 3903 | } |
| 3904 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3905 | if (!isa<CXXRecordDecl>(D)) { |
| 3906 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3907 | << Attr.getName() << ExpectedClass; |
| 3908 | return; |
| 3909 | } |
| 3910 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3911 | StringRef StrRef; |
| 3912 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3913 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3914 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3915 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3916 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3917 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3918 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3919 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3920 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3921 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3922 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3923 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3924 | return; |
| 3925 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3926 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3927 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3928 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3929 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3930 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3931 | return; |
| 3932 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3933 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3934 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3935 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3936 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3937 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3938 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3939 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3940 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3943 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3944 | if (!S.LangOpts.CPlusPlus) { |
| 3945 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3946 | << Attr.getName() << AttributeLangSupport::C; |
| 3947 | return; |
| 3948 | } |
| 3949 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3950 | D, Attr.getRange(), /*BestCase=*/true, |
| 3951 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3952 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
| 3953 | if (IA) |
| 3954 | D->addAttr(IA); |
| 3955 | } |
| 3956 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 3957 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 3958 | const AttributeList &Attr) { |
| 3959 | VarDecl *VD = cast<VarDecl>(D); |
| 3960 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 3961 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 3962 | return; |
| 3963 | } |
| 3964 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 3965 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 3966 | return; |
| 3967 | } |
| 3968 | if (VD->hasLocalStorage()) { |
| 3969 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 3970 | return; |
| 3971 | } |
| 3972 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 3973 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3974 | } |
| 3975 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3976 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 3977 | const AttributeList &Attr) { |
| 3978 | // Check the attribute arguments. |
| 3979 | if (Attr.getNumArgs() > 1) { |
| 3980 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3981 | << Attr.getName() << 1; |
| 3982 | return; |
| 3983 | } |
| 3984 | |
| 3985 | StringRef Str; |
| 3986 | SourceLocation ArgLoc; |
| 3987 | |
| 3988 | if (Attr.getNumArgs() == 0) |
| 3989 | Str = ""; |
| 3990 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 3991 | return; |
| 3992 | |
| 3993 | ARMInterruptAttr::InterruptType Kind; |
| 3994 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 3995 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3996 | << Attr.getName() << Str << ArgLoc; |
| 3997 | return; |
| 3998 | } |
| 3999 | |
| 4000 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 4001 | D->addAttr(::new (S.Context) |
| 4002 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 4003 | } |
| 4004 | |
| 4005 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 4006 | const AttributeList &Attr) { |
| 4007 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4008 | return; |
| 4009 | |
| 4010 | if (!Attr.isArgExpr(0)) { |
| 4011 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 4012 | << AANT_ArgumentIntegerConstant; |
| 4013 | return; |
| 4014 | } |
| 4015 | |
| 4016 | // FIXME: Check for decl - it should be void ()(void). |
| 4017 | |
| 4018 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4019 | llvm::APSInt NumParams(32); |
| 4020 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 4021 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4022 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4023 | << NumParamsExpr->getSourceRange(); |
| 4024 | return; |
| 4025 | } |
| 4026 | |
| 4027 | unsigned Num = NumParams.getLimitedValue(255); |
| 4028 | if ((Num & 1) || Num > 30) { |
| 4029 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 4030 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 4031 | << NumParamsExpr->getSourceRange(); |
| 4032 | return; |
| 4033 | } |
| 4034 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4035 | D->addAttr(::new (S.Context) |
| 4036 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 4037 | Attr.getAttributeSpellingListIndex())); |
| 4038 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4042 | // Dispatch the interrupt attribute based on the current target. |
| 4043 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 4044 | handleMSP430InterruptAttr(S, D, Attr); |
| 4045 | else |
| 4046 | handleARMInterruptAttr(S, D, Attr); |
| 4047 | } |
| 4048 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4049 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 4050 | const AttributeList &Attr) { |
| 4051 | uint32_t NumRegs; |
| 4052 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4053 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4054 | return; |
| 4055 | |
| 4056 | D->addAttr(::new (S.Context) |
| 4057 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, |
| 4058 | NumRegs, |
| 4059 | Attr.getAttributeSpellingListIndex())); |
| 4060 | } |
| 4061 | |
| 4062 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 4063 | const AttributeList &Attr) { |
| 4064 | uint32_t NumRegs; |
| 4065 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4066 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4067 | return; |
| 4068 | |
| 4069 | D->addAttr(::new (S.Context) |
| 4070 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, |
| 4071 | NumRegs, |
| 4072 | Attr.getAttributeSpellingListIndex())); |
| 4073 | } |
| 4074 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4075 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 4076 | const AttributeList& Attr) { |
| 4077 | // If we try to apply it to a function pointer, don't warn, but don't |
| 4078 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 4079 | // special about calling a force_align_arg_pointer function. |
| 4080 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 4081 | if (VD && VD->getType()->isFunctionPointerType()) |
| 4082 | return; |
| 4083 | // Also don't warn on function pointer typedefs. |
| 4084 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 4085 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 4086 | TD->getUnderlyingType()->isFunctionType())) |
| 4087 | return; |
| 4088 | // Attribute can only be applied to function types. |
| 4089 | if (!isa<FunctionDecl>(D)) { |
| 4090 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4091 | << Attr.getName() << /* function */0; |
| 4092 | return; |
| 4093 | } |
| 4094 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4095 | D->addAttr(::new (S.Context) |
| 4096 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 4097 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 4101 | unsigned AttrSpellingListIndex) { |
| 4102 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4103 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4104 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4108 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4109 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4110 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4113 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 4114 | unsigned AttrSpellingListIndex) { |
| 4115 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4116 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4117 | D->dropAttr<DLLImportAttr>(); |
| 4118 | } |
| 4119 | |
| 4120 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4121 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4122 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4123 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4126 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 4127 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 4128 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4129 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 4130 | << A.getName(); |
| 4131 | return; |
| 4132 | } |
| 4133 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 4134 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4135 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 4136 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4137 | // MinGW doesn't allow dllimport on inline functions. |
| 4138 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 4139 | << A.getName(); |
| 4140 | return; |
| 4141 | } |
| 4142 | } |
| 4143 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4144 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 4145 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 4146 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 4147 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4148 | if (NewAttr) |
| 4149 | D->addAttr(NewAttr); |
| 4150 | } |
| 4151 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4152 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4153 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4154 | unsigned AttrSpellingListIndex, |
| 4155 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 4156 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 4157 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4158 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4159 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 4160 | << 1 /*previous declaration*/; |
| 4161 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 4162 | D->dropAttr<MSInheritanceAttr>(); |
| 4163 | } |
| 4164 | |
| 4165 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 4166 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4167 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 4168 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4169 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4170 | } |
| 4171 | } else { |
| 4172 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 4173 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 4174 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4175 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4176 | } |
| 4177 | if (RD->getDescribedClassTemplate()) { |
| 4178 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 4179 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4180 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4185 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4188 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4189 | // The capability attributes take a single string parameter for the name of |
| 4190 | // the capability they represent. The lockable attribute does not take any |
| 4191 | // parameters. However, semantically, both attributes represent the same |
| 4192 | // concept, and so they use the same semantic attribute. Eventually, the |
| 4193 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4194 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 4195 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4196 | // literal will be considered a "mutex." |
| 4197 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4198 | SourceLocation LiteralLoc; |
| 4199 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 4200 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 4201 | return; |
| 4202 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 4203 | // Currently, there are only two names allowed for a capability: role and |
| 4204 | // mutex (case insensitive). Diagnose other capability names. |
| 4205 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 4206 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 4207 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4208 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 4209 | Attr.getAttributeSpellingListIndex())); |
| 4210 | } |
| 4211 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4212 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 4213 | const AttributeList &Attr) { |
| 4214 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 4215 | Attr.getArgAsExpr(0), |
| 4216 | Attr.getAttributeSpellingListIndex())); |
| 4217 | } |
| 4218 | |
| 4219 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 4220 | const AttributeList &Attr) { |
| 4221 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4222 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4223 | return; |
| 4224 | |
| 4225 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 4226 | S.Context, |
| 4227 | Args.data(), Args.size(), |
| 4228 | Attr.getAttributeSpellingListIndex())); |
| 4229 | } |
| 4230 | |
| 4231 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 4232 | const AttributeList &Attr) { |
| 4233 | SmallVector<Expr*, 2> Args; |
| 4234 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 4235 | return; |
| 4236 | |
| 4237 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 4238 | S.Context, |
| 4239 | Attr.getArgAsExpr(0), |
| 4240 | Args.data(), |
| 4241 | Args.size(), |
| 4242 | Attr.getAttributeSpellingListIndex())); |
| 4243 | } |
| 4244 | |
| 4245 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 4246 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4247 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4248 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 4249 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4250 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 4251 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 4252 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 4253 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4256 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 4257 | const AttributeList &Attr) { |
| 4258 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4259 | return; |
| 4260 | |
| 4261 | // check that all arguments are lockable objects |
| 4262 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 4263 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4264 | if (Args.empty()) |
| 4265 | return; |
| 4266 | |
| 4267 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 4268 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 4269 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 4270 | |
| 4271 | D->addAttr(RCA); |
| 4272 | } |
| 4273 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4274 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4275 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 4276 | if (NSD->isAnonymousNamespace()) { |
| 4277 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 4278 | // Do not want to attach the attribute to the namespace because that will |
| 4279 | // cause confusing diagnostic reports for uses of declarations within the |
| 4280 | // namespace. |
| 4281 | return; |
| 4282 | } |
| 4283 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 4284 | |
| 4285 | if (!S.getLangOpts().CPlusPlus14) |
| 4286 | if (Attr.isCXX11Attribute() && |
| 4287 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Saleem Abdulrasool | b47d606 | 2015-02-18 04:33:26 +0000 | [diff] [blame] | 4288 | S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 4289 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4290 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
| 4291 | } |
| 4292 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4293 | /// Handles semantic checking for features that are common to all attributes, |
| 4294 | /// such as checking whether a parameter was properly specified, or the correct |
| 4295 | /// number of arguments were passed, etc. |
| 4296 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4297 | const AttributeList &Attr) { |
| 4298 | // Several attributes carry different semantics than the parsing requires, so |
| 4299 | // those are opted out of the common handling. |
| 4300 | // |
| 4301 | // We also bail on unknown and ignored attributes because those are handled |
| 4302 | // as part of the target-specific handling logic. |
| 4303 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4304 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4305 | return false; |
| 4306 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4307 | // Check whether the attribute requires specific language extensions to be |
| 4308 | // enabled. |
| 4309 | if (!Attr.diagnoseLangOpts(S)) |
| 4310 | return true; |
| 4311 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 4312 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 4313 | // If there are no optional arguments, then checking for the argument count |
| 4314 | // is trivial. |
| 4315 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4316 | return true; |
| 4317 | } else { |
| 4318 | // There are optional arguments, so checking is slightly more involved. |
| 4319 | if (Attr.getMinArgs() && |
| 4320 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 4321 | return true; |
| 4322 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 4323 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 4324 | return true; |
| 4325 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4326 | |
| 4327 | // Check whether the attribute appertains to the given subject. |
| 4328 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 4329 | return true; |
| 4330 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4331 | return false; |
| 4332 | } |
| 4333 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4334 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4335 | // Top Level Sema Entry Points |
| 4336 | //===----------------------------------------------------------------------===// |
| 4337 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4338 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4339 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4340 | /// silently ignore it if a GNU attribute. |
| 4341 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4342 | const AttributeList &Attr, |
| 4343 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4344 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4345 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4346 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4347 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4348 | // instead. |
| 4349 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4350 | return; |
| 4351 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4352 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 4353 | // which do not apply to the current target architecture are treated as |
| 4354 | // though they were unknown attributes. |
| 4355 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
| 4356 | !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4357 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 4358 | ? diag::warn_unhandled_ms_attribute_ignored |
| 4359 | : diag::warn_unknown_attribute_ignored) |
| 4360 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4361 | return; |
| 4362 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4363 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4364 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4365 | return; |
| 4366 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4367 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4368 | default: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4369 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4370 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4371 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4372 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4373 | handleInterruptAttr(S, D, Attr); |
| 4374 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4375 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4376 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 4377 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4378 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4379 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4380 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4381 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4382 | case AttributeList::AT_Mips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4383 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); |
| 4384 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4385 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4386 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 4387 | break; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4388 | case AttributeList::AT_AMDGPUNumVGPR: |
| 4389 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 4390 | break; |
| 4391 | case AttributeList::AT_AMDGPUNumSGPR: |
| 4392 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 4393 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 4394 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4395 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 4396 | break; |
| 4397 | case AttributeList::AT_IBOutlet: |
| 4398 | handleIBOutlet(S, D, Attr); |
| 4399 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4400 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4401 | handleIBOutletCollection(S, D, Attr); |
| 4402 | break; |
| 4403 | case AttributeList::AT_Alias: |
| 4404 | handleAliasAttr(S, D, Attr); |
| 4405 | break; |
| 4406 | case AttributeList::AT_Aligned: |
| 4407 | handleAlignedAttr(S, D, Attr); |
| 4408 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 4409 | case AttributeList::AT_AlignValue: |
| 4410 | handleAlignValueAttr(S, D, Attr); |
| 4411 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4412 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4413 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4414 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4415 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4416 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 4417 | break; |
| 4418 | case AttributeList::AT_TLSModel: |
| 4419 | handleTLSModelAttr(S, D, Attr); |
| 4420 | break; |
| 4421 | case AttributeList::AT_Annotate: |
| 4422 | handleAnnotateAttr(S, D, Attr); |
| 4423 | break; |
| 4424 | case AttributeList::AT_Availability: |
| 4425 | handleAvailabilityAttr(S, D, Attr); |
| 4426 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4427 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4428 | handleDependencyAttr(S, scope, D, Attr); |
| 4429 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4430 | case AttributeList::AT_Common: |
| 4431 | handleCommonAttr(S, D, Attr); |
| 4432 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4433 | case AttributeList::AT_CUDAConstant: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4434 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); |
| 4435 | break; |
| 4436 | case AttributeList::AT_Constructor: |
| 4437 | handleConstructorAttr(S, D, Attr); |
| 4438 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4439 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4440 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 4441 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4442 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 4443 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4444 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4445 | case AttributeList::AT_Destructor: |
| 4446 | handleDestructorAttr(S, D, Attr); |
| 4447 | break; |
| 4448 | case AttributeList::AT_EnableIf: |
| 4449 | handleEnableIfAttr(S, D, Attr); |
| 4450 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4451 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4452 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4453 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4454 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4455 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4456 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4457 | case AttributeList::AT_OptimizeNone: |
| 4458 | handleOptimizeNoneAttr(S, D, Attr); |
| 4459 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 4460 | case AttributeList::AT_FlagEnum: |
| 4461 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 4462 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 4463 | case AttributeList::AT_Flatten: |
| 4464 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 4465 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4466 | case AttributeList::AT_Format: |
| 4467 | handleFormatAttr(S, D, Attr); |
| 4468 | break; |
| 4469 | case AttributeList::AT_FormatArg: |
| 4470 | handleFormatArgAttr(S, D, Attr); |
| 4471 | break; |
| 4472 | case AttributeList::AT_CUDAGlobal: |
| 4473 | handleGlobalAttr(S, D, Attr); |
| 4474 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 4475 | case AttributeList::AT_CUDADevice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4476 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); |
| 4477 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4478 | case AttributeList::AT_CUDAHost: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4479 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); |
| 4480 | break; |
| 4481 | case AttributeList::AT_GNUInline: |
| 4482 | handleGNUInlineAttr(S, D, Attr); |
| 4483 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4484 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4485 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4486 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 4487 | case AttributeList::AT_Restrict: |
| 4488 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4489 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4490 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4491 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 4492 | break; |
| 4493 | case AttributeList::AT_Mode: |
| 4494 | handleModeAttr(S, D, Attr); |
| 4495 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4496 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4497 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 4498 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 4499 | case AttributeList::AT_NoSplitStack: |
| 4500 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 4501 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 4502 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4503 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 4504 | handleNonNullAttrParameter(S, PVD, Attr); |
| 4505 | else |
| 4506 | handleNonNullAttr(S, D, Attr); |
| 4507 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 4508 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4509 | handleReturnsNonNullAttr(S, D, Attr); |
| 4510 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4511 | case AttributeList::AT_AssumeAligned: |
| 4512 | handleAssumeAlignedAttr(S, D, Attr); |
| 4513 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4514 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4515 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 4516 | break; |
| 4517 | case AttributeList::AT_Ownership: |
| 4518 | handleOwnershipAttr(S, D, Attr); |
| 4519 | break; |
| 4520 | case AttributeList::AT_Cold: |
| 4521 | handleColdAttr(S, D, Attr); |
| 4522 | break; |
| 4523 | case AttributeList::AT_Hot: |
| 4524 | handleHotAttr(S, D, Attr); |
| 4525 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4526 | case AttributeList::AT_Naked: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4527 | handleSimpleAttribute<NakedAttr>(S, D, Attr); |
| 4528 | break; |
| 4529 | case AttributeList::AT_NoReturn: |
| 4530 | handleNoReturnAttr(S, D, Attr); |
| 4531 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4532 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4533 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 4534 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4535 | case AttributeList::AT_CUDAShared: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4536 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); |
| 4537 | break; |
| 4538 | case AttributeList::AT_VecReturn: |
| 4539 | handleVecReturnAttr(S, D, Attr); |
| 4540 | break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4541 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4542 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4543 | handleObjCOwnershipAttr(S, D, Attr); |
| 4544 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4545 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4546 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 4547 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4548 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4549 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4550 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 4551 | break; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4552 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4553 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4554 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 4555 | break; |
| 4556 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4557 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4558 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 4559 | break; |
| 4560 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4561 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4562 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 4563 | break; |
| 4564 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4565 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4566 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 4567 | break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4568 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4569 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4570 | handleObjCDesignatedInitializer(S, D, Attr); |
| 4571 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4572 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4573 | case AttributeList::AT_ObjCRuntimeName: |
| 4574 | handleObjCRuntimeName(S, D, Attr); |
| 4575 | break; |
| 4576 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4577 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4578 | handleCFAuditedTransferAttr(S, D, Attr); |
| 4579 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4580 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4581 | handleCFUnknownTransferAttr(S, D, Attr); |
| 4582 | break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4583 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4584 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4585 | case AttributeList::AT_NSConsumed: |
| 4586 | handleNSConsumedAttr(S, D, Attr); |
| 4587 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4588 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4589 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 4590 | break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4591 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4592 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4593 | case AttributeList::AT_NSReturnsNotRetained: |
| 4594 | case AttributeList::AT_CFReturnsNotRetained: |
| 4595 | case AttributeList::AT_NSReturnsRetained: |
| 4596 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4597 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 4598 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4599 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4600 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 4601 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4602 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4603 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 4604 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4605 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4606 | handleVecTypeHint(S, D, Attr); |
| 4607 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4608 | |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4609 | case AttributeList::AT_InitPriority: |
| 4610 | handleInitPriorityAttr(S, D, Attr); |
| 4611 | break; |
| 4612 | |
| 4613 | case AttributeList::AT_Packed: |
| 4614 | handlePackedAttr(S, D, Attr); |
| 4615 | break; |
| 4616 | case AttributeList::AT_Section: |
| 4617 | handleSectionAttr(S, D, Attr); |
| 4618 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4619 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4620 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4621 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4622 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 4623 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 4624 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4625 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4626 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 4627 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4628 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 4629 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4630 | break; |
| 4631 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4632 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 4633 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 4634 | case AttributeList::AT_Unused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4635 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); |
| 4636 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4637 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4638 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 4639 | break; |
| 4640 | case AttributeList::AT_Used: |
| 4641 | handleUsedAttr(S, D, Attr); |
| 4642 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4643 | case AttributeList::AT_Visibility: |
| 4644 | handleVisibilityAttr(S, D, Attr, false); |
| 4645 | break; |
| 4646 | case AttributeList::AT_TypeVisibility: |
| 4647 | handleVisibilityAttr(S, D, Attr, true); |
| 4648 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4649 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4650 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 4651 | break; |
| 4652 | case AttributeList::AT_WarnUnusedResult: |
| 4653 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4654 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4655 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4656 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 4657 | break; |
| 4658 | case AttributeList::AT_WeakRef: |
| 4659 | handleWeakRefAttr(S, D, Attr); |
| 4660 | break; |
| 4661 | case AttributeList::AT_WeakImport: |
| 4662 | handleWeakImportAttr(S, D, Attr); |
| 4663 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4664 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4665 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4666 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4667 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4668 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 4669 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4670 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4671 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4672 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4673 | case AttributeList::AT_ObjCNSObject: |
| 4674 | handleObjCNSObject(S, D, Attr); |
| 4675 | break; |
| 4676 | case AttributeList::AT_Blocks: |
| 4677 | handleBlocksAttr(S, D, Attr); |
| 4678 | break; |
| 4679 | case AttributeList::AT_Sentinel: |
| 4680 | handleSentinelAttr(S, D, Attr); |
| 4681 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4682 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4683 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 4684 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4685 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4686 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 4687 | break; |
| 4688 | case AttributeList::AT_Cleanup: |
| 4689 | handleCleanupAttr(S, D, Attr); |
| 4690 | break; |
| 4691 | case AttributeList::AT_NoDebug: |
| 4692 | handleNoDebugAttr(S, D, Attr); |
| 4693 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 4694 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4695 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 4696 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4697 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4698 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 4699 | break; |
| 4700 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 4701 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 4702 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4703 | case AttributeList::AT_StdCall: |
| 4704 | case AttributeList::AT_CDecl: |
| 4705 | case AttributeList::AT_FastCall: |
| 4706 | case AttributeList::AT_ThisCall: |
| 4707 | case AttributeList::AT_Pascal: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4708 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4709 | case AttributeList::AT_MSABI: |
| 4710 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4711 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4712 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4713 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4714 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4715 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4716 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 4717 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4718 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4719 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); |
| 4720 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4721 | |
| 4722 | // Microsoft attributes: |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 4723 | case AttributeList::AT_MSNoVTable: |
| 4724 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 4725 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 4726 | case AttributeList::AT_MSStruct: |
| 4727 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4728 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4729 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4730 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4731 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4732 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4733 | handleMSInheritanceAttr(S, D, Attr); |
| 4734 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4736 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 4737 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 4738 | case AttributeList::AT_Thread: |
| 4739 | handleDeclspecThreadAttr(S, D, Attr); |
| 4740 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4741 | |
| 4742 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4743 | case AttributeList::AT_AssertExclusiveLock: |
| 4744 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4745 | break; |
| 4746 | case AttributeList::AT_AssertSharedLock: |
| 4747 | handleAssertSharedLockAttr(S, D, Attr); |
| 4748 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4749 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4750 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 4751 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4752 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4753 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4754 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4755 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4756 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 4757 | break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4758 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4759 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4760 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4761 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4762 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4763 | break; |
| 4764 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4765 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4766 | break; |
| 4767 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4768 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4769 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4770 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4771 | handleGuardedByAttr(S, D, Attr); |
| 4772 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4773 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4774 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4775 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4776 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4777 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4778 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4779 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4780 | handleLockReturnedAttr(S, D, Attr); |
| 4781 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4782 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4783 | handleLocksExcludedAttr(S, D, Attr); |
| 4784 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4785 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4786 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4787 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4788 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4789 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4790 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4791 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4792 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4793 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4794 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4795 | // Capability analysis attributes. |
| 4796 | case AttributeList::AT_Capability: |
| 4797 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4798 | handleCapabilityAttr(S, D, Attr); |
| 4799 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4800 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4801 | handleRequiresCapabilityAttr(S, D, Attr); |
| 4802 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4803 | |
| 4804 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4805 | handleAssertCapabilityAttr(S, D, Attr); |
| 4806 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4807 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4808 | handleAcquireCapabilityAttr(S, D, Attr); |
| 4809 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4810 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4811 | handleReleaseCapabilityAttr(S, D, Attr); |
| 4812 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4813 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4814 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 4815 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4816 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4817 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4818 | case AttributeList::AT_Consumable: |
| 4819 | handleConsumableAttr(S, D, Attr); |
| 4820 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4822 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 4823 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4824 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4825 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 4826 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4827 | case AttributeList::AT_CallableWhen: |
| 4828 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4829 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4830 | case AttributeList::AT_ParamTypestate: |
| 4831 | handleParamTypestateAttr(S, D, Attr); |
| 4832 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4833 | case AttributeList::AT_ReturnTypestate: |
| 4834 | handleReturnTypestateAttr(S, D, Attr); |
| 4835 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_SetTypestate: |
| 4837 | handleSetTypestateAttr(S, D, Attr); |
| 4838 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4839 | case AttributeList::AT_TestTypestate: |
| 4840 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4841 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4842 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4843 | // Type safety attributes. |
| 4844 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4845 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4846 | break; |
| 4847 | case AttributeList::AT_TypeTagForDatatype: |
| 4848 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4849 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4850 | } |
| 4851 | } |
| 4852 | |
| 4853 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4854 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4855 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4856 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4857 | bool IncludeCXX11Attributes) { |
| 4858 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4859 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4860 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4861 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4862 | // GCC accepts |
| 4863 | // static int a9 __attribute__((weakref)); |
| 4864 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4865 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 4866 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 4867 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4868 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4869 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4870 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4871 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 4872 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 4873 | // good to have a way to specify "these attributes must appear as a group", |
| 4874 | // for these. Additionally, it would be good to have a way to specify "these |
| 4875 | // 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] | 4876 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4877 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4878 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 4879 | // FIXME: This emits a different error message than |
| 4880 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4881 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4882 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 4883 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4884 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4885 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 4886 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4887 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4888 | D->setInvalidDecl(); |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 4889 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 4890 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 4891 | << A << ExpectedKernelFunction; |
| 4892 | D->setInvalidDecl(); |
| 4893 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 4894 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 4895 | << A << ExpectedKernelFunction; |
| 4896 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4897 | } |
| 4898 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4901 | // Annotation attributes are the only attributes allowed after an access |
| 4902 | // specifier. |
| 4903 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4904 | const AttributeList *AttrList) { |
| 4905 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4906 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 4907 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4908 | } else { |
| 4909 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4910 | return true; |
| 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | return false; |
| 4915 | } |
| 4916 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4917 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4918 | /// contains any decl attributes that we should warn about. |
| 4919 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4920 | for ( ; A; A = A->getNext()) { |
| 4921 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4922 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4923 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4924 | |
| 4925 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4926 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4927 | << A->getName() << A->getRange(); |
| 4928 | } else { |
| 4929 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4930 | << A->getName() << A->getRange(); |
| 4931 | } |
| 4932 | } |
| 4933 | } |
| 4934 | |
| 4935 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4936 | /// used to build a declaration, complain about any decl attributes |
| 4937 | /// which might be lying around on it. |
| 4938 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4939 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4940 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4941 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4942 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4943 | } |
| 4944 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4945 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4946 | /// \#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] | 4947 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4948 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4949 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4950 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4951 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4952 | FunctionDecl *NewFD; |
| 4953 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4954 | // FIXME: Mangling? |
| 4955 | // FIXME: Is the qualifier info correct? |
| 4956 | // FIXME: Is the DeclContext correct? |
| 4957 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4958 | Loc, Loc, DeclarationName(II), |
| 4959 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4960 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4961 | FD->hasPrototype(), |
| 4962 | false/*isConstexprSpecified*/); |
| 4963 | NewD = NewFD; |
| 4964 | |
| 4965 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4966 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4967 | |
| 4968 | // Fake up parameter variables; they are declared as if this were |
| 4969 | // a typedef. |
| 4970 | QualType FDTy = FD->getType(); |
| 4971 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4972 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4973 | for (const auto &AI : FT->param_types()) { |
| 4974 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4975 | Param->setScopeInfo(0, Params.size()); |
| 4976 | Params.push_back(Param); |
| 4977 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4978 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4979 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4980 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4981 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4982 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4983 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4984 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4985 | if (VD->getQualifier()) { |
| 4986 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4987 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4988 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4989 | } |
| 4990 | return NewD; |
| 4991 | } |
| 4992 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4993 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4994 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4995 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4996 | if (W.getUsed()) return; // only do this once |
| 4997 | W.setUsed(true); |
| 4998 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4999 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5000 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5001 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 5002 | W.getLocation())); |
| 5003 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5004 | WeakTopLevelDecl.push_back(NewD); |
| 5005 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5006 | // to insert Decl at TU scope, sorry. |
| 5007 | DeclContext *SavedContext = CurContext; |
| 5008 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 5009 | NewD->setDeclContext(CurContext); |
| 5010 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5011 | PushOnScopeChains(NewD, S); |
| 5012 | CurContext = SavedContext; |
| 5013 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5014 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5015 | } |
| 5016 | } |
| 5017 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5018 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5019 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5020 | // have to do this. |
| 5021 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5022 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5023 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5024 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5025 | if (VD->isExternC()) |
| 5026 | ND = VD; |
| 5027 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5028 | if (FD->isExternC()) |
| 5029 | ND = FD; |
| 5030 | if (ND) { |
| 5031 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 5032 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5033 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5034 | WeakInfo W = I->second; |
| 5035 | DeclApplyPragmaWeak(S, ND, W); |
| 5036 | WeakUndeclaredIdentifiers[Id] = W; |
| 5037 | } |
| 5038 | } |
| 5039 | } |
| 5040 | } |
| 5041 | } |
| 5042 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5043 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5044 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5045 | /// 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] | 5046 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5047 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5048 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5049 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5050 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5051 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5052 | // position to the decl itself. This handles cases like: |
| 5053 | // int *__attr__(x)** D; |
| 5054 | // when X is a decl attribute. |
| 5055 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5056 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5057 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5058 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5059 | // Finally, apply any attributes on the decl itself. |
| 5060 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5061 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5062 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5063 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5064 | /// Is the given declaration allowed to use a forbidden type? |
| 5065 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5066 | // Private ivars are always okay. Unfortunately, people don't |
| 5067 | // always properly make their ivars private, even in system headers. |
| 5068 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5069 | // Function declarations in sys headers will be marked unavailable. |
| 5070 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5071 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5072 | return false; |
| 5073 | |
| 5074 | // Require it to be declared in a system header. |
| 5075 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5076 | } |
| 5077 | |
| 5078 | /// Handle a delayed forbidden-type diagnostic. |
| 5079 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5080 | Decl *decl) { |
| 5081 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5082 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, |
| 5083 | "this system declaration uses an unsupported type", |
| 5084 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5085 | return; |
| 5086 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5087 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5088 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5089 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5090 | // kind of forbidden type messages on unavailable functions. |
| 5091 | if (FD->hasAttr<UnavailableAttr>() && |
| 5092 | diag.getForbiddenTypeDiagnostic() == |
| 5093 | diag::err_arc_array_param_no_ownership) { |
| 5094 | diag.Triggered = true; |
| 5095 | return; |
| 5096 | } |
| 5097 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5098 | |
| 5099 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5100 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5101 | diag.Triggered = true; |
| 5102 | } |
| 5103 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5104 | |
| 5105 | static bool isDeclDeprecated(Decl *D) { |
| 5106 | do { |
| 5107 | if (D->isDeprecated()) |
| 5108 | return true; |
| 5109 | // A category implicitly has the availability of the interface. |
| 5110 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5111 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 5112 | return Interface->isDeprecated(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5113 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5114 | return false; |
| 5115 | } |
| 5116 | |
| 5117 | static bool isDeclUnavailable(Decl *D) { |
| 5118 | do { |
| 5119 | if (D->isUnavailable()) |
| 5120 | return true; |
| 5121 | // A category implicitly has the availability of the interface. |
| 5122 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 5123 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 5124 | return Interface->isUnavailable(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5125 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5126 | return false; |
| 5127 | } |
| 5128 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5129 | static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5130 | Decl *Ctx, const NamedDecl *D, |
| 5131 | StringRef Message, SourceLocation Loc, |
| 5132 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5133 | const ObjCPropertyDecl *ObjCProperty, |
| 5134 | bool ObjCPropertyAccess) { |
| 5135 | // Diagnostics for deprecated or unavailable. |
| 5136 | unsigned diag, diag_message, diag_fwdclass_message; |
| 5137 | |
| 5138 | // Matches 'diag::note_property_attribute' options. |
| 5139 | unsigned property_note_select; |
| 5140 | |
| 5141 | // Matches diag::note_availability_specified_here. |
| 5142 | unsigned available_here_select_kind; |
| 5143 | |
| 5144 | // Don't warn if our current context is deprecated or unavailable. |
| 5145 | switch (K) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5146 | case Sema::AD_Deprecation: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5147 | if (isDeclDeprecated(Ctx)) |
| 5148 | return; |
| 5149 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 5150 | : diag::warn_property_method_deprecated; |
| 5151 | diag_message = diag::warn_deprecated_message; |
| 5152 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 5153 | property_note_select = /* deprecated */ 0; |
| 5154 | available_here_select_kind = /* deprecated */ 2; |
| 5155 | break; |
| 5156 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5157 | case Sema::AD_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5158 | if (isDeclUnavailable(Ctx)) |
| 5159 | return; |
| 5160 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 5161 | : diag::err_property_method_unavailable; |
| 5162 | diag_message = diag::err_unavailable_message; |
| 5163 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 5164 | property_note_select = /* unavailable */ 1; |
| 5165 | available_here_select_kind = /* unavailable */ 0; |
| 5166 | break; |
| 5167 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5168 | case Sema::AD_Partial: |
| 5169 | diag = diag::warn_partial_availability; |
| 5170 | diag_message = diag::warn_partial_message; |
| 5171 | diag_fwdclass_message = diag::warn_partial_fwdclass_message; |
| 5172 | property_note_select = /* partial */ 2; |
| 5173 | available_here_select_kind = /* partial */ 3; |
| 5174 | break; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5177 | if (!Message.empty()) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5178 | S.Diag(Loc, diag_message) << D << Message; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5179 | if (ObjCProperty) |
| 5180 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 5181 | << ObjCProperty->getDeclName() << property_note_select; |
| 5182 | } else if (!UnknownObjCClass) { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5183 | S.Diag(Loc, diag) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5184 | if (ObjCProperty) |
| 5185 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 5186 | << ObjCProperty->getDeclName() << property_note_select; |
| 5187 | } else { |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5188 | S.Diag(Loc, diag_fwdclass_message) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5189 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5190 | } |
| 5191 | |
| 5192 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 5193 | << D << available_here_select_kind; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5194 | if (K == Sema::AD_Partial) |
| 5195 | S.Diag(Loc, diag::note_partial_availability_silence) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
| 5198 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 5199 | Decl *Ctx) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5200 | assert(DD.Kind == DelayedDiagnostic::Deprecation || |
| 5201 | DD.Kind == DelayedDiagnostic::Unavailable); |
| 5202 | Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation |
| 5203 | ? Sema::AD_Deprecation |
| 5204 | : Sema::AD_Unavailable; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5205 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5206 | DoEmitAvailabilityWarning( |
| 5207 | S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc, |
| 5208 | DD.getUnknownObjCClass(), DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5211 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5212 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5213 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5214 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5215 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5216 | // When delaying diagnostics to run in the context of a parsed |
| 5217 | // declaration, we only want to actually emit anything if parsing |
| 5218 | // succeeds. |
| 5219 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5220 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5221 | // We emit all the active diagnostics in this pool or any of its |
| 5222 | // parents. In general, we'll get one pool for the decl spec |
| 5223 | // and a child pool for each declarator; in a decl group like: |
| 5224 | // deprecated_typedef foo, *bar, baz(); |
| 5225 | // only the declarator pops will be passed decls. This is correct; |
| 5226 | // we really do need to consider delayed diagnostics from the decl spec |
| 5227 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5228 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5229 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5230 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5231 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5232 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5233 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5234 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5235 | continue; |
| 5236 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5237 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5238 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5239 | case DelayedDiagnostic::Unavailable: |
| 5240 | // Don't bother giving deprecation/unavailable diagnostics if |
| 5241 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5242 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 5243 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5244 | break; |
| 5245 | |
| 5246 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5247 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5248 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5249 | |
| 5250 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5251 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5252 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5253 | } |
| 5254 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5255 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5258 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5259 | /// been delayed in the current context instead of in the given pool. |
| 5260 | /// Essentially, this just moves them to the current pool. |
| 5261 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5262 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5263 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5264 | curPool->steal(pool); |
| 5265 | } |
| 5266 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5267 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 5268 | NamedDecl *D, StringRef Message, |
| 5269 | SourceLocation Loc, |
| 5270 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame] | 5271 | const ObjCPropertyDecl *ObjCProperty, |
| 5272 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5273 | // Delay if we're currently parsing a declaration. |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5274 | if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) { |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 5275 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability( |
| 5276 | AD, Loc, D, UnknownObjCClass, ObjCProperty, Message, |
| 5277 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5278 | return; |
| 5279 | } |
| 5280 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 5281 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 5282 | DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass, |
| 5283 | ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5284 | } |