Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 33 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 35 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 36 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 37 | C, |
| 38 | Cpp, |
| 39 | ObjC |
| 40 | }; |
| 41 | } |
| 42 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | // Helper functions |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 47 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 48 | /// type (function or function-typed variable) or an Objective-C |
| 49 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 50 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 51 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 52 | } |
| 53 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 54 | /// Return true if the given decl has a declarator that should have |
| 55 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 56 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 57 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 58 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 59 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 62 | /// hasFunctionProto - Return true if the given decl has a argument |
| 63 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 64 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 65 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 66 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 67 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 68 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 71 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 72 | /// 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] | 73 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 74 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 75 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 76 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 77 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 78 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 82 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 83 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 84 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 85 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 86 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 87 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 88 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 91 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 92 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 93 | return cast<FunctionProtoType>(FnTy)->getReturnType(); |
| 94 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 98 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 99 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 100 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 101 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 102 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 103 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 104 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | static bool isInstanceMethod(const Decl *D) { |
| 109 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 110 | return MethodDecl->isInstance(); |
| 111 | return false; |
| 112 | } |
| 113 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 114 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 115 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 116 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 117 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 118 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 119 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 120 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 121 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 122 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 123 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 125 | // FIXME: Should we walk the chain of classes? |
| 126 | return ClsName == &Ctx.Idents.get("NSString") || |
| 127 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 128 | } |
| 129 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 130 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 131 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 132 | if (!PT) |
| 133 | return false; |
| 134 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 135 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 136 | if (!RT) |
| 137 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 138 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 139 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 140 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 141 | return false; |
| 142 | |
| 143 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 144 | } |
| 145 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 146 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 147 | // FIXME: Include the type in the argument list. |
| 148 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 149 | } |
| 150 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 151 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 152 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 153 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 154 | unsigned Num) { |
| 155 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 156 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 157 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 164 | /// \brief Check if the attribute has at least as many args as Num. May |
| 165 | /// output an error. |
| 166 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 167 | unsigned Num) { |
| 168 | if (getNumAttributeArgs(Attr) < Num) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 169 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) |
| 170 | << Attr.getName() << Num; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 177 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 178 | /// expression and return success or failure. May output an error. |
| 179 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 180 | const Expr *Expr, uint32_t &Val, |
| 181 | unsigned Idx = UINT_MAX) { |
| 182 | llvm::APSInt I(32); |
| 183 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 184 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 185 | if (Idx != UINT_MAX) |
| 186 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 187 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 188 | << Expr->getSourceRange(); |
| 189 | else |
| 190 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 191 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 192 | << Expr->getSourceRange(); |
| 193 | return false; |
| 194 | } |
| 195 | Val = (uint32_t)I.getZExtValue(); |
| 196 | return true; |
| 197 | } |
| 198 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 199 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 200 | /// declaration. Returns true if diagnosed. |
| 201 | template <typename AttrTy> |
| 202 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 203 | const AttributeList &Attr) { |
| 204 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 205 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 206 | << Attr.getName() << A; |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 212 | /// \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] | 213 | /// instance method D. May output an error. |
| 214 | /// |
| 215 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 216 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 217 | const AttributeList &Attr, |
| 218 | unsigned AttrArgNum, |
| 219 | const Expr *IdxExpr, |
| 220 | uint64_t &Idx) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 221 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 222 | |
| 223 | // In C++ the implicit 'this' function parameter also counts. |
| 224 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 225 | bool HP = hasFunctionProto(D); |
| 226 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 227 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 228 | unsigned NumParams = |
| 229 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 230 | |
| 231 | llvm::APSInt IdxInt; |
| 232 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 233 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 234 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 235 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 236 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
| 240 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 241 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 242 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 243 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | Idx--; // Convert to zero-based. |
| 247 | if (HasImplicitThisParam) { |
| 248 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 249 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 250 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 251 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | --Idx; |
| 255 | } |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 260 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 261 | /// If not emit an error and return false. If the argument is an identifier it |
| 262 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 263 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 264 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 265 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 266 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 267 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 268 | if (Attr.isArgIdent(ArgNum)) { |
| 269 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 270 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 271 | << Attr.getName() << AANT_ArgumentString |
| 272 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 273 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 274 | Str = Loc->Ident->getName(); |
| 275 | if (ArgLocation) |
| 276 | *ArgLocation = Loc->Loc; |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | // Now check for an actual string literal. |
| 281 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 282 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 283 | if (ArgLocation) |
| 284 | *ArgLocation = ArgExpr->getLocStart(); |
| 285 | |
| 286 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 287 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 288 | << Attr.getName() << AANT_ArgumentString; |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | Str = Literal->getString(); |
| 293 | return true; |
| 294 | } |
| 295 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 296 | /// \brief Applies the given attribute to the Decl without performing any |
| 297 | /// additional semantic checking. |
| 298 | template <typename AttrType> |
| 299 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 300 | const AttributeList &Attr) { |
| 301 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 302 | Attr.getAttributeSpellingListIndex())); |
| 303 | } |
| 304 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 305 | /// \brief Check if the passed-in expression is of type int or bool. |
| 306 | static bool isIntOrBool(Expr *Exp) { |
| 307 | QualType QT = Exp->getType(); |
| 308 | return QT->isBooleanType() || QT->isIntegerType(); |
| 309 | } |
| 310 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 311 | |
| 312 | // Check to see if the type is a smart pointer of some kind. We assume |
| 313 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 314 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 315 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 316 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 317 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 318 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 319 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 320 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 321 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 322 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 323 | return false; |
| 324 | |
| 325 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 328 | /// \brief Check if passed in Decl is a pointer type. |
| 329 | /// Note that this function may produce an error message. |
| 330 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 331 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 332 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 333 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 334 | QualType QT = vd->getType(); |
| 335 | if (QT->isAnyPointerType()) |
| 336 | return true; |
| 337 | |
| 338 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 339 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 340 | // (We don't want to force template instantiation if we can avoid it, |
| 341 | // since that would alter the order in which templates are instantiated.) |
| 342 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 343 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 344 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 345 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 346 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 347 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 348 | |
| 349 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 350 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 354 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 355 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 356 | static const RecordType *getRecordType(QualType QT) { |
| 357 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 358 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 359 | |
| 360 | // Now check if we point to record type. |
| 361 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 362 | return PT->getPointeeType()->getAs<RecordType>(); |
| 363 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 364 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 367 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 368 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 369 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 370 | if (!RT) |
| 371 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 372 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 373 | // Don't check for the capability if the class hasn't been defined yet. |
DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 374 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 375 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 376 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 377 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 378 | // FIXME -- Check the type that the smart pointer points to. |
| 379 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 380 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 381 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 382 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 383 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 384 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 385 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 386 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 387 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 388 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 389 | CXXBasePaths BPaths(false, false); |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 390 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P, |
| 391 | void *) { |
| 392 | return BS->getType()->getAs<RecordType>() |
| 393 | ->getDecl()->hasAttr<CapabilityAttr>(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 394 | }, nullptr, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 395 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 396 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 400 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 401 | const auto *TD = Ty->getAs<TypedefType>(); |
| 402 | if (!TD) |
| 403 | return false; |
| 404 | |
| 405 | TypedefNameDecl *TN = TD->getDecl(); |
| 406 | if (!TN) |
| 407 | return false; |
| 408 | |
| 409 | return TN->hasAttr<CapabilityAttr>(); |
| 410 | } |
| 411 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 412 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 413 | if (checkTypedefTypeForCapability(Ty)) |
| 414 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 415 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 416 | if (checkRecordTypeForCapability(S, Ty)) |
| 417 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 418 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | |
| 422 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 423 | // Capability expressions are simple expressions involving the boolean logic |
| 424 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 425 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 426 | // is a capability or not. |
| 427 | |
| 428 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 429 | return typeHasCapability(S, E->getType()); |
| 430 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 431 | return isCapabilityExpr(S, E->getSubExpr()); |
| 432 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 433 | return isCapabilityExpr(S, E->getSubExpr()); |
| 434 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 435 | if (E->getOpcode() == UO_LNot) |
| 436 | return isCapabilityExpr(S, E->getSubExpr()); |
| 437 | return false; |
| 438 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 439 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 440 | return isCapabilityExpr(S, E->getLHS()) && |
| 441 | isCapabilityExpr(S, E->getRHS()); |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 448 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 449 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 450 | /// \param Sidx The attribute argument index to start checking with. |
| 451 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 452 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 453 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 454 | const AttributeList &Attr, |
| 455 | SmallVectorImpl<Expr *> &Args, |
| 456 | int Sidx = 0, |
| 457 | bool ParamIdxOk = false) { |
| 458 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 459 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 460 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 461 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 462 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 463 | Args.push_back(ArgExp); |
| 464 | continue; |
| 465 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 466 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 467 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 468 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 469 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 470 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 471 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 472 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 473 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 474 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 475 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 476 | // We allow constant strings to be used as a placeholder for expressions |
| 477 | // that are not valid C++ syntax, but warn that they are ignored. |
| 478 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 479 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 480 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 481 | continue; |
| 482 | } |
| 483 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 484 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 485 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 486 | // A pointer to member expression of the form &MyClass::mu is treated |
| 487 | // specially -- we need to look at the type of the member. |
| 488 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 489 | if (UOp->getOpcode() == UO_AddrOf) |
| 490 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 491 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 492 | ArgTy = DRE->getDecl()->getType(); |
| 493 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 494 | // First see if we can just cast to record type, or pointer to record type. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 495 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 496 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 497 | // Now check if we index into a record type function param. |
| 498 | if(!RT && ParamIdxOk) { |
| 499 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 500 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 501 | if(FD && IL) { |
| 502 | unsigned int NumParams = FD->getNumParams(); |
| 503 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 504 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 505 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 506 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 507 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 508 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 509 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 510 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 511 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 515 | // If the type does not have a capability, see if the components of the |
| 516 | // expression have capabilities. This allows for writing C code where the |
| 517 | // capability may be on the type, and the expression is a capability |
| 518 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 519 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 520 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 521 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 522 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 523 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 524 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 527 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 528 | // Attribute Implementations |
| 529 | //===----------------------------------------------------------------------===// |
| 530 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 531 | // FIXME: All this manual attribute parsing code is gross. At the |
| 532 | // least add some helper functions to check most argument patterns (# |
| 533 | // and types of args). |
| 534 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 535 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 536 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 537 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 538 | return; |
| 539 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 540 | D->addAttr(::new (S.Context) |
| 541 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 542 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 545 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 546 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 547 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 548 | SmallVector<Expr*, 1> Args; |
| 549 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 550 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 551 | unsigned Size = Args.size(); |
| 552 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 554 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 556 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 557 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 560 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 561 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 562 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 563 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 564 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 565 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 566 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 569 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 570 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 571 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 572 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 573 | return; |
| 574 | |
| 575 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 576 | return; |
| 577 | |
| 578 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 579 | S.Context, Arg, |
| 580 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 583 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 584 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 585 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 586 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 587 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 588 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 589 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 590 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 591 | if (!QT->isDependentType()) { |
| 592 | const RecordType *RT = getRecordType(QT); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 593 | if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 594 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | << Attr.getName(); |
| 596 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 600 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 601 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 602 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 603 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 604 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 605 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 608 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 609 | const AttributeList &Attr) { |
| 610 | SmallVector<Expr*, 1> Args; |
| 611 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 612 | return; |
| 613 | |
| 614 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 615 | D->addAttr(::new (S.Context) |
| 616 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 617 | StartArg, Args.size(), |
| 618 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 621 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 622 | const AttributeList &Attr) { |
| 623 | SmallVector<Expr*, 1> Args; |
| 624 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 625 | return; |
| 626 | |
| 627 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 628 | D->addAttr(::new (S.Context) |
| 629 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 630 | StartArg, Args.size(), |
| 631 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 634 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 635 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 636 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 637 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 638 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 639 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 640 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 641 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 642 | } |
| 643 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 644 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 645 | const AttributeList &Attr) { |
| 646 | SmallVector<Expr*, 1> Args; |
| 647 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 648 | return; |
| 649 | |
| 650 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 651 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 652 | D->addAttr(::new (S.Context) |
| 653 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 654 | Attr.getAttributeSpellingListIndex())); |
| 655 | } |
| 656 | |
| 657 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 658 | const AttributeList &Attr) { |
| 659 | SmallVector<Expr*, 1> Args; |
| 660 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 661 | return; |
| 662 | |
| 663 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 664 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 665 | D->addAttr(::new (S.Context) |
| 666 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 667 | StartArg, Size, |
| 668 | Attr.getAttributeSpellingListIndex())); |
| 669 | } |
| 670 | |
| 671 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 672 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 673 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 674 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 675 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 676 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 677 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 678 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 679 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 680 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 681 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 685 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 686 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 687 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 690 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 691 | const AttributeList &Attr) { |
| 692 | SmallVector<Expr*, 2> Args; |
| 693 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 694 | return; |
| 695 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 696 | D->addAttr(::new (S.Context) |
| 697 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 698 | Attr.getArgAsExpr(0), |
| 699 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 700 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 703 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 704 | const AttributeList &Attr) { |
| 705 | SmallVector<Expr*, 2> Args; |
| 706 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 707 | return; |
| 708 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 709 | D->addAttr(::new (S.Context) |
| 710 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 711 | Attr.getArgAsExpr(0), |
| 712 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 713 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 716 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 717 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 718 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 719 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 720 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 721 | unsigned Size = Args.size(); |
| 722 | if (Size == 0) |
| 723 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 724 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 725 | D->addAttr(::new (S.Context) |
| 726 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 727 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 731 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 732 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 733 | return; |
| 734 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 735 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 736 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 737 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 738 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 739 | if (Size == 0) |
| 740 | return; |
| 741 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 742 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 743 | D->addAttr(::new (S.Context) |
| 744 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 745 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 748 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 749 | Expr *Cond = Attr.getArgAsExpr(0); |
| 750 | if (!Cond->isTypeDependent()) { |
| 751 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 752 | if (Converted.isInvalid()) |
| 753 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 754 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | StringRef Msg; |
| 758 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 759 | return; |
| 760 | |
| 761 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 762 | if (!Cond->isValueDependent() && |
| 763 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 764 | Diags)) { |
| 765 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 766 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 767 | S.Diag(Diags[I].first, Diags[I].second); |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | D->addAttr(::new (S.Context) |
| 772 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 773 | Attr.getAttributeSpellingListIndex())); |
| 774 | } |
| 775 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 776 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 777 | ConsumableAttr::ConsumedState DefaultState; |
| 778 | |
| 779 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 780 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 781 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 782 | DefaultState)) { |
| 783 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 784 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 785 | return; |
| 786 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 787 | } else { |
| 788 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 789 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 790 | return; |
| 791 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 792 | |
| 793 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 794 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 795 | Attr.getAttributeSpellingListIndex())); |
| 796 | } |
| 797 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 798 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 799 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 800 | const AttributeList &Attr) { |
| 801 | ASTContext &CurrContext = S.getASTContext(); |
| 802 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 803 | |
| 804 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 805 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 806 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 807 | RD->getNameAsString(); |
| 808 | |
| 809 | return false; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | return true; |
| 814 | } |
| 815 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 816 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 817 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 818 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 819 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 820 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 821 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 822 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 823 | return; |
| 824 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 825 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 826 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 827 | CallableWhenAttr::ConsumedState CallableState; |
| 828 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 829 | StringRef StateString; |
| 830 | SourceLocation Loc; |
| 831 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 832 | return; |
| 833 | |
| 834 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 835 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 836 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 837 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 838 | return; |
| 839 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 840 | |
| 841 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 842 | } |
| 843 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 844 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 845 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 846 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 847 | } |
| 848 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 849 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 850 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 851 | const AttributeList &Attr) { |
| 852 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 853 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 854 | ParamTypestateAttr::ConsumedState ParamState; |
| 855 | |
| 856 | if (Attr.isArgIdent(0)) { |
| 857 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 858 | StringRef StateString = Ident->Ident->getName(); |
| 859 | |
| 860 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 861 | ParamState)) { |
| 862 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 863 | << Attr.getName() << StateString; |
| 864 | return; |
| 865 | } |
| 866 | } else { |
| 867 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 868 | Attr.getName() << AANT_ArgumentIdentifier; |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | // FIXME: This check is currently being done in the analysis. It can be |
| 873 | // enabled here only after the parser propagates attributes at |
| 874 | // template specialization definition, not declaration. |
| 875 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 876 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 877 | // |
| 878 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 879 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 880 | // ReturnType.getAsString(); |
| 881 | // return; |
| 882 | //} |
| 883 | |
| 884 | D->addAttr(::new (S.Context) |
| 885 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 886 | Attr.getAttributeSpellingListIndex())); |
| 887 | } |
| 888 | |
| 889 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 890 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 891 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 892 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 893 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 894 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 895 | |
| 896 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 897 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 898 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 899 | ReturnState)) { |
| 900 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 901 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 902 | return; |
| 903 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 904 | } else { |
| 905 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 906 | Attr.getName() << AANT_ArgumentIdentifier; |
| 907 | return; |
| 908 | } |
| 909 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 910 | // FIXME: This check is currently being done in the analysis. It can be |
| 911 | // enabled here only after the parser propagates attributes at |
| 912 | // template specialization definition, not declaration. |
| 913 | //QualType ReturnType; |
| 914 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 915 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 916 | // ReturnType = Param->getType(); |
| 917 | // |
| 918 | //} else if (const CXXConstructorDecl *Constructor = |
| 919 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 920 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 921 | // |
| 922 | //} else { |
| 923 | // |
| 924 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 925 | //} |
| 926 | // |
| 927 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 928 | // |
| 929 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 930 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 931 | // ReturnType.getAsString(); |
| 932 | // return; |
| 933 | //} |
| 934 | |
| 935 | D->addAttr(::new (S.Context) |
| 936 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 937 | Attr.getAttributeSpellingListIndex())); |
| 938 | } |
| 939 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 940 | |
| 941 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 942 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 943 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 944 | |
| 945 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 946 | return; |
| 947 | |
| 948 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 949 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 950 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 951 | StringRef Param = Ident->Ident->getName(); |
| 952 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 953 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 954 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 955 | return; |
| 956 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 957 | } else { |
| 958 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 959 | Attr.getName() << AANT_ArgumentIdentifier; |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | D->addAttr(::new (S.Context) |
| 964 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 965 | Attr.getAttributeSpellingListIndex())); |
| 966 | } |
| 967 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 968 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 969 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 970 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 971 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 972 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 973 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 974 | return; |
| 975 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 976 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 977 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 978 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 979 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 980 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 981 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 982 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 983 | return; |
| 984 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 985 | } else { |
| 986 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 987 | Attr.getName() << AANT_ArgumentIdentifier; |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 992 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 993 | Attr.getAttributeSpellingListIndex())); |
| 994 | } |
| 995 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 996 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 997 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 998 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 999 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1002 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1003 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1004 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1005 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1006 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1007 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1008 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1009 | if (!FD->getType()->isDependentType() && |
| 1010 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1011 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1012 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1013 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1014 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1015 | FD->addAttr(::new (S.Context) |
| 1016 | PackedAttr(Attr.getRange(), S.Context, |
| 1017 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1018 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1019 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1022 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1023 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1024 | // variables or properties of Objective-C classes. The outlet must also |
| 1025 | // have an object reference type. |
| 1026 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1027 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1028 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1029 | << Attr.getName() << VD->getType() << 0; |
| 1030 | return false; |
| 1031 | } |
| 1032 | } |
| 1033 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1034 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1035 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1036 | << Attr.getName() << PD->getType() << 1; |
| 1037 | return false; |
| 1038 | } |
| 1039 | } |
| 1040 | else { |
| 1041 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1042 | return false; |
| 1043 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1044 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1045 | return true; |
| 1046 | } |
| 1047 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1048 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1049 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1050 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1051 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1052 | D->addAttr(::new (S.Context) |
| 1053 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1054 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1057 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1058 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1059 | |
| 1060 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1061 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1062 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1063 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1064 | return; |
| 1065 | } |
| 1066 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1067 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1068 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1069 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1070 | ParsedType PT; |
| 1071 | |
| 1072 | if (Attr.hasParsedType()) |
| 1073 | PT = Attr.getTypeArg(); |
| 1074 | else { |
| 1075 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1076 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1077 | if (!PT) { |
| 1078 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1079 | return; |
| 1080 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1081 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1082 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1083 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1084 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1085 | if (!QTLoc) |
| 1086 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1087 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1088 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1089 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1090 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1091 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1092 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1093 | S.Diag(Attr.getLoc(), |
| 1094 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1095 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1096 | return; |
| 1097 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1098 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1099 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1100 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1101 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1104 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1105 | if (const RecordType *UT = T->getAsUnionType()) |
| 1106 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1107 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1108 | for (const auto *I : UD->fields()) { |
| 1109 | QualType QT = I->getType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1110 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1111 | T = QT; |
| 1112 | return; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1118 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1119 | SourceRange R, bool isReturnValue = false) { |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1120 | T = T.getNonReferenceType(); |
| 1121 | possibleTransparentUnionPointerType(T); |
| 1122 | |
| 1123 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1124 | S.Diag(Attr.getLoc(), |
| 1125 | isReturnValue ? diag::warn_attribute_return_pointers_only |
| 1126 | : diag::warn_attribute_pointers_only) |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1127 | << Attr.getName() << R; |
| 1128 | return false; |
| 1129 | } |
| 1130 | return true; |
| 1131 | } |
| 1132 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1133 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1134 | SmallVector<unsigned, 8> NonNullArgs; |
| 1135 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1136 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1137 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1138 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1139 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Is the function argument a pointer type? |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1142 | // FIXME: Should also highlight argument in decl in the diagnostic. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1143 | if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
| 1144 | Ex->getSourceRange())) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1145 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1146 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1147 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1148 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1149 | |
| 1150 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1151 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1152 | if (NonNullArgs.empty()) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1153 | for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) { |
| 1154 | QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1155 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1156 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1157 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1158 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1159 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1160 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1161 | if (NonNullArgs.empty()) { |
| 1162 | // Warn the trivial case only if attribute is not coming from a |
| 1163 | // macro instantiation. |
| 1164 | if (Attr.getLoc().isFileID()) |
| 1165 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1166 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1167 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1168 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1169 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1170 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1171 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1172 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1173 | D->addAttr(::new (S.Context) |
| 1174 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1175 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1178 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1179 | const AttributeList &Attr) { |
| 1180 | if (Attr.getNumArgs() > 0) { |
| 1181 | if (D->getFunctionType()) { |
| 1182 | handleNonNullAttr(S, D, Attr); |
| 1183 | } else { |
| 1184 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1185 | << D->getSourceRange(); |
| 1186 | } |
| 1187 | return; |
| 1188 | } |
| 1189 | |
| 1190 | // Is the argument a pointer type? |
| 1191 | if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange())) |
| 1192 | return; |
| 1193 | |
| 1194 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1195 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1196 | Attr.getAttributeSpellingListIndex())); |
| 1197 | } |
| 1198 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1199 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1200 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1201 | QualType ResultType = getFunctionOrMethodResultType(D); |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1202 | if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(), |
| 1203 | /* isReturnValue */ true)) |
| 1204 | return; |
| 1205 | |
| 1206 | D->addAttr(::new (S.Context) |
| 1207 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1208 | Attr.getAttributeSpellingListIndex())); |
| 1209 | } |
| 1210 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1211 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1212 | // This attribute must be applied to a function declaration. The first |
| 1213 | // argument to the attribute must be an identifier, the name of the resource, |
| 1214 | // for example: malloc. The following arguments must be argument indexes, the |
| 1215 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1216 | // 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] | 1217 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1218 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1219 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1220 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1221 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1222 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1223 | return; |
| 1224 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1225 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1226 | // Figure out our Kind. |
| 1227 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1228 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1229 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1230 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1231 | // Check arguments. |
| 1232 | switch (K) { |
| 1233 | case OwnershipAttr::Takes: |
| 1234 | case OwnershipAttr::Holds: |
| 1235 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1236 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1237 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1238 | return; |
| 1239 | } |
| 1240 | break; |
| 1241 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1242 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1243 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1244 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1247 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1250 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1251 | |
| 1252 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1253 | StringRef ModuleName = Module->getName(); |
| 1254 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1255 | ModuleName.size() > 4) { |
| 1256 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1257 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1258 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1259 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1260 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1261 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1262 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1263 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1264 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1265 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1266 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1267 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1268 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1269 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1270 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1271 | case OwnershipAttr::Takes: |
| 1272 | case OwnershipAttr::Holds: |
| 1273 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1274 | Err = 0; |
| 1275 | break; |
| 1276 | case OwnershipAttr::Returns: |
| 1277 | if (!T->isIntegerType()) |
| 1278 | Err = 1; |
| 1279 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1280 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1281 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1282 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1283 | << Ex->getSourceRange(); |
| 1284 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1285 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1288 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1289 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1290 | // with a different index too. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1291 | if (I->getOwnKind() != K && I->args_end() != |
| 1292 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1293 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1294 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1295 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1298 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | unsigned* start = OwnershipArgs.data(); |
| 1302 | unsigned size = OwnershipArgs.size(); |
| 1303 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1304 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1305 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1306 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1307 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1310 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1311 | // Check the attribute arguments. |
| 1312 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1313 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1314 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1315 | return; |
| 1316 | } |
| 1317 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1318 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1319 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1320 | // gcc rejects |
| 1321 | // class c { |
| 1322 | // static int a __attribute__((weakref ("v2"))); |
| 1323 | // static int b() __attribute__((weakref ("f3"))); |
| 1324 | // }; |
| 1325 | // and ignores the attributes of |
| 1326 | // void f(void) { |
| 1327 | // static int a __attribute__((weakref ("v2"))); |
| 1328 | // } |
| 1329 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1330 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1331 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1332 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1333 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1334 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | // The GCC manual says |
| 1338 | // |
| 1339 | // At present, a declaration to which `weakref' is attached can only |
| 1340 | // be `static'. |
| 1341 | // |
| 1342 | // It also says |
| 1343 | // |
| 1344 | // Without a TARGET, |
| 1345 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1346 | // equivalent to `weak'. |
| 1347 | // |
| 1348 | // gcc 4.4.1 will accept |
| 1349 | // int a7 __attribute__((weakref)); |
| 1350 | // as |
| 1351 | // int a7 __attribute__((weak)); |
| 1352 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1353 | // it if this behaviour is actually used. |
| 1354 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1355 | // GCC rejects |
| 1356 | // static ((alias ("y"), weakref)). |
| 1357 | // Should we? How to check that weakref is before or after alias? |
| 1358 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1359 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1360 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1361 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1362 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1363 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1364 | // GCC will accept anything as the argument of weakref. Should we |
| 1365 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1366 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1367 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1368 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1369 | D->addAttr(::new (S.Context) |
| 1370 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1371 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1374 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1375 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1376 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1377 | return; |
| 1378 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1379 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1380 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1381 | return; |
| 1382 | } |
| 1383 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1384 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1385 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1386 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1387 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1390 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1391 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1392 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1393 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1394 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1395 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1399 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1400 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1401 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1402 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1403 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1406 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1407 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1408 | StringRef Model; |
| 1409 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1410 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1411 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1412 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1413 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1414 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1415 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1416 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1417 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1418 | return; |
| 1419 | } |
| 1420 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1421 | D->addAttr(::new (S.Context) |
| 1422 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1423 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1426 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1427 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1428 | QualType RetTy = FD->getReturnType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1429 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1430 | D->addAttr(::new (S.Context) |
| 1431 | MallocAttr(Attr.getRange(), S.Context, |
| 1432 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1433 | return; |
| 1434 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1437 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1440 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1441 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1442 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1443 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | } |
| 1446 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1447 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1448 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1451 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1452 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1453 | |
| 1454 | if (S.CheckNoReturnAttr(attr)) return; |
| 1455 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1456 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1457 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1458 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1459 | return; |
| 1460 | } |
| 1461 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1462 | D->addAttr(::new (S.Context) |
| 1463 | NoReturnAttr(attr.getRange(), S.Context, |
| 1464 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1468 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1469 | attr.setInvalid(); |
| 1470 | return true; |
| 1471 | } |
| 1472 | |
| 1473 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1476 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1477 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1478 | |
| 1479 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1480 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1481 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1482 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1483 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1484 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1485 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1486 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1487 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1488 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1489 | return; |
| 1490 | } |
| 1491 | } |
| 1492 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1493 | D->addAttr(::new (S.Context) |
| 1494 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1495 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1498 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1499 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1500 | /* |
| 1501 | Returning a Vector Class in Registers |
| 1502 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1503 | According to the PPU ABI specifications, a class with a single member of |
| 1504 | vector type is returned in memory when used as the return value of a function. |
| 1505 | This results in inefficient code when implementing vector classes. To return |
| 1506 | the value in a single vector register, add the vecreturn attribute to the |
| 1507 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1508 | |
| 1509 | Example: |
| 1510 | |
| 1511 | struct Vector |
| 1512 | { |
| 1513 | __vector float xyzw; |
| 1514 | } __attribute__((vecreturn)); |
| 1515 | |
| 1516 | Vector Add(Vector lhs, Vector rhs) |
| 1517 | { |
| 1518 | Vector result; |
| 1519 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1520 | return result; // This will be returned in a register |
| 1521 | } |
| 1522 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1523 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1524 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1525 | return; |
| 1526 | } |
| 1527 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1528 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1529 | int count = 0; |
| 1530 | |
| 1531 | if (!isa<CXXRecordDecl>(record)) { |
| 1532 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1533 | return; |
| 1534 | } |
| 1535 | |
| 1536 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1537 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1538 | return; |
| 1539 | } |
| 1540 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1541 | for (const auto *I : record->fields()) { |
| 1542 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1543 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1544 | return; |
| 1545 | } |
| 1546 | count++; |
| 1547 | } |
| 1548 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1549 | D->addAttr(::new (S.Context) |
| 1550 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1551 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1554 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1555 | const AttributeList &Attr) { |
| 1556 | if (isa<ParmVarDecl>(D)) { |
| 1557 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1558 | // parameter of a function declaration or lambda. |
| 1559 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1560 | S.Diag(Attr.getLoc(), |
| 1561 | diag::err_carries_dependency_param_not_function_decl); |
| 1562 | return; |
| 1563 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1564 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1565 | |
| 1566 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1567 | Attr.getRange(), S.Context, |
| 1568 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1571 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1572 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1573 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1574 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1575 | return; |
| 1576 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1577 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1578 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1579 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1580 | return; |
| 1581 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1582 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1583 | D->addAttr(::new (S.Context) |
| 1584 | UsedAttr(Attr.getRange(), S.Context, |
| 1585 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1588 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1589 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1590 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1591 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1592 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1593 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1594 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1595 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1596 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1597 | if (Attr.getNumArgs() > 0 && |
| 1598 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1599 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1600 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1601 | D->addAttr(::new (S.Context) |
| 1602 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1603 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1606 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1607 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1608 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1609 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1610 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1611 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1612 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1613 | |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1614 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1615 | if (Attr.getNumArgs() > 0 && |
| 1616 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1617 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1618 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1619 | D->addAttr(::new (S.Context) |
| 1620 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1621 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1624 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1625 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1626 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1627 | unsigned NumArgs = Attr.getNumArgs(); |
| 1628 | if (NumArgs > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1629 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1630 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1631 | return; |
| 1632 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1635 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1636 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1637 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1638 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1639 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1640 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1643 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1644 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1645 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 1646 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 1647 | << Attr.getName() << Attr.getRange(); |
| 1648 | return; |
| 1649 | } |
| 1650 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1651 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1652 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1653 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1656 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1657 | IdentifierInfo *Platform, |
| 1658 | VersionTuple Introduced, |
| 1659 | VersionTuple Deprecated, |
| 1660 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1661 | StringRef PlatformName |
| 1662 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1663 | if (PlatformName.empty()) |
| 1664 | PlatformName = Platform->getName(); |
| 1665 | |
| 1666 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1667 | // of these steps are needed). |
| 1668 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1669 | !(Introduced <= Deprecated)) { |
| 1670 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1671 | << 1 << PlatformName << Deprecated.getAsString() |
| 1672 | << 0 << Introduced.getAsString(); |
| 1673 | return true; |
| 1674 | } |
| 1675 | |
| 1676 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1677 | !(Introduced <= Obsoleted)) { |
| 1678 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1679 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1680 | << 0 << Introduced.getAsString(); |
| 1681 | return true; |
| 1682 | } |
| 1683 | |
| 1684 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1685 | !(Deprecated <= Obsoleted)) { |
| 1686 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1687 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1688 | << 1 << Deprecated.getAsString(); |
| 1689 | return true; |
| 1690 | } |
| 1691 | |
| 1692 | return false; |
| 1693 | } |
| 1694 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1695 | /// \brief Check whether the two versions match. |
| 1696 | /// |
| 1697 | /// If either version tuple is empty, then they are assumed to match. If |
| 1698 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1699 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1700 | bool BeforeIsOkay) { |
| 1701 | if (X.empty() || Y.empty()) |
| 1702 | return true; |
| 1703 | |
| 1704 | if (X == Y) |
| 1705 | return true; |
| 1706 | |
| 1707 | if (BeforeIsOkay && X < Y) |
| 1708 | return true; |
| 1709 | |
| 1710 | return false; |
| 1711 | } |
| 1712 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1713 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1714 | IdentifierInfo *Platform, |
| 1715 | VersionTuple Introduced, |
| 1716 | VersionTuple Deprecated, |
| 1717 | VersionTuple Obsoleted, |
| 1718 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1719 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1720 | bool Override, |
| 1721 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1722 | VersionTuple MergedIntroduced = Introduced; |
| 1723 | VersionTuple MergedDeprecated = Deprecated; |
| 1724 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1725 | bool FoundAny = false; |
| 1726 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1727 | if (D->hasAttrs()) { |
| 1728 | AttrVec &Attrs = D->getAttrs(); |
| 1729 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1730 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1731 | if (!OldAA) { |
| 1732 | ++i; |
| 1733 | continue; |
| 1734 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1735 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1736 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1737 | if (OldPlatform != Platform) { |
| 1738 | ++i; |
| 1739 | continue; |
| 1740 | } |
| 1741 | |
| 1742 | FoundAny = true; |
| 1743 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1744 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1745 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1746 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1747 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1748 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1749 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1750 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1751 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1752 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1753 | if (Override) { |
| 1754 | int Which = -1; |
| 1755 | VersionTuple FirstVersion; |
| 1756 | VersionTuple SecondVersion; |
| 1757 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1758 | Which = 0; |
| 1759 | FirstVersion = OldIntroduced; |
| 1760 | SecondVersion = Introduced; |
| 1761 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1762 | Which = 1; |
| 1763 | FirstVersion = Deprecated; |
| 1764 | SecondVersion = OldDeprecated; |
| 1765 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1766 | Which = 2; |
| 1767 | FirstVersion = Obsoleted; |
| 1768 | SecondVersion = OldObsoleted; |
| 1769 | } |
| 1770 | |
| 1771 | if (Which == -1) { |
| 1772 | Diag(OldAA->getLocation(), |
| 1773 | diag::warn_mismatched_availability_override_unavail) |
| 1774 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1775 | } else { |
| 1776 | Diag(OldAA->getLocation(), |
| 1777 | diag::warn_mismatched_availability_override) |
| 1778 | << Which |
| 1779 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1780 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1781 | } |
| 1782 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1783 | } else { |
| 1784 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1785 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1786 | } |
| 1787 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1788 | Attrs.erase(Attrs.begin() + i); |
| 1789 | --e; |
| 1790 | continue; |
| 1791 | } |
| 1792 | |
| 1793 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1794 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1795 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1796 | |
| 1797 | if (MergedIntroduced2.empty()) |
| 1798 | MergedIntroduced2 = OldIntroduced; |
| 1799 | if (MergedDeprecated2.empty()) |
| 1800 | MergedDeprecated2 = OldDeprecated; |
| 1801 | if (MergedObsoleted2.empty()) |
| 1802 | MergedObsoleted2 = OldObsoleted; |
| 1803 | |
| 1804 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1805 | MergedIntroduced2, MergedDeprecated2, |
| 1806 | MergedObsoleted2)) { |
| 1807 | Attrs.erase(Attrs.begin() + i); |
| 1808 | --e; |
| 1809 | continue; |
| 1810 | } |
| 1811 | |
| 1812 | MergedIntroduced = MergedIntroduced2; |
| 1813 | MergedDeprecated = MergedDeprecated2; |
| 1814 | MergedObsoleted = MergedObsoleted2; |
| 1815 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1816 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | if (FoundAny && |
| 1820 | MergedIntroduced == Introduced && |
| 1821 | MergedDeprecated == Deprecated && |
| 1822 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1823 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1824 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1825 | // Only create a new attribute if !Override, but we want to do |
| 1826 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1827 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1828 | MergedDeprecated, MergedObsoleted) && |
| 1829 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1830 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1831 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1832 | Obsoleted, IsUnavailable, Message, |
| 1833 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1834 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1835 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1838 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1839 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1840 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1841 | return; |
| 1842 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1843 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1844 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1845 | IdentifierInfo *II = Platform->Ident; |
| 1846 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1847 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1848 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1849 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1850 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1851 | if (!ND) { |
| 1852 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1853 | return; |
| 1854 | } |
| 1855 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1856 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1857 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1858 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1859 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1860 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1861 | if (const StringLiteral *SE = |
| 1862 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1863 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1864 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1865 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1866 | Introduced.Version, |
| 1867 | Deprecated.Version, |
| 1868 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1869 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1870 | /*Override=*/false, |
| 1871 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1872 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1873 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1876 | template <class T> |
| 1877 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1878 | typename T::VisibilityType value, |
| 1879 | unsigned attrSpellingListIndex) { |
| 1880 | T *existingAttr = D->getAttr<T>(); |
| 1881 | if (existingAttr) { |
| 1882 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1883 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1884 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1885 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1886 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1887 | D->dropAttr<T>(); |
| 1888 | } |
| 1889 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1890 | } |
| 1891 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1892 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1893 | VisibilityAttr::VisibilityType Vis, |
| 1894 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1895 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1896 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1899 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1900 | TypeVisibilityAttr::VisibilityType Vis, |
| 1901 | unsigned AttrSpellingListIndex) { |
| 1902 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1903 | AttrSpellingListIndex); |
| 1904 | } |
| 1905 | |
| 1906 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1907 | bool isTypeVisibility) { |
| 1908 | // Visibility attributes don't mean anything on a typedef. |
| 1909 | if (isa<TypedefNameDecl>(D)) { |
| 1910 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 1911 | << Attr.getName(); |
| 1912 | return; |
| 1913 | } |
| 1914 | |
| 1915 | // 'type_visibility' can only go on a type or namespace. |
| 1916 | if (isTypeVisibility && |
| 1917 | !(isa<TagDecl>(D) || |
| 1918 | isa<ObjCInterfaceDecl>(D) || |
| 1919 | isa<NamespaceDecl>(D))) { |
| 1920 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 1921 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 1922 | return; |
| 1923 | } |
| 1924 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 1925 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1926 | StringRef TypeStr; |
| 1927 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1928 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1929 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1930 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1931 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1932 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1933 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1934 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1935 | return; |
| 1936 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1937 | |
| 1938 | // Complain about attempts to use protected visibility on targets |
| 1939 | // (like Darwin) that don't support it. |
| 1940 | if (type == VisibilityAttr::Protected && |
| 1941 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 1942 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 1943 | type = VisibilityAttr::Default; |
| 1944 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1945 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1946 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1947 | clang::Attr *newAttr; |
| 1948 | if (isTypeVisibility) { |
| 1949 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 1950 | (TypeVisibilityAttr::VisibilityType) type, |
| 1951 | Index); |
| 1952 | } else { |
| 1953 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 1954 | } |
| 1955 | if (newAttr) |
| 1956 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1959 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 1960 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1961 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1962 | if (!Attr.isArgIdent(0)) { |
| 1963 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 1964 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1965 | return; |
| 1966 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1967 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1968 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1969 | ObjCMethodFamilyAttr::FamilyKind F; |
| 1970 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 1971 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 1972 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1973 | return; |
| 1974 | } |
| 1975 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1976 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 1977 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1978 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1979 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1980 | // Ignore the attribute. |
| 1981 | return; |
| 1982 | } |
| 1983 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1984 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1985 | S.Context, F, |
| 1986 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1989 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1990 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1991 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 1992 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1993 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1994 | return; |
| 1995 | } |
| 1996 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 1997 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1998 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 1999 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2000 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2001 | return; |
| 2002 | } |
| 2003 | } |
| 2004 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2005 | // It is okay to include this attribute on properties, e.g.: |
| 2006 | // |
| 2007 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2008 | // |
| 2009 | // In this case it follows tradition and suppresses an error in the above |
| 2010 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2011 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2012 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2013 | D->addAttr(::new (S.Context) |
| 2014 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2015 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2018 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2019 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2020 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2021 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2022 | return; |
| 2023 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2024 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2025 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2026 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2027 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2028 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2029 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2030 | return; |
| 2031 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2032 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2033 | D->addAttr(::new (S.Context) |
| 2034 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2035 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2038 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2039 | // check the attribute arguments. |
| 2040 | if (Attr.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 2041 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 2042 | << Attr.getName() << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2043 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2046 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2047 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2048 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2049 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2050 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2051 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2052 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2053 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2054 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2055 | return; |
| 2056 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2057 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2058 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2059 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2060 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2061 | return; |
| 2062 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2063 | |
| 2064 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2067 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2068 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2069 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2070 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2071 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2072 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2073 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2074 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2075 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2076 | return; |
| 2077 | } |
| 2078 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2079 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2080 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2081 | // FIXME: This error message could be improved, it would be nice |
| 2082 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2083 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2084 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2085 | return; |
| 2086 | } |
| 2087 | } |
| 2088 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2089 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2090 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2091 | if (isa<FunctionNoProtoType>(FT)) { |
| 2092 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2093 | return; |
| 2094 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2095 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2096 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2097 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2098 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2099 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2100 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2101 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2102 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2103 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2104 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2105 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2106 | if (!BD->isVariadic()) { |
| 2107 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2108 | return; |
| 2109 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2110 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2111 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2112 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2113 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2114 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2115 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2116 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2117 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2118 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2119 | return; |
| 2120 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2121 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2122 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2123 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2124 | return; |
| 2125 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2126 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2127 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2128 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2129 | return; |
| 2130 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2131 | D->addAttr(::new (S.Context) |
| 2132 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2133 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2136 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2137 | if (D->getFunctionType() && |
| 2138 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2139 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2140 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2141 | return; |
| 2142 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2143 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2144 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2145 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2146 | << Attr.getName() << 1; |
| 2147 | return; |
| 2148 | } |
| 2149 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2150 | D->addAttr(::new (S.Context) |
| 2151 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2152 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2155 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2156 | // weak_import only applies to variable & function declarations. |
| 2157 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2158 | if (!D->canBeWeakImported(isDef)) { |
| 2159 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2160 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2161 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2162 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2163 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2164 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2165 | // Nothing to warn about here. |
| 2166 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2167 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2168 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2169 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2170 | return; |
| 2171 | } |
| 2172 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2173 | D->addAttr(::new (S.Context) |
| 2174 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2175 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2178 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2179 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2180 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2181 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2182 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2183 | for (unsigned i = 0; i < 3; ++i) { |
| 2184 | const Expr *E = Attr.getArgAsExpr(i); |
| 2185 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2186 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2187 | if (WGSize[i] == 0) { |
| 2188 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2189 | << Attr.getName() << E->getSourceRange(); |
| 2190 | return; |
| 2191 | } |
| 2192 | } |
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 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2195 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2196 | Existing->getYDim() == WGSize[1] && |
| 2197 | Existing->getZDim() == WGSize[2])) |
| 2198 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2199 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2200 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2201 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2202 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2205 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2206 | if (!Attr.hasParsedType()) { |
| 2207 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2208 | << Attr.getName() << 1; |
| 2209 | return; |
| 2210 | } |
| 2211 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2212 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2213 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2214 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2215 | |
| 2216 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2217 | (ParmType->isBooleanType() || |
| 2218 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2219 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2220 | << ParmType; |
| 2221 | return; |
| 2222 | } |
| 2223 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2224 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2225 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2226 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2227 | return; |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2232 | ParmTSI, |
| 2233 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2236 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2237 | StringRef Name, |
| 2238 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2239 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2240 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2241 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2242 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2243 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2244 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2245 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2246 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2247 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2250 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2251 | // Make sure that there is a string literal as the sections's single |
| 2252 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2253 | StringRef Str; |
| 2254 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2255 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2256 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2258 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2259 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2260 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2261 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2262 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2263 | return; |
| 2264 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2266 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2267 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2268 | if (NewAttr) |
| 2269 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2272 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2273 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2274 | VarDecl *VD = cast<VarDecl>(D); |
| 2275 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2276 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2277 | return; |
| 2278 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2279 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2280 | Expr *E = Attr.getArgAsExpr(0); |
| 2281 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2282 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2283 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2284 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2285 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2286 | // will warn the user. |
| 2287 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2288 | if (DRE->hasQualifier()) |
| 2289 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2290 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2291 | NI = DRE->getNameInfo(); |
| 2292 | if (!FD) { |
| 2293 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2294 | << NI.getName(); |
| 2295 | return; |
| 2296 | } |
| 2297 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2298 | if (ULE->hasExplicitTemplateArgs()) |
| 2299 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2300 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2301 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2302 | if (!FD) { |
| 2303 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2304 | << NI.getName(); |
| 2305 | if (ULE->getType() == S.Context.OverloadTy) |
| 2306 | S.NoteAllOverloadCandidates(ULE); |
| 2307 | return; |
| 2308 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2309 | } else { |
| 2310 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2311 | return; |
| 2312 | } |
| 2313 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2314 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2315 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2316 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2317 | return; |
| 2318 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2319 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2320 | // We're currently more strict than GCC about what function types we accept. |
| 2321 | // If this ever proves to be a problem it should be easy to fix. |
| 2322 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2323 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2324 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2325 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2326 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2327 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2328 | return; |
| 2329 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2330 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2331 | D->addAttr(::new (S.Context) |
| 2332 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2333 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2336 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2337 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2338 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2339 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2340 | uint64_t Idx; |
| 2341 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2342 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2343 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2344 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2345 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2346 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2347 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2348 | if (not_nsstring_type && |
| 2349 | !isCFStringType(Ty, S.Context) && |
| 2350 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2351 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2352 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2353 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2354 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2355 | << IdxExpr->getSourceRange(); |
| 2356 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2357 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2358 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2359 | if (!isNSStringType(Ty, S.Context) && |
| 2360 | !isCFStringType(Ty, S.Context) && |
| 2361 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2362 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2363 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2364 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2365 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2366 | << IdxExpr->getSourceRange(); |
| 2367 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2370 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2371 | // because that has corrected for the implicit this parameter, and is zero- |
| 2372 | // based. The attribute expects what the user wrote explicitly. |
| 2373 | llvm::APSInt Val; |
| 2374 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2375 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2376 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2377 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2378 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2381 | enum FormatAttrKind { |
| 2382 | CFStringFormat, |
| 2383 | NSStringFormat, |
| 2384 | StrftimeFormat, |
| 2385 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2386 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2387 | InvalidFormat |
| 2388 | }; |
| 2389 | |
| 2390 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2391 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2392 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2393 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2394 | // Check for formats that get handled specially. |
| 2395 | .Case("NSString", NSStringFormat) |
| 2396 | .Case("CFString", CFStringFormat) |
| 2397 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2398 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2399 | // Otherwise, check for supported formats. |
| 2400 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2401 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2402 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2403 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2404 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2405 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2408 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2409 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2410 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2411 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2412 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2413 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2414 | return; |
| 2415 | } |
| 2416 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2417 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2418 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2419 | Attr.setInvalid(); |
| 2420 | return; |
| 2421 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2422 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2423 | if (S.Context.getAsArrayType(T)) |
| 2424 | T = S.Context.getBaseElementType(T); |
| 2425 | if (!T->getAs<RecordType>()) { |
| 2426 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2427 | Attr.setInvalid(); |
| 2428 | return; |
| 2429 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2430 | |
| 2431 | Expr *E = Attr.getArgAsExpr(0); |
| 2432 | uint32_t prioritynum; |
| 2433 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2434 | Attr.setInvalid(); |
| 2435 | return; |
| 2436 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2437 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2438 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2439 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2440 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2441 | Attr.setInvalid(); |
| 2442 | return; |
| 2443 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2444 | D->addAttr(::new (S.Context) |
| 2445 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2446 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2449 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2450 | IdentifierInfo *Format, int FormatIdx, |
| 2451 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2452 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2453 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2454 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 2455 | if (F->getType() == Format && |
| 2456 | F->getFormatIdx() == FormatIdx && |
| 2457 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2458 | // If we don't have a valid location for this attribute, adopt the |
| 2459 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2460 | if (F->getLocation().isInvalid()) |
| 2461 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2462 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2463 | } |
| 2464 | } |
| 2465 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2466 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2467 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2470 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2471 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2472 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2473 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2474 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2475 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2476 | return; |
| 2477 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2478 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2479 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2480 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2481 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2482 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2483 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2484 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2485 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2486 | |
| 2487 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2488 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2489 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2490 | // If we've modified the string name, we need a new identifier for it. |
| 2491 | II = &S.Context.Idents.get(Format); |
| 2492 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2493 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2494 | // Check for supported formats. |
| 2495 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2496 | |
| 2497 | if (Kind == IgnoredFormat) |
| 2498 | return; |
| 2499 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2500 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2501 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2502 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2503 | return; |
| 2504 | } |
| 2505 | |
| 2506 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2507 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2508 | uint32_t Idx; |
| 2509 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2510 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2511 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2512 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2513 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2514 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2515 | return; |
| 2516 | } |
| 2517 | |
| 2518 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2519 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2520 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2521 | if (HasImplicitThisParam) { |
| 2522 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2523 | S.Diag(Attr.getLoc(), |
| 2524 | diag::err_format_attribute_implicit_this_format_string) |
| 2525 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2526 | return; |
| 2527 | } |
| 2528 | ArgIdx--; |
| 2529 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2531 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2532 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2533 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2534 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2535 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2536 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2537 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2538 | return; |
| 2539 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2540 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2541 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2542 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2543 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2544 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2545 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2546 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2547 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2548 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2549 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2550 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2551 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2552 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2553 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2554 | return; |
| 2555 | } |
| 2556 | |
| 2557 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2558 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2559 | uint32_t FirstArg; |
| 2560 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2561 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2562 | |
| 2563 | // check if the function is variadic if the 3rd argument non-zero |
| 2564 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2565 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2566 | ++NumArgs; // +1 for ... |
| 2567 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2568 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2569 | return; |
| 2570 | } |
| 2571 | } |
| 2572 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2573 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2574 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2575 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2576 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2577 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2578 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2582 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2583 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2584 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2585 | return; |
| 2586 | } |
| 2587 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2588 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2589 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2590 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2591 | if (NewAttr) |
| 2592 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2595 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2596 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2597 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2598 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2599 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2600 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2601 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2602 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2603 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2604 | |
| 2605 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2606 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2607 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2608 | return; |
| 2609 | } |
| 2610 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2611 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2612 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2613 | diag::warn_transparent_union_attribute_not_definition); |
| 2614 | return; |
| 2615 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2616 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2617 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2618 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2619 | if (Field == FieldEnd) { |
| 2620 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2621 | return; |
| 2622 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2623 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2624 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2625 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2626 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2627 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2628 | diag::warn_transparent_union_attribute_floating) |
| 2629 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2630 | return; |
| 2631 | } |
| 2632 | |
| 2633 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2634 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2635 | for (; Field != FieldEnd; ++Field) { |
| 2636 | QualType FieldType = Field->getType(); |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2637 | // FIXME: this isn't fully correct; we also need to test whether the |
| 2638 | // members of the union would all have the same calling convention as the |
| 2639 | // first member of the union. Checking just the size and alignment isn't |
| 2640 | // sufficient (consider structs passed on the stack instead of in registers |
| 2641 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2642 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2643 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2644 | // Warn if we drop the attribute. |
| 2645 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2646 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2647 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2648 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2649 | diag::warn_transparent_union_attribute_field_size_align) |
| 2650 | << isSize << Field->getDeclName() << FieldBits; |
| 2651 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2652 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2653 | diag::note_transparent_union_first_field_size_align) |
| 2654 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2655 | return; |
| 2656 | } |
| 2657 | } |
| 2658 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2659 | RD->addAttr(::new (S.Context) |
| 2660 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2661 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2664 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2665 | // Make sure that there is a string literal as the annotation's single |
| 2666 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2667 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2668 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2669 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2670 | |
| 2671 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2672 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 2673 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 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, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2692 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 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, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2790 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 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. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2820 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2821 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2822 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2823 | if (I->isAlignmentDependent()) |
| 2824 | return; |
| 2825 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2826 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2827 | Align = std::max(Align, I->getAlignment(Context)); |
| 2828 | } |
| 2829 | |
| 2830 | if (AlignasAttr && Align) { |
| 2831 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 2832 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 2833 | if (NaturalAlign > RequestedAlign) |
| 2834 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 2835 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 2836 | } |
| 2837 | } |
| 2838 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2839 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2840 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2841 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 2842 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 2843 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2844 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 2845 | // have to wait until the record is defined to catch any mismatches. |
| 2846 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 2847 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2848 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2849 | // The unspecified model never matches what a definition could need. |
| 2850 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 2851 | return false; |
| 2852 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2853 | if (BestCase) { |
| 2854 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 2855 | return false; |
| 2856 | } else { |
| 2857 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 2858 | return false; |
| 2859 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2860 | |
| 2861 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 2862 | << 0 /*definition*/; |
| 2863 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 2864 | << RD->getNameAsString(); |
| 2865 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2868 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2869 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2870 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2871 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2872 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2873 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2874 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2875 | // This attribute isn't documented, but glibc uses it. It changes |
| 2876 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2877 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 2878 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 2879 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2880 | return; |
| 2881 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2882 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2883 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 2884 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2885 | |
| 2886 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2887 | if (Str.startswith("__") && Str.endswith("__")) |
| 2888 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2889 | |
| 2890 | unsigned DestWidth = 0; |
| 2891 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2892 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2893 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2894 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2895 | switch (Str[0]) { |
| 2896 | case 'Q': DestWidth = 8; break; |
| 2897 | case 'H': DestWidth = 16; break; |
| 2898 | case 'S': DestWidth = 32; break; |
| 2899 | case 'D': DestWidth = 64; break; |
| 2900 | case 'X': DestWidth = 96; break; |
| 2901 | case 'T': DestWidth = 128; break; |
| 2902 | } |
| 2903 | if (Str[1] == 'F') { |
| 2904 | IntegerMode = false; |
| 2905 | } else if (Str[1] == 'C') { |
| 2906 | IntegerMode = false; |
| 2907 | ComplexMode = true; |
| 2908 | } else if (Str[1] != 'I') { |
| 2909 | DestWidth = 0; |
| 2910 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2911 | break; |
| 2912 | case 4: |
| 2913 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2914 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2915 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2916 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2917 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2918 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2919 | break; |
| 2920 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2921 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2922 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2923 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2924 | case 11: |
| 2925 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 2926 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2927 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2931 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2932 | OldTy = TD->getUnderlyingType(); |
| 2933 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2934 | OldTy = VD->getType(); |
| 2935 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2936 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 2937 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2938 | return; |
| 2939 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2940 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2941 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2942 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2943 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2944 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2945 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2946 | } else if (ComplexMode) { |
| 2947 | if (!OldTy->isComplexType()) |
| 2948 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2949 | } else { |
| 2950 | if (!OldTy->isFloatingType()) |
| 2951 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2952 | } |
| 2953 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2954 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2955 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2956 | // FIXME: Make sure floating-point mappings are accurate |
| 2957 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2958 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2959 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2960 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | QualType NewTy; |
| 2964 | |
| 2965 | if (IntegerMode) |
| 2966 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 2967 | OldTy->isSignedIntegerType()); |
| 2968 | else |
| 2969 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 2970 | |
| 2971 | if (NewTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2972 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2973 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2976 | if (ComplexMode) { |
| 2977 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 2981 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 2982 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 2983 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2984 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 2985 | |
| 2986 | D->addAttr(::new (S.Context) |
| 2987 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 2988 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2989 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2990 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2991 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 2992 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2993 | if (!VD->hasGlobalStorage()) |
| 2994 | S.Diag(Attr.getLoc(), |
| 2995 | diag::warn_attribute_requires_functions_or_static_globals) |
| 2996 | << Attr.getName(); |
| 2997 | } else if (!isFunctionOrMethod(D)) { |
| 2998 | S.Diag(Attr.getLoc(), |
| 2999 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3000 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3001 | return; |
| 3002 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3003 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3004 | D->addAttr(::new (S.Context) |
| 3005 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3006 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3009 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 3010 | const AttributeList &Attr) { |
| 3011 | if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr)) |
| 3012 | return; |
| 3013 | |
| 3014 | D->addAttr(::new (S.Context) |
| 3015 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 3016 | Attr.getAttributeSpellingListIndex())); |
| 3017 | } |
| 3018 | |
| 3019 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 3020 | const AttributeList &Attr) { |
| 3021 | if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr)) |
| 3022 | return; |
| 3023 | |
| 3024 | D->addAttr(::new (S.Context) |
| 3025 | OptimizeNoneAttr(Attr.getRange(), S.Context, |
| 3026 | Attr.getAttributeSpellingListIndex())); |
| 3027 | } |
| 3028 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3029 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3030 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3031 | if (!FD->getReturnType()->isVoidType()) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3032 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| 3033 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
| 3034 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3035 | << FD->getType() |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 3036 | << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(), |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3037 | "void"); |
| 3038 | } else { |
| 3039 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3040 | << FD->getType(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3041 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3042 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3043 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3044 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3045 | D->addAttr(::new (S.Context) |
| 3046 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3047 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3050 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3051 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3052 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3053 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3054 | return; |
| 3055 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3056 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3057 | D->addAttr(::new (S.Context) |
| 3058 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3059 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3062 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3063 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3064 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3065 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3066 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3067 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3068 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3069 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3070 | return; |
| 3071 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3072 | if (!isa<ObjCMethodDecl>(D)) { |
| 3073 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3074 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3075 | return; |
| 3076 | } |
| 3077 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3078 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3079 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3080 | D->addAttr(::new (S.Context) |
| 3081 | FastCallAttr(Attr.getRange(), S.Context, |
| 3082 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3083 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3084 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3085 | D->addAttr(::new (S.Context) |
| 3086 | StdCallAttr(Attr.getRange(), S.Context, |
| 3087 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3088 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3089 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3090 | D->addAttr(::new (S.Context) |
| 3091 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3092 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3093 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3094 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3095 | D->addAttr(::new (S.Context) |
| 3096 | CDeclAttr(Attr.getRange(), S.Context, |
| 3097 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3098 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3099 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3100 | D->addAttr(::new (S.Context) |
| 3101 | PascalAttr(Attr.getRange(), S.Context, |
| 3102 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3103 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3104 | case AttributeList::AT_MSABI: |
| 3105 | D->addAttr(::new (S.Context) |
| 3106 | MSABIAttr(Attr.getRange(), S.Context, |
| 3107 | Attr.getAttributeSpellingListIndex())); |
| 3108 | return; |
| 3109 | case AttributeList::AT_SysVABI: |
| 3110 | D->addAttr(::new (S.Context) |
| 3111 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3112 | Attr.getAttributeSpellingListIndex())); |
| 3113 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3114 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3115 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3116 | switch (CC) { |
| 3117 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3118 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3119 | break; |
| 3120 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3121 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3122 | break; |
| 3123 | default: |
| 3124 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3127 | D->addAttr(::new (S.Context) |
| 3128 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3129 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3130 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3131 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3132 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3133 | D->addAttr(::new (S.Context) |
| 3134 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3135 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3136 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3137 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3138 | D->addAttr(::new (S.Context) |
| 3139 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3140 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3141 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3142 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3143 | default: |
| 3144 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3148 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3149 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3150 | if (attr.isInvalid()) |
| 3151 | return true; |
| 3152 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3153 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3154 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3155 | attr.setInvalid(); |
| 3156 | return true; |
| 3157 | } |
| 3158 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3159 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3160 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3161 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3162 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3163 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3164 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3165 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3166 | case AttributeList::AT_MSABI: |
| 3167 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3168 | CC_X86_64Win64; |
| 3169 | break; |
| 3170 | case AttributeList::AT_SysVABI: |
| 3171 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3172 | CC_C; |
| 3173 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3174 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3175 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3176 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3177 | attr.setInvalid(); |
| 3178 | return true; |
| 3179 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3180 | if (StrRef == "aapcs") { |
| 3181 | CC = CC_AAPCS; |
| 3182 | break; |
| 3183 | } else if (StrRef == "aapcs-vfp") { |
| 3184 | CC = CC_AAPCS_VFP; |
| 3185 | break; |
| 3186 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3187 | |
| 3188 | attr.setInvalid(); |
| 3189 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3190 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3191 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3192 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3193 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3194 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3197 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3198 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3199 | if (A == TargetInfo::CCCR_Warning) { |
| 3200 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3201 | |
| 3202 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3203 | if (FD) |
| 3204 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3205 | TargetInfo::CCMT_NonMember; |
| 3206 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3209 | return false; |
| 3210 | } |
| 3211 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3212 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3213 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3214 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3215 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3216 | return true; |
| 3217 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3218 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3219 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3220 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3221 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3222 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3223 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3224 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3225 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3226 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3227 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3230 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3231 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3232 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3233 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3234 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3237 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3238 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3239 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3240 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3241 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3242 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3245 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3248 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3249 | const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3250 | // check the attribute arguments. |
| 3251 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| 3252 | // FIXME: 0 is not okay. |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 3253 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3254 | << Attr.getName() << 2; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3255 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3256 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3257 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3258 | uint32_t MaxThreads, MinBlocks = 0; |
| 3259 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3260 | return; |
| 3261 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3262 | Attr.getArgAsExpr(1), |
| 3263 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3264 | return; |
| 3265 | |
| 3266 | D->addAttr(::new (S.Context) |
| 3267 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3268 | MaxThreads, MinBlocks, |
| 3269 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3272 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3273 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3274 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3275 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3276 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3277 | return; |
| 3278 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3279 | |
| 3280 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3281 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3282 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3283 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3284 | |
| 3285 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3286 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3287 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3288 | return; |
| 3289 | } |
| 3290 | |
| 3291 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3292 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3293 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3294 | return; |
| 3295 | |
| 3296 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3297 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3298 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3299 | return; |
| 3300 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3301 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3302 | if (IsPointer) { |
| 3303 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3304 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3305 | if (!BufferTy->isPointerType()) { |
| 3306 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3307 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3311 | D->addAttr(::new (S.Context) |
| 3312 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3313 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3314 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3318 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3319 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3320 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3321 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3322 | return; |
| 3323 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3324 | |
| 3325 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3326 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3327 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3328 | if (!isa<VarDecl>(D)) { |
| 3329 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3330 | << Attr.getName() << ExpectedVariable; |
| 3331 | return; |
| 3332 | } |
| 3333 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3334 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3335 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3336 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3337 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3338 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3339 | D->addAttr(::new (S.Context) |
| 3340 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3341 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3342 | Attr.getLayoutCompatible(), |
| 3343 | Attr.getMustBeNull(), |
| 3344 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3347 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3348 | // Checker-specific attribute handlers. |
| 3349 | //===----------------------------------------------------------------------===// |
| 3350 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3351 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3352 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3353 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3356 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3357 | return type->isDependentType() || |
| 3358 | type->isObjCObjectPointerType() || |
| 3359 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3360 | } |
| 3361 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3362 | return type->isDependentType() || |
| 3363 | type->isPointerType() || |
| 3364 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3367 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3368 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3369 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3370 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3371 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3372 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3373 | cf = false; |
| 3374 | } else { |
| 3375 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3376 | cf = true; |
| 3377 | } |
| 3378 | |
| 3379 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3380 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3381 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3386 | param->addAttr(::new (S.Context) |
| 3387 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3388 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3389 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3390 | param->addAttr(::new (S.Context) |
| 3391 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3392 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3395 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3396 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3397 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3398 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3399 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3400 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3401 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3402 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3403 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3404 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3405 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3406 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3407 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3408 | returnType = FD->getReturnType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3409 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3410 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3411 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3412 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3413 | return; |
| 3414 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3415 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3416 | bool typeOK; |
| 3417 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3418 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3419 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3420 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 3421 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 3422 | cf = false; |
| 3423 | break; |
| 3424 | |
| 3425 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3426 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3427 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3428 | cf = false; |
| 3429 | break; |
| 3430 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3431 | case AttributeList::AT_CFReturnsRetained: |
| 3432 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3433 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3434 | cf = true; |
| 3435 | break; |
| 3436 | } |
| 3437 | |
| 3438 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3439 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3440 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3441 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3442 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3443 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3444 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3445 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3446 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3447 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3448 | D->addAttr(::new (S.Context) |
| 3449 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3450 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3451 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3452 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3453 | D->addAttr(::new (S.Context) |
| 3454 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3455 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3456 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3457 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3458 | D->addAttr(::new (S.Context) |
| 3459 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3460 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3461 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3462 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3463 | D->addAttr(::new (S.Context) |
| 3464 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3465 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3466 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3467 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3468 | D->addAttr(::new (S.Context) |
| 3469 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3470 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3471 | return; |
| 3472 | }; |
| 3473 | } |
| 3474 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3475 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3476 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3477 | const int EP_ObjCMethod = 1; |
| 3478 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3479 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3480 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3481 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3482 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3483 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3484 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3485 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3486 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3487 | if (!resultType->isReferenceType() && |
| 3488 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3489 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3490 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3491 | << attr.getName() |
| 3492 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3493 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3494 | |
| 3495 | // Drop the attribute. |
| 3496 | return; |
| 3497 | } |
| 3498 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3499 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3500 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3501 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3504 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3505 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3506 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3507 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3508 | DeclContext *DC = method->getDeclContext(); |
| 3509 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3510 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3511 | << attr.getName() << 0; |
| 3512 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3513 | return; |
| 3514 | } |
| 3515 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3516 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3517 | << attr.getName() << 1; |
| 3518 | return; |
| 3519 | } |
| 3520 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3521 | method->addAttr(::new (S.Context) |
| 3522 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3523 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3526 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3527 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3528 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3529 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3530 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3531 | D->addAttr(::new (S.Context) |
| 3532 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3533 | Attr.getAttributeSpellingListIndex())); |
| 3534 | } |
| 3535 | |
| 3536 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3537 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3538 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3539 | return; |
| 3540 | |
| 3541 | D->addAttr(::new (S.Context) |
| 3542 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3543 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3546 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3547 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3548 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3549 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3550 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3551 | 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] | 3552 | return; |
| 3553 | } |
| 3554 | |
| 3555 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3556 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3557 | Attr.getAttributeSpellingListIndex())); |
| 3558 | } |
| 3559 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3560 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3561 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3562 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 3563 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3564 | if (!Parm) { |
| 3565 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3566 | return; |
| 3567 | } |
| 3568 | |
| 3569 | D->addAttr(::new (S.Context) |
| 3570 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3571 | Attr.getAttributeSpellingListIndex())); |
| 3572 | } |
| 3573 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3574 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3575 | const AttributeList &Attr) { |
| 3576 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3577 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3578 | if (!RelatedClass) { |
| 3579 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3580 | return; |
| 3581 | } |
| 3582 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3583 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3584 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3585 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3586 | D->addAttr(::new (S.Context) |
| 3587 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3588 | ClassMethod, InstanceMethod, |
| 3589 | Attr.getAttributeSpellingListIndex())); |
| 3590 | } |
| 3591 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3592 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3593 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 3594 | ObjCInterfaceDecl *IFace; |
| 3595 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
| 3596 | IFace = CatDecl->getClassInterface(); |
| 3597 | else |
| 3598 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3599 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3600 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3601 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3602 | Attr.getAttributeSpellingListIndex())); |
| 3603 | } |
| 3604 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3605 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3606 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3607 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3608 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3609 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3610 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3613 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3614 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3615 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3616 | QualType type = vd->getType(); |
| 3617 | |
| 3618 | if (!type->isDependentType() && |
| 3619 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3620 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3621 | << type; |
| 3622 | return; |
| 3623 | } |
| 3624 | |
| 3625 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3626 | |
| 3627 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3628 | // going to infer. |
| 3629 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3630 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3631 | |
| 3632 | switch (lifetime) { |
| 3633 | case Qualifiers::OCL_None: |
| 3634 | assert(type->isDependentType() && |
| 3635 | "didn't infer lifetime for non-dependent type?"); |
| 3636 | break; |
| 3637 | |
| 3638 | case Qualifiers::OCL_Weak: // meaningful |
| 3639 | case Qualifiers::OCL_Strong: // meaningful |
| 3640 | break; |
| 3641 | |
| 3642 | case Qualifiers::OCL_ExplicitNone: |
| 3643 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3644 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3645 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3646 | break; |
| 3647 | } |
| 3648 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3649 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3650 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3651 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3654 | //===----------------------------------------------------------------------===// |
| 3655 | // Microsoft specific attribute handlers. |
| 3656 | //===----------------------------------------------------------------------===// |
| 3657 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3658 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3659 | if (!S.LangOpts.CPlusPlus) { |
| 3660 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3661 | << Attr.getName() << AttributeLangSupport::C; |
| 3662 | return; |
| 3663 | } |
| 3664 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3665 | if (!isa<CXXRecordDecl>(D)) { |
| 3666 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3667 | << Attr.getName() << ExpectedClass; |
| 3668 | return; |
| 3669 | } |
| 3670 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3671 | StringRef StrRef; |
| 3672 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3673 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3674 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3675 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3676 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3677 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3678 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3679 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3680 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3681 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3682 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3683 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3684 | return; |
| 3685 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3686 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3687 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3688 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3689 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3690 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3691 | return; |
| 3692 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3693 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3694 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3695 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3696 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3697 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3698 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3699 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3700 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3703 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3704 | if (!S.LangOpts.CPlusPlus) { |
| 3705 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3706 | << Attr.getName() << AttributeLangSupport::C; |
| 3707 | return; |
| 3708 | } |
| 3709 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3710 | D, Attr.getRange(), /*BestCase=*/true, |
| 3711 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3712 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
| 3713 | if (IA) |
| 3714 | D->addAttr(IA); |
| 3715 | } |
| 3716 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 3717 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 3718 | const AttributeList &Attr) { |
| 3719 | VarDecl *VD = cast<VarDecl>(D); |
| 3720 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 3721 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 3722 | return; |
| 3723 | } |
| 3724 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 3725 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 3726 | return; |
| 3727 | } |
| 3728 | if (VD->hasLocalStorage()) { |
| 3729 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 3730 | return; |
| 3731 | } |
| 3732 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 3733 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3734 | } |
| 3735 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3736 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 3737 | const AttributeList &Attr) { |
| 3738 | // Check the attribute arguments. |
| 3739 | if (Attr.getNumArgs() > 1) { |
| 3740 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3741 | << Attr.getName() << 1; |
| 3742 | return; |
| 3743 | } |
| 3744 | |
| 3745 | StringRef Str; |
| 3746 | SourceLocation ArgLoc; |
| 3747 | |
| 3748 | if (Attr.getNumArgs() == 0) |
| 3749 | Str = ""; |
| 3750 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 3751 | return; |
| 3752 | |
| 3753 | ARMInterruptAttr::InterruptType Kind; |
| 3754 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 3755 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3756 | << Attr.getName() << Str << ArgLoc; |
| 3757 | return; |
| 3758 | } |
| 3759 | |
| 3760 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3761 | D->addAttr(::new (S.Context) |
| 3762 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 3763 | } |
| 3764 | |
| 3765 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 3766 | const AttributeList &Attr) { |
| 3767 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3768 | return; |
| 3769 | |
| 3770 | if (!Attr.isArgExpr(0)) { |
| 3771 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3772 | << AANT_ArgumentIntegerConstant; |
| 3773 | return; |
| 3774 | } |
| 3775 | |
| 3776 | // FIXME: Check for decl - it should be void ()(void). |
| 3777 | |
| 3778 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 3779 | llvm::APSInt NumParams(32); |
| 3780 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 3781 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3782 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3783 | << NumParamsExpr->getSourceRange(); |
| 3784 | return; |
| 3785 | } |
| 3786 | |
| 3787 | unsigned Num = NumParams.getLimitedValue(255); |
| 3788 | if ((Num & 1) || Num > 30) { |
| 3789 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3790 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 3791 | << NumParamsExpr->getSourceRange(); |
| 3792 | return; |
| 3793 | } |
| 3794 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3795 | D->addAttr(::new (S.Context) |
| 3796 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 3797 | Attr.getAttributeSpellingListIndex())); |
| 3798 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3802 | // Dispatch the interrupt attribute based on the current target. |
| 3803 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 3804 | handleMSP430InterruptAttr(S, D, Attr); |
| 3805 | else |
| 3806 | handleARMInterruptAttr(S, D, Attr); |
| 3807 | } |
| 3808 | |
| 3809 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 3810 | const AttributeList& Attr) { |
| 3811 | // If we try to apply it to a function pointer, don't warn, but don't |
| 3812 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 3813 | // special about calling a force_align_arg_pointer function. |
| 3814 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3815 | if (VD && VD->getType()->isFunctionPointerType()) |
| 3816 | return; |
| 3817 | // Also don't warn on function pointer typedefs. |
| 3818 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 3819 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 3820 | TD->getUnderlyingType()->isFunctionType())) |
| 3821 | return; |
| 3822 | // Attribute can only be applied to function types. |
| 3823 | if (!isa<FunctionDecl>(D)) { |
| 3824 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3825 | << Attr.getName() << /* function */0; |
| 3826 | return; |
| 3827 | } |
| 3828 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3829 | D->addAttr(::new (S.Context) |
| 3830 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 3831 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 3835 | unsigned AttrSpellingListIndex) { |
| 3836 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 3837 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3838 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3842 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3843 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3844 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3848 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3849 | DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); |
| 3850 | if (NewAttr) |
| 3851 | D->addAttr(NewAttr); |
| 3852 | } |
| 3853 | |
| 3854 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 3855 | unsigned AttrSpellingListIndex) { |
| 3856 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 3857 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3858 | D->dropAttr<DLLImportAttr>(); |
| 3859 | } |
| 3860 | |
| 3861 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3862 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3863 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3864 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3868 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3869 | DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); |
| 3870 | if (NewAttr) |
| 3871 | D->addAttr(NewAttr); |
| 3872 | } |
| 3873 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3874 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3875 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3876 | unsigned AttrSpellingListIndex, |
| 3877 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3878 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 3879 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3880 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3881 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 3882 | << 1 /*previous declaration*/; |
| 3883 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 3884 | D->dropAttr<MSInheritanceAttr>(); |
| 3885 | } |
| 3886 | |
| 3887 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 3888 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3889 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 3890 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3891 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3892 | } |
| 3893 | } else { |
| 3894 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 3895 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 3896 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3897 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3898 | } |
| 3899 | if (RD->getDescribedClassTemplate()) { |
| 3900 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 3901 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3902 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3907 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3910 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3911 | // The capability attributes take a single string parameter for the name of |
| 3912 | // the capability they represent. The lockable attribute does not take any |
| 3913 | // parameters. However, semantically, both attributes represent the same |
| 3914 | // concept, and so they use the same semantic attribute. Eventually, the |
| 3915 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 3916 | // |
| 3917 | // For backwards compatibility, any capability which has no specified string |
| 3918 | // literal will be considered a "mutex." |
| 3919 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3920 | SourceLocation LiteralLoc; |
| 3921 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 3922 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 3923 | return; |
| 3924 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 3925 | // Currently, there are only two names allowed for a capability: role and |
| 3926 | // mutex (case insensitive). Diagnose other capability names. |
| 3927 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 3928 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 3929 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3930 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 3931 | Attr.getAttributeSpellingListIndex())); |
| 3932 | } |
| 3933 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 3934 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 3935 | const AttributeList &Attr) { |
| 3936 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 3937 | Attr.getArgAsExpr(0), |
| 3938 | Attr.getAttributeSpellingListIndex())); |
| 3939 | } |
| 3940 | |
| 3941 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 3942 | const AttributeList &Attr) { |
| 3943 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 3944 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 3945 | return; |
| 3946 | |
| 3947 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 3948 | S.Context, |
| 3949 | Args.data(), Args.size(), |
| 3950 | Attr.getAttributeSpellingListIndex())); |
| 3951 | } |
| 3952 | |
| 3953 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 3954 | const AttributeList &Attr) { |
| 3955 | SmallVector<Expr*, 2> Args; |
| 3956 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 3957 | return; |
| 3958 | |
| 3959 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 3960 | S.Context, |
| 3961 | Attr.getArgAsExpr(0), |
| 3962 | Args.data(), |
| 3963 | Args.size(), |
| 3964 | Attr.getAttributeSpellingListIndex())); |
| 3965 | } |
| 3966 | |
| 3967 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 3968 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 3969 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 3970 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 3971 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 3972 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 3973 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 3974 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 3975 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3978 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 3979 | const AttributeList &Attr) { |
| 3980 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 3981 | return; |
| 3982 | |
| 3983 | // check that all arguments are lockable objects |
| 3984 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 3985 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 3986 | if (Args.empty()) |
| 3987 | return; |
| 3988 | |
| 3989 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 3990 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 3991 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 3992 | |
| 3993 | D->addAttr(RCA); |
| 3994 | } |
| 3995 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3996 | /// Handles semantic checking for features that are common to all attributes, |
| 3997 | /// such as checking whether a parameter was properly specified, or the correct |
| 3998 | /// number of arguments were passed, etc. |
| 3999 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4000 | const AttributeList &Attr) { |
| 4001 | // Several attributes carry different semantics than the parsing requires, so |
| 4002 | // those are opted out of the common handling. |
| 4003 | // |
| 4004 | // We also bail on unknown and ignored attributes because those are handled |
| 4005 | // as part of the target-specific handling logic. |
| 4006 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4007 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4008 | return false; |
| 4009 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4010 | // Check whether the attribute requires specific language extensions to be |
| 4011 | // enabled. |
| 4012 | if (!Attr.diagnoseLangOpts(S)) |
| 4013 | return true; |
| 4014 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4015 | // If there are no optional arguments, then checking for the argument count |
| 4016 | // is trivial. |
| 4017 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4018 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4019 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4020 | |
| 4021 | // Check whether the attribute appertains to the given subject. |
| 4022 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 4023 | return true; |
| 4024 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4025 | return false; |
| 4026 | } |
| 4027 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4028 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4029 | // Top Level Sema Entry Points |
| 4030 | //===----------------------------------------------------------------------===// |
| 4031 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4032 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4033 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4034 | /// silently ignore it if a GNU attribute. |
| 4035 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4036 | const AttributeList &Attr, |
| 4037 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4038 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4039 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4040 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4041 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4042 | // instead. |
| 4043 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4044 | return; |
| 4045 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4046 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 4047 | // which do not apply to the current target architecture are treated as |
| 4048 | // though they were unknown attributes. |
| 4049 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
| 4050 | !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4051 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 4052 | ? diag::warn_unhandled_ms_attribute_ignored |
| 4053 | : diag::warn_unknown_attribute_ignored) |
| 4054 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4055 | return; |
| 4056 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4057 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4058 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4059 | return; |
| 4060 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4061 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4062 | default: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4063 | // Type attributes are handled elsewhere; silently move on. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4064 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4065 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4066 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4067 | handleInterruptAttr(S, D, Attr); |
| 4068 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4069 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4070 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 4071 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4072 | case AttributeList::AT_DLLExport: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4073 | handleDLLExportAttr(S, D, Attr); |
| 4074 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4075 | case AttributeList::AT_DLLImport: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4076 | handleDLLImportAttr(S, D, Attr); |
| 4077 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4078 | case AttributeList::AT_Mips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4079 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); |
| 4080 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4081 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4082 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 4083 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 4084 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4085 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 4086 | break; |
| 4087 | case AttributeList::AT_IBOutlet: |
| 4088 | handleIBOutlet(S, D, Attr); |
| 4089 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4090 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4091 | handleIBOutletCollection(S, D, Attr); |
| 4092 | break; |
| 4093 | case AttributeList::AT_Alias: |
| 4094 | handleAliasAttr(S, D, Attr); |
| 4095 | break; |
| 4096 | case AttributeList::AT_Aligned: |
| 4097 | handleAlignedAttr(S, D, Attr); |
| 4098 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4099 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4100 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4101 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4102 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4103 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 4104 | break; |
| 4105 | case AttributeList::AT_TLSModel: |
| 4106 | handleTLSModelAttr(S, D, Attr); |
| 4107 | break; |
| 4108 | case AttributeList::AT_Annotate: |
| 4109 | handleAnnotateAttr(S, D, Attr); |
| 4110 | break; |
| 4111 | case AttributeList::AT_Availability: |
| 4112 | handleAvailabilityAttr(S, D, Attr); |
| 4113 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4114 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4115 | handleDependencyAttr(S, scope, D, Attr); |
| 4116 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4117 | case AttributeList::AT_Common: |
| 4118 | handleCommonAttr(S, D, Attr); |
| 4119 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4120 | case AttributeList::AT_CUDAConstant: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4121 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); |
| 4122 | break; |
| 4123 | case AttributeList::AT_Constructor: |
| 4124 | handleConstructorAttr(S, D, Attr); |
| 4125 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4126 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4127 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 4128 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4129 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4130 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4131 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4132 | case AttributeList::AT_Destructor: |
| 4133 | handleDestructorAttr(S, D, Attr); |
| 4134 | break; |
| 4135 | case AttributeList::AT_EnableIf: |
| 4136 | handleEnableIfAttr(S, D, Attr); |
| 4137 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4138 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4139 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4140 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4141 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4142 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4143 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4144 | case AttributeList::AT_OptimizeNone: |
| 4145 | handleOptimizeNoneAttr(S, D, Attr); |
| 4146 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 4147 | case AttributeList::AT_Flatten: |
| 4148 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 4149 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4150 | case AttributeList::AT_Format: |
| 4151 | handleFormatAttr(S, D, Attr); |
| 4152 | break; |
| 4153 | case AttributeList::AT_FormatArg: |
| 4154 | handleFormatArgAttr(S, D, Attr); |
| 4155 | break; |
| 4156 | case AttributeList::AT_CUDAGlobal: |
| 4157 | handleGlobalAttr(S, D, Attr); |
| 4158 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 4159 | case AttributeList::AT_CUDADevice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4160 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); |
| 4161 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4162 | case AttributeList::AT_CUDAHost: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4163 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); |
| 4164 | break; |
| 4165 | case AttributeList::AT_GNUInline: |
| 4166 | handleGNUInlineAttr(S, D, Attr); |
| 4167 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4168 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4169 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4170 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4171 | case AttributeList::AT_Malloc: |
| 4172 | handleMallocAttr(S, D, Attr); |
| 4173 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4174 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4175 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 4176 | break; |
| 4177 | case AttributeList::AT_Mode: |
| 4178 | handleModeAttr(S, D, Attr); |
| 4179 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4180 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4181 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 4182 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 4183 | case AttributeList::AT_NoSplitStack: |
| 4184 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 4185 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 4186 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4187 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 4188 | handleNonNullAttrParameter(S, PVD, Attr); |
| 4189 | else |
| 4190 | handleNonNullAttr(S, D, Attr); |
| 4191 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 4192 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4193 | handleReturnsNonNullAttr(S, D, Attr); |
| 4194 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4195 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4196 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 4197 | break; |
| 4198 | case AttributeList::AT_Ownership: |
| 4199 | handleOwnershipAttr(S, D, Attr); |
| 4200 | break; |
| 4201 | case AttributeList::AT_Cold: |
| 4202 | handleColdAttr(S, D, Attr); |
| 4203 | break; |
| 4204 | case AttributeList::AT_Hot: |
| 4205 | handleHotAttr(S, D, Attr); |
| 4206 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4207 | case AttributeList::AT_Naked: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4208 | handleSimpleAttribute<NakedAttr>(S, D, Attr); |
| 4209 | break; |
| 4210 | case AttributeList::AT_NoReturn: |
| 4211 | handleNoReturnAttr(S, D, Attr); |
| 4212 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4213 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4214 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 4215 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4216 | case AttributeList::AT_CUDAShared: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4217 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); |
| 4218 | break; |
| 4219 | case AttributeList::AT_VecReturn: |
| 4220 | handleVecReturnAttr(S, D, Attr); |
| 4221 | break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4222 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4223 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4224 | handleObjCOwnershipAttr(S, D, Attr); |
| 4225 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4226 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4227 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 4228 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4229 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4230 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4231 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 4232 | break; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4233 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4234 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4235 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 4236 | break; |
| 4237 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4238 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4239 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 4240 | break; |
| 4241 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4242 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4243 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 4244 | break; |
| 4245 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4246 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4247 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 4248 | break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4249 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4250 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4251 | handleObjCDesignatedInitializer(S, D, Attr); |
| 4252 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4253 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4254 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4255 | handleCFAuditedTransferAttr(S, D, Attr); |
| 4256 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4257 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4258 | handleCFUnknownTransferAttr(S, D, Attr); |
| 4259 | break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4260 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4261 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4262 | case AttributeList::AT_NSConsumed: |
| 4263 | handleNSConsumedAttr(S, D, Attr); |
| 4264 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4265 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4266 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 4267 | break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4268 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4269 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4270 | case AttributeList::AT_NSReturnsNotRetained: |
| 4271 | case AttributeList::AT_CFReturnsNotRetained: |
| 4272 | case AttributeList::AT_NSReturnsRetained: |
| 4273 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4274 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 4275 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4276 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4277 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 4278 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4279 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4280 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 4281 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4282 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4283 | handleVecTypeHint(S, D, Attr); |
| 4284 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4285 | |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4286 | case AttributeList::AT_InitPriority: |
| 4287 | handleInitPriorityAttr(S, D, Attr); |
| 4288 | break; |
| 4289 | |
| 4290 | case AttributeList::AT_Packed: |
| 4291 | handlePackedAttr(S, D, Attr); |
| 4292 | break; |
| 4293 | case AttributeList::AT_Section: |
| 4294 | handleSectionAttr(S, D, Attr); |
| 4295 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4296 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4297 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4298 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4299 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 4300 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 4301 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4302 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4303 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 4304 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4305 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 4306 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4307 | break; |
| 4308 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4309 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 4310 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 4311 | case AttributeList::AT_Unused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4312 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); |
| 4313 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4314 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4315 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 4316 | break; |
| 4317 | case AttributeList::AT_Used: |
| 4318 | handleUsedAttr(S, D, Attr); |
| 4319 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4320 | case AttributeList::AT_Visibility: |
| 4321 | handleVisibilityAttr(S, D, Attr, false); |
| 4322 | break; |
| 4323 | case AttributeList::AT_TypeVisibility: |
| 4324 | handleVisibilityAttr(S, D, Attr, true); |
| 4325 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4326 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4327 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 4328 | break; |
| 4329 | case AttributeList::AT_WarnUnusedResult: |
| 4330 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4331 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4332 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4333 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 4334 | break; |
| 4335 | case AttributeList::AT_WeakRef: |
| 4336 | handleWeakRefAttr(S, D, Attr); |
| 4337 | break; |
| 4338 | case AttributeList::AT_WeakImport: |
| 4339 | handleWeakImportAttr(S, D, Attr); |
| 4340 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4341 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4342 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4343 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4344 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4345 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 4346 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4347 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4348 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4349 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4350 | case AttributeList::AT_ObjCNSObject: |
| 4351 | handleObjCNSObject(S, D, Attr); |
| 4352 | break; |
| 4353 | case AttributeList::AT_Blocks: |
| 4354 | handleBlocksAttr(S, D, Attr); |
| 4355 | break; |
| 4356 | case AttributeList::AT_Sentinel: |
| 4357 | handleSentinelAttr(S, D, Attr); |
| 4358 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4359 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4360 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 4361 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4362 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4363 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 4364 | break; |
| 4365 | case AttributeList::AT_Cleanup: |
| 4366 | handleCleanupAttr(S, D, Attr); |
| 4367 | break; |
| 4368 | case AttributeList::AT_NoDebug: |
| 4369 | handleNoDebugAttr(S, D, Attr); |
| 4370 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 4371 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4372 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 4373 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4374 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4375 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 4376 | break; |
| 4377 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 4378 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 4379 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4380 | case AttributeList::AT_StdCall: |
| 4381 | case AttributeList::AT_CDecl: |
| 4382 | case AttributeList::AT_FastCall: |
| 4383 | case AttributeList::AT_ThisCall: |
| 4384 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4385 | case AttributeList::AT_MSABI: |
| 4386 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4387 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4388 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4389 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4390 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4391 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4392 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4393 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 4394 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4395 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4396 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); |
| 4397 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4398 | |
| 4399 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4400 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4401 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4402 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4403 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4404 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4405 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4406 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4407 | handleMSInheritanceAttr(S, D, Attr); |
| 4408 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4409 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4410 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 4411 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 4412 | case AttributeList::AT_Thread: |
| 4413 | handleDeclspecThreadAttr(S, D, Attr); |
| 4414 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4415 | |
| 4416 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4417 | case AttributeList::AT_AssertExclusiveLock: |
| 4418 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4419 | break; |
| 4420 | case AttributeList::AT_AssertSharedLock: |
| 4421 | handleAssertSharedLockAttr(S, D, Attr); |
| 4422 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4423 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4424 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 4425 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4426 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4427 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4428 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4429 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4430 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 4431 | break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4432 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4433 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4434 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4435 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4436 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4437 | break; |
| 4438 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4439 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4440 | break; |
| 4441 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4442 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4443 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4444 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4445 | handleGuardedByAttr(S, D, Attr); |
| 4446 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4447 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4448 | handlePtGuardedByAttr(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_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4451 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4452 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4453 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4454 | handleLockReturnedAttr(S, D, Attr); |
| 4455 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4456 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4457 | handleLocksExcludedAttr(S, D, Attr); |
| 4458 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4459 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4460 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4461 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4462 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4463 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4464 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4465 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4466 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4467 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4468 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4469 | // Capability analysis attributes. |
| 4470 | case AttributeList::AT_Capability: |
| 4471 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4472 | handleCapabilityAttr(S, D, Attr); |
| 4473 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4474 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4475 | handleRequiresCapabilityAttr(S, D, Attr); |
| 4476 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4477 | |
| 4478 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4479 | handleAssertCapabilityAttr(S, D, Attr); |
| 4480 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4481 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4482 | handleAcquireCapabilityAttr(S, D, Attr); |
| 4483 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4484 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4485 | handleReleaseCapabilityAttr(S, D, Attr); |
| 4486 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 4487 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4488 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 4489 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 4490 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4491 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4492 | case AttributeList::AT_Consumable: |
| 4493 | handleConsumableAttr(S, D, Attr); |
| 4494 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4495 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4496 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 4497 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4498 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 4499 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 4500 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4501 | case AttributeList::AT_CallableWhen: |
| 4502 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4503 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4504 | case AttributeList::AT_ParamTypestate: |
| 4505 | handleParamTypestateAttr(S, D, Attr); |
| 4506 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4507 | case AttributeList::AT_ReturnTypestate: |
| 4508 | handleReturnTypestateAttr(S, D, Attr); |
| 4509 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4510 | case AttributeList::AT_SetTypestate: |
| 4511 | handleSetTypestateAttr(S, D, Attr); |
| 4512 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4513 | case AttributeList::AT_TestTypestate: |
| 4514 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4515 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4516 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4517 | // Type safety attributes. |
| 4518 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4519 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4520 | break; |
| 4521 | case AttributeList::AT_TypeTagForDatatype: |
| 4522 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4523 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4528 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4529 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4530 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4531 | bool IncludeCXX11Attributes) { |
| 4532 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4533 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4534 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4535 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4536 | // GCC accepts |
| 4537 | // static int a9 __attribute__((weakref)); |
| 4538 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4539 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 4540 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 4541 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4542 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4543 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4544 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4545 | |
| 4546 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4547 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4548 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 4549 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4550 | D->setInvalidDecl(); |
| 4551 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4552 | if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
| 4553 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4554 | D->setInvalidDecl(); |
| 4555 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4556 | if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
| 4557 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4558 | D->setInvalidDecl(); |
| 4559 | } |
| 4560 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4563 | // Annotation attributes are the only attributes allowed after an access |
| 4564 | // specifier. |
| 4565 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4566 | const AttributeList *AttrList) { |
| 4567 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4568 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4569 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4570 | } else { |
| 4571 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4572 | return true; |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | return false; |
| 4577 | } |
| 4578 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4579 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4580 | /// contains any decl attributes that we should warn about. |
| 4581 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4582 | for ( ; A; A = A->getNext()) { |
| 4583 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4584 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4585 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4586 | |
| 4587 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4588 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4589 | << A->getName() << A->getRange(); |
| 4590 | } else { |
| 4591 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4592 | << A->getName() << A->getRange(); |
| 4593 | } |
| 4594 | } |
| 4595 | } |
| 4596 | |
| 4597 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4598 | /// used to build a declaration, complain about any decl attributes |
| 4599 | /// which might be lying around on it. |
| 4600 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4601 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4602 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4603 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4604 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4605 | } |
| 4606 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4607 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4608 | /// \#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] | 4609 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4610 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4611 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4612 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4613 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4614 | FunctionDecl *NewFD; |
| 4615 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4616 | // FIXME: Mangling? |
| 4617 | // FIXME: Is the qualifier info correct? |
| 4618 | // FIXME: Is the DeclContext correct? |
| 4619 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4620 | Loc, Loc, DeclarationName(II), |
| 4621 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4622 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4623 | FD->hasPrototype(), |
| 4624 | false/*isConstexprSpecified*/); |
| 4625 | NewD = NewFD; |
| 4626 | |
| 4627 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4628 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4629 | |
| 4630 | // Fake up parameter variables; they are declared as if this were |
| 4631 | // a typedef. |
| 4632 | QualType FDTy = FD->getType(); |
| 4633 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4634 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4635 | for (const auto &AI : FT->param_types()) { |
| 4636 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4637 | Param->setScopeInfo(0, Params.size()); |
| 4638 | Params.push_back(Param); |
| 4639 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4640 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4641 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4642 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4643 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4644 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4645 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4646 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4647 | if (VD->getQualifier()) { |
| 4648 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4649 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4650 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4651 | } |
| 4652 | return NewD; |
| 4653 | } |
| 4654 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4655 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4656 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4657 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4658 | if (W.getUsed()) return; // only do this once |
| 4659 | W.setUsed(true); |
| 4660 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4661 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4662 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4663 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 4664 | W.getLocation())); |
| 4665 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4666 | WeakTopLevelDecl.push_back(NewD); |
| 4667 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4668 | // to insert Decl at TU scope, sorry. |
| 4669 | DeclContext *SavedContext = CurContext; |
| 4670 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 4671 | NewD->setDeclContext(CurContext); |
| 4672 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4673 | PushOnScopeChains(NewD, S); |
| 4674 | CurContext = SavedContext; |
| 4675 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4676 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4677 | } |
| 4678 | } |
| 4679 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4680 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4681 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4682 | // have to do this. |
| 4683 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4684 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4685 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4686 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4687 | if (VD->isExternC()) |
| 4688 | ND = VD; |
| 4689 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4690 | if (FD->isExternC()) |
| 4691 | ND = FD; |
| 4692 | if (ND) { |
| 4693 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4694 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4695 | = WeakUndeclaredIdentifiers.find(Id); |
| 4696 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4697 | WeakInfo W = I->second; |
| 4698 | DeclApplyPragmaWeak(S, ND, W); |
| 4699 | WeakUndeclaredIdentifiers[Id] = W; |
| 4700 | } |
| 4701 | } |
| 4702 | } |
| 4703 | } |
| 4704 | } |
| 4705 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4706 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4707 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4708 | /// 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] | 4709 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4710 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4711 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4712 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4713 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4714 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4715 | // position to the decl itself. This handles cases like: |
| 4716 | // int *__attr__(x)** D; |
| 4717 | // when X is a decl attribute. |
| 4718 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4719 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4720 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4721 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4722 | // Finally, apply any attributes on the decl itself. |
| 4723 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4724 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4725 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4726 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4727 | /// Is the given declaration allowed to use a forbidden type? |
| 4728 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4729 | // Private ivars are always okay. Unfortunately, people don't |
| 4730 | // always properly make their ivars private, even in system headers. |
| 4731 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4732 | // Function declarations in sys headers will be marked unavailable. |
| 4733 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4734 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4735 | return false; |
| 4736 | |
| 4737 | // Require it to be declared in a system header. |
| 4738 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4739 | } |
| 4740 | |
| 4741 | /// Handle a delayed forbidden-type diagnostic. |
| 4742 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4743 | Decl *decl) { |
| 4744 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4745 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, |
| 4746 | "this system declaration uses an unsupported type", |
| 4747 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4748 | return; |
| 4749 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4750 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4751 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4752 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4753 | // kind of forbidden type messages on unavailable functions. |
| 4754 | if (FD->hasAttr<UnavailableAttr>() && |
| 4755 | diag.getForbiddenTypeDiagnostic() == |
| 4756 | diag::err_arc_array_param_no_ownership) { |
| 4757 | diag.Triggered = true; |
| 4758 | return; |
| 4759 | } |
| 4760 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4761 | |
| 4762 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4763 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4764 | diag.Triggered = true; |
| 4765 | } |
| 4766 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4767 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4768 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4769 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4770 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4771 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4772 | // When delaying diagnostics to run in the context of a parsed |
| 4773 | // declaration, we only want to actually emit anything if parsing |
| 4774 | // succeeds. |
| 4775 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4776 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4777 | // We emit all the active diagnostics in this pool or any of its |
| 4778 | // parents. In general, we'll get one pool for the decl spec |
| 4779 | // and a child pool for each declarator; in a decl group like: |
| 4780 | // deprecated_typedef foo, *bar, baz(); |
| 4781 | // only the declarator pops will be passed decls. This is correct; |
| 4782 | // we really do need to consider delayed diagnostics from the decl spec |
| 4783 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4784 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4785 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4786 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4787 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4788 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4789 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4790 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4791 | continue; |
| 4792 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4793 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4794 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4795 | case DelayedDiagnostic::Unavailable: |
| 4796 | // Don't bother giving deprecation/unavailable diagnostics if |
| 4797 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4798 | if (!decl->isInvalidDecl()) |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4799 | HandleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4800 | break; |
| 4801 | |
| 4802 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4803 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4804 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4805 | |
| 4806 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4807 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4808 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4809 | } |
| 4810 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4811 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4814 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4815 | /// been delayed in the current context instead of in the given pool. |
| 4816 | /// Essentially, this just moves them to the current pool. |
| 4817 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4818 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4819 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4820 | curPool->steal(pool); |
| 4821 | } |
| 4822 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4823 | static bool isDeclDeprecated(Decl *D) { |
| 4824 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4825 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4826 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4827 | // A category implicitly has the availability of the interface. |
| 4828 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4829 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4830 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4831 | return false; |
| 4832 | } |
| 4833 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4834 | static bool isDeclUnavailable(Decl *D) { |
| 4835 | do { |
| 4836 | if (D->isUnavailable()) |
| 4837 | return true; |
| 4838 | // A category implicitly has the availability of the interface. |
| 4839 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4840 | return CatD->getClassInterface()->isUnavailable(); |
| 4841 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4842 | return false; |
| 4843 | } |
| 4844 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4845 | static void |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4846 | DoEmitAvailabilityWarning(Sema &S, |
| 4847 | DelayedDiagnostic::DDKind K, |
| 4848 | Decl *Ctx, |
| 4849 | const NamedDecl *D, |
| 4850 | StringRef Message, |
| 4851 | SourceLocation Loc, |
| 4852 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame^] | 4853 | const ObjCPropertyDecl *ObjCProperty, |
| 4854 | bool ObjCPropertyAccess) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4855 | |
| 4856 | // Diagnostics for deprecated or unavailable. |
| 4857 | unsigned diag, diag_message, diag_fwdclass_message; |
| 4858 | |
| 4859 | // Matches 'diag::note_property_attribute' options. |
| 4860 | unsigned property_note_select; |
| 4861 | |
| 4862 | // Matches diag::note_availability_specified_here. |
| 4863 | unsigned available_here_select_kind; |
| 4864 | |
| 4865 | // Don't warn if our current context is deprecated or unavailable. |
| 4866 | switch (K) { |
| 4867 | case DelayedDiagnostic::Deprecation: |
| 4868 | if (isDeclDeprecated(Ctx)) |
| 4869 | return; |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame^] | 4870 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 4871 | : diag::warn_property_method_deprecated; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4872 | diag_message = diag::warn_deprecated_message; |
| 4873 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 4874 | property_note_select = /* deprecated */ 0; |
| 4875 | available_here_select_kind = /* deprecated */ 2; |
| 4876 | break; |
| 4877 | |
| 4878 | case DelayedDiagnostic::Unavailable: |
| 4879 | if (isDeclUnavailable(Ctx)) |
| 4880 | return; |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame^] | 4881 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 4882 | : diag::err_property_method_unavailable; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4883 | diag_message = diag::err_unavailable_message; |
| 4884 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 4885 | property_note_select = /* unavailable */ 1; |
| 4886 | available_here_select_kind = /* unavailable */ 0; |
| 4887 | break; |
| 4888 | |
| 4889 | default: |
| 4890 | llvm_unreachable("Neither a deprecation or unavailable kind"); |
| 4891 | } |
| 4892 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4893 | DeclarationName Name = D->getDeclName(); |
| 4894 | if (!Message.empty()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4895 | S.Diag(Loc, diag_message) << Name << Message; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4896 | if (ObjCProperty) |
| 4897 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4898 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4899 | } else if (!UnknownObjCClass) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4900 | S.Diag(Loc, diag) << Name; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4901 | if (ObjCProperty) |
| 4902 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4903 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4904 | } else { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4905 | S.Diag(Loc, diag_fwdclass_message) << Name; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4906 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4907 | } |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4908 | |
| 4909 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 4910 | << D << available_here_select_kind; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4913 | void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD, |
| 4914 | Decl *Ctx) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4915 | DD.Triggered = true; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4916 | DoEmitAvailabilityWarning(*this, |
| 4917 | (DelayedDiagnostic::DDKind) DD.Kind, |
| 4918 | Ctx, |
| 4919 | DD.getDeprecationDecl(), |
| 4920 | DD.getDeprecationMessage(), |
| 4921 | DD.Loc, |
| 4922 | DD.getUnknownObjCClass(), |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame^] | 4923 | DD.getObjCProperty(), false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4926 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 4927 | NamedDecl *D, StringRef Message, |
| 4928 | SourceLocation Loc, |
| 4929 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame^] | 4930 | const ObjCPropertyDecl *ObjCProperty, |
| 4931 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4932 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4933 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4934 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D, |
| 4935 | UnknownObjCClass, |
| 4936 | ObjCProperty, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame^] | 4937 | Message, |
| 4938 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4939 | return; |
| 4940 | } |
| 4941 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4942 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
| 4943 | DelayedDiagnostic::DDKind K; |
| 4944 | switch (AD) { |
| 4945 | case AD_Deprecation: |
| 4946 | K = DelayedDiagnostic::Deprecation; |
| 4947 | break; |
| 4948 | case AD_Unavailable: |
| 4949 | K = DelayedDiagnostic::Unavailable; |
| 4950 | break; |
| 4951 | } |
| 4952 | |
| 4953 | DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame^] | 4954 | UnknownObjCClass, ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4955 | } |