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" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 21 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 22 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 34 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 35 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 36 | C, |
| 37 | Cpp, |
| 38 | ObjC |
| 39 | }; |
| 40 | } |
| 41 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // Helper functions |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 46 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 47 | /// type (function or function-typed variable) or an Objective-C |
| 48 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 49 | static bool isFunctionOrMethod(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 50 | return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 51 | } |
| 52 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 53 | /// Return true if the given decl has a declarator that should have |
| 54 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 55 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 56 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 58 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 61 | /// hasFunctionProto - Return true if the given decl has a argument |
| 62 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 63 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 64 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 65 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 66 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 67 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 70 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 71 | /// 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] | 72 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 73 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 74 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 75 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 76 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 77 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 81 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 82 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 83 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 84 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 85 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 87 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 90 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 91 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 92 | return cast<FunctionProtoType>(FnTy)->getReturnType(); |
| 93 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 96 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 97 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 98 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 99 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 100 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 101 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 102 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 103 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 107 | static bool isInstanceMethod(const Decl *D) { |
| 108 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 109 | return MethodDecl->isInstance(); |
| 110 | return false; |
| 111 | } |
| 112 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 113 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 114 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 115 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 116 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 117 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 118 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 119 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 120 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 121 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 122 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 124 | // FIXME: Should we walk the chain of classes? |
| 125 | return ClsName == &Ctx.Idents.get("NSString") || |
| 126 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 127 | } |
| 128 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 129 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 130 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 131 | if (!PT) |
| 132 | return false; |
| 133 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 134 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 135 | if (!RT) |
| 136 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 137 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 138 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 139 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 140 | return false; |
| 141 | |
| 142 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 143 | } |
| 144 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 145 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 146 | // FIXME: Include the type in the argument list. |
| 147 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 148 | } |
| 149 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 150 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 151 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 152 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 153 | unsigned Num) { |
| 154 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 155 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 156 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 163 | /// \brief Check if the attribute has at least as many args as Num. May |
| 164 | /// output an error. |
| 165 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 166 | unsigned Num) { |
| 167 | if (getNumAttributeArgs(Attr) < Num) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 168 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) |
| 169 | << Attr.getName() << Num; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 176 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 177 | /// expression and return success or failure. May output an error. |
| 178 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 179 | const Expr *Expr, uint32_t &Val, |
| 180 | unsigned Idx = UINT_MAX) { |
| 181 | llvm::APSInt I(32); |
| 182 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 183 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 184 | if (Idx != UINT_MAX) |
| 185 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 186 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 187 | << Expr->getSourceRange(); |
| 188 | else |
| 189 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 190 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 191 | << Expr->getSourceRange(); |
| 192 | return false; |
| 193 | } |
| 194 | Val = (uint32_t)I.getZExtValue(); |
| 195 | return true; |
| 196 | } |
| 197 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 198 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 199 | /// declaration. Returns true if diagnosed. |
| 200 | template <typename AttrTy> |
| 201 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 202 | const AttributeList &Attr) { |
| 203 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 204 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 205 | << Attr.getName() << A; |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 211 | /// \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] | 212 | /// instance method D. May output an error. |
| 213 | /// |
| 214 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 215 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 216 | const AttributeList &Attr, |
| 217 | unsigned AttrArgNum, |
| 218 | const Expr *IdxExpr, |
| 219 | uint64_t &Idx) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 220 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 221 | |
| 222 | // In C++ the implicit 'this' function parameter also counts. |
| 223 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 224 | bool HP = hasFunctionProto(D); |
| 225 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 226 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 227 | unsigned NumParams = |
| 228 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 229 | |
| 230 | llvm::APSInt IdxInt; |
| 231 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 232 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 233 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 234 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 235 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | |
| 239 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 240 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 241 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 242 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | Idx--; // Convert to zero-based. |
| 246 | if (HasImplicitThisParam) { |
| 247 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 248 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 249 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 250 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | --Idx; |
| 254 | } |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 259 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 260 | /// If not emit an error and return false. If the argument is an identifier it |
| 261 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 262 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 263 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 264 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 265 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 266 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 267 | if (Attr.isArgIdent(ArgNum)) { |
| 268 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 269 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 270 | << Attr.getName() << AANT_ArgumentString |
| 271 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 272 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 273 | Str = Loc->Ident->getName(); |
| 274 | if (ArgLocation) |
| 275 | *ArgLocation = Loc->Loc; |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | // Now check for an actual string literal. |
| 280 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 281 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 282 | if (ArgLocation) |
| 283 | *ArgLocation = ArgExpr->getLocStart(); |
| 284 | |
| 285 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 286 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 287 | << Attr.getName() << AANT_ArgumentString; |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | Str = Literal->getString(); |
| 292 | return true; |
| 293 | } |
| 294 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 295 | /// \brief Applies the given attribute to the Decl without performing any |
| 296 | /// additional semantic checking. |
| 297 | template <typename AttrType> |
| 298 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 299 | const AttributeList &Attr) { |
| 300 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 301 | Attr.getAttributeSpellingListIndex())); |
| 302 | } |
| 303 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 304 | /// \brief Check if the passed-in expression is of type int or bool. |
| 305 | static bool isIntOrBool(Expr *Exp) { |
| 306 | QualType QT = Exp->getType(); |
| 307 | return QT->isBooleanType() || QT->isIntegerType(); |
| 308 | } |
| 309 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 310 | |
| 311 | // Check to see if the type is a smart pointer of some kind. We assume |
| 312 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 313 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 314 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 315 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 316 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 317 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 318 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 319 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 320 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 321 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 322 | return false; |
| 323 | |
| 324 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 327 | /// \brief Check if passed in Decl is a pointer type. |
| 328 | /// Note that this function may produce an error message. |
| 329 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 330 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 331 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 332 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 333 | QualType QT = vd->getType(); |
| 334 | if (QT->isAnyPointerType()) |
| 335 | return true; |
| 336 | |
| 337 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 338 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 339 | // (We don't want to force template instantiation if we can avoid it, |
| 340 | // since that would alter the order in which templates are instantiated.) |
| 341 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 342 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 343 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 344 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 345 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 346 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 347 | |
| 348 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 349 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 353 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 354 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 355 | static const RecordType *getRecordType(QualType QT) { |
| 356 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 357 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 358 | |
| 359 | // Now check if we point to record type. |
| 360 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 361 | return PT->getPointeeType()->getAs<RecordType>(); |
| 362 | |
| 363 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 364 | } |
| 365 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 366 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 367 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 368 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 369 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 370 | return RT->getDecl()->hasAttr<CapabilityAttr>(); |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 374 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 375 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 376 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 377 | QualType Ty) { |
| 378 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 379 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 380 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 381 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 382 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 383 | << Attr.getName() << Ty; |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 384 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 385 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 386 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 387 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 388 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 389 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 390 | |
| 391 | // Allow smart pointers to be used as lockable objects. |
| 392 | // FIXME -- Check the type that the smart pointer points to. |
| 393 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 394 | return; |
| 395 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 396 | // Check if the type is lockable. |
| 397 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 398 | if (RD->hasAttr<CapabilityAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 399 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 400 | |
| 401 | // Else check if any base classes are lockable. |
| 402 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 403 | CXXBasePaths BPaths(false, false); |
| 404 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 405 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 406 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 407 | |
| 408 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 409 | << Attr.getName() << Ty; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 412 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 413 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 414 | /// \param Sidx The attribute argument index to start checking with. |
| 415 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 416 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 417 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 418 | const AttributeList &Attr, |
| 419 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 420 | int Sidx = 0, |
| 421 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 422 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 423 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 424 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 425 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 426 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 427 | Args.push_back(ArgExp); |
| 428 | continue; |
| 429 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 430 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 431 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 432 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 433 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 434 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 435 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 436 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 437 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 438 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 439 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 440 | // We allow constant strings to be used as a placeholder for expressions |
| 441 | // that are not valid C++ syntax, but warn that they are ignored. |
| 442 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 443 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 444 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 445 | continue; |
| 446 | } |
| 447 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 448 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 449 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 450 | // A pointer to member expression of the form &MyClass::mu is treated |
| 451 | // specially -- we need to look at the type of the member. |
| 452 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 453 | if (UOp->getOpcode() == UO_AddrOf) |
| 454 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 455 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 456 | ArgTy = DRE->getDecl()->getType(); |
| 457 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 458 | // First see if we can just cast to record type, or point to record type. |
| 459 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 460 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 461 | // Now check if we index into a record type function param. |
| 462 | if(!RT && ParamIdxOk) { |
| 463 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 464 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 465 | if(FD && IL) { |
| 466 | unsigned int NumParams = FD->getNumParams(); |
| 467 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 468 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 469 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 470 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 471 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 472 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 473 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 474 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 475 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 479 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 480 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 481 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 485 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 486 | // Attribute Implementations |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 489 | // FIXME: All this manual attribute parsing code is gross. At the |
| 490 | // least add some helper functions to check most argument patterns (# |
| 491 | // and types of args). |
| 492 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 493 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 494 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 495 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 496 | return; |
| 497 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 498 | D->addAttr(::new (S.Context) |
| 499 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 500 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 503 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 504 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 505 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 506 | SmallVector<Expr*, 1> Args; |
| 507 | // check that all arguments are lockable objects |
| 508 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 509 | unsigned Size = Args.size(); |
| 510 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 511 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 512 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 513 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 514 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 515 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 518 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 519 | Expr *Arg = 0; |
| 520 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 521 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 522 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 523 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 524 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 527 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 528 | const AttributeList &Attr) { |
| 529 | Expr *Arg = 0; |
| 530 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 531 | return; |
| 532 | |
| 533 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 534 | return; |
| 535 | |
| 536 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 537 | S.Context, Arg, |
| 538 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 541 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 542 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 543 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 544 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 545 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 546 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 547 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 548 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 549 | if (!QT->isDependentType()) { |
| 550 | const RecordType *RT = getRecordType(QT); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 551 | if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 552 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | << Attr.getName(); |
| 554 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 558 | // Check that all arguments are lockable objects. |
| 559 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 560 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 561 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 562 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 563 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 566 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 567 | const AttributeList &Attr) { |
| 568 | SmallVector<Expr*, 1> Args; |
| 569 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 570 | return; |
| 571 | |
| 572 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 573 | D->addAttr(::new (S.Context) |
| 574 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 575 | StartArg, Args.size(), |
| 576 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 579 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 580 | const AttributeList &Attr) { |
| 581 | SmallVector<Expr*, 1> Args; |
| 582 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 583 | return; |
| 584 | |
| 585 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 586 | D->addAttr(::new (S.Context) |
| 587 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 588 | StartArg, Args.size(), |
| 589 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 592 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 593 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 594 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 595 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 596 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 597 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 598 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 602 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 603 | const AttributeList &Attr) { |
| 604 | SmallVector<Expr*, 1> Args; |
| 605 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 606 | return; |
| 607 | |
| 608 | unsigned Size = Args.size(); |
| 609 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 610 | D->addAttr(::new (S.Context) |
| 611 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 612 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 615 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 616 | const AttributeList &Attr) { |
| 617 | SmallVector<Expr*, 1> Args; |
| 618 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 619 | return; |
| 620 | |
| 621 | unsigned Size = Args.size(); |
| 622 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 623 | D->addAttr(::new (S.Context) |
| 624 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 625 | StartArg, Size, |
| 626 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 627 | } |
| 628 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 629 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 630 | const AttributeList &Attr) { |
| 631 | SmallVector<Expr*, 1> Args; |
| 632 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 633 | return; |
| 634 | |
| 635 | unsigned Size = Args.size(); |
| 636 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 637 | D->addAttr(::new (S.Context) |
| 638 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 639 | Attr.getAttributeSpellingListIndex())); |
| 640 | } |
| 641 | |
| 642 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 643 | const AttributeList &Attr) { |
| 644 | SmallVector<Expr*, 1> Args; |
| 645 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 646 | return; |
| 647 | |
| 648 | unsigned Size = Args.size(); |
| 649 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 650 | D->addAttr(::new (S.Context) |
| 651 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 652 | StartArg, Size, |
| 653 | Attr.getAttributeSpellingListIndex())); |
| 654 | } |
| 655 | |
| 656 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 657 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 658 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 659 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 661 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 662 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 663 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 664 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 665 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 666 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 670 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 671 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 672 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 675 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 676 | const AttributeList &Attr) { |
| 677 | SmallVector<Expr*, 2> Args; |
| 678 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 679 | return; |
| 680 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 681 | D->addAttr(::new (S.Context) |
| 682 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 683 | Attr.getArgAsExpr(0), |
| 684 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 685 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 688 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 689 | const AttributeList &Attr) { |
| 690 | SmallVector<Expr*, 2> Args; |
| 691 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 692 | return; |
| 693 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 694 | D->addAttr(::new (S.Context) |
| 695 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 696 | Attr.getArgAsExpr(0), |
| 697 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 698 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 701 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 702 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 703 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 704 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 705 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 706 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 707 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 708 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 709 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 710 | D->addAttr(::new (S.Context) |
| 711 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 712 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 716 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 717 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 718 | SmallVector<Expr*, 1> Args; |
| 719 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 720 | unsigned Size = Args.size(); |
| 721 | if (Size == 0) |
| 722 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 723 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 724 | D->addAttr(::new (S.Context) |
| 725 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 726 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 730 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 731 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 732 | return; |
| 733 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 734 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 735 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 736 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 737 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 738 | if (Size == 0) |
| 739 | return; |
| 740 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 741 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 742 | D->addAttr(::new (S.Context) |
| 743 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 744 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 747 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 748 | Expr *Cond = Attr.getArgAsExpr(0); |
| 749 | if (!Cond->isTypeDependent()) { |
| 750 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 751 | if (Converted.isInvalid()) |
| 752 | return; |
| 753 | Cond = Converted.take(); |
| 754 | } |
| 755 | |
| 756 | StringRef Msg; |
| 757 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 758 | return; |
| 759 | |
| 760 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 761 | if (!Cond->isValueDependent() && |
| 762 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 763 | Diags)) { |
| 764 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 765 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 766 | S.Diag(Diags[I].first, Diags[I].second); |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | D->addAttr(::new (S.Context) |
| 771 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 772 | Attr.getAttributeSpellingListIndex())); |
| 773 | } |
| 774 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 775 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 776 | ConsumableAttr::ConsumedState DefaultState; |
| 777 | |
| 778 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 779 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 780 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 781 | DefaultState)) { |
| 782 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 783 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 784 | return; |
| 785 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 786 | } else { |
| 787 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 788 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 789 | return; |
| 790 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 791 | |
| 792 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 793 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 794 | Attr.getAttributeSpellingListIndex())); |
| 795 | } |
| 796 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 797 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 798 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 799 | const AttributeList &Attr) { |
| 800 | ASTContext &CurrContext = S.getASTContext(); |
| 801 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 802 | |
| 803 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 804 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 805 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 806 | RD->getNameAsString(); |
| 807 | |
| 808 | return false; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | return true; |
| 813 | } |
| 814 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 815 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 816 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 817 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 818 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 819 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 820 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 821 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 822 | return; |
| 823 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 824 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 825 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 826 | CallableWhenAttr::ConsumedState CallableState; |
| 827 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 828 | StringRef StateString; |
| 829 | SourceLocation Loc; |
| 830 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 831 | return; |
| 832 | |
| 833 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 834 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 835 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 836 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 837 | return; |
| 838 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 839 | |
| 840 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 841 | } |
| 842 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 843 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 844 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 845 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 846 | } |
| 847 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 848 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 849 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 850 | const AttributeList &Attr) { |
| 851 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 852 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 853 | ParamTypestateAttr::ConsumedState ParamState; |
| 854 | |
| 855 | if (Attr.isArgIdent(0)) { |
| 856 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 857 | StringRef StateString = Ident->Ident->getName(); |
| 858 | |
| 859 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 860 | ParamState)) { |
| 861 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 862 | << Attr.getName() << StateString; |
| 863 | return; |
| 864 | } |
| 865 | } else { |
| 866 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 867 | Attr.getName() << AANT_ArgumentIdentifier; |
| 868 | return; |
| 869 | } |
| 870 | |
| 871 | // FIXME: This check is currently being done in the analysis. It can be |
| 872 | // enabled here only after the parser propagates attributes at |
| 873 | // template specialization definition, not declaration. |
| 874 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 875 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 876 | // |
| 877 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 878 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 879 | // ReturnType.getAsString(); |
| 880 | // return; |
| 881 | //} |
| 882 | |
| 883 | D->addAttr(::new (S.Context) |
| 884 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 885 | Attr.getAttributeSpellingListIndex())); |
| 886 | } |
| 887 | |
| 888 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 889 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 890 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 891 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 892 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 893 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 894 | |
| 895 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 896 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 897 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 898 | ReturnState)) { |
| 899 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 900 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 901 | return; |
| 902 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 903 | } else { |
| 904 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 905 | Attr.getName() << AANT_ArgumentIdentifier; |
| 906 | return; |
| 907 | } |
| 908 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 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; |
| 913 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 914 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 915 | // ReturnType = Param->getType(); |
| 916 | // |
| 917 | //} else if (const CXXConstructorDecl *Constructor = |
| 918 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 919 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 920 | // |
| 921 | //} else { |
| 922 | // |
| 923 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 924 | //} |
| 925 | // |
| 926 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 927 | // |
| 928 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 929 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 930 | // ReturnType.getAsString(); |
| 931 | // return; |
| 932 | //} |
| 933 | |
| 934 | D->addAttr(::new (S.Context) |
| 935 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 936 | Attr.getAttributeSpellingListIndex())); |
| 937 | } |
| 938 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 939 | |
| 940 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 941 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 942 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 943 | |
| 944 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 945 | return; |
| 946 | |
| 947 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 948 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 949 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 950 | StringRef Param = Ident->Ident->getName(); |
| 951 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 952 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 953 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 954 | return; |
| 955 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 956 | } else { |
| 957 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 958 | Attr.getName() << AANT_ArgumentIdentifier; |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | D->addAttr(::new (S.Context) |
| 963 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 964 | Attr.getAttributeSpellingListIndex())); |
| 965 | } |
| 966 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 967 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 968 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 969 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 970 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 971 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 972 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 973 | return; |
| 974 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 975 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 976 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 977 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 978 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 979 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 980 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 981 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 982 | return; |
| 983 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 984 | } else { |
| 985 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 986 | Attr.getName() << AANT_ArgumentIdentifier; |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 991 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 992 | Attr.getAttributeSpellingListIndex())); |
| 993 | } |
| 994 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 995 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 996 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 997 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 998 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1001 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1002 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1003 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1004 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1005 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1006 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1007 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1008 | if (!FD->getType()->isDependentType() && |
| 1009 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1010 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1011 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1012 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1013 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1014 | FD->addAttr(::new (S.Context) |
| 1015 | PackedAttr(Attr.getRange(), S.Context, |
| 1016 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1017 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1018 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1021 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1022 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1023 | // variables or properties of Objective-C classes. The outlet must also |
| 1024 | // have an object reference type. |
| 1025 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1026 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1027 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1028 | << Attr.getName() << VD->getType() << 0; |
| 1029 | return false; |
| 1030 | } |
| 1031 | } |
| 1032 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1033 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1034 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1035 | << Attr.getName() << PD->getType() << 1; |
| 1036 | return false; |
| 1037 | } |
| 1038 | } |
| 1039 | else { |
| 1040 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1041 | return false; |
| 1042 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1043 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1044 | return true; |
| 1045 | } |
| 1046 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1047 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1048 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1049 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1050 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1051 | D->addAttr(::new (S.Context) |
| 1052 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1053 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1056 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1057 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1058 | |
| 1059 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1060 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1061 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1062 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1063 | return; |
| 1064 | } |
| 1065 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1066 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1067 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1068 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1069 | ParsedType PT; |
| 1070 | |
| 1071 | if (Attr.hasParsedType()) |
| 1072 | PT = Attr.getTypeArg(); |
| 1073 | else { |
| 1074 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1075 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1076 | if (!PT) { |
| 1077 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1078 | return; |
| 1079 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1080 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1081 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1082 | TypeSourceInfo *QTLoc = 0; |
| 1083 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1084 | if (!QTLoc) |
| 1085 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1086 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1087 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1088 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1089 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1090 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1091 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1092 | S.Diag(Attr.getLoc(), |
| 1093 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1094 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1095 | return; |
| 1096 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1097 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1098 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1099 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1100 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1103 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1104 | if (const RecordType *UT = T->getAsUnionType()) |
| 1105 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1106 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame^] | 1107 | for (const auto *I : UD->fields()) { |
| 1108 | QualType QT = I->getType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1109 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1110 | T = QT; |
| 1111 | return; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1117 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1118 | SourceRange R, bool isReturnValue = false) { |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1119 | T = T.getNonReferenceType(); |
| 1120 | possibleTransparentUnionPointerType(T); |
| 1121 | |
| 1122 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1123 | S.Diag(Attr.getLoc(), |
| 1124 | isReturnValue ? diag::warn_attribute_return_pointers_only |
| 1125 | : diag::warn_attribute_pointers_only) |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1126 | << Attr.getName() << R; |
| 1127 | return false; |
| 1128 | } |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1132 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1133 | SmallVector<unsigned, 8> NonNullArgs; |
| 1134 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1135 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1136 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1137 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1138 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Is the function argument a pointer type? |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1141 | // FIXME: Should also highlight argument in decl in the diagnostic. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1142 | if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
| 1143 | Ex->getSourceRange())) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1144 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1145 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1146 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1147 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1148 | |
| 1149 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1150 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1151 | if (NonNullArgs.empty()) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1152 | for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) { |
| 1153 | QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1154 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1155 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1156 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1157 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1158 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1159 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1160 | if (NonNullArgs.empty()) { |
| 1161 | // Warn the trivial case only if attribute is not coming from a |
| 1162 | // macro instantiation. |
| 1163 | if (Attr.getLoc().isFileID()) |
| 1164 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1165 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1166 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1167 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1168 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1169 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1170 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1171 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1172 | D->addAttr(::new (S.Context) |
| 1173 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1174 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1177 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1178 | const AttributeList &Attr) { |
| 1179 | if (Attr.getNumArgs() > 0) { |
| 1180 | if (D->getFunctionType()) { |
| 1181 | handleNonNullAttr(S, D, Attr); |
| 1182 | } else { |
| 1183 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1184 | << D->getSourceRange(); |
| 1185 | } |
| 1186 | return; |
| 1187 | } |
| 1188 | |
| 1189 | // Is the argument a pointer type? |
| 1190 | if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange())) |
| 1191 | return; |
| 1192 | |
| 1193 | D->addAttr(::new (S.Context) |
| 1194 | NonNullAttr(Attr.getRange(), S.Context, 0, 0, |
| 1195 | Attr.getAttributeSpellingListIndex())); |
| 1196 | } |
| 1197 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1198 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1199 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1200 | QualType ResultType = getFunctionOrMethodResultType(D); |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1201 | if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(), |
| 1202 | /* isReturnValue */ true)) |
| 1203 | return; |
| 1204 | |
| 1205 | D->addAttr(::new (S.Context) |
| 1206 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1207 | Attr.getAttributeSpellingListIndex())); |
| 1208 | } |
| 1209 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1210 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1211 | // This attribute must be applied to a function declaration. The first |
| 1212 | // argument to the attribute must be an identifier, the name of the resource, |
| 1213 | // for example: malloc. The following arguments must be argument indexes, the |
| 1214 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1215 | // 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] | 1216 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1217 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1218 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1219 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1220 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1221 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1222 | return; |
| 1223 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1224 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1225 | // Figure out our Kind. |
| 1226 | OwnershipAttr::OwnershipKind K = |
| 1227 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1228 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1229 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1230 | // Check arguments. |
| 1231 | switch (K) { |
| 1232 | case OwnershipAttr::Takes: |
| 1233 | case OwnershipAttr::Holds: |
| 1234 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1235 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1236 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1237 | return; |
| 1238 | } |
| 1239 | break; |
| 1240 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1241 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1242 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1243 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1244 | return; |
| 1245 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1246 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1249 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1250 | |
| 1251 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1252 | StringRef ModuleName = Module->getName(); |
| 1253 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1254 | ModuleName.size() > 4) { |
| 1255 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1256 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1257 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1258 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1259 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1260 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1261 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1262 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1263 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1264 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1265 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1266 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1267 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1268 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1269 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1270 | case OwnershipAttr::Takes: |
| 1271 | case OwnershipAttr::Holds: |
| 1272 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1273 | Err = 0; |
| 1274 | break; |
| 1275 | case OwnershipAttr::Returns: |
| 1276 | if (!T->isIntegerType()) |
| 1277 | Err = 1; |
| 1278 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1279 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1280 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1281 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1282 | << Ex->getSourceRange(); |
| 1283 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1284 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1287 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1288 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1289 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1290 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1291 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1292 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1293 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1294 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 05a6378 | 2014-01-20 15:06:09 +0000 | [diff] [blame] | 1295 | << AL.getName() << *i; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1296 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1299 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | unsigned* start = OwnershipArgs.data(); |
| 1303 | unsigned size = OwnershipArgs.size(); |
| 1304 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1305 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1306 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1307 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1308 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1311 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1312 | // Check the attribute arguments. |
| 1313 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1314 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1315 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1316 | return; |
| 1317 | } |
| 1318 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1319 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1320 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1321 | // gcc rejects |
| 1322 | // class c { |
| 1323 | // static int a __attribute__((weakref ("v2"))); |
| 1324 | // static int b() __attribute__((weakref ("f3"))); |
| 1325 | // }; |
| 1326 | // and ignores the attributes of |
| 1327 | // void f(void) { |
| 1328 | // static int a __attribute__((weakref ("v2"))); |
| 1329 | // } |
| 1330 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1331 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1332 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1333 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1334 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1335 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | // The GCC manual says |
| 1339 | // |
| 1340 | // At present, a declaration to which `weakref' is attached can only |
| 1341 | // be `static'. |
| 1342 | // |
| 1343 | // It also says |
| 1344 | // |
| 1345 | // Without a TARGET, |
| 1346 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1347 | // equivalent to `weak'. |
| 1348 | // |
| 1349 | // gcc 4.4.1 will accept |
| 1350 | // int a7 __attribute__((weakref)); |
| 1351 | // as |
| 1352 | // int a7 __attribute__((weak)); |
| 1353 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1354 | // it if this behaviour is actually used. |
| 1355 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1356 | // GCC rejects |
| 1357 | // static ((alias ("y"), weakref)). |
| 1358 | // Should we? How to check that weakref is before or after alias? |
| 1359 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1360 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1361 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1362 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1363 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1364 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1365 | // GCC will accept anything as the argument of weakref. Should we |
| 1366 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1367 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1368 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1369 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1370 | D->addAttr(::new (S.Context) |
| 1371 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1372 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1375 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1376 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1377 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1378 | return; |
| 1379 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1380 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1381 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1382 | return; |
| 1383 | } |
| 1384 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1385 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1386 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1387 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1388 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1391 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1392 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1393 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1394 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1395 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1396 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1400 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1401 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1402 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1403 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1404 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1407 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1408 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1409 | StringRef Model; |
| 1410 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1411 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1412 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1413 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1414 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1415 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1416 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1417 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1418 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
| 1421 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1422 | D->addAttr(::new (S.Context) |
| 1423 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1424 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1427 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1428 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1429 | QualType RetTy = FD->getReturnType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1430 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1431 | D->addAttr(::new (S.Context) |
| 1432 | MallocAttr(Attr.getRange(), S.Context, |
| 1433 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1434 | return; |
| 1435 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1438 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1441 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1442 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1443 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1444 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1445 | return; |
| 1446 | } |
| 1447 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1448 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1449 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1452 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1453 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (S.CheckNoReturnAttr(attr)) return; |
| 1456 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1457 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1458 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1459 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1463 | D->addAttr(::new (S.Context) |
| 1464 | NoReturnAttr(attr.getRange(), S.Context, |
| 1465 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1469 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1470 | attr.setInvalid(); |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
| 1474 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1477 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1478 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1479 | |
| 1480 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1481 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1482 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1483 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1484 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1485 | && !VD->getType()->isFunctionPointerType())) { |
| 1486 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1487 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1488 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1489 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1490 | return; |
| 1491 | } |
| 1492 | } |
| 1493 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1494 | D->addAttr(::new (S.Context) |
| 1495 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1496 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1499 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1500 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1501 | /* |
| 1502 | Returning a Vector Class in Registers |
| 1503 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1504 | According to the PPU ABI specifications, a class with a single member of |
| 1505 | vector type is returned in memory when used as the return value of a function. |
| 1506 | This results in inefficient code when implementing vector classes. To return |
| 1507 | the value in a single vector register, add the vecreturn attribute to the |
| 1508 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1509 | |
| 1510 | Example: |
| 1511 | |
| 1512 | struct Vector |
| 1513 | { |
| 1514 | __vector float xyzw; |
| 1515 | } __attribute__((vecreturn)); |
| 1516 | |
| 1517 | Vector Add(Vector lhs, Vector rhs) |
| 1518 | { |
| 1519 | Vector result; |
| 1520 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1521 | return result; // This will be returned in a register |
| 1522 | } |
| 1523 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1524 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1525 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | } |
| 1528 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1529 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1530 | int count = 0; |
| 1531 | |
| 1532 | if (!isa<CXXRecordDecl>(record)) { |
| 1533 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1534 | return; |
| 1535 | } |
| 1536 | |
| 1537 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1538 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1539 | return; |
| 1540 | } |
| 1541 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame^] | 1542 | for (const auto *I : record->fields()) { |
| 1543 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1544 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1545 | return; |
| 1546 | } |
| 1547 | count++; |
| 1548 | } |
| 1549 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1550 | D->addAttr(::new (S.Context) |
| 1551 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1552 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1555 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1556 | const AttributeList &Attr) { |
| 1557 | if (isa<ParmVarDecl>(D)) { |
| 1558 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1559 | // parameter of a function declaration or lambda. |
| 1560 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1561 | S.Diag(Attr.getLoc(), |
| 1562 | diag::err_carries_dependency_param_not_function_decl); |
| 1563 | return; |
| 1564 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1565 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1566 | |
| 1567 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1568 | Attr.getRange(), S.Context, |
| 1569 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1572 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1573 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1574 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1575 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1576 | return; |
| 1577 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1578 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1579 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1580 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1581 | return; |
| 1582 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1583 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1584 | D->addAttr(::new (S.Context) |
| 1585 | UsedAttr(Attr.getRange(), S.Context, |
| 1586 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1589 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1590 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1591 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1592 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1593 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1594 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1595 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1596 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1597 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1598 | if (Attr.getNumArgs() > 0 && |
| 1599 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1600 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1601 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1602 | D->addAttr(::new (S.Context) |
| 1603 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1604 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1607 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1608 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1609 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1610 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1611 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1612 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1613 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1614 | |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1615 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1616 | if (Attr.getNumArgs() > 0 && |
| 1617 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1618 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1619 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1620 | D->addAttr(::new (S.Context) |
| 1621 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1622 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1625 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1626 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1627 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1628 | unsigned NumArgs = Attr.getNumArgs(); |
| 1629 | if (NumArgs > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1630 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1631 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1632 | return; |
| 1633 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1634 | |
| 1635 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1636 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1637 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1638 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1639 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1640 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1641 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1644 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1645 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1646 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 1647 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 1648 | << Attr.getName() << Attr.getRange(); |
| 1649 | return; |
| 1650 | } |
| 1651 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1652 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1653 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1654 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1657 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1658 | IdentifierInfo *Platform, |
| 1659 | VersionTuple Introduced, |
| 1660 | VersionTuple Deprecated, |
| 1661 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1662 | StringRef PlatformName |
| 1663 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1664 | if (PlatformName.empty()) |
| 1665 | PlatformName = Platform->getName(); |
| 1666 | |
| 1667 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1668 | // of these steps are needed). |
| 1669 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1670 | !(Introduced <= Deprecated)) { |
| 1671 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1672 | << 1 << PlatformName << Deprecated.getAsString() |
| 1673 | << 0 << Introduced.getAsString(); |
| 1674 | return true; |
| 1675 | } |
| 1676 | |
| 1677 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1678 | !(Introduced <= Obsoleted)) { |
| 1679 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1680 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1681 | << 0 << Introduced.getAsString(); |
| 1682 | return true; |
| 1683 | } |
| 1684 | |
| 1685 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1686 | !(Deprecated <= Obsoleted)) { |
| 1687 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1688 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1689 | << 1 << Deprecated.getAsString(); |
| 1690 | return true; |
| 1691 | } |
| 1692 | |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1696 | /// \brief Check whether the two versions match. |
| 1697 | /// |
| 1698 | /// If either version tuple is empty, then they are assumed to match. If |
| 1699 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1700 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1701 | bool BeforeIsOkay) { |
| 1702 | if (X.empty() || Y.empty()) |
| 1703 | return true; |
| 1704 | |
| 1705 | if (X == Y) |
| 1706 | return true; |
| 1707 | |
| 1708 | if (BeforeIsOkay && X < Y) |
| 1709 | return true; |
| 1710 | |
| 1711 | return false; |
| 1712 | } |
| 1713 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1714 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1715 | IdentifierInfo *Platform, |
| 1716 | VersionTuple Introduced, |
| 1717 | VersionTuple Deprecated, |
| 1718 | VersionTuple Obsoleted, |
| 1719 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1720 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1721 | bool Override, |
| 1722 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1723 | VersionTuple MergedIntroduced = Introduced; |
| 1724 | VersionTuple MergedDeprecated = Deprecated; |
| 1725 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1726 | bool FoundAny = false; |
| 1727 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1728 | if (D->hasAttrs()) { |
| 1729 | AttrVec &Attrs = D->getAttrs(); |
| 1730 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1731 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1732 | if (!OldAA) { |
| 1733 | ++i; |
| 1734 | continue; |
| 1735 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1736 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1737 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1738 | if (OldPlatform != Platform) { |
| 1739 | ++i; |
| 1740 | continue; |
| 1741 | } |
| 1742 | |
| 1743 | FoundAny = true; |
| 1744 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1745 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1746 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1747 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1748 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1749 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1750 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1751 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1752 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1753 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1754 | if (Override) { |
| 1755 | int Which = -1; |
| 1756 | VersionTuple FirstVersion; |
| 1757 | VersionTuple SecondVersion; |
| 1758 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1759 | Which = 0; |
| 1760 | FirstVersion = OldIntroduced; |
| 1761 | SecondVersion = Introduced; |
| 1762 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1763 | Which = 1; |
| 1764 | FirstVersion = Deprecated; |
| 1765 | SecondVersion = OldDeprecated; |
| 1766 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1767 | Which = 2; |
| 1768 | FirstVersion = Obsoleted; |
| 1769 | SecondVersion = OldObsoleted; |
| 1770 | } |
| 1771 | |
| 1772 | if (Which == -1) { |
| 1773 | Diag(OldAA->getLocation(), |
| 1774 | diag::warn_mismatched_availability_override_unavail) |
| 1775 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1776 | } else { |
| 1777 | Diag(OldAA->getLocation(), |
| 1778 | diag::warn_mismatched_availability_override) |
| 1779 | << Which |
| 1780 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1781 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1782 | } |
| 1783 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1784 | } else { |
| 1785 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1786 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1787 | } |
| 1788 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1789 | Attrs.erase(Attrs.begin() + i); |
| 1790 | --e; |
| 1791 | continue; |
| 1792 | } |
| 1793 | |
| 1794 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1795 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1796 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1797 | |
| 1798 | if (MergedIntroduced2.empty()) |
| 1799 | MergedIntroduced2 = OldIntroduced; |
| 1800 | if (MergedDeprecated2.empty()) |
| 1801 | MergedDeprecated2 = OldDeprecated; |
| 1802 | if (MergedObsoleted2.empty()) |
| 1803 | MergedObsoleted2 = OldObsoleted; |
| 1804 | |
| 1805 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1806 | MergedIntroduced2, MergedDeprecated2, |
| 1807 | MergedObsoleted2)) { |
| 1808 | Attrs.erase(Attrs.begin() + i); |
| 1809 | --e; |
| 1810 | continue; |
| 1811 | } |
| 1812 | |
| 1813 | MergedIntroduced = MergedIntroduced2; |
| 1814 | MergedDeprecated = MergedDeprecated2; |
| 1815 | MergedObsoleted = MergedObsoleted2; |
| 1816 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1817 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | if (FoundAny && |
| 1821 | MergedIntroduced == Introduced && |
| 1822 | MergedDeprecated == Deprecated && |
| 1823 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1824 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1825 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1826 | // Only create a new attribute if !Override, but we want to do |
| 1827 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1828 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1829 | MergedDeprecated, MergedObsoleted) && |
| 1830 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1831 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1832 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1833 | Obsoleted, IsUnavailable, Message, |
| 1834 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1835 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1836 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1839 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1840 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1841 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1842 | return; |
| 1843 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1844 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1845 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1846 | IdentifierInfo *II = Platform->Ident; |
| 1847 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1848 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1849 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1850 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1851 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1852 | if (!ND) { |
| 1853 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1854 | return; |
| 1855 | } |
| 1856 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1857 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1858 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1859 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1860 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1861 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1862 | if (const StringLiteral *SE = |
| 1863 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1864 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1865 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1866 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1867 | Introduced.Version, |
| 1868 | Deprecated.Version, |
| 1869 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1870 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1871 | /*Override=*/false, |
| 1872 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1873 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1874 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1877 | template <class T> |
| 1878 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1879 | typename T::VisibilityType value, |
| 1880 | unsigned attrSpellingListIndex) { |
| 1881 | T *existingAttr = D->getAttr<T>(); |
| 1882 | if (existingAttr) { |
| 1883 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1884 | if (existingValue == value) |
| 1885 | return NULL; |
| 1886 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1887 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1888 | D->dropAttr<T>(); |
| 1889 | } |
| 1890 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1891 | } |
| 1892 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1893 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1894 | VisibilityAttr::VisibilityType Vis, |
| 1895 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1896 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1897 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1900 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1901 | TypeVisibilityAttr::VisibilityType Vis, |
| 1902 | unsigned AttrSpellingListIndex) { |
| 1903 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1904 | AttrSpellingListIndex); |
| 1905 | } |
| 1906 | |
| 1907 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1908 | bool isTypeVisibility) { |
| 1909 | // Visibility attributes don't mean anything on a typedef. |
| 1910 | if (isa<TypedefNameDecl>(D)) { |
| 1911 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 1912 | << Attr.getName(); |
| 1913 | return; |
| 1914 | } |
| 1915 | |
| 1916 | // 'type_visibility' can only go on a type or namespace. |
| 1917 | if (isTypeVisibility && |
| 1918 | !(isa<TagDecl>(D) || |
| 1919 | isa<ObjCInterfaceDecl>(D) || |
| 1920 | isa<NamespaceDecl>(D))) { |
| 1921 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 1922 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 1923 | return; |
| 1924 | } |
| 1925 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 1926 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1927 | StringRef TypeStr; |
| 1928 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1929 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1930 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1931 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1932 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1933 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1934 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1935 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1936 | return; |
| 1937 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Complain about attempts to use protected visibility on targets |
| 1940 | // (like Darwin) that don't support it. |
| 1941 | if (type == VisibilityAttr::Protected && |
| 1942 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 1943 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 1944 | type = VisibilityAttr::Default; |
| 1945 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1946 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1947 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1948 | clang::Attr *newAttr; |
| 1949 | if (isTypeVisibility) { |
| 1950 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 1951 | (TypeVisibilityAttr::VisibilityType) type, |
| 1952 | Index); |
| 1953 | } else { |
| 1954 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 1955 | } |
| 1956 | if (newAttr) |
| 1957 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1960 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 1961 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1962 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1963 | if (!Attr.isArgIdent(0)) { |
| 1964 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 1965 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1966 | return; |
| 1967 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1968 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1969 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1970 | ObjCMethodFamilyAttr::FamilyKind F; |
| 1971 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 1972 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 1973 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1974 | return; |
| 1975 | } |
| 1976 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1977 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 1978 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1979 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1980 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1981 | // Ignore the attribute. |
| 1982 | return; |
| 1983 | } |
| 1984 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1985 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1986 | S.Context, F, |
| 1987 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1990 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1991 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1992 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 1993 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1994 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1995 | return; |
| 1996 | } |
| 1997 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 1998 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1999 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2000 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2001 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2002 | return; |
| 2003 | } |
| 2004 | } |
| 2005 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2006 | // It is okay to include this attribute on properties, e.g.: |
| 2007 | // |
| 2008 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2009 | // |
| 2010 | // In this case it follows tradition and suppresses an error in the above |
| 2011 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2012 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2013 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2014 | D->addAttr(::new (S.Context) |
| 2015 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2016 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2019 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2020 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2021 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2022 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2023 | return; |
| 2024 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2025 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2026 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2027 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2028 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2029 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2030 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2031 | return; |
| 2032 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2033 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2034 | D->addAttr(::new (S.Context) |
| 2035 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2036 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2039 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2040 | // check the attribute arguments. |
| 2041 | if (Attr.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 2042 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 2043 | << Attr.getName() << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2044 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2047 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2048 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2049 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2050 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2051 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2052 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2053 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2054 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2055 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2056 | return; |
| 2057 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2058 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2059 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2060 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2061 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2064 | |
| 2065 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2068 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2069 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2070 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2071 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2072 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2073 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2074 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2075 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2076 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
| 2079 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2080 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2081 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2082 | // FIXME: This error message could be improved, it would be nice |
| 2083 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2084 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2085 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2086 | return; |
| 2087 | } |
| 2088 | } |
| 2089 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2090 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2091 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2092 | if (isa<FunctionNoProtoType>(FT)) { |
| 2093 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2094 | return; |
| 2095 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2096 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2097 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2098 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2099 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2100 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2101 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2102 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2103 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2104 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2105 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2106 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2107 | if (!BD->isVariadic()) { |
| 2108 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2109 | return; |
| 2110 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2111 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2112 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2113 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2114 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2115 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2116 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2117 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2118 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2119 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2120 | return; |
| 2121 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2122 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2123 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2124 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2125 | return; |
| 2126 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2127 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2128 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2129 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2130 | return; |
| 2131 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2132 | D->addAttr(::new (S.Context) |
| 2133 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2134 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2137 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2138 | if (D->getFunctionType() && |
| 2139 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2140 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2141 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2142 | return; |
| 2143 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2144 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2145 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2146 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2147 | << Attr.getName() << 1; |
| 2148 | return; |
| 2149 | } |
| 2150 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2151 | D->addAttr(::new (S.Context) |
| 2152 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2153 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2156 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2157 | // weak_import only applies to variable & function declarations. |
| 2158 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2159 | if (!D->canBeWeakImported(isDef)) { |
| 2160 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2161 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2162 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2163 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2164 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2165 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2166 | // Nothing to warn about here. |
| 2167 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2168 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2169 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2170 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2171 | return; |
| 2172 | } |
| 2173 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2174 | D->addAttr(::new (S.Context) |
| 2175 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2176 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2179 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2180 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2181 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2182 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2183 | uint32_t WGSize[3]; |
| 2184 | for (unsigned i = 0; i < 3; ++i) |
| 2185 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2186 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2187 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2188 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2189 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2190 | Existing->getYDim() == WGSize[1] && |
| 2191 | Existing->getZDim() == WGSize[2])) |
| 2192 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2193 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2194 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2195 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2196 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2199 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2200 | if (!Attr.hasParsedType()) { |
| 2201 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2202 | << Attr.getName() << 1; |
| 2203 | return; |
| 2204 | } |
| 2205 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2206 | TypeSourceInfo *ParmTSI = 0; |
| 2207 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2208 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2209 | |
| 2210 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2211 | (ParmType->isBooleanType() || |
| 2212 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2213 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2214 | << ParmType; |
| 2215 | return; |
| 2216 | } |
| 2217 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2218 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2219 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2220 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2221 | return; |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2226 | ParmTSI, |
| 2227 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2230 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2231 | StringRef Name, |
| 2232 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2233 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2234 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2235 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2236 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2237 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2238 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2239 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2240 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2241 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2244 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2245 | // Make sure that there is a string literal as the sections's single |
| 2246 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2247 | StringRef Str; |
| 2248 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2249 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2250 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2252 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2253 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2254 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2255 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2256 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2257 | return; |
| 2258 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2260 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2261 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2262 | if (NewAttr) |
| 2263 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2266 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2267 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2268 | VarDecl *VD = cast<VarDecl>(D); |
| 2269 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2270 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2271 | return; |
| 2272 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2273 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2274 | Expr *E = Attr.getArgAsExpr(0); |
| 2275 | SourceLocation Loc = E->getExprLoc(); |
| 2276 | FunctionDecl *FD = 0; |
| 2277 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2278 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2279 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2280 | // will warn the user. |
| 2281 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2282 | if (DRE->hasQualifier()) |
| 2283 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2284 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2285 | NI = DRE->getNameInfo(); |
| 2286 | if (!FD) { |
| 2287 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2288 | << NI.getName(); |
| 2289 | return; |
| 2290 | } |
| 2291 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2292 | if (ULE->hasExplicitTemplateArgs()) |
| 2293 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2294 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2295 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2296 | if (!FD) { |
| 2297 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2298 | << NI.getName(); |
| 2299 | if (ULE->getType() == S.Context.OverloadTy) |
| 2300 | S.NoteAllOverloadCandidates(ULE); |
| 2301 | return; |
| 2302 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2303 | } else { |
| 2304 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2305 | return; |
| 2306 | } |
| 2307 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2308 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2309 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2310 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2311 | return; |
| 2312 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2313 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2314 | // We're currently more strict than GCC about what function types we accept. |
| 2315 | // If this ever proves to be a problem it should be easy to fix. |
| 2316 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2317 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2318 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2319 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2320 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2321 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2322 | return; |
| 2323 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2324 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2325 | D->addAttr(::new (S.Context) |
| 2326 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2327 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2330 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2331 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2332 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2333 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2334 | uint64_t Idx; |
| 2335 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2336 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2337 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2338 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2339 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2340 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2341 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2342 | if (not_nsstring_type && |
| 2343 | !isCFStringType(Ty, S.Context) && |
| 2344 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2345 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2346 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2347 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2348 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2349 | << IdxExpr->getSourceRange(); |
| 2350 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2351 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2352 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2353 | if (!isNSStringType(Ty, S.Context) && |
| 2354 | !isCFStringType(Ty, S.Context) && |
| 2355 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2356 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2357 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2358 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2359 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2360 | << IdxExpr->getSourceRange(); |
| 2361 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2364 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2365 | // because that has corrected for the implicit this parameter, and is zero- |
| 2366 | // based. The attribute expects what the user wrote explicitly. |
| 2367 | llvm::APSInt Val; |
| 2368 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2369 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2370 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2371 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2372 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2375 | enum FormatAttrKind { |
| 2376 | CFStringFormat, |
| 2377 | NSStringFormat, |
| 2378 | StrftimeFormat, |
| 2379 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2380 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2381 | InvalidFormat |
| 2382 | }; |
| 2383 | |
| 2384 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2385 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2386 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2387 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2388 | // Check for formats that get handled specially. |
| 2389 | .Case("NSString", NSStringFormat) |
| 2390 | .Case("CFString", CFStringFormat) |
| 2391 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2392 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2393 | // Otherwise, check for supported formats. |
| 2394 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2395 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2396 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2397 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2398 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2399 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2402 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2403 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2404 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2405 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2406 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2407 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2408 | return; |
| 2409 | } |
| 2410 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2411 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2412 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2413 | Attr.setInvalid(); |
| 2414 | return; |
| 2415 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2416 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2417 | if (S.Context.getAsArrayType(T)) |
| 2418 | T = S.Context.getBaseElementType(T); |
| 2419 | if (!T->getAs<RecordType>()) { |
| 2420 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2421 | Attr.setInvalid(); |
| 2422 | return; |
| 2423 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2424 | |
| 2425 | Expr *E = Attr.getArgAsExpr(0); |
| 2426 | uint32_t prioritynum; |
| 2427 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2428 | Attr.setInvalid(); |
| 2429 | return; |
| 2430 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2431 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2432 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2433 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2434 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2435 | Attr.setInvalid(); |
| 2436 | return; |
| 2437 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2438 | D->addAttr(::new (S.Context) |
| 2439 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2440 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2443 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2444 | IdentifierInfo *Format, int FormatIdx, |
| 2445 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2446 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2447 | // Check whether we already have an equivalent format attribute. |
| 2448 | for (specific_attr_iterator<FormatAttr> |
| 2449 | i = D->specific_attr_begin<FormatAttr>(), |
| 2450 | e = D->specific_attr_end<FormatAttr>(); |
| 2451 | i != e ; ++i) { |
| 2452 | FormatAttr *f = *i; |
| 2453 | if (f->getType() == Format && |
| 2454 | f->getFormatIdx() == FormatIdx && |
| 2455 | f->getFirstArg() == FirstArg) { |
| 2456 | // If we don't have a valid location for this attribute, adopt the |
| 2457 | // location. |
| 2458 | if (f->getLocation().isInvalid()) |
| 2459 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2460 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2464 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2465 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2468 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2469 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2470 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2471 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2472 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2473 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2474 | return; |
| 2475 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2476 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2477 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2478 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2479 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2480 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2481 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2482 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2483 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2484 | |
| 2485 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2486 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2487 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2488 | // If we've modified the string name, we need a new identifier for it. |
| 2489 | II = &S.Context.Idents.get(Format); |
| 2490 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2491 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2492 | // Check for supported formats. |
| 2493 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2494 | |
| 2495 | if (Kind == IgnoredFormat) |
| 2496 | return; |
| 2497 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2498 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2499 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2500 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2501 | return; |
| 2502 | } |
| 2503 | |
| 2504 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2505 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2506 | uint32_t Idx; |
| 2507 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2508 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2509 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2510 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2511 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2512 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2513 | return; |
| 2514 | } |
| 2515 | |
| 2516 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2517 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2518 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2519 | if (HasImplicitThisParam) { |
| 2520 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2521 | S.Diag(Attr.getLoc(), |
| 2522 | diag::err_format_attribute_implicit_this_format_string) |
| 2523 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2524 | return; |
| 2525 | } |
| 2526 | ArgIdx--; |
| 2527 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2529 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2530 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2531 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2532 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2533 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2534 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2535 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2536 | return; |
| 2537 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2538 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2539 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2540 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2541 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2542 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2543 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2544 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2545 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2546 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2547 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2548 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2549 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2550 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2551 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2552 | return; |
| 2553 | } |
| 2554 | |
| 2555 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2556 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2557 | uint32_t FirstArg; |
| 2558 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2559 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2560 | |
| 2561 | // check if the function is variadic if the 3rd argument non-zero |
| 2562 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2563 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2564 | ++NumArgs; // +1 for ... |
| 2565 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2566 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2567 | return; |
| 2568 | } |
| 2569 | } |
| 2570 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2571 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2572 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2573 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2574 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2575 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2576 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | } |
| 2579 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2580 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2581 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2582 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2583 | return; |
| 2584 | } |
| 2585 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2586 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2587 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2588 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2589 | if (NewAttr) |
| 2590 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2593 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2594 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2595 | // Try to find the underlying union declaration. |
| 2596 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2597 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2598 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2599 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2600 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2601 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2602 | |
| 2603 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2604 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2605 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
| 2608 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2609 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2610 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2611 | diag::warn_transparent_union_attribute_not_definition); |
| 2612 | return; |
| 2613 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2614 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2615 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2616 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2617 | if (Field == FieldEnd) { |
| 2618 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2619 | return; |
| 2620 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2621 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2622 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2623 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2624 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2625 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2626 | diag::warn_transparent_union_attribute_floating) |
| 2627 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2628 | return; |
| 2629 | } |
| 2630 | |
| 2631 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2632 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2633 | for (; Field != FieldEnd; ++Field) { |
| 2634 | QualType FieldType = Field->getType(); |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2635 | // FIXME: this isn't fully correct; we also need to test whether the |
| 2636 | // members of the union would all have the same calling convention as the |
| 2637 | // first member of the union. Checking just the size and alignment isn't |
| 2638 | // sufficient (consider structs passed on the stack instead of in registers |
| 2639 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2640 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2641 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2642 | // Warn if we drop the attribute. |
| 2643 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2644 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2645 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2646 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2647 | diag::warn_transparent_union_attribute_field_size_align) |
| 2648 | << isSize << Field->getDeclName() << FieldBits; |
| 2649 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2650 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2651 | diag::note_transparent_union_first_field_size_align) |
| 2652 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2653 | return; |
| 2654 | } |
| 2655 | } |
| 2656 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2657 | RD->addAttr(::new (S.Context) |
| 2658 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2659 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2662 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2663 | // Make sure that there is a string literal as the annotation's single |
| 2664 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2665 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2666 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2667 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Don't duplicate annotations that are already set. |
| 2670 | for (specific_attr_iterator<AnnotateAttr> |
| 2671 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2672 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2673 | if ((*i)->getAnnotation() == Str) |
| 2674 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2675 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2676 | |
| 2677 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2678 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2679 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2682 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2683 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2684 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2685 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2686 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2687 | return; |
| 2688 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2689 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2690 | if (Attr.getNumArgs() == 0) { |
| 2691 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2692 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2693 | return; |
| 2694 | } |
| 2695 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2696 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2697 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2698 | S.Diag(Attr.getEllipsisLoc(), |
| 2699 | diag::err_pack_expansion_without_parameter_packs); |
| 2700 | return; |
| 2701 | } |
| 2702 | |
| 2703 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2704 | return; |
| 2705 | |
| 2706 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2707 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2711 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2712 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2713 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2714 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2715 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2716 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2717 | // C++11 [dcl.align]p1: |
| 2718 | // An alignment-specifier may be applied to a variable or to a class |
| 2719 | // data member, but it shall not be applied to a bit-field, a function |
| 2720 | // parameter, the formal parameter of a catch clause, or a variable |
| 2721 | // declared with the register storage class specifier. An |
| 2722 | // alignment-specifier may also be applied to the declaration of a class |
| 2723 | // or enumeration type. |
| 2724 | // C11 6.7.5/2: |
| 2725 | // An alignment attribute shall not be specified in a declaration of |
| 2726 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2727 | // object declared with the register storage-class specifier. |
| 2728 | int DiagKind = -1; |
| 2729 | if (isa<ParmVarDecl>(D)) { |
| 2730 | DiagKind = 0; |
| 2731 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2732 | if (VD->getStorageClass() == SC_Register) |
| 2733 | DiagKind = 1; |
| 2734 | if (VD->isExceptionVariable()) |
| 2735 | DiagKind = 2; |
| 2736 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2737 | if (FD->isBitField()) |
| 2738 | DiagKind = 3; |
| 2739 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2740 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2741 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2742 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2743 | return; |
| 2744 | } |
| 2745 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2746 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2747 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2748 | return; |
| 2749 | } |
| 2750 | } |
| 2751 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2752 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2753 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2754 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2755 | AA->setPackExpansion(IsPackExpansion); |
| 2756 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2757 | return; |
| 2758 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2759 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2760 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2761 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2762 | ExprResult ICE |
| 2763 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2764 | diag::err_aligned_attribute_argument_not_int, |
| 2765 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2766 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2767 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2768 | |
| 2769 | // C++11 [dcl.align]p2: |
| 2770 | // -- if the constant expression evaluates to zero, the alignment |
| 2771 | // specifier shall have no effect |
| 2772 | // C11 6.7.5p6: |
| 2773 | // An alignment specification of zero has no effect. |
| 2774 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2775 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2776 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2777 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2778 | return; |
| 2779 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2780 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 2781 | // Alignment calculations can wrap around if it's greater than 2**28. |
| 2782 | unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456; |
| 2783 | if (Alignment.getZExtValue() > MaxValidAlignment) { |
| 2784 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 2785 | << E->getSourceRange(); |
| 2786 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2787 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2788 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2789 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 2790 | ICE.take(), SpellingListIndex); |
| 2791 | AA->setPackExpansion(IsPackExpansion); |
| 2792 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2795 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2796 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2797 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2798 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2799 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2800 | SpellingListIndex); |
| 2801 | AA->setPackExpansion(IsPackExpansion); |
| 2802 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2803 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2804 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2805 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2806 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2807 | |
| 2808 | QualType Ty; |
| 2809 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2810 | Ty = VD->getType(); |
| 2811 | else |
| 2812 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 2813 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2814 | return; |
| 2815 | |
| 2816 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2817 | // The combined effect of all alignment attributes in a declaration shall |
| 2818 | // not specify an alignment that is less strict than the alignment that |
| 2819 | // would otherwise be required for the entity being declared. |
| 2820 | AlignedAttr *AlignasAttr = 0; |
| 2821 | unsigned Align = 0; |
| 2822 | for (specific_attr_iterator<AlignedAttr> |
| 2823 | I = D->specific_attr_begin<AlignedAttr>(), |
| 2824 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 2825 | if (I->isAlignmentDependent()) |
| 2826 | return; |
| 2827 | if (I->isAlignas()) |
| 2828 | AlignasAttr = *I; |
| 2829 | Align = std::max(Align, I->getAlignment(Context)); |
| 2830 | } |
| 2831 | |
| 2832 | if (AlignasAttr && Align) { |
| 2833 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 2834 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 2835 | if (NaturalAlign > RequestedAlign) |
| 2836 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 2837 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 2838 | } |
| 2839 | } |
| 2840 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2841 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2842 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2843 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 2844 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 2845 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2846 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 2847 | // have to wait until the record is defined to catch any mismatches. |
| 2848 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 2849 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2850 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2851 | // The unspecified model never matches what a definition could need. |
| 2852 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 2853 | return false; |
| 2854 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2855 | if (BestCase) { |
| 2856 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 2857 | return false; |
| 2858 | } else { |
| 2859 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 2860 | return false; |
| 2861 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2862 | |
| 2863 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 2864 | << 0 /*definition*/; |
| 2865 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 2866 | << RD->getNameAsString(); |
| 2867 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2870 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2871 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2872 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2873 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2874 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2875 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2876 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2877 | // This attribute isn't documented, but glibc uses it. It changes |
| 2878 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2879 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 2880 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 2881 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2882 | return; |
| 2883 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2884 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2885 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 2886 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2887 | |
| 2888 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2889 | if (Str.startswith("__") && Str.endswith("__")) |
| 2890 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2891 | |
| 2892 | unsigned DestWidth = 0; |
| 2893 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2894 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2895 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2896 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2897 | switch (Str[0]) { |
| 2898 | case 'Q': DestWidth = 8; break; |
| 2899 | case 'H': DestWidth = 16; break; |
| 2900 | case 'S': DestWidth = 32; break; |
| 2901 | case 'D': DestWidth = 64; break; |
| 2902 | case 'X': DestWidth = 96; break; |
| 2903 | case 'T': DestWidth = 128; break; |
| 2904 | } |
| 2905 | if (Str[1] == 'F') { |
| 2906 | IntegerMode = false; |
| 2907 | } else if (Str[1] == 'C') { |
| 2908 | IntegerMode = false; |
| 2909 | ComplexMode = true; |
| 2910 | } else if (Str[1] != 'I') { |
| 2911 | DestWidth = 0; |
| 2912 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2913 | break; |
| 2914 | case 4: |
| 2915 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2916 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2917 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2918 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2919 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2920 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2921 | break; |
| 2922 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2923 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2924 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2925 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2926 | case 11: |
| 2927 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 2928 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2929 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2933 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2934 | OldTy = TD->getUnderlyingType(); |
| 2935 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2936 | OldTy = VD->getType(); |
| 2937 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2938 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 2939 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2940 | return; |
| 2941 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2942 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2943 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2944 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2945 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2946 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2947 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2948 | } else if (ComplexMode) { |
| 2949 | if (!OldTy->isComplexType()) |
| 2950 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2951 | } else { |
| 2952 | if (!OldTy->isFloatingType()) |
| 2953 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2954 | } |
| 2955 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2956 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2957 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2958 | // FIXME: Make sure floating-point mappings are accurate |
| 2959 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2960 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2961 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2962 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | QualType NewTy; |
| 2966 | |
| 2967 | if (IntegerMode) |
| 2968 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 2969 | OldTy->isSignedIntegerType()); |
| 2970 | else |
| 2971 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 2972 | |
| 2973 | if (NewTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2974 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2975 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2978 | if (ComplexMode) { |
| 2979 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 2983 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 2984 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 2985 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2986 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 2987 | |
| 2988 | D->addAttr(::new (S.Context) |
| 2989 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 2990 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2991 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2992 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2993 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 2994 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2995 | if (!VD->hasGlobalStorage()) |
| 2996 | S.Diag(Attr.getLoc(), |
| 2997 | diag::warn_attribute_requires_functions_or_static_globals) |
| 2998 | << Attr.getName(); |
| 2999 | } else if (!isFunctionOrMethod(D)) { |
| 3000 | S.Diag(Attr.getLoc(), |
| 3001 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3002 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3003 | return; |
| 3004 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3005 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3006 | D->addAttr(::new (S.Context) |
| 3007 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3008 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3011 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3012 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3013 | if (!FD->getReturnType()->isVoidType()) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3014 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| 3015 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
| 3016 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3017 | << FD->getType() |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 3018 | << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(), |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3019 | "void"); |
| 3020 | } else { |
| 3021 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3022 | << FD->getType(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3023 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3024 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3025 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3026 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3027 | D->addAttr(::new (S.Context) |
| 3028 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3029 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3032 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3033 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3034 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3035 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3036 | return; |
| 3037 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3038 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3039 | D->addAttr(::new (S.Context) |
| 3040 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3041 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3044 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3045 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3046 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3047 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3048 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3049 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3050 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3051 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3052 | return; |
| 3053 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3054 | if (!isa<ObjCMethodDecl>(D)) { |
| 3055 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3056 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3057 | return; |
| 3058 | } |
| 3059 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3060 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3061 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3062 | D->addAttr(::new (S.Context) |
| 3063 | FastCallAttr(Attr.getRange(), S.Context, |
| 3064 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3065 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3066 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3067 | D->addAttr(::new (S.Context) |
| 3068 | StdCallAttr(Attr.getRange(), S.Context, |
| 3069 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3070 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3071 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3072 | D->addAttr(::new (S.Context) |
| 3073 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3074 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3075 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3076 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3077 | D->addAttr(::new (S.Context) |
| 3078 | CDeclAttr(Attr.getRange(), S.Context, |
| 3079 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3080 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3081 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3082 | D->addAttr(::new (S.Context) |
| 3083 | PascalAttr(Attr.getRange(), S.Context, |
| 3084 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3085 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3086 | case AttributeList::AT_MSABI: |
| 3087 | D->addAttr(::new (S.Context) |
| 3088 | MSABIAttr(Attr.getRange(), S.Context, |
| 3089 | Attr.getAttributeSpellingListIndex())); |
| 3090 | return; |
| 3091 | case AttributeList::AT_SysVABI: |
| 3092 | D->addAttr(::new (S.Context) |
| 3093 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3094 | Attr.getAttributeSpellingListIndex())); |
| 3095 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3096 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3097 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3098 | switch (CC) { |
| 3099 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3100 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3101 | break; |
| 3102 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3103 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3104 | break; |
| 3105 | default: |
| 3106 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3109 | D->addAttr(::new (S.Context) |
| 3110 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3111 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3112 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3113 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3114 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3115 | D->addAttr(::new (S.Context) |
| 3116 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3117 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3118 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3119 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3120 | D->addAttr(::new (S.Context) |
| 3121 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3122 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3123 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3124 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3125 | default: |
| 3126 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3130 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3131 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3132 | if (attr.isInvalid()) |
| 3133 | return true; |
| 3134 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3135 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3136 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3137 | attr.setInvalid(); |
| 3138 | return true; |
| 3139 | } |
| 3140 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3141 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3142 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3143 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3144 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3145 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3146 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3147 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3148 | case AttributeList::AT_MSABI: |
| 3149 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3150 | CC_X86_64Win64; |
| 3151 | break; |
| 3152 | case AttributeList::AT_SysVABI: |
| 3153 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3154 | CC_C; |
| 3155 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3156 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3157 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3158 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3159 | attr.setInvalid(); |
| 3160 | return true; |
| 3161 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3162 | if (StrRef == "aapcs") { |
| 3163 | CC = CC_AAPCS; |
| 3164 | break; |
| 3165 | } else if (StrRef == "aapcs-vfp") { |
| 3166 | CC = CC_AAPCS_VFP; |
| 3167 | break; |
| 3168 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3169 | |
| 3170 | attr.setInvalid(); |
| 3171 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3172 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3173 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3174 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3175 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3176 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3179 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3180 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3181 | if (A == TargetInfo::CCCR_Warning) { |
| 3182 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3183 | |
| 3184 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3185 | if (FD) |
| 3186 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3187 | TargetInfo::CCMT_NonMember; |
| 3188 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3191 | return false; |
| 3192 | } |
| 3193 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3194 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3195 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3196 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3197 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3198 | return true; |
| 3199 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3200 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3201 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3202 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3203 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3204 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3205 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3206 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3207 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3208 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3209 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3212 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3213 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3214 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3215 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3216 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3219 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3220 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3221 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3222 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3223 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3224 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3227 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3230 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3231 | const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3232 | // check the attribute arguments. |
| 3233 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| 3234 | // FIXME: 0 is not okay. |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 3235 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3236 | << Attr.getName() << 2; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3237 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3238 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3239 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3240 | uint32_t MaxThreads, MinBlocks = 0; |
| 3241 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3242 | return; |
| 3243 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3244 | Attr.getArgAsExpr(1), |
| 3245 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3246 | return; |
| 3247 | |
| 3248 | D->addAttr(::new (S.Context) |
| 3249 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3250 | MaxThreads, MinBlocks, |
| 3251 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3254 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3255 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3256 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3257 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3258 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3259 | return; |
| 3260 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3261 | |
| 3262 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3263 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3264 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3265 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3266 | |
| 3267 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3268 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3269 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3270 | return; |
| 3271 | } |
| 3272 | |
| 3273 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3274 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3275 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3276 | return; |
| 3277 | |
| 3278 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3279 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3280 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3281 | return; |
| 3282 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3283 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3284 | if (IsPointer) { |
| 3285 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3286 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3287 | if (!BufferTy->isPointerType()) { |
| 3288 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3289 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3290 | } |
| 3291 | } |
| 3292 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3293 | D->addAttr(::new (S.Context) |
| 3294 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3295 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3296 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3300 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3301 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3302 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3303 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3304 | return; |
| 3305 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3306 | |
| 3307 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3308 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3309 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3310 | if (!isa<VarDecl>(D)) { |
| 3311 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3312 | << Attr.getName() << ExpectedVariable; |
| 3313 | return; |
| 3314 | } |
| 3315 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3316 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3317 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3318 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3319 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3320 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3321 | D->addAttr(::new (S.Context) |
| 3322 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3323 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3324 | Attr.getLayoutCompatible(), |
| 3325 | Attr.getMustBeNull(), |
| 3326 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3329 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3330 | // Checker-specific attribute handlers. |
| 3331 | //===----------------------------------------------------------------------===// |
| 3332 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3333 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3334 | return type->isDependentType() || |
| 3335 | type->isObjCObjectPointerType() || |
| 3336 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3337 | } |
| 3338 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3339 | return type->isDependentType() || |
| 3340 | type->isPointerType() || |
| 3341 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3344 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3345 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3346 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3347 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3348 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3349 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3350 | cf = false; |
| 3351 | } else { |
| 3352 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3353 | cf = true; |
| 3354 | } |
| 3355 | |
| 3356 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3357 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3358 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3359 | return; |
| 3360 | } |
| 3361 | |
| 3362 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3363 | param->addAttr(::new (S.Context) |
| 3364 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3365 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3366 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3367 | param->addAttr(::new (S.Context) |
| 3368 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3369 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3372 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3373 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3374 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3375 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3376 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3377 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3378 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3379 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3380 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3381 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3382 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3383 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3384 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3385 | returnType = FD->getReturnType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3386 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3387 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3388 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3389 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3390 | return; |
| 3391 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3392 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3393 | bool typeOK; |
| 3394 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3395 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3396 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3397 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3398 | case AttributeList::AT_NSReturnsRetained: |
| 3399 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3400 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3401 | cf = false; |
| 3402 | break; |
| 3403 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3404 | case AttributeList::AT_CFReturnsRetained: |
| 3405 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3406 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3407 | cf = true; |
| 3408 | break; |
| 3409 | } |
| 3410 | |
| 3411 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3412 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3413 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3414 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3415 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3416 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3417 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3418 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3419 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3420 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3421 | D->addAttr(::new (S.Context) |
| 3422 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3423 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3424 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3425 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3426 | D->addAttr(::new (S.Context) |
| 3427 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3428 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3429 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3430 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3431 | D->addAttr(::new (S.Context) |
| 3432 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3433 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3434 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3435 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3436 | D->addAttr(::new (S.Context) |
| 3437 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3438 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3439 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3440 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3441 | D->addAttr(::new (S.Context) |
| 3442 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3443 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3444 | return; |
| 3445 | }; |
| 3446 | } |
| 3447 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3448 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3449 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3450 | const int EP_ObjCMethod = 1; |
| 3451 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3452 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3453 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3454 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3455 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3456 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3457 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3458 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3459 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3460 | if (!resultType->isReferenceType() && |
| 3461 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3462 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3463 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3464 | << attr.getName() |
| 3465 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3466 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3467 | |
| 3468 | // Drop the attribute. |
| 3469 | return; |
| 3470 | } |
| 3471 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3472 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3473 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3474 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3477 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3478 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3479 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3480 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3481 | DeclContext *DC = method->getDeclContext(); |
| 3482 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3483 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3484 | << attr.getName() << 0; |
| 3485 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3486 | return; |
| 3487 | } |
| 3488 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3489 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3490 | << attr.getName() << 1; |
| 3491 | return; |
| 3492 | } |
| 3493 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3494 | method->addAttr(::new (S.Context) |
| 3495 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3496 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3499 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3500 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3501 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3502 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3503 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3504 | D->addAttr(::new (S.Context) |
| 3505 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3506 | Attr.getAttributeSpellingListIndex())); |
| 3507 | } |
| 3508 | |
| 3509 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3510 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3511 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3512 | return; |
| 3513 | |
| 3514 | D->addAttr(::new (S.Context) |
| 3515 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3516 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3519 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3520 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3521 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3522 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3523 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3524 | 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] | 3525 | return; |
| 3526 | } |
| 3527 | |
| 3528 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3529 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3530 | Attr.getAttributeSpellingListIndex())); |
| 3531 | } |
| 3532 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3533 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3534 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3535 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3536 | |
| 3537 | if (!Parm) { |
| 3538 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3539 | return; |
| 3540 | } |
| 3541 | |
| 3542 | D->addAttr(::new (S.Context) |
| 3543 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3544 | Attr.getAttributeSpellingListIndex())); |
| 3545 | } |
| 3546 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3547 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3548 | const AttributeList &Attr) { |
| 3549 | IdentifierInfo *RelatedClass = |
| 3550 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0; |
| 3551 | if (!RelatedClass) { |
| 3552 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3553 | return; |
| 3554 | } |
| 3555 | IdentifierInfo *ClassMethod = |
| 3556 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0; |
| 3557 | IdentifierInfo *InstanceMethod = |
| 3558 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0; |
| 3559 | D->addAttr(::new (S.Context) |
| 3560 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3561 | ClassMethod, InstanceMethod, |
| 3562 | Attr.getAttributeSpellingListIndex())); |
| 3563 | } |
| 3564 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3565 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3566 | const AttributeList &Attr) { |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3567 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3568 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3569 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3570 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3571 | Attr.getAttributeSpellingListIndex())); |
| 3572 | } |
| 3573 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3574 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3575 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3576 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3577 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3578 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3579 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3582 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3583 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3584 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3585 | QualType type = vd->getType(); |
| 3586 | |
| 3587 | if (!type->isDependentType() && |
| 3588 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3589 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3590 | << type; |
| 3591 | return; |
| 3592 | } |
| 3593 | |
| 3594 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3595 | |
| 3596 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3597 | // going to infer. |
| 3598 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3599 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3600 | |
| 3601 | switch (lifetime) { |
| 3602 | case Qualifiers::OCL_None: |
| 3603 | assert(type->isDependentType() && |
| 3604 | "didn't infer lifetime for non-dependent type?"); |
| 3605 | break; |
| 3606 | |
| 3607 | case Qualifiers::OCL_Weak: // meaningful |
| 3608 | case Qualifiers::OCL_Strong: // meaningful |
| 3609 | break; |
| 3610 | |
| 3611 | case Qualifiers::OCL_ExplicitNone: |
| 3612 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3613 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3614 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3615 | break; |
| 3616 | } |
| 3617 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3618 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3619 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3620 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3623 | //===----------------------------------------------------------------------===// |
| 3624 | // Microsoft specific attribute handlers. |
| 3625 | //===----------------------------------------------------------------------===// |
| 3626 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3627 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3628 | if (!S.LangOpts.CPlusPlus) { |
| 3629 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3630 | << Attr.getName() << AttributeLangSupport::C; |
| 3631 | return; |
| 3632 | } |
| 3633 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3634 | if (!isa<CXXRecordDecl>(D)) { |
| 3635 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3636 | << Attr.getName() << ExpectedClass; |
| 3637 | return; |
| 3638 | } |
| 3639 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3640 | StringRef StrRef; |
| 3641 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3642 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3643 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3644 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3645 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3646 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3647 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3648 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3649 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3650 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3651 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3652 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3653 | return; |
| 3654 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3655 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3656 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3657 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3658 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3659 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3660 | return; |
| 3661 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3662 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3663 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3664 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3665 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3666 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3667 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3668 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3669 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3672 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3673 | if (!S.LangOpts.CPlusPlus) { |
| 3674 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3675 | << Attr.getName() << AttributeLangSupport::C; |
| 3676 | return; |
| 3677 | } |
| 3678 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3679 | D, Attr.getRange(), /*BestCase=*/true, |
| 3680 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3681 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
| 3682 | if (IA) |
| 3683 | D->addAttr(IA); |
| 3684 | } |
| 3685 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3686 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 3687 | const AttributeList &Attr) { |
| 3688 | // Check the attribute arguments. |
| 3689 | if (Attr.getNumArgs() > 1) { |
| 3690 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3691 | << Attr.getName() << 1; |
| 3692 | return; |
| 3693 | } |
| 3694 | |
| 3695 | StringRef Str; |
| 3696 | SourceLocation ArgLoc; |
| 3697 | |
| 3698 | if (Attr.getNumArgs() == 0) |
| 3699 | Str = ""; |
| 3700 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 3701 | return; |
| 3702 | |
| 3703 | ARMInterruptAttr::InterruptType Kind; |
| 3704 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 3705 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3706 | << Attr.getName() << Str << ArgLoc; |
| 3707 | return; |
| 3708 | } |
| 3709 | |
| 3710 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3711 | D->addAttr(::new (S.Context) |
| 3712 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 3713 | } |
| 3714 | |
| 3715 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 3716 | const AttributeList &Attr) { |
| 3717 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3718 | return; |
| 3719 | |
| 3720 | if (!Attr.isArgExpr(0)) { |
| 3721 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3722 | << AANT_ArgumentIntegerConstant; |
| 3723 | return; |
| 3724 | } |
| 3725 | |
| 3726 | // FIXME: Check for decl - it should be void ()(void). |
| 3727 | |
| 3728 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 3729 | llvm::APSInt NumParams(32); |
| 3730 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 3731 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3732 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3733 | << NumParamsExpr->getSourceRange(); |
| 3734 | return; |
| 3735 | } |
| 3736 | |
| 3737 | unsigned Num = NumParams.getLimitedValue(255); |
| 3738 | if ((Num & 1) || Num > 30) { |
| 3739 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3740 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 3741 | << NumParamsExpr->getSourceRange(); |
| 3742 | return; |
| 3743 | } |
| 3744 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3745 | D->addAttr(::new (S.Context) |
| 3746 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 3747 | Attr.getAttributeSpellingListIndex())); |
| 3748 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3752 | // Dispatch the interrupt attribute based on the current target. |
| 3753 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 3754 | handleMSP430InterruptAttr(S, D, Attr); |
| 3755 | else |
| 3756 | handleARMInterruptAttr(S, D, Attr); |
| 3757 | } |
| 3758 | |
| 3759 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 3760 | const AttributeList& Attr) { |
| 3761 | // If we try to apply it to a function pointer, don't warn, but don't |
| 3762 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 3763 | // special about calling a force_align_arg_pointer function. |
| 3764 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3765 | if (VD && VD->getType()->isFunctionPointerType()) |
| 3766 | return; |
| 3767 | // Also don't warn on function pointer typedefs. |
| 3768 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 3769 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 3770 | TD->getUnderlyingType()->isFunctionType())) |
| 3771 | return; |
| 3772 | // Attribute can only be applied to function types. |
| 3773 | if (!isa<FunctionDecl>(D)) { |
| 3774 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3775 | << Attr.getName() << /* function */0; |
| 3776 | return; |
| 3777 | } |
| 3778 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3779 | D->addAttr(::new (S.Context) |
| 3780 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 3781 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 3785 | unsigned AttrSpellingListIndex) { |
| 3786 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 3787 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3788 | return NULL; |
| 3789 | } |
| 3790 | |
| 3791 | if (D->hasAttr<DLLImportAttr>()) |
| 3792 | return NULL; |
| 3793 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3794 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3798 | // Attribute can be applied only to functions or variables. |
| 3799 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3800 | if (!FD && !isa<VarDecl>(D)) { |
| 3801 | // Apparently Visual C++ thinks it is okay to not emit a warning |
| 3802 | // in this case, so only emit a warning when -fms-extensions is not |
| 3803 | // specified. |
| 3804 | if (!S.getLangOpts().MicrosoftExt) |
| 3805 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3806 | << Attr.getName() << ExpectedVariableOrFunction; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3807 | return; |
| 3808 | } |
| 3809 | |
| 3810 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 3811 | // Warning is emitted. |
| 3812 | if (FD && FD->isInlineSpecified()) { |
| 3813 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3814 | return; |
| 3815 | } |
| 3816 | |
| 3817 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3818 | DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); |
| 3819 | if (NewAttr) |
| 3820 | D->addAttr(NewAttr); |
| 3821 | } |
| 3822 | |
| 3823 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 3824 | unsigned AttrSpellingListIndex) { |
| 3825 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 3826 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3827 | D->dropAttr<DLLImportAttr>(); |
| 3828 | } |
| 3829 | |
| 3830 | if (D->hasAttr<DLLExportAttr>()) |
| 3831 | return NULL; |
| 3832 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3833 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3837 | // Currently, the dllexport attribute is ignored for inlined functions, unless |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3838 | // the -fkeep-inline-functions flag has been used. Warning is emitted. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3839 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) { |
| 3840 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 3841 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3842 | return; |
| 3843 | } |
| 3844 | |
| 3845 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3846 | DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); |
| 3847 | if (NewAttr) |
| 3848 | D->addAttr(NewAttr); |
| 3849 | } |
| 3850 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3851 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3852 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3853 | unsigned AttrSpellingListIndex, |
| 3854 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3855 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 3856 | if (IA->getSemanticSpelling() == SemanticSpelling) |
| 3857 | return 0; |
| 3858 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 3859 | << 1 /*previous declaration*/; |
| 3860 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 3861 | D->dropAttr<MSInheritanceAttr>(); |
| 3862 | } |
| 3863 | |
| 3864 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 3865 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3866 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 3867 | SemanticSpelling)) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3868 | return 0; |
| 3869 | } |
| 3870 | } else { |
| 3871 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 3872 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 3873 | << 1 /*partial specialization*/; |
| 3874 | return 0; |
| 3875 | } |
| 3876 | if (RD->getDescribedClassTemplate()) { |
| 3877 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 3878 | << 0 /*primary template*/; |
| 3879 | return 0; |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3884 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3887 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3888 | // The capability attributes take a single string parameter for the name of |
| 3889 | // the capability they represent. The lockable attribute does not take any |
| 3890 | // parameters. However, semantically, both attributes represent the same |
| 3891 | // concept, and so they use the same semantic attribute. Eventually, the |
| 3892 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 3893 | // |
| 3894 | // For backwards compatibility, any capability which has no specified string |
| 3895 | // literal will be considered a "mutex." |
| 3896 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3897 | SourceLocation LiteralLoc; |
| 3898 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 3899 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 3900 | return; |
| 3901 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 3902 | // Currently, there are only two names allowed for a capability: role and |
| 3903 | // mutex (case insensitive). Diagnose other capability names. |
| 3904 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 3905 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 3906 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3907 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 3908 | Attr.getAttributeSpellingListIndex())); |
| 3909 | } |
| 3910 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 3911 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 3912 | const AttributeList &Attr) { |
| 3913 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 3914 | Attr.getArgAsExpr(0), |
| 3915 | Attr.getAttributeSpellingListIndex())); |
| 3916 | } |
| 3917 | |
| 3918 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 3919 | const AttributeList &Attr) { |
| 3920 | SmallVector<Expr*, 1> Args; |
| 3921 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 3922 | return; |
| 3923 | |
| 3924 | // Check that all arguments are lockable objects. |
| 3925 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 3926 | if (Args.empty()) |
| 3927 | return; |
| 3928 | |
| 3929 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 3930 | S.Context, |
| 3931 | Args.data(), Args.size(), |
| 3932 | Attr.getAttributeSpellingListIndex())); |
| 3933 | } |
| 3934 | |
| 3935 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 3936 | const AttributeList &Attr) { |
| 3937 | SmallVector<Expr*, 2> Args; |
| 3938 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 3939 | return; |
| 3940 | |
| 3941 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 3942 | S.Context, |
| 3943 | Attr.getArgAsExpr(0), |
| 3944 | Args.data(), |
| 3945 | Args.size(), |
| 3946 | Attr.getAttributeSpellingListIndex())); |
| 3947 | } |
| 3948 | |
| 3949 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 3950 | const AttributeList &Attr) { |
| 3951 | SmallVector<Expr*, 1> Args; |
| 3952 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 3953 | return; |
| 3954 | |
| 3955 | // Check that all arguments are lockable objects. |
| 3956 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 3957 | if (Args.empty()) |
| 3958 | return; |
| 3959 | |
| 3960 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr(Attr.getRange(), |
| 3961 | S.Context, |
| 3962 | Args.data(), Args.size(), |
| 3963 | Attr.getAttributeSpellingListIndex())); |
| 3964 | } |
| 3965 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3966 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 3967 | const AttributeList &Attr) { |
| 3968 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 3969 | return; |
| 3970 | |
| 3971 | // check that all arguments are lockable objects |
| 3972 | SmallVector<Expr*, 1> Args; |
| 3973 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 3974 | if (Args.empty()) |
| 3975 | return; |
| 3976 | |
| 3977 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 3978 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 3979 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 3980 | |
| 3981 | D->addAttr(RCA); |
| 3982 | } |
| 3983 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3984 | /// Handles semantic checking for features that are common to all attributes, |
| 3985 | /// such as checking whether a parameter was properly specified, or the correct |
| 3986 | /// number of arguments were passed, etc. |
| 3987 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 3988 | const AttributeList &Attr) { |
| 3989 | // Several attributes carry different semantics than the parsing requires, so |
| 3990 | // those are opted out of the common handling. |
| 3991 | // |
| 3992 | // We also bail on unknown and ignored attributes because those are handled |
| 3993 | // as part of the target-specific handling logic. |
| 3994 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3995 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3996 | return false; |
| 3997 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3998 | // Check whether the attribute requires specific language extensions to be |
| 3999 | // enabled. |
| 4000 | if (!Attr.diagnoseLangOpts(S)) |
| 4001 | return true; |
| 4002 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4003 | // If there are no optional arguments, then checking for the argument count |
| 4004 | // is trivial. |
| 4005 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4006 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4007 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4008 | |
| 4009 | // Check whether the attribute appertains to the given subject. |
| 4010 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 4011 | return true; |
| 4012 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4013 | return false; |
| 4014 | } |
| 4015 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4016 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4017 | // Top Level Sema Entry Points |
| 4018 | //===----------------------------------------------------------------------===// |
| 4019 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4020 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4021 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4022 | /// silently ignore it if a GNU attribute. |
| 4023 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4024 | const AttributeList &Attr, |
| 4025 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4026 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4027 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4028 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4029 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4030 | // instead. |
| 4031 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4032 | return; |
| 4033 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4034 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 4035 | // which do not apply to the current target architecture are treated as |
| 4036 | // though they were unknown attributes. |
| 4037 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
| 4038 | !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4039 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 4040 | ? diag::warn_unhandled_ms_attribute_ignored |
| 4041 | : diag::warn_unknown_attribute_ignored) |
| 4042 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4043 | return; |
| 4044 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4045 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4046 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4047 | return; |
| 4048 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4049 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4050 | default: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4051 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4052 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4053 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4054 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4055 | handleInterruptAttr(S, D, Attr); |
| 4056 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4057 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4058 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 4059 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4060 | case AttributeList::AT_DLLExport: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4061 | handleDLLExportAttr(S, D, Attr); |
| 4062 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4063 | case AttributeList::AT_DLLImport: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4064 | handleDLLImportAttr(S, D, Attr); |
| 4065 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4066 | case AttributeList::AT_Mips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4067 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); |
| 4068 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4069 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4070 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 4071 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 4072 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4073 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 4074 | break; |
| 4075 | case AttributeList::AT_IBOutlet: |
| 4076 | handleIBOutlet(S, D, Attr); |
| 4077 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4078 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4079 | handleIBOutletCollection(S, D, Attr); |
| 4080 | break; |
| 4081 | case AttributeList::AT_Alias: |
| 4082 | handleAliasAttr(S, D, Attr); |
| 4083 | break; |
| 4084 | case AttributeList::AT_Aligned: |
| 4085 | handleAlignedAttr(S, D, Attr); |
| 4086 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4087 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4088 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); |
| 4089 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4090 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4091 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 4092 | break; |
| 4093 | case AttributeList::AT_TLSModel: |
| 4094 | handleTLSModelAttr(S, D, Attr); |
| 4095 | break; |
| 4096 | case AttributeList::AT_Annotate: |
| 4097 | handleAnnotateAttr(S, D, Attr); |
| 4098 | break; |
| 4099 | case AttributeList::AT_Availability: |
| 4100 | handleAvailabilityAttr(S, D, Attr); |
| 4101 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4102 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4103 | handleDependencyAttr(S, scope, D, Attr); |
| 4104 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4105 | case AttributeList::AT_Common: |
| 4106 | handleCommonAttr(S, D, Attr); |
| 4107 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4108 | case AttributeList::AT_CUDAConstant: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4109 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); |
| 4110 | break; |
| 4111 | case AttributeList::AT_Constructor: |
| 4112 | handleConstructorAttr(S, D, Attr); |
| 4113 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4114 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4115 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 4116 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4117 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4118 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4119 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4120 | case AttributeList::AT_Destructor: |
| 4121 | handleDestructorAttr(S, D, Attr); |
| 4122 | break; |
| 4123 | case AttributeList::AT_EnableIf: |
| 4124 | handleEnableIfAttr(S, D, Attr); |
| 4125 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4126 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4127 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4128 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4129 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4130 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4131 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4132 | case AttributeList::AT_Format: |
| 4133 | handleFormatAttr(S, D, Attr); |
| 4134 | break; |
| 4135 | case AttributeList::AT_FormatArg: |
| 4136 | handleFormatArgAttr(S, D, Attr); |
| 4137 | break; |
| 4138 | case AttributeList::AT_CUDAGlobal: |
| 4139 | handleGlobalAttr(S, D, Attr); |
| 4140 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 4141 | case AttributeList::AT_CUDADevice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4142 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); |
| 4143 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4144 | case AttributeList::AT_CUDAHost: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4145 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); |
| 4146 | break; |
| 4147 | case AttributeList::AT_GNUInline: |
| 4148 | handleGNUInlineAttr(S, D, Attr); |
| 4149 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4150 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4151 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4152 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4153 | case AttributeList::AT_Malloc: |
| 4154 | handleMallocAttr(S, D, Attr); |
| 4155 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4156 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4157 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 4158 | break; |
| 4159 | case AttributeList::AT_Mode: |
| 4160 | handleModeAttr(S, D, Attr); |
| 4161 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4162 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4163 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 4164 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 4165 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4166 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 4167 | handleNonNullAttrParameter(S, PVD, Attr); |
| 4168 | else |
| 4169 | handleNonNullAttr(S, D, Attr); |
| 4170 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 4171 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4172 | handleReturnsNonNullAttr(S, D, Attr); |
| 4173 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4174 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4175 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 4176 | break; |
| 4177 | case AttributeList::AT_Ownership: |
| 4178 | handleOwnershipAttr(S, D, Attr); |
| 4179 | break; |
| 4180 | case AttributeList::AT_Cold: |
| 4181 | handleColdAttr(S, D, Attr); |
| 4182 | break; |
| 4183 | case AttributeList::AT_Hot: |
| 4184 | handleHotAttr(S, D, Attr); |
| 4185 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4186 | case AttributeList::AT_Naked: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4187 | handleSimpleAttribute<NakedAttr>(S, D, Attr); |
| 4188 | break; |
| 4189 | case AttributeList::AT_NoReturn: |
| 4190 | handleNoReturnAttr(S, D, Attr); |
| 4191 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4192 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4193 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 4194 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4195 | case AttributeList::AT_CUDAShared: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4196 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); |
| 4197 | break; |
| 4198 | case AttributeList::AT_VecReturn: |
| 4199 | handleVecReturnAttr(S, D, Attr); |
| 4200 | break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4201 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4202 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4203 | handleObjCOwnershipAttr(S, D, Attr); |
| 4204 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4205 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4206 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 4207 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4208 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4209 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4210 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 4211 | break; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4212 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4213 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4214 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 4215 | break; |
| 4216 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4217 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4218 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 4219 | break; |
| 4220 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4221 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4222 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 4223 | break; |
| 4224 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4225 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4226 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 4227 | break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4228 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4229 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4230 | handleObjCDesignatedInitializer(S, D, Attr); |
| 4231 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4232 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4233 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4234 | handleCFAuditedTransferAttr(S, D, Attr); |
| 4235 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4236 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4237 | handleCFUnknownTransferAttr(S, D, Attr); |
| 4238 | break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4239 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4240 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4241 | case AttributeList::AT_NSConsumed: |
| 4242 | handleNSConsumedAttr(S, D, Attr); |
| 4243 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4244 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4245 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 4246 | break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4247 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4248 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4249 | case AttributeList::AT_NSReturnsNotRetained: |
| 4250 | case AttributeList::AT_CFReturnsNotRetained: |
| 4251 | case AttributeList::AT_NSReturnsRetained: |
| 4252 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4253 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 4254 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4255 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4256 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 4257 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4258 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4259 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 4260 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4261 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4262 | handleVecTypeHint(S, D, Attr); |
| 4263 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4264 | |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4265 | case AttributeList::AT_InitPriority: |
| 4266 | handleInitPriorityAttr(S, D, Attr); |
| 4267 | break; |
| 4268 | |
| 4269 | case AttributeList::AT_Packed: |
| 4270 | handlePackedAttr(S, D, Attr); |
| 4271 | break; |
| 4272 | case AttributeList::AT_Section: |
| 4273 | handleSectionAttr(S, D, Attr); |
| 4274 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4275 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4276 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4277 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4278 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 4279 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 4280 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4281 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4282 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 4283 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4284 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 4285 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4286 | break; |
| 4287 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4288 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 4289 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 4290 | case AttributeList::AT_Unused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4291 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); |
| 4292 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4293 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4294 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 4295 | break; |
| 4296 | case AttributeList::AT_Used: |
| 4297 | handleUsedAttr(S, D, Attr); |
| 4298 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4299 | case AttributeList::AT_Visibility: |
| 4300 | handleVisibilityAttr(S, D, Attr, false); |
| 4301 | break; |
| 4302 | case AttributeList::AT_TypeVisibility: |
| 4303 | handleVisibilityAttr(S, D, Attr, true); |
| 4304 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4305 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4306 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 4307 | break; |
| 4308 | case AttributeList::AT_WarnUnusedResult: |
| 4309 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4310 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4311 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4312 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 4313 | break; |
| 4314 | case AttributeList::AT_WeakRef: |
| 4315 | handleWeakRefAttr(S, D, Attr); |
| 4316 | break; |
| 4317 | case AttributeList::AT_WeakImport: |
| 4318 | handleWeakImportAttr(S, D, Attr); |
| 4319 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4320 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4321 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4322 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4323 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4324 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 4325 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4326 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4327 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4328 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4329 | case AttributeList::AT_ObjCNSObject: |
| 4330 | handleObjCNSObject(S, D, Attr); |
| 4331 | break; |
| 4332 | case AttributeList::AT_Blocks: |
| 4333 | handleBlocksAttr(S, D, Attr); |
| 4334 | break; |
| 4335 | case AttributeList::AT_Sentinel: |
| 4336 | handleSentinelAttr(S, D, Attr); |
| 4337 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4338 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4339 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 4340 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4341 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4342 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 4343 | break; |
| 4344 | case AttributeList::AT_Cleanup: |
| 4345 | handleCleanupAttr(S, D, Attr); |
| 4346 | break; |
| 4347 | case AttributeList::AT_NoDebug: |
| 4348 | handleNoDebugAttr(S, D, Attr); |
| 4349 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 4350 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4351 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 4352 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4353 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4354 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 4355 | break; |
| 4356 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 4357 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 4358 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4359 | case AttributeList::AT_StdCall: |
| 4360 | case AttributeList::AT_CDecl: |
| 4361 | case AttributeList::AT_FastCall: |
| 4362 | case AttributeList::AT_ThisCall: |
| 4363 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4364 | case AttributeList::AT_MSABI: |
| 4365 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4366 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4367 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4368 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4369 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4370 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4371 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4372 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 4373 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4374 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4375 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); |
| 4376 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4377 | |
| 4378 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4379 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4380 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4381 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4382 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4383 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4384 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4385 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4386 | handleMSInheritanceAttr(S, D, Attr); |
| 4387 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4388 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4389 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 4390 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4391 | |
| 4392 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4393 | case AttributeList::AT_AssertExclusiveLock: |
| 4394 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4395 | break; |
| 4396 | case AttributeList::AT_AssertSharedLock: |
| 4397 | handleAssertSharedLockAttr(S, D, Attr); |
| 4398 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4399 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4400 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 4401 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4402 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4403 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4404 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4405 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4406 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 4407 | break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4408 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4409 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4410 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4411 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4412 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4413 | break; |
| 4414 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4415 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4416 | break; |
| 4417 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4418 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4419 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4420 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4421 | handleGuardedByAttr(S, D, Attr); |
| 4422 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4423 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4424 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4425 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4426 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4427 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4428 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4429 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4430 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4431 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4432 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4433 | handleLockReturnedAttr(S, D, Attr); |
| 4434 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4435 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4436 | handleLocksExcludedAttr(S, D, Attr); |
| 4437 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4438 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4439 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4440 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4441 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4442 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4443 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4444 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4445 | handleUnlockFunAttr(S, D, Attr); |
| 4446 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4447 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4448 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4449 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4450 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4451 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4452 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4453 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4454 | // Capability analysis attributes. |
| 4455 | case AttributeList::AT_Capability: |
| 4456 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4457 | handleCapabilityAttr(S, D, Attr); |
| 4458 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4459 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4460 | handleRequiresCapabilityAttr(S, D, Attr); |
| 4461 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4462 | |
| 4463 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4464 | handleAssertCapabilityAttr(S, D, Attr); |
| 4465 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4466 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4467 | handleAcquireCapabilityAttr(S, D, Attr); |
| 4468 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4469 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4470 | handleReleaseCapabilityAttr(S, D, Attr); |
| 4471 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4472 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4473 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 4474 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4475 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4476 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4477 | case AttributeList::AT_Consumable: |
| 4478 | handleConsumableAttr(S, D, Attr); |
| 4479 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4480 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4481 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 4482 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4483 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4484 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 4485 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4486 | case AttributeList::AT_CallableWhen: |
| 4487 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4488 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4489 | case AttributeList::AT_ParamTypestate: |
| 4490 | handleParamTypestateAttr(S, D, Attr); |
| 4491 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4492 | case AttributeList::AT_ReturnTypestate: |
| 4493 | handleReturnTypestateAttr(S, D, Attr); |
| 4494 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4495 | case AttributeList::AT_SetTypestate: |
| 4496 | handleSetTypestateAttr(S, D, Attr); |
| 4497 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4498 | case AttributeList::AT_TestTypestate: |
| 4499 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4500 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4501 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4502 | // Type safety attributes. |
| 4503 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4504 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4505 | break; |
| 4506 | case AttributeList::AT_TypeTagForDatatype: |
| 4507 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4508 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | |
| 4512 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4513 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4514 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4515 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4516 | bool IncludeCXX11Attributes) { |
| 4517 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4518 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4519 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4520 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4521 | // GCC accepts |
| 4522 | // static int a9 __attribute__((weakref)); |
| 4523 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4524 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 4525 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 4526 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4527 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4528 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4529 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4530 | |
| 4531 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4532 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4533 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 4534 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4535 | D->setInvalidDecl(); |
| 4536 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4537 | if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
| 4538 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4539 | D->setInvalidDecl(); |
| 4540 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4541 | if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
| 4542 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4543 | D->setInvalidDecl(); |
| 4544 | } |
| 4545 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4548 | // Annotation attributes are the only attributes allowed after an access |
| 4549 | // specifier. |
| 4550 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4551 | const AttributeList *AttrList) { |
| 4552 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4553 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4554 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4555 | } else { |
| 4556 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4557 | return true; |
| 4558 | } |
| 4559 | } |
| 4560 | |
| 4561 | return false; |
| 4562 | } |
| 4563 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4564 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4565 | /// contains any decl attributes that we should warn about. |
| 4566 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4567 | for ( ; A; A = A->getNext()) { |
| 4568 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4569 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4570 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4571 | |
| 4572 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4573 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4574 | << A->getName() << A->getRange(); |
| 4575 | } else { |
| 4576 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4577 | << A->getName() << A->getRange(); |
| 4578 | } |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4583 | /// used to build a declaration, complain about any decl attributes |
| 4584 | /// which might be lying around on it. |
| 4585 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4586 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4587 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4588 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4589 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4590 | } |
| 4591 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4592 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4593 | /// \#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] | 4594 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4595 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4596 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4597 | NamedDecl *NewD = 0; |
| 4598 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4599 | FunctionDecl *NewFD; |
| 4600 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4601 | // FIXME: Mangling? |
| 4602 | // FIXME: Is the qualifier info correct? |
| 4603 | // FIXME: Is the DeclContext correct? |
| 4604 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4605 | Loc, Loc, DeclarationName(II), |
| 4606 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4607 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4608 | FD->hasPrototype(), |
| 4609 | false/*isConstexprSpecified*/); |
| 4610 | NewD = NewFD; |
| 4611 | |
| 4612 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4613 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4614 | |
| 4615 | // Fake up parameter variables; they are declared as if this were |
| 4616 | // a typedef. |
| 4617 | QualType FDTy = FD->getType(); |
| 4618 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4619 | SmallVector<ParmVarDecl*, 16> Params; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4620 | for (FunctionProtoType::param_type_iterator AI = FT->param_type_begin(), |
| 4621 | AE = FT->param_type_end(); |
| 4622 | AI != AE; ++AI) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4623 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4624 | Param->setScopeInfo(0, Params.size()); |
| 4625 | Params.push_back(Param); |
| 4626 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4627 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4628 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4629 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4630 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4631 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4632 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4633 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4634 | if (VD->getQualifier()) { |
| 4635 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4636 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4637 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4638 | } |
| 4639 | return NewD; |
| 4640 | } |
| 4641 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4642 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4643 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4644 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4645 | if (W.getUsed()) return; // only do this once |
| 4646 | W.setUsed(true); |
| 4647 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4648 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4649 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4650 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 4651 | W.getLocation())); |
| 4652 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4653 | WeakTopLevelDecl.push_back(NewD); |
| 4654 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4655 | // to insert Decl at TU scope, sorry. |
| 4656 | DeclContext *SavedContext = CurContext; |
| 4657 | CurContext = Context.getTranslationUnitDecl(); |
| 4658 | PushOnScopeChains(NewD, S); |
| 4659 | CurContext = SavedContext; |
| 4660 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4661 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4662 | } |
| 4663 | } |
| 4664 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4665 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4666 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4667 | // have to do this. |
| 4668 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4669 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4670 | NamedDecl *ND = NULL; |
| 4671 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4672 | if (VD->isExternC()) |
| 4673 | ND = VD; |
| 4674 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4675 | if (FD->isExternC()) |
| 4676 | ND = FD; |
| 4677 | if (ND) { |
| 4678 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4679 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4680 | = WeakUndeclaredIdentifiers.find(Id); |
| 4681 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4682 | WeakInfo W = I->second; |
| 4683 | DeclApplyPragmaWeak(S, ND, W); |
| 4684 | WeakUndeclaredIdentifiers[Id] = W; |
| 4685 | } |
| 4686 | } |
| 4687 | } |
| 4688 | } |
| 4689 | } |
| 4690 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4691 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4692 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4693 | /// 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] | 4694 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4695 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4696 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4697 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4698 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4699 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4700 | // position to the decl itself. This handles cases like: |
| 4701 | // int *__attr__(x)** D; |
| 4702 | // when X is a decl attribute. |
| 4703 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4704 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4705 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4706 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4707 | // Finally, apply any attributes on the decl itself. |
| 4708 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4709 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4710 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4711 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4712 | /// Is the given declaration allowed to use a forbidden type? |
| 4713 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4714 | // Private ivars are always okay. Unfortunately, people don't |
| 4715 | // always properly make their ivars private, even in system headers. |
| 4716 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4717 | // Function declarations in sys headers will be marked unavailable. |
| 4718 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4719 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4720 | return false; |
| 4721 | |
| 4722 | // Require it to be declared in a system header. |
| 4723 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4724 | } |
| 4725 | |
| 4726 | /// Handle a delayed forbidden-type diagnostic. |
| 4727 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4728 | Decl *decl) { |
| 4729 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4730 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, |
| 4731 | "this system declaration uses an unsupported type", |
| 4732 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4733 | return; |
| 4734 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4735 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4736 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4737 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4738 | // kind of forbidden type messages on unavailable functions. |
| 4739 | if (FD->hasAttr<UnavailableAttr>() && |
| 4740 | diag.getForbiddenTypeDiagnostic() == |
| 4741 | diag::err_arc_array_param_no_ownership) { |
| 4742 | diag.Triggered = true; |
| 4743 | return; |
| 4744 | } |
| 4745 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4746 | |
| 4747 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4748 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4749 | diag.Triggered = true; |
| 4750 | } |
| 4751 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4752 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4753 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4754 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4755 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4756 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4757 | // When delaying diagnostics to run in the context of a parsed |
| 4758 | // declaration, we only want to actually emit anything if parsing |
| 4759 | // succeeds. |
| 4760 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4761 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4762 | // We emit all the active diagnostics in this pool or any of its |
| 4763 | // parents. In general, we'll get one pool for the decl spec |
| 4764 | // and a child pool for each declarator; in a decl group like: |
| 4765 | // deprecated_typedef foo, *bar, baz(); |
| 4766 | // only the declarator pops will be passed decls. This is correct; |
| 4767 | // we really do need to consider delayed diagnostics from the decl spec |
| 4768 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4769 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4770 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4771 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4772 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4773 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4774 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4775 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4776 | continue; |
| 4777 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4778 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4779 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4780 | case DelayedDiagnostic::Unavailable: |
| 4781 | // Don't bother giving deprecation/unavailable diagnostics if |
| 4782 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4783 | if (!decl->isInvalidDecl()) |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4784 | HandleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4785 | break; |
| 4786 | |
| 4787 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4788 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4789 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4790 | |
| 4791 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4792 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4793 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4794 | } |
| 4795 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4796 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4799 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4800 | /// been delayed in the current context instead of in the given pool. |
| 4801 | /// Essentially, this just moves them to the current pool. |
| 4802 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4803 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4804 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4805 | curPool->steal(pool); |
| 4806 | } |
| 4807 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4808 | static bool isDeclDeprecated(Decl *D) { |
| 4809 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4810 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4811 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4812 | // A category implicitly has the availability of the interface. |
| 4813 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4814 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4815 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4816 | return false; |
| 4817 | } |
| 4818 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4819 | static bool isDeclUnavailable(Decl *D) { |
| 4820 | do { |
| 4821 | if (D->isUnavailable()) |
| 4822 | return true; |
| 4823 | // A category implicitly has the availability of the interface. |
| 4824 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4825 | return CatD->getClassInterface()->isUnavailable(); |
| 4826 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4827 | return false; |
| 4828 | } |
| 4829 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4830 | static void |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4831 | DoEmitAvailabilityWarning(Sema &S, |
| 4832 | DelayedDiagnostic::DDKind K, |
| 4833 | Decl *Ctx, |
| 4834 | const NamedDecl *D, |
| 4835 | StringRef Message, |
| 4836 | SourceLocation Loc, |
| 4837 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4838 | const ObjCPropertyDecl *ObjCProperty) { |
| 4839 | |
| 4840 | // Diagnostics for deprecated or unavailable. |
| 4841 | unsigned diag, diag_message, diag_fwdclass_message; |
| 4842 | |
| 4843 | // Matches 'diag::note_property_attribute' options. |
| 4844 | unsigned property_note_select; |
| 4845 | |
| 4846 | // Matches diag::note_availability_specified_here. |
| 4847 | unsigned available_here_select_kind; |
| 4848 | |
| 4849 | // Don't warn if our current context is deprecated or unavailable. |
| 4850 | switch (K) { |
| 4851 | case DelayedDiagnostic::Deprecation: |
| 4852 | if (isDeclDeprecated(Ctx)) |
| 4853 | return; |
| 4854 | diag = diag::warn_deprecated; |
| 4855 | diag_message = diag::warn_deprecated_message; |
| 4856 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 4857 | property_note_select = /* deprecated */ 0; |
| 4858 | available_here_select_kind = /* deprecated */ 2; |
| 4859 | break; |
| 4860 | |
| 4861 | case DelayedDiagnostic::Unavailable: |
| 4862 | if (isDeclUnavailable(Ctx)) |
| 4863 | return; |
| 4864 | diag = diag::err_unavailable; |
| 4865 | diag_message = diag::err_unavailable_message; |
| 4866 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 4867 | property_note_select = /* unavailable */ 1; |
| 4868 | available_here_select_kind = /* unavailable */ 0; |
| 4869 | break; |
| 4870 | |
| 4871 | default: |
| 4872 | llvm_unreachable("Neither a deprecation or unavailable kind"); |
| 4873 | } |
| 4874 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4875 | DeclarationName Name = D->getDeclName(); |
| 4876 | if (!Message.empty()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4877 | S.Diag(Loc, diag_message) << Name << Message; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4878 | if (ObjCProperty) |
| 4879 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4880 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4881 | } else if (!UnknownObjCClass) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4882 | S.Diag(Loc, diag) << Name; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4883 | if (ObjCProperty) |
| 4884 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4885 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4886 | } else { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4887 | S.Diag(Loc, diag_fwdclass_message) << Name; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4888 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4889 | } |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4890 | |
| 4891 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 4892 | << D << available_here_select_kind; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4895 | void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD, |
| 4896 | Decl *Ctx) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4897 | DD.Triggered = true; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4898 | DoEmitAvailabilityWarning(*this, |
| 4899 | (DelayedDiagnostic::DDKind) DD.Kind, |
| 4900 | Ctx, |
| 4901 | DD.getDeprecationDecl(), |
| 4902 | DD.getDeprecationMessage(), |
| 4903 | DD.Loc, |
| 4904 | DD.getUnknownObjCClass(), |
| 4905 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4908 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 4909 | NamedDecl *D, StringRef Message, |
| 4910 | SourceLocation Loc, |
| 4911 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4912 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4913 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4914 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4915 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D, |
| 4916 | UnknownObjCClass, |
| 4917 | ObjCProperty, |
| 4918 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4919 | return; |
| 4920 | } |
| 4921 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4922 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
| 4923 | DelayedDiagnostic::DDKind K; |
| 4924 | switch (AD) { |
| 4925 | case AD_Deprecation: |
| 4926 | K = DelayedDiagnostic::Deprecation; |
| 4927 | break; |
| 4928 | case AD_Unavailable: |
| 4929 | K = DelayedDiagnostic::Unavailable; |
| 4930 | break; |
| 4931 | } |
| 4932 | |
| 4933 | DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc, |
| 4934 | UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4935 | } |