Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 21 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 22 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 34 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 35 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 36 | C, |
| 37 | Cpp, |
| 38 | ObjC |
| 39 | }; |
| 40 | } |
| 41 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // Helper functions |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 46 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 47 | /// type (function or function-typed variable) or an Objective-C |
| 48 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 49 | static bool isFunctionOrMethod(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 50 | return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 51 | } |
| 52 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 53 | /// Return true if the given decl has a declarator that should have |
| 54 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 55 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 56 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 58 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 61 | /// hasFunctionProto - Return true if the given decl has a argument |
| 62 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 63 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 64 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 65 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 66 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 67 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 71 | /// arguments. It is an error to call this on a K&R function (use |
| 72 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 73 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 74 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 75 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 76 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 77 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 81 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 82 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 83 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 84 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 85 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 87 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 90 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 91 | if (const FunctionType *FnTy = D->getFunctionType()) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 92 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 96 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 97 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 98 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 99 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 100 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 101 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 102 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 103 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 107 | static bool isInstanceMethod(const Decl *D) { |
| 108 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 109 | return MethodDecl->isInstance(); |
| 110 | return false; |
| 111 | } |
| 112 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 113 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 114 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 115 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 116 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 117 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 118 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 119 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 120 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 121 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 122 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 124 | // FIXME: Should we walk the chain of classes? |
| 125 | return ClsName == &Ctx.Idents.get("NSString") || |
| 126 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 127 | } |
| 128 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 129 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 130 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 131 | if (!PT) |
| 132 | return false; |
| 133 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 134 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 135 | if (!RT) |
| 136 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 137 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 138 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 139 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 140 | return false; |
| 141 | |
| 142 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 143 | } |
| 144 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 145 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 146 | // FIXME: Include the type in the argument list. |
| 147 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 148 | } |
| 149 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 150 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 151 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 152 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 153 | unsigned Num) { |
| 154 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 155 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 156 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 163 | /// \brief Check if the attribute has at least as many args as Num. May |
| 164 | /// output an error. |
| 165 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 166 | unsigned Num) { |
| 167 | if (getNumAttributeArgs(Attr) < Num) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 168 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) |
| 169 | << Attr.getName() << Num; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 176 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 177 | /// expression and return success or failure. May output an error. |
| 178 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 179 | const Expr *Expr, uint32_t &Val, |
| 180 | unsigned Idx = UINT_MAX) { |
| 181 | llvm::APSInt I(32); |
| 182 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 183 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 184 | if (Idx != UINT_MAX) |
| 185 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 186 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 187 | << Expr->getSourceRange(); |
| 188 | else |
| 189 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 190 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 191 | << Expr->getSourceRange(); |
| 192 | return false; |
| 193 | } |
| 194 | Val = (uint32_t)I.getZExtValue(); |
| 195 | return true; |
| 196 | } |
| 197 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 198 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 199 | /// declaration. Returns true if diagnosed. |
| 200 | template <typename AttrTy> |
| 201 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 202 | const AttributeList &Attr) { |
| 203 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 204 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 205 | << Attr.getName() << A; |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 211 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 212 | /// instance method D. May output an error. |
| 213 | /// |
| 214 | /// \returns true if IdxExpr is a valid index. |
| 215 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 216 | const AttributeList &Attr, |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 217 | unsigned AttrArgNum, |
| 218 | const Expr *IdxExpr, |
| 219 | uint64_t &Idx) |
| 220 | { |
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); |
| 228 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 229 | 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(); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 241 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
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 | |
| 364 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 365 | } |
| 366 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 367 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 368 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 369 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 370 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 371 | return RT->getDecl()->hasAttr<LockableAttr>(); |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 375 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 376 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 377 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 378 | QualType Ty) { |
| 379 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 380 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 381 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 382 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 383 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 384 | << Attr.getName() << Ty; |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 385 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 386 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 387 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 388 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 389 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 390 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 391 | |
| 392 | // Allow smart pointers to be used as lockable objects. |
| 393 | // FIXME -- Check the type that the smart pointer points to. |
| 394 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 395 | return; |
| 396 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 397 | // Check if the type is lockable. |
| 398 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 399 | if (RD->hasAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 400 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 401 | |
| 402 | // Else check if any base classes are lockable. |
| 403 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 404 | CXXBasePaths BPaths(false, false); |
| 405 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 406 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 407 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 408 | |
| 409 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 410 | << Attr.getName() << Ty; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 413 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 414 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 415 | /// \param Sidx The attribute argument index to start checking with. |
| 416 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 417 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 418 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 419 | const AttributeList &Attr, |
| 420 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 421 | int Sidx = 0, |
| 422 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 423 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 424 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 425 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 426 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 427 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 428 | Args.push_back(ArgExp); |
| 429 | continue; |
| 430 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 431 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 432 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 433 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 434 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 435 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 436 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 437 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 438 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 439 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 440 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 441 | // We allow constant strings to be used as a placeholder for expressions |
| 442 | // that are not valid C++ syntax, but warn that they are ignored. |
| 443 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 444 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 445 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 446 | continue; |
| 447 | } |
| 448 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 449 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 450 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 451 | // A pointer to member expression of the form &MyClass::mu is treated |
| 452 | // specially -- we need to look at the type of the member. |
| 453 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 454 | if (UOp->getOpcode() == UO_AddrOf) |
| 455 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 456 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 457 | ArgTy = DRE->getDecl()->getType(); |
| 458 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 459 | // First see if we can just cast to record type, or point to record type. |
| 460 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 461 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 462 | // Now check if we index into a record type function param. |
| 463 | if(!RT && ParamIdxOk) { |
| 464 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 465 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 466 | if(FD && IL) { |
| 467 | unsigned int NumParams = FD->getNumParams(); |
| 468 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 469 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 470 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 471 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 472 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 473 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 474 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 476 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 480 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 481 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 482 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 483 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 486 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 487 | // Attribute Implementations |
| 488 | //===----------------------------------------------------------------------===// |
| 489 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 490 | // FIXME: All this manual attribute parsing code is gross. At the |
| 491 | // least add some helper functions to check most argument patterns (# |
| 492 | // and types of args). |
| 493 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 494 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 495 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 496 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 497 | return; |
| 498 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 499 | D->addAttr(::new (S.Context) |
| 500 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 501 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 504 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 505 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 506 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 507 | SmallVector<Expr*, 1> Args; |
| 508 | // check that all arguments are lockable objects |
| 509 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 510 | unsigned Size = Args.size(); |
| 511 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 512 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 513 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 514 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 515 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 516 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 519 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 520 | Expr *Arg = 0; |
| 521 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 522 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 523 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 524 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 525 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 528 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 529 | const AttributeList &Attr) { |
| 530 | Expr *Arg = 0; |
| 531 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 532 | return; |
| 533 | |
| 534 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 535 | return; |
| 536 | |
| 537 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 538 | S.Context, Arg, |
| 539 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 542 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 543 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 544 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 545 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 546 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 547 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 548 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 549 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 550 | if (!QT->isDependentType()) { |
| 551 | const RecordType *RT = getRecordType(QT); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 552 | if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 553 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 554 | << Attr.getName(); |
| 555 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 559 | // Check that all arguments are lockable objects. |
| 560 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 561 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 562 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 563 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 564 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 567 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 568 | const AttributeList &Attr) { |
| 569 | SmallVector<Expr*, 1> Args; |
| 570 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 571 | return; |
| 572 | |
| 573 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 574 | D->addAttr(::new (S.Context) |
| 575 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 576 | StartArg, Args.size(), |
| 577 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 580 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 581 | const AttributeList &Attr) { |
| 582 | SmallVector<Expr*, 1> Args; |
| 583 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 584 | return; |
| 585 | |
| 586 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 587 | D->addAttr(::new (S.Context) |
| 588 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 589 | StartArg, Args.size(), |
| 590 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 593 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 594 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 595 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 596 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 597 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 598 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 599 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 600 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 603 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 604 | const AttributeList &Attr) { |
| 605 | SmallVector<Expr*, 1> Args; |
| 606 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 607 | return; |
| 608 | |
| 609 | unsigned Size = Args.size(); |
| 610 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 611 | D->addAttr(::new (S.Context) |
| 612 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 613 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 616 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 617 | const AttributeList &Attr) { |
| 618 | SmallVector<Expr*, 1> Args; |
| 619 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 620 | return; |
| 621 | |
| 622 | unsigned Size = Args.size(); |
| 623 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 624 | D->addAttr(::new (S.Context) |
| 625 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 626 | StartArg, Size, |
| 627 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 628 | } |
| 629 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 630 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 631 | const AttributeList &Attr) { |
| 632 | SmallVector<Expr*, 1> Args; |
| 633 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 634 | return; |
| 635 | |
| 636 | unsigned Size = Args.size(); |
| 637 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 638 | D->addAttr(::new (S.Context) |
| 639 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 640 | Attr.getAttributeSpellingListIndex())); |
| 641 | } |
| 642 | |
| 643 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 644 | const AttributeList &Attr) { |
| 645 | SmallVector<Expr*, 1> Args; |
| 646 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 647 | return; |
| 648 | |
| 649 | unsigned Size = Args.size(); |
| 650 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 651 | D->addAttr(::new (S.Context) |
| 652 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 653 | StartArg, Size, |
| 654 | Attr.getAttributeSpellingListIndex())); |
| 655 | } |
| 656 | |
| 657 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 658 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 659 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 660 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 661 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 662 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 663 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 664 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 665 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 666 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 667 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 671 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 672 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 673 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 676 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 677 | const AttributeList &Attr) { |
| 678 | SmallVector<Expr*, 2> Args; |
| 679 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 680 | return; |
| 681 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 682 | D->addAttr(::new (S.Context) |
| 683 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 684 | Attr.getArgAsExpr(0), |
| 685 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 686 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 689 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 690 | const AttributeList &Attr) { |
| 691 | SmallVector<Expr*, 2> Args; |
| 692 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 693 | return; |
| 694 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 695 | D->addAttr(::new (S.Context) |
| 696 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 697 | Attr.getArgAsExpr(0), |
| 698 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 699 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 702 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 703 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 704 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 705 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 706 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 707 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 708 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 709 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 710 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 711 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 712 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 713 | return true; |
| 714 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 715 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 716 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 717 | const AttributeList &Attr) { |
| 718 | SmallVector<Expr*, 1> Args; |
| 719 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 720 | return; |
| 721 | |
| 722 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 723 | D->addAttr(::new (S.Context) |
| 724 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 725 | StartArg, Args.size(), |
| 726 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 729 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 730 | const AttributeList &Attr) { |
| 731 | SmallVector<Expr*, 1> Args; |
| 732 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 733 | return; |
| 734 | |
| 735 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 736 | D->addAttr(::new (S.Context) |
| 737 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 738 | StartArg, Args.size(), |
| 739 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 742 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 743 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 744 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 745 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 746 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 747 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 748 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 749 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 750 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 751 | D->addAttr(::new (S.Context) |
| 752 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 753 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 757 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 758 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 759 | SmallVector<Expr*, 1> Args; |
| 760 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 761 | unsigned Size = Args.size(); |
| 762 | if (Size == 0) |
| 763 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 764 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 765 | D->addAttr(::new (S.Context) |
| 766 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 767 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 771 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 772 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 773 | return; |
| 774 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 775 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 776 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 777 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 778 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 779 | if (Size == 0) |
| 780 | return; |
| 781 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 782 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 783 | D->addAttr(::new (S.Context) |
| 784 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 785 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 788 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 789 | Expr *Cond = Attr.getArgAsExpr(0); |
| 790 | if (!Cond->isTypeDependent()) { |
| 791 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 792 | if (Converted.isInvalid()) |
| 793 | return; |
| 794 | Cond = Converted.take(); |
| 795 | } |
| 796 | |
| 797 | StringRef Msg; |
| 798 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 799 | return; |
| 800 | |
| 801 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 802 | if (!Cond->isValueDependent() && |
| 803 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 804 | Diags)) { |
| 805 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 806 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 807 | S.Diag(Diags[I].first, Diags[I].second); |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | D->addAttr(::new (S.Context) |
| 812 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 813 | Attr.getAttributeSpellingListIndex())); |
| 814 | } |
| 815 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 816 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 817 | ConsumableAttr::ConsumedState DefaultState; |
| 818 | |
| 819 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 820 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 821 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 822 | DefaultState)) { |
| 823 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 824 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 825 | return; |
| 826 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 827 | } else { |
| 828 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 829 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 830 | return; |
| 831 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 832 | |
| 833 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 834 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 835 | Attr.getAttributeSpellingListIndex())); |
| 836 | } |
| 837 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 838 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 839 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 840 | const AttributeList &Attr) { |
| 841 | ASTContext &CurrContext = S.getASTContext(); |
| 842 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 843 | |
| 844 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 845 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 846 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 847 | RD->getNameAsString(); |
| 848 | |
| 849 | return false; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | return true; |
| 854 | } |
| 855 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 856 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 857 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 858 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 859 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 860 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 861 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 862 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 863 | return; |
| 864 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 865 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 866 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 867 | CallableWhenAttr::ConsumedState CallableState; |
| 868 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 869 | StringRef StateString; |
| 870 | SourceLocation Loc; |
| 871 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 872 | return; |
| 873 | |
| 874 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 875 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 876 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 877 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 878 | return; |
| 879 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 880 | |
| 881 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 882 | } |
| 883 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 884 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 885 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 886 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 887 | } |
| 888 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 889 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 890 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 891 | const AttributeList &Attr) { |
| 892 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 893 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 894 | ParamTypestateAttr::ConsumedState ParamState; |
| 895 | |
| 896 | if (Attr.isArgIdent(0)) { |
| 897 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 898 | StringRef StateString = Ident->Ident->getName(); |
| 899 | |
| 900 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 901 | ParamState)) { |
| 902 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 903 | << Attr.getName() << StateString; |
| 904 | return; |
| 905 | } |
| 906 | } else { |
| 907 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 908 | Attr.getName() << AANT_ArgumentIdentifier; |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | // FIXME: This check is currently being done in the analysis. It can be |
| 913 | // enabled here only after the parser propagates attributes at |
| 914 | // template specialization definition, not declaration. |
| 915 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 916 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 917 | // |
| 918 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 919 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 920 | // ReturnType.getAsString(); |
| 921 | // return; |
| 922 | //} |
| 923 | |
| 924 | D->addAttr(::new (S.Context) |
| 925 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 926 | Attr.getAttributeSpellingListIndex())); |
| 927 | } |
| 928 | |
| 929 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 930 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 931 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 932 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 933 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 934 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 935 | |
| 936 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 937 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 938 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 939 | ReturnState)) { |
| 940 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 941 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 942 | return; |
| 943 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 944 | } else { |
| 945 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 946 | Attr.getName() << AANT_ArgumentIdentifier; |
| 947 | return; |
| 948 | } |
| 949 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 950 | // FIXME: This check is currently being done in the analysis. It can be |
| 951 | // enabled here only after the parser propagates attributes at |
| 952 | // template specialization definition, not declaration. |
| 953 | //QualType ReturnType; |
| 954 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 955 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 956 | // ReturnType = Param->getType(); |
| 957 | // |
| 958 | //} else if (const CXXConstructorDecl *Constructor = |
| 959 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 960 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 961 | // |
| 962 | //} else { |
| 963 | // |
| 964 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 965 | //} |
| 966 | // |
| 967 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 968 | // |
| 969 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 970 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 971 | // ReturnType.getAsString(); |
| 972 | // return; |
| 973 | //} |
| 974 | |
| 975 | D->addAttr(::new (S.Context) |
| 976 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 977 | Attr.getAttributeSpellingListIndex())); |
| 978 | } |
| 979 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 980 | |
| 981 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 982 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 983 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 984 | |
| 985 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 986 | return; |
| 987 | |
| 988 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 989 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 990 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 991 | StringRef Param = Ident->Ident->getName(); |
| 992 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 993 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 994 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 995 | return; |
| 996 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 997 | } else { |
| 998 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 999 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | D->addAttr(::new (S.Context) |
| 1004 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1005 | Attr.getAttributeSpellingListIndex())); |
| 1006 | } |
| 1007 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1008 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1009 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1010 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1011 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1012 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1013 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1014 | return; |
| 1015 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1016 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1017 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1018 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1019 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1020 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1021 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1022 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1023 | return; |
| 1024 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1025 | } else { |
| 1026 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1027 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1032 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1033 | Attr.getAttributeSpellingListIndex())); |
| 1034 | } |
| 1035 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1036 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1037 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1038 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1039 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1042 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1043 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1044 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1045 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1046 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1047 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1048 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1049 | if (!FD->getType()->isDependentType() && |
| 1050 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1051 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1052 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1053 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1054 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1055 | FD->addAttr(::new (S.Context) |
| 1056 | PackedAttr(Attr.getRange(), S.Context, |
| 1057 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1058 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1059 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1062 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1063 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1064 | // variables or properties of Objective-C classes. The outlet must also |
| 1065 | // have an object reference type. |
| 1066 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1067 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1068 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1069 | << Attr.getName() << VD->getType() << 0; |
| 1070 | return false; |
| 1071 | } |
| 1072 | } |
| 1073 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1074 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1075 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1076 | << Attr.getName() << PD->getType() << 1; |
| 1077 | return false; |
| 1078 | } |
| 1079 | } |
| 1080 | else { |
| 1081 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1082 | return false; |
| 1083 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1084 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1085 | return true; |
| 1086 | } |
| 1087 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1088 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1089 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1090 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1091 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1092 | D->addAttr(::new (S.Context) |
| 1093 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1094 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1097 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1098 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1099 | |
| 1100 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1101 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1102 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1103 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1104 | return; |
| 1105 | } |
| 1106 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1107 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1108 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1109 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1110 | ParsedType PT; |
| 1111 | |
| 1112 | if (Attr.hasParsedType()) |
| 1113 | PT = Attr.getTypeArg(); |
| 1114 | else { |
| 1115 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1116 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1117 | if (!PT) { |
| 1118 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1119 | return; |
| 1120 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1121 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1122 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1123 | TypeSourceInfo *QTLoc = 0; |
| 1124 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1125 | if (!QTLoc) |
| 1126 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1127 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1128 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1129 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1130 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1131 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1132 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1133 | S.Diag(Attr.getLoc(), |
| 1134 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1135 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1136 | return; |
| 1137 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1138 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1139 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1140 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1141 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1144 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1145 | if (const RecordType *UT = T->getAsUnionType()) |
| 1146 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1147 | RecordDecl *UD = UT->getDecl(); |
| 1148 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1149 | itend = UD->field_end(); it != itend; ++it) { |
| 1150 | QualType QT = it->getType(); |
| 1151 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1152 | T = QT; |
| 1153 | return; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1159 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame^] | 1160 | SourceRange R, bool isReturnValue = false) { |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1161 | T = T.getNonReferenceType(); |
| 1162 | possibleTransparentUnionPointerType(T); |
| 1163 | |
| 1164 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame^] | 1165 | S.Diag(Attr.getLoc(), |
| 1166 | isReturnValue ? diag::warn_attribute_return_pointers_only |
| 1167 | : diag::warn_attribute_pointers_only) |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1168 | << Attr.getName() << R; |
| 1169 | return false; |
| 1170 | } |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1175 | const AttributeList &Attr) { |
| 1176 | // Is the argument a pointer type? |
| 1177 | if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange())) |
| 1178 | return; |
| 1179 | |
| 1180 | if (Attr.getNumArgs() > 0) { |
| 1181 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1182 | << D->getSourceRange(); |
| 1183 | return; |
| 1184 | } |
| 1185 | |
| 1186 | D->addAttr(::new (S.Context) |
| 1187 | NonNullAttr(Attr.getRange(), S.Context, 0, 0, |
| 1188 | Attr.getAttributeSpellingListIndex())); |
| 1189 | } |
| 1190 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1191 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1192 | SmallVector<unsigned, 8> NonNullArgs; |
| 1193 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1194 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1195 | uint64_t Idx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 1196 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1197 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Is the function argument a pointer type? |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1200 | // FIXME: Should also highlight argument in decl in the diagnostic. |
| 1201 | if (!attrNonNullArgCheck(S, getFunctionOrMethodArgType(D, Idx), |
| 1202 | Attr, Ex->getSourceRange())) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1203 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1204 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1205 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1206 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1207 | |
| 1208 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1209 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1210 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1211 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1212 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1213 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1214 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1215 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1216 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1217 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1218 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1219 | if (NonNullArgs.empty()) { |
| 1220 | // Warn the trivial case only if attribute is not coming from a |
| 1221 | // macro instantiation. |
| 1222 | if (Attr.getLoc().isFileID()) |
| 1223 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1224 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1225 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1226 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1227 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1228 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1229 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1230 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1231 | D->addAttr(::new (S.Context) |
| 1232 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1233 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame^] | 1236 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1237 | const AttributeList &Attr) { |
| 1238 | QualType ResultType; |
| 1239 | if (const FunctionType *Ty = D->getFunctionType()) |
| 1240 | ResultType = Ty->getResultType(); |
| 1241 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1242 | ResultType = MD->getResultType(); |
| 1243 | |
| 1244 | if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(), |
| 1245 | /* isReturnValue */ true)) |
| 1246 | return; |
| 1247 | |
| 1248 | D->addAttr(::new (S.Context) |
| 1249 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1250 | Attr.getAttributeSpellingListIndex())); |
| 1251 | } |
| 1252 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1253 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1254 | switch (K) { |
| 1255 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1256 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1257 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1258 | } |
| 1259 | llvm_unreachable("unknown ownership"); |
| 1260 | } |
| 1261 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1262 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1263 | // This attribute must be applied to a function declaration. The first |
| 1264 | // argument to the attribute must be an identifier, the name of the resource, |
| 1265 | // for example: malloc. The following arguments must be argument indexes, the |
| 1266 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1267 | // 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] | 1268 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1269 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1270 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1271 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1272 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1273 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1274 | return; |
| 1275 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1276 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1277 | // Figure out our Kind. |
| 1278 | OwnershipAttr::OwnershipKind K = |
| 1279 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1280 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1281 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1282 | // Check arguments. |
| 1283 | switch (K) { |
| 1284 | case OwnershipAttr::Takes: |
| 1285 | case OwnershipAttr::Holds: |
| 1286 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1287 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1288 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1289 | return; |
| 1290 | } |
| 1291 | break; |
| 1292 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1293 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1294 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1295 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1296 | return; |
| 1297 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1298 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1301 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1302 | |
| 1303 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1304 | StringRef ModuleName = Module->getName(); |
| 1305 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1306 | ModuleName.size() > 4) { |
| 1307 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1308 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1309 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1310 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1311 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1312 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1313 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1314 | uint64_t Idx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 1315 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1316 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1317 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1318 | // Is the function argument a pointer type? |
| 1319 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1320 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1321 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1322 | case OwnershipAttr::Takes: |
| 1323 | case OwnershipAttr::Holds: |
| 1324 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1325 | Err = 0; |
| 1326 | break; |
| 1327 | case OwnershipAttr::Returns: |
| 1328 | if (!T->isIntegerType()) |
| 1329 | Err = 1; |
| 1330 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1331 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1332 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1333 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1334 | << Ex->getSourceRange(); |
| 1335 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1336 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1339 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1340 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1341 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1342 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1343 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1344 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1345 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1346 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1347 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1348 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1351 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | unsigned* start = OwnershipArgs.data(); |
| 1355 | unsigned size = OwnershipArgs.size(); |
| 1356 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1357 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1358 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1359 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1360 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1363 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1364 | // Check the attribute arguments. |
| 1365 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1366 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1367 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1368 | return; |
| 1369 | } |
| 1370 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1371 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1372 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1373 | // gcc rejects |
| 1374 | // class c { |
| 1375 | // static int a __attribute__((weakref ("v2"))); |
| 1376 | // static int b() __attribute__((weakref ("f3"))); |
| 1377 | // }; |
| 1378 | // and ignores the attributes of |
| 1379 | // void f(void) { |
| 1380 | // static int a __attribute__((weakref ("v2"))); |
| 1381 | // } |
| 1382 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1383 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1384 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1385 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1386 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1387 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | // The GCC manual says |
| 1391 | // |
| 1392 | // At present, a declaration to which `weakref' is attached can only |
| 1393 | // be `static'. |
| 1394 | // |
| 1395 | // It also says |
| 1396 | // |
| 1397 | // Without a TARGET, |
| 1398 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1399 | // equivalent to `weak'. |
| 1400 | // |
| 1401 | // gcc 4.4.1 will accept |
| 1402 | // int a7 __attribute__((weakref)); |
| 1403 | // as |
| 1404 | // int a7 __attribute__((weak)); |
| 1405 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1406 | // it if this behaviour is actually used. |
| 1407 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1408 | // GCC rejects |
| 1409 | // static ((alias ("y"), weakref)). |
| 1410 | // Should we? How to check that weakref is before or after alias? |
| 1411 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1412 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1413 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1414 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1415 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1416 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1417 | // GCC will accept anything as the argument of weakref. Should we |
| 1418 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1419 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1420 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1421 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1422 | D->addAttr(::new (S.Context) |
| 1423 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1424 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1427 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1428 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1429 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1430 | return; |
| 1431 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1432 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1433 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1434 | return; |
| 1435 | } |
| 1436 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1437 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1438 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1439 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1440 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1443 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1444 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1445 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1446 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1447 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1448 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1452 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1453 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1454 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1455 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1456 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1459 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1460 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1461 | StringRef Model; |
| 1462 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1463 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1464 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1465 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1466 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1467 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1468 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1469 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1470 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1471 | return; |
| 1472 | } |
| 1473 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1474 | D->addAttr(::new (S.Context) |
| 1475 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1476 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1479 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1480 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1482 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1483 | D->addAttr(::new (S.Context) |
| 1484 | MallocAttr(Attr.getRange(), S.Context, |
| 1485 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1486 | return; |
| 1487 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1490 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1493 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1494 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1495 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1496 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1497 | return; |
| 1498 | } |
| 1499 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1500 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1501 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1504 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1505 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1506 | |
| 1507 | if (S.CheckNoReturnAttr(attr)) return; |
| 1508 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1509 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1510 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1511 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1512 | return; |
| 1513 | } |
| 1514 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1515 | D->addAttr(::new (S.Context) |
| 1516 | NoReturnAttr(attr.getRange(), S.Context, |
| 1517 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1521 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1522 | attr.setInvalid(); |
| 1523 | return true; |
| 1524 | } |
| 1525 | |
| 1526 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1529 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1530 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1531 | |
| 1532 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1533 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1534 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1535 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1536 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1537 | && !VD->getType()->isFunctionPointerType())) { |
| 1538 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1539 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1540 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1541 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1542 | return; |
| 1543 | } |
| 1544 | } |
| 1545 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1546 | D->addAttr(::new (S.Context) |
| 1547 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1548 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1551 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1552 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1553 | /* |
| 1554 | Returning a Vector Class in Registers |
| 1555 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1556 | According to the PPU ABI specifications, a class with a single member of |
| 1557 | vector type is returned in memory when used as the return value of a function. |
| 1558 | This results in inefficient code when implementing vector classes. To return |
| 1559 | the value in a single vector register, add the vecreturn attribute to the |
| 1560 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1561 | |
| 1562 | Example: |
| 1563 | |
| 1564 | struct Vector |
| 1565 | { |
| 1566 | __vector float xyzw; |
| 1567 | } __attribute__((vecreturn)); |
| 1568 | |
| 1569 | Vector Add(Vector lhs, Vector rhs) |
| 1570 | { |
| 1571 | Vector result; |
| 1572 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1573 | return result; // This will be returned in a register |
| 1574 | } |
| 1575 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1576 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1577 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1578 | return; |
| 1579 | } |
| 1580 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1581 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1582 | int count = 0; |
| 1583 | |
| 1584 | if (!isa<CXXRecordDecl>(record)) { |
| 1585 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1590 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1591 | return; |
| 1592 | } |
| 1593 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1594 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1595 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1596 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1597 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1598 | return; |
| 1599 | } |
| 1600 | count++; |
| 1601 | } |
| 1602 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1603 | D->addAttr(::new (S.Context) |
| 1604 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1605 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1608 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1609 | const AttributeList &Attr) { |
| 1610 | if (isa<ParmVarDecl>(D)) { |
| 1611 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1612 | // parameter of a function declaration or lambda. |
| 1613 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1614 | S.Diag(Attr.getLoc(), |
| 1615 | diag::err_carries_dependency_param_not_function_decl); |
| 1616 | return; |
| 1617 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1618 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1619 | |
| 1620 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1621 | Attr.getRange(), S.Context, |
| 1622 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1625 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1626 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1627 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1628 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1629 | return; |
| 1630 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1631 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1632 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1633 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1634 | return; |
| 1635 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1636 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1637 | D->addAttr(::new (S.Context) |
| 1638 | UsedAttr(Attr.getRange(), S.Context, |
| 1639 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1642 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1643 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1644 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1645 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1646 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1647 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1648 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1649 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1650 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1651 | if (Attr.getNumArgs() > 0 && |
| 1652 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1653 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1654 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1655 | D->addAttr(::new (S.Context) |
| 1656 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1657 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1660 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1661 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1662 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1663 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1664 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1665 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1666 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1667 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1668 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1669 | if (Attr.getNumArgs() > 0 && |
| 1670 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1671 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1672 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1673 | D->addAttr(::new (S.Context) |
| 1674 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1675 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1678 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1679 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1680 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1681 | unsigned NumArgs = Attr.getNumArgs(); |
| 1682 | if (NumArgs > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1683 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1684 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1685 | return; |
| 1686 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1689 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1690 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1691 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1692 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1693 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1694 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1697 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 1698 | const AttributeList &Attr) { |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1699 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1700 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1701 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1704 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1705 | IdentifierInfo *Platform, |
| 1706 | VersionTuple Introduced, |
| 1707 | VersionTuple Deprecated, |
| 1708 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1709 | StringRef PlatformName |
| 1710 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1711 | if (PlatformName.empty()) |
| 1712 | PlatformName = Platform->getName(); |
| 1713 | |
| 1714 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1715 | // of these steps are needed). |
| 1716 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1717 | !(Introduced <= Deprecated)) { |
| 1718 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1719 | << 1 << PlatformName << Deprecated.getAsString() |
| 1720 | << 0 << Introduced.getAsString(); |
| 1721 | return true; |
| 1722 | } |
| 1723 | |
| 1724 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1725 | !(Introduced <= Obsoleted)) { |
| 1726 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1727 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1728 | << 0 << Introduced.getAsString(); |
| 1729 | return true; |
| 1730 | } |
| 1731 | |
| 1732 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1733 | !(Deprecated <= Obsoleted)) { |
| 1734 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1735 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1736 | << 1 << Deprecated.getAsString(); |
| 1737 | return true; |
| 1738 | } |
| 1739 | |
| 1740 | return false; |
| 1741 | } |
| 1742 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1743 | /// \brief Check whether the two versions match. |
| 1744 | /// |
| 1745 | /// If either version tuple is empty, then they are assumed to match. If |
| 1746 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1747 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1748 | bool BeforeIsOkay) { |
| 1749 | if (X.empty() || Y.empty()) |
| 1750 | return true; |
| 1751 | |
| 1752 | if (X == Y) |
| 1753 | return true; |
| 1754 | |
| 1755 | if (BeforeIsOkay && X < Y) |
| 1756 | return true; |
| 1757 | |
| 1758 | return false; |
| 1759 | } |
| 1760 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1761 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1762 | IdentifierInfo *Platform, |
| 1763 | VersionTuple Introduced, |
| 1764 | VersionTuple Deprecated, |
| 1765 | VersionTuple Obsoleted, |
| 1766 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1767 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1768 | bool Override, |
| 1769 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1770 | VersionTuple MergedIntroduced = Introduced; |
| 1771 | VersionTuple MergedDeprecated = Deprecated; |
| 1772 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1773 | bool FoundAny = false; |
| 1774 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1775 | if (D->hasAttrs()) { |
| 1776 | AttrVec &Attrs = D->getAttrs(); |
| 1777 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1778 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1779 | if (!OldAA) { |
| 1780 | ++i; |
| 1781 | continue; |
| 1782 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1783 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1784 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1785 | if (OldPlatform != Platform) { |
| 1786 | ++i; |
| 1787 | continue; |
| 1788 | } |
| 1789 | |
| 1790 | FoundAny = true; |
| 1791 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1792 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1793 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1794 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1796 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1797 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1798 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1799 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1800 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1801 | if (Override) { |
| 1802 | int Which = -1; |
| 1803 | VersionTuple FirstVersion; |
| 1804 | VersionTuple SecondVersion; |
| 1805 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1806 | Which = 0; |
| 1807 | FirstVersion = OldIntroduced; |
| 1808 | SecondVersion = Introduced; |
| 1809 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1810 | Which = 1; |
| 1811 | FirstVersion = Deprecated; |
| 1812 | SecondVersion = OldDeprecated; |
| 1813 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1814 | Which = 2; |
| 1815 | FirstVersion = Obsoleted; |
| 1816 | SecondVersion = OldObsoleted; |
| 1817 | } |
| 1818 | |
| 1819 | if (Which == -1) { |
| 1820 | Diag(OldAA->getLocation(), |
| 1821 | diag::warn_mismatched_availability_override_unavail) |
| 1822 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1823 | } else { |
| 1824 | Diag(OldAA->getLocation(), |
| 1825 | diag::warn_mismatched_availability_override) |
| 1826 | << Which |
| 1827 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1828 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1829 | } |
| 1830 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1831 | } else { |
| 1832 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1833 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1834 | } |
| 1835 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1836 | Attrs.erase(Attrs.begin() + i); |
| 1837 | --e; |
| 1838 | continue; |
| 1839 | } |
| 1840 | |
| 1841 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1842 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1843 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1844 | |
| 1845 | if (MergedIntroduced2.empty()) |
| 1846 | MergedIntroduced2 = OldIntroduced; |
| 1847 | if (MergedDeprecated2.empty()) |
| 1848 | MergedDeprecated2 = OldDeprecated; |
| 1849 | if (MergedObsoleted2.empty()) |
| 1850 | MergedObsoleted2 = OldObsoleted; |
| 1851 | |
| 1852 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1853 | MergedIntroduced2, MergedDeprecated2, |
| 1854 | MergedObsoleted2)) { |
| 1855 | Attrs.erase(Attrs.begin() + i); |
| 1856 | --e; |
| 1857 | continue; |
| 1858 | } |
| 1859 | |
| 1860 | MergedIntroduced = MergedIntroduced2; |
| 1861 | MergedDeprecated = MergedDeprecated2; |
| 1862 | MergedObsoleted = MergedObsoleted2; |
| 1863 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1864 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | if (FoundAny && |
| 1868 | MergedIntroduced == Introduced && |
| 1869 | MergedDeprecated == Deprecated && |
| 1870 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1871 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1872 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1873 | // Only create a new attribute if !Override, but we want to do |
| 1874 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1875 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1876 | MergedDeprecated, MergedObsoleted) && |
| 1877 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1878 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1879 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1880 | Obsoleted, IsUnavailable, Message, |
| 1881 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1882 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1883 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1886 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1887 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1888 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1889 | return; |
| 1890 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1891 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1892 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1893 | IdentifierInfo *II = Platform->Ident; |
| 1894 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1895 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1896 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1897 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1898 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1899 | if (!ND) { |
| 1900 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1901 | return; |
| 1902 | } |
| 1903 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1904 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1905 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1906 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1907 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1908 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1909 | if (const StringLiteral *SE = |
| 1910 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1911 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1912 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1913 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1914 | Introduced.Version, |
| 1915 | Deprecated.Version, |
| 1916 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1917 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1918 | /*Override=*/false, |
| 1919 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1920 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1921 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1924 | template <class T> |
| 1925 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1926 | typename T::VisibilityType value, |
| 1927 | unsigned attrSpellingListIndex) { |
| 1928 | T *existingAttr = D->getAttr<T>(); |
| 1929 | if (existingAttr) { |
| 1930 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1931 | if (existingValue == value) |
| 1932 | return NULL; |
| 1933 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1934 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1935 | D->dropAttr<T>(); |
| 1936 | } |
| 1937 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1938 | } |
| 1939 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1940 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1941 | VisibilityAttr::VisibilityType Vis, |
| 1942 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1943 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1944 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1947 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1948 | TypeVisibilityAttr::VisibilityType Vis, |
| 1949 | unsigned AttrSpellingListIndex) { |
| 1950 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1951 | AttrSpellingListIndex); |
| 1952 | } |
| 1953 | |
| 1954 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1955 | bool isTypeVisibility) { |
| 1956 | // Visibility attributes don't mean anything on a typedef. |
| 1957 | if (isa<TypedefNameDecl>(D)) { |
| 1958 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 1959 | << Attr.getName(); |
| 1960 | return; |
| 1961 | } |
| 1962 | |
| 1963 | // 'type_visibility' can only go on a type or namespace. |
| 1964 | if (isTypeVisibility && |
| 1965 | !(isa<TagDecl>(D) || |
| 1966 | isa<ObjCInterfaceDecl>(D) || |
| 1967 | isa<NamespaceDecl>(D))) { |
| 1968 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 1969 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 1970 | return; |
| 1971 | } |
| 1972 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 1973 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1974 | StringRef TypeStr; |
| 1975 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1976 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1977 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1978 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1979 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1980 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1981 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1982 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1983 | return; |
| 1984 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Complain about attempts to use protected visibility on targets |
| 1987 | // (like Darwin) that don't support it. |
| 1988 | if (type == VisibilityAttr::Protected && |
| 1989 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 1990 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 1991 | type = VisibilityAttr::Default; |
| 1992 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1993 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1994 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1995 | clang::Attr *newAttr; |
| 1996 | if (isTypeVisibility) { |
| 1997 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 1998 | (TypeVisibilityAttr::VisibilityType) type, |
| 1999 | Index); |
| 2000 | } else { |
| 2001 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2002 | } |
| 2003 | if (newAttr) |
| 2004 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2007 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2008 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2009 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2010 | if (!Attr.isArgIdent(0)) { |
| 2011 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2012 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2013 | return; |
| 2014 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2015 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2016 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2017 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2018 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2019 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2020 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2021 | return; |
| 2022 | } |
| 2023 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2024 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2025 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2026 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2027 | << method->getResultType(); |
| 2028 | // Ignore the attribute. |
| 2029 | return; |
| 2030 | } |
| 2031 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2032 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2033 | S.Context, F, |
| 2034 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2037 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2038 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2039 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2040 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2041 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2042 | return; |
| 2043 | } |
| 2044 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2045 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2046 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2047 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2048 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2049 | return; |
| 2050 | } |
| 2051 | } |
| 2052 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2053 | // It is okay to include this attribute on properties, e.g.: |
| 2054 | // |
| 2055 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2056 | // |
| 2057 | // In this case it follows tradition and suppresses an error in the above |
| 2058 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2059 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2060 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2061 | D->addAttr(::new (S.Context) |
| 2062 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2063 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2066 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2067 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2068 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2069 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2070 | return; |
| 2071 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2072 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2073 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2074 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2075 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2076 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2077 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2078 | return; |
| 2079 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2080 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2081 | D->addAttr(::new (S.Context) |
| 2082 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2083 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2086 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2087 | // check the attribute arguments. |
| 2088 | if (Attr.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 2089 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 2090 | << Attr.getName() << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2091 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2094 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2095 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2096 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2097 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2098 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2099 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2100 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2101 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2102 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2103 | return; |
| 2104 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2105 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2106 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2107 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2108 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2109 | return; |
| 2110 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2111 | |
| 2112 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2115 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2116 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2117 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2118 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2119 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2120 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2121 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2122 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2123 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2124 | return; |
| 2125 | } |
| 2126 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2127 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2128 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2129 | // FIXME: This error message could be improved, it would be nice |
| 2130 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2131 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2132 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2133 | return; |
| 2134 | } |
| 2135 | } |
| 2136 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2137 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2138 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2139 | if (isa<FunctionNoProtoType>(FT)) { |
| 2140 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2141 | return; |
| 2142 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2143 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2144 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2145 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2146 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2147 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2148 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2149 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2150 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2151 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2152 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2153 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2154 | if (!BD->isVariadic()) { |
| 2155 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2156 | return; |
| 2157 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2158 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2159 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2160 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2161 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2162 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2163 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2164 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2165 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2166 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2167 | return; |
| 2168 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2169 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2170 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2171 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2172 | return; |
| 2173 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2174 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2175 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2176 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2177 | return; |
| 2178 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2179 | D->addAttr(::new (S.Context) |
| 2180 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2181 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2184 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2185 | if (D->getFunctionType() && D->getFunctionType()->getResultType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2186 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2187 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2188 | return; |
| 2189 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2190 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2191 | if (MD->getResultType()->isVoidType()) { |
| 2192 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2193 | << Attr.getName() << 1; |
| 2194 | return; |
| 2195 | } |
| 2196 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2197 | D->addAttr(::new (S.Context) |
| 2198 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2199 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2202 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2203 | // weak_import only applies to variable & function declarations. |
| 2204 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2205 | if (!D->canBeWeakImported(isDef)) { |
| 2206 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2207 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2208 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2209 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2210 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2211 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2212 | // Nothing to warn about here. |
| 2213 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2214 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2215 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2216 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2217 | return; |
| 2218 | } |
| 2219 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2220 | D->addAttr(::new (S.Context) |
| 2221 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2222 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2225 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2226 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2227 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2228 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2229 | uint32_t WGSize[3]; |
| 2230 | for (unsigned i = 0; i < 3; ++i) |
| 2231 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2232 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2233 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2234 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2235 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2236 | Existing->getYDim() == WGSize[1] && |
| 2237 | Existing->getZDim() == WGSize[2])) |
| 2238 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2239 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2240 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2241 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2242 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2245 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2246 | if (!Attr.hasParsedType()) { |
| 2247 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2248 | << Attr.getName() << 1; |
| 2249 | return; |
| 2250 | } |
| 2251 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2252 | TypeSourceInfo *ParmTSI = 0; |
| 2253 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2254 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2255 | |
| 2256 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2257 | (ParmType->isBooleanType() || |
| 2258 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2259 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2260 | << ParmType; |
| 2261 | return; |
| 2262 | } |
| 2263 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2264 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2265 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2266 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2267 | return; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2272 | ParmTSI, |
| 2273 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2276 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2277 | StringRef Name, |
| 2278 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2279 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2280 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2281 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2282 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2283 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2284 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2285 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2286 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2287 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2290 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2291 | // Make sure that there is a string literal as the sections's single |
| 2292 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2293 | StringRef Str; |
| 2294 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2295 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2296 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2298 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2299 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2300 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2301 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2302 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2303 | return; |
| 2304 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2305 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2306 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2307 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2308 | if (NewAttr) |
| 2309 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2312 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2313 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2314 | VarDecl *VD = cast<VarDecl>(D); |
| 2315 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2316 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.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 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2320 | Expr *E = Attr.getArgAsExpr(0); |
| 2321 | SourceLocation Loc = E->getExprLoc(); |
| 2322 | FunctionDecl *FD = 0; |
| 2323 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2324 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2325 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2326 | // will warn the user. |
| 2327 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2328 | if (DRE->hasQualifier()) |
| 2329 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2330 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2331 | NI = DRE->getNameInfo(); |
| 2332 | if (!FD) { |
| 2333 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2334 | << NI.getName(); |
| 2335 | return; |
| 2336 | } |
| 2337 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2338 | if (ULE->hasExplicitTemplateArgs()) |
| 2339 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2340 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2341 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2342 | if (!FD) { |
| 2343 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2344 | << NI.getName(); |
| 2345 | if (ULE->getType() == S.Context.OverloadTy) |
| 2346 | S.NoteAllOverloadCandidates(ULE); |
| 2347 | return; |
| 2348 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2349 | } else { |
| 2350 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2351 | return; |
| 2352 | } |
| 2353 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2354 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2355 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2356 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2357 | return; |
| 2358 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2359 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2360 | // We're currently more strict than GCC about what function types we accept. |
| 2361 | // If this ever proves to be a problem it should be easy to fix. |
| 2362 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2363 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2364 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2365 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2366 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2367 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2368 | return; |
| 2369 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2370 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2371 | D->addAttr(::new (S.Context) |
| 2372 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2373 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2376 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2377 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2378 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2379 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2380 | uint64_t ArgIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2381 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2382 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2383 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2384 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2385 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2386 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2387 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2388 | if (not_nsstring_type && |
| 2389 | !isCFStringType(Ty, S.Context) && |
| 2390 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2391 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2392 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2393 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2394 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2395 | << IdxExpr->getSourceRange(); |
| 2396 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2397 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2398 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2399 | if (!isNSStringType(Ty, S.Context) && |
| 2400 | !isCFStringType(Ty, S.Context) && |
| 2401 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2402 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2403 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2404 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2405 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2406 | << IdxExpr->getSourceRange(); |
| 2407 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2410 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2411 | // because that has corrected for the implicit this parameter, and is zero- |
| 2412 | // based. The attribute expects what the user wrote explicitly. |
| 2413 | llvm::APSInt Val; |
| 2414 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2415 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2416 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2417 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2418 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2421 | enum FormatAttrKind { |
| 2422 | CFStringFormat, |
| 2423 | NSStringFormat, |
| 2424 | StrftimeFormat, |
| 2425 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2426 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2427 | InvalidFormat |
| 2428 | }; |
| 2429 | |
| 2430 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2431 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2432 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2433 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2434 | // Check for formats that get handled specially. |
| 2435 | .Case("NSString", NSStringFormat) |
| 2436 | .Case("CFString", CFStringFormat) |
| 2437 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2438 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2439 | // Otherwise, check for supported formats. |
| 2440 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2441 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2442 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2443 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2444 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2445 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2448 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2449 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2450 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2451 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2452 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2453 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2454 | return; |
| 2455 | } |
| 2456 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2457 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2458 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2459 | Attr.setInvalid(); |
| 2460 | return; |
| 2461 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2462 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2463 | if (S.Context.getAsArrayType(T)) |
| 2464 | T = S.Context.getBaseElementType(T); |
| 2465 | if (!T->getAs<RecordType>()) { |
| 2466 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2467 | Attr.setInvalid(); |
| 2468 | return; |
| 2469 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2470 | |
| 2471 | Expr *E = Attr.getArgAsExpr(0); |
| 2472 | uint32_t prioritynum; |
| 2473 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2474 | Attr.setInvalid(); |
| 2475 | return; |
| 2476 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2477 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2478 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2479 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2480 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2481 | Attr.setInvalid(); |
| 2482 | return; |
| 2483 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2484 | D->addAttr(::new (S.Context) |
| 2485 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2486 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2489 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2490 | IdentifierInfo *Format, int FormatIdx, |
| 2491 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2492 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2493 | // Check whether we already have an equivalent format attribute. |
| 2494 | for (specific_attr_iterator<FormatAttr> |
| 2495 | i = D->specific_attr_begin<FormatAttr>(), |
| 2496 | e = D->specific_attr_end<FormatAttr>(); |
| 2497 | i != e ; ++i) { |
| 2498 | FormatAttr *f = *i; |
| 2499 | if (f->getType() == Format && |
| 2500 | f->getFormatIdx() == FormatIdx && |
| 2501 | f->getFirstArg() == FirstArg) { |
| 2502 | // If we don't have a valid location for this attribute, adopt the |
| 2503 | // location. |
| 2504 | if (f->getLocation().isInvalid()) |
| 2505 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2506 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2510 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2511 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2514 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2515 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2516 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2517 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2518 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2519 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2520 | return; |
| 2521 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2522 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2523 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2524 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2525 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2526 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2527 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2528 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2529 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2530 | |
| 2531 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2532 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2533 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2534 | // If we've modified the string name, we need a new identifier for it. |
| 2535 | II = &S.Context.Idents.get(Format); |
| 2536 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2537 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2538 | // Check for supported formats. |
| 2539 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2540 | |
| 2541 | if (Kind == IgnoredFormat) |
| 2542 | return; |
| 2543 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2544 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2545 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2546 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2547 | return; |
| 2548 | } |
| 2549 | |
| 2550 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2551 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2552 | uint32_t Idx; |
| 2553 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2554 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2555 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2556 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2557 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2558 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2559 | return; |
| 2560 | } |
| 2561 | |
| 2562 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2563 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2564 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2565 | if (HasImplicitThisParam) { |
| 2566 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2567 | S.Diag(Attr.getLoc(), |
| 2568 | diag::err_format_attribute_implicit_this_format_string) |
| 2569 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2570 | return; |
| 2571 | } |
| 2572 | ArgIdx--; |
| 2573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2575 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2576 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2577 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2578 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2579 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2580 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2581 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2582 | return; |
| 2583 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2584 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2585 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2586 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2587 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2588 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2589 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2590 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2591 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2592 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2593 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2594 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2595 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2596 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2597 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2598 | return; |
| 2599 | } |
| 2600 | |
| 2601 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2602 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2603 | uint32_t FirstArg; |
| 2604 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2605 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2606 | |
| 2607 | // check if the function is variadic if the 3rd argument non-zero |
| 2608 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2609 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2610 | ++NumArgs; // +1 for ... |
| 2611 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2612 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
| 2615 | } |
| 2616 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2617 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2618 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2619 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2620 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2621 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2622 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2623 | return; |
| 2624 | } |
| 2625 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2626 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2627 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2628 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2629 | return; |
| 2630 | } |
| 2631 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2632 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2633 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2634 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2635 | if (NewAttr) |
| 2636 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2639 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2640 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2641 | // Try to find the underlying union declaration. |
| 2642 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2643 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2644 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2645 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2646 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2647 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2648 | |
| 2649 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2650 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2651 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2652 | return; |
| 2653 | } |
| 2654 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2655 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2656 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2657 | diag::warn_transparent_union_attribute_not_definition); |
| 2658 | return; |
| 2659 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2660 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2661 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2662 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2663 | if (Field == FieldEnd) { |
| 2664 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2665 | return; |
| 2666 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2667 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2668 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2669 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2670 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2671 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2672 | diag::warn_transparent_union_attribute_floating) |
| 2673 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2674 | return; |
| 2675 | } |
| 2676 | |
| 2677 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2678 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2679 | for (; Field != FieldEnd; ++Field) { |
| 2680 | QualType FieldType = Field->getType(); |
| 2681 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2682 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2683 | // Warn if we drop the attribute. |
| 2684 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2685 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2686 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2687 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2688 | diag::warn_transparent_union_attribute_field_size_align) |
| 2689 | << isSize << Field->getDeclName() << FieldBits; |
| 2690 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2691 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2692 | diag::note_transparent_union_first_field_size_align) |
| 2693 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2694 | return; |
| 2695 | } |
| 2696 | } |
| 2697 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2698 | RD->addAttr(::new (S.Context) |
| 2699 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2700 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2703 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2704 | // Make sure that there is a string literal as the annotation's single |
| 2705 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2706 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2707 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2708 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2709 | |
| 2710 | // Don't duplicate annotations that are already set. |
| 2711 | for (specific_attr_iterator<AnnotateAttr> |
| 2712 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2713 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2714 | if ((*i)->getAnnotation() == Str) |
| 2715 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2716 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2717 | |
| 2718 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2719 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2720 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2723 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2724 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2725 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2726 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2727 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2728 | return; |
| 2729 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2730 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2731 | if (Attr.getNumArgs() == 0) { |
| 2732 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2733 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2734 | return; |
| 2735 | } |
| 2736 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2737 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2738 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2739 | S.Diag(Attr.getEllipsisLoc(), |
| 2740 | diag::err_pack_expansion_without_parameter_packs); |
| 2741 | return; |
| 2742 | } |
| 2743 | |
| 2744 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2745 | return; |
| 2746 | |
| 2747 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2748 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2752 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2753 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2754 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2755 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2756 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2757 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2758 | // C++11 [dcl.align]p1: |
| 2759 | // An alignment-specifier may be applied to a variable or to a class |
| 2760 | // data member, but it shall not be applied to a bit-field, a function |
| 2761 | // parameter, the formal parameter of a catch clause, or a variable |
| 2762 | // declared with the register storage class specifier. An |
| 2763 | // alignment-specifier may also be applied to the declaration of a class |
| 2764 | // or enumeration type. |
| 2765 | // C11 6.7.5/2: |
| 2766 | // An alignment attribute shall not be specified in a declaration of |
| 2767 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2768 | // object declared with the register storage-class specifier. |
| 2769 | int DiagKind = -1; |
| 2770 | if (isa<ParmVarDecl>(D)) { |
| 2771 | DiagKind = 0; |
| 2772 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2773 | if (VD->getStorageClass() == SC_Register) |
| 2774 | DiagKind = 1; |
| 2775 | if (VD->isExceptionVariable()) |
| 2776 | DiagKind = 2; |
| 2777 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2778 | if (FD->isBitField()) |
| 2779 | DiagKind = 3; |
| 2780 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2781 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2782 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2783 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2784 | return; |
| 2785 | } |
| 2786 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2787 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2788 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2789 | return; |
| 2790 | } |
| 2791 | } |
| 2792 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2793 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2794 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2795 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2796 | AA->setPackExpansion(IsPackExpansion); |
| 2797 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2798 | return; |
| 2799 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2800 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2801 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2802 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2803 | ExprResult ICE |
| 2804 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2805 | diag::err_aligned_attribute_argument_not_int, |
| 2806 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2807 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2808 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2809 | |
| 2810 | // C++11 [dcl.align]p2: |
| 2811 | // -- if the constant expression evaluates to zero, the alignment |
| 2812 | // specifier shall have no effect |
| 2813 | // C11 6.7.5p6: |
| 2814 | // An alignment specification of zero has no effect. |
| 2815 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2816 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2817 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2818 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2819 | return; |
| 2820 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2821 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2822 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2823 | // We've already verified it's a power of 2, now let's make sure it's |
| 2824 | // 8192 or less. |
| 2825 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2826 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2827 | << E->getSourceRange(); |
| 2828 | return; |
| 2829 | } |
| 2830 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2831 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2832 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 2833 | ICE.take(), SpellingListIndex); |
| 2834 | AA->setPackExpansion(IsPackExpansion); |
| 2835 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2838 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2839 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2840 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2841 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2842 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2843 | SpellingListIndex); |
| 2844 | AA->setPackExpansion(IsPackExpansion); |
| 2845 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2846 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2847 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2848 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2849 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2850 | |
| 2851 | QualType Ty; |
| 2852 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2853 | Ty = VD->getType(); |
| 2854 | else |
| 2855 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 2856 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2857 | return; |
| 2858 | |
| 2859 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2860 | // The combined effect of all alignment attributes in a declaration shall |
| 2861 | // not specify an alignment that is less strict than the alignment that |
| 2862 | // would otherwise be required for the entity being declared. |
| 2863 | AlignedAttr *AlignasAttr = 0; |
| 2864 | unsigned Align = 0; |
| 2865 | for (specific_attr_iterator<AlignedAttr> |
| 2866 | I = D->specific_attr_begin<AlignedAttr>(), |
| 2867 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 2868 | if (I->isAlignmentDependent()) |
| 2869 | return; |
| 2870 | if (I->isAlignas()) |
| 2871 | AlignasAttr = *I; |
| 2872 | Align = std::max(Align, I->getAlignment(Context)); |
| 2873 | } |
| 2874 | |
| 2875 | if (AlignasAttr && Align) { |
| 2876 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 2877 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 2878 | if (NaturalAlign > RequestedAlign) |
| 2879 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 2880 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 2881 | } |
| 2882 | } |
| 2883 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2884 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2885 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2886 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2887 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2888 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2889 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2890 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2891 | // This attribute isn't documented, but glibc uses it. It changes |
| 2892 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2893 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 2894 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 2895 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2896 | return; |
| 2897 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2898 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2899 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 2900 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2901 | |
| 2902 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2903 | if (Str.startswith("__") && Str.endswith("__")) |
| 2904 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2905 | |
| 2906 | unsigned DestWidth = 0; |
| 2907 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2908 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2909 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2910 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2911 | switch (Str[0]) { |
| 2912 | case 'Q': DestWidth = 8; break; |
| 2913 | case 'H': DestWidth = 16; break; |
| 2914 | case 'S': DestWidth = 32; break; |
| 2915 | case 'D': DestWidth = 64; break; |
| 2916 | case 'X': DestWidth = 96; break; |
| 2917 | case 'T': DestWidth = 128; break; |
| 2918 | } |
| 2919 | if (Str[1] == 'F') { |
| 2920 | IntegerMode = false; |
| 2921 | } else if (Str[1] == 'C') { |
| 2922 | IntegerMode = false; |
| 2923 | ComplexMode = true; |
| 2924 | } else if (Str[1] != 'I') { |
| 2925 | DestWidth = 0; |
| 2926 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2927 | break; |
| 2928 | case 4: |
| 2929 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2930 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2931 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2932 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2933 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2934 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2935 | break; |
| 2936 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2937 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2938 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2939 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2940 | case 11: |
| 2941 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 2942 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2943 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2947 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2948 | OldTy = TD->getUnderlyingType(); |
| 2949 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2950 | OldTy = VD->getType(); |
| 2951 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2952 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 2953 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2954 | return; |
| 2955 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2956 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2957 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2958 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2959 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2960 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2961 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2962 | } else if (ComplexMode) { |
| 2963 | if (!OldTy->isComplexType()) |
| 2964 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2965 | } else { |
| 2966 | if (!OldTy->isFloatingType()) |
| 2967 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2968 | } |
| 2969 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2970 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2971 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2972 | // FIXME: Make sure floating-point mappings are accurate |
| 2973 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2974 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2975 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2976 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | QualType NewTy; |
| 2980 | |
| 2981 | if (IntegerMode) |
| 2982 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 2983 | OldTy->isSignedIntegerType()); |
| 2984 | else |
| 2985 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 2986 | |
| 2987 | if (NewTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2988 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2989 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2992 | if (ComplexMode) { |
| 2993 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 2997 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 2998 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 2999 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3000 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3001 | |
| 3002 | D->addAttr(::new (S.Context) |
| 3003 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3004 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3005 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3006 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3007 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3008 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3009 | if (!VD->hasGlobalStorage()) |
| 3010 | S.Diag(Attr.getLoc(), |
| 3011 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3012 | << Attr.getName(); |
| 3013 | } else if (!isFunctionOrMethod(D)) { |
| 3014 | S.Diag(Attr.getLoc(), |
| 3015 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3016 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3017 | return; |
| 3018 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3019 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3020 | D->addAttr(::new (S.Context) |
| 3021 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3022 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3025 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3026 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 3027 | if (!FD->getResultType()->isVoidType()) { |
| 3028 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| 3029 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
| 3030 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3031 | << FD->getType() |
| 3032 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
| 3033 | "void"); |
| 3034 | } else { |
| 3035 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3036 | << FD->getType(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3037 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3038 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3039 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3040 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3041 | D->addAttr(::new (S.Context) |
| 3042 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3043 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3046 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3047 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3048 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3049 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3050 | return; |
| 3051 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3052 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3053 | D->addAttr(::new (S.Context) |
| 3054 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3055 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3058 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3059 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3060 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3061 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3062 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3063 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3064 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3065 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3066 | return; |
| 3067 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3068 | if (!isa<ObjCMethodDecl>(D)) { |
| 3069 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3070 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3071 | return; |
| 3072 | } |
| 3073 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3074 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3075 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3076 | D->addAttr(::new (S.Context) |
| 3077 | FastCallAttr(Attr.getRange(), S.Context, |
| 3078 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3079 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3080 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3081 | D->addAttr(::new (S.Context) |
| 3082 | StdCallAttr(Attr.getRange(), S.Context, |
| 3083 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3084 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3085 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3086 | D->addAttr(::new (S.Context) |
| 3087 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3088 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3089 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3090 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3091 | D->addAttr(::new (S.Context) |
| 3092 | CDeclAttr(Attr.getRange(), S.Context, |
| 3093 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3094 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3095 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3096 | D->addAttr(::new (S.Context) |
| 3097 | PascalAttr(Attr.getRange(), S.Context, |
| 3098 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3099 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3100 | case AttributeList::AT_MSABI: |
| 3101 | D->addAttr(::new (S.Context) |
| 3102 | MSABIAttr(Attr.getRange(), S.Context, |
| 3103 | Attr.getAttributeSpellingListIndex())); |
| 3104 | return; |
| 3105 | case AttributeList::AT_SysVABI: |
| 3106 | D->addAttr(::new (S.Context) |
| 3107 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3108 | Attr.getAttributeSpellingListIndex())); |
| 3109 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3110 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3111 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3112 | switch (CC) { |
| 3113 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3114 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3115 | break; |
| 3116 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3117 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3118 | break; |
| 3119 | default: |
| 3120 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3123 | D->addAttr(::new (S.Context) |
| 3124 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3125 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3126 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3127 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3128 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3129 | D->addAttr(::new (S.Context) |
| 3130 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3131 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3132 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3133 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3134 | D->addAttr(::new (S.Context) |
| 3135 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3136 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3137 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3138 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3139 | default: |
| 3140 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3144 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3145 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3146 | if (attr.isInvalid()) |
| 3147 | return true; |
| 3148 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3149 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3150 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3151 | attr.setInvalid(); |
| 3152 | return true; |
| 3153 | } |
| 3154 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3155 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3156 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3157 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3158 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3159 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3160 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3161 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3162 | case AttributeList::AT_MSABI: |
| 3163 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3164 | CC_X86_64Win64; |
| 3165 | break; |
| 3166 | case AttributeList::AT_SysVABI: |
| 3167 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3168 | CC_C; |
| 3169 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3170 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3171 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3172 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3173 | attr.setInvalid(); |
| 3174 | return true; |
| 3175 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3176 | if (StrRef == "aapcs") { |
| 3177 | CC = CC_AAPCS; |
| 3178 | break; |
| 3179 | } else if (StrRef == "aapcs-vfp") { |
| 3180 | CC = CC_AAPCS_VFP; |
| 3181 | break; |
| 3182 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3183 | |
| 3184 | attr.setInvalid(); |
| 3185 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3186 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3187 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3188 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3189 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3190 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3193 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3194 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3195 | if (A == TargetInfo::CCCR_Warning) { |
| 3196 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3197 | |
| 3198 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3199 | if (FD) |
| 3200 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3201 | TargetInfo::CCMT_NonMember; |
| 3202 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3205 | return false; |
| 3206 | } |
| 3207 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3208 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3209 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3210 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3211 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3212 | return true; |
| 3213 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3214 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3215 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3216 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3217 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3218 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3219 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3220 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3221 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3222 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3223 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3226 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3227 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3228 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3229 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3230 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3233 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3234 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3235 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3236 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3237 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3238 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3241 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3244 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3245 | const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3246 | // check the attribute arguments. |
| 3247 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| 3248 | // FIXME: 0 is not okay. |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 3249 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3250 | << Attr.getName() << 2; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3251 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3252 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3253 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3254 | uint32_t MaxThreads, MinBlocks = 0; |
| 3255 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3256 | return; |
| 3257 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3258 | Attr.getArgAsExpr(1), |
| 3259 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3260 | return; |
| 3261 | |
| 3262 | D->addAttr(::new (S.Context) |
| 3263 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3264 | MaxThreads, MinBlocks, |
| 3265 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3268 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3269 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3270 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3271 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3272 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3273 | return; |
| 3274 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3275 | |
| 3276 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3277 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3278 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3279 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3280 | |
| 3281 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3282 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3283 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3284 | return; |
| 3285 | } |
| 3286 | |
| 3287 | uint64_t ArgumentIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3288 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3289 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3290 | return; |
| 3291 | |
| 3292 | uint64_t TypeTagIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3293 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3294 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3295 | return; |
| 3296 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3297 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3298 | if (IsPointer) { |
| 3299 | // Ensure that buffer has a pointer type. |
| 3300 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3301 | if (!BufferTy->isPointerType()) { |
| 3302 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3303 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3307 | D->addAttr(::new (S.Context) |
| 3308 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3309 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3310 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3314 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3315 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3316 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3317 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3318 | return; |
| 3319 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3320 | |
| 3321 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3322 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3323 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3324 | if (!isa<VarDecl>(D)) { |
| 3325 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3326 | << Attr.getName() << ExpectedVariable; |
| 3327 | return; |
| 3328 | } |
| 3329 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3330 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3331 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3332 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3333 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3334 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3335 | D->addAttr(::new (S.Context) |
| 3336 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3337 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3338 | Attr.getLayoutCompatible(), |
| 3339 | Attr.getMustBeNull(), |
| 3340 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3343 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3344 | // Checker-specific attribute handlers. |
| 3345 | //===----------------------------------------------------------------------===// |
| 3346 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3347 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3348 | return type->isDependentType() || |
| 3349 | type->isObjCObjectPointerType() || |
| 3350 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3351 | } |
| 3352 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3353 | return type->isDependentType() || |
| 3354 | type->isPointerType() || |
| 3355 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3358 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3359 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3360 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3361 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3362 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3363 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3364 | cf = false; |
| 3365 | } else { |
| 3366 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3367 | cf = true; |
| 3368 | } |
| 3369 | |
| 3370 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3371 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3372 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3373 | return; |
| 3374 | } |
| 3375 | |
| 3376 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3377 | param->addAttr(::new (S.Context) |
| 3378 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3379 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3380 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3381 | param->addAttr(::new (S.Context) |
| 3382 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3383 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3386 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3387 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3388 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3389 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3390 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3391 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3392 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3393 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3394 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3395 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3396 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3397 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3398 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3399 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3400 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3401 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3402 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3403 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3404 | return; |
| 3405 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3406 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3407 | bool typeOK; |
| 3408 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3409 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3410 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3411 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3412 | case AttributeList::AT_NSReturnsRetained: |
| 3413 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3414 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3415 | cf = false; |
| 3416 | break; |
| 3417 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3418 | case AttributeList::AT_CFReturnsRetained: |
| 3419 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3420 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3421 | cf = true; |
| 3422 | break; |
| 3423 | } |
| 3424 | |
| 3425 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3426 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3427 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3428 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3429 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3430 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3431 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3432 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3433 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3434 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3435 | D->addAttr(::new (S.Context) |
| 3436 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3437 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3438 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3439 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3440 | D->addAttr(::new (S.Context) |
| 3441 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3442 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3443 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3444 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3445 | D->addAttr(::new (S.Context) |
| 3446 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3447 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3448 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3449 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3450 | D->addAttr(::new (S.Context) |
| 3451 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3452 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3453 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3454 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3455 | D->addAttr(::new (S.Context) |
| 3456 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3457 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3458 | return; |
| 3459 | }; |
| 3460 | } |
| 3461 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3462 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3463 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3464 | const int EP_ObjCMethod = 1; |
| 3465 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3466 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3467 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3468 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3469 | if (isa<ObjCMethodDecl>(D)) |
| 3470 | resultType = cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3471 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3472 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3473 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3474 | if (!resultType->isReferenceType() && |
| 3475 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3476 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3477 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3478 | << attr.getName() |
| 3479 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3480 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3481 | |
| 3482 | // Drop the attribute. |
| 3483 | return; |
| 3484 | } |
| 3485 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3486 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3487 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3488 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3491 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3492 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3493 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3494 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3495 | DeclContext *DC = method->getDeclContext(); |
| 3496 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3497 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3498 | << attr.getName() << 0; |
| 3499 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3500 | return; |
| 3501 | } |
| 3502 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3503 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3504 | << attr.getName() << 1; |
| 3505 | return; |
| 3506 | } |
| 3507 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3508 | method->addAttr(::new (S.Context) |
| 3509 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3510 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3513 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3514 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3515 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3516 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3517 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3518 | D->addAttr(::new (S.Context) |
| 3519 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3520 | Attr.getAttributeSpellingListIndex())); |
| 3521 | } |
| 3522 | |
| 3523 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3524 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3525 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3526 | return; |
| 3527 | |
| 3528 | D->addAttr(::new (S.Context) |
| 3529 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3530 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3533 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3534 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3535 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3536 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3537 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3538 | 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] | 3539 | return; |
| 3540 | } |
| 3541 | |
| 3542 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3543 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3544 | Attr.getAttributeSpellingListIndex())); |
| 3545 | } |
| 3546 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3547 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3548 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3549 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3550 | |
| 3551 | if (!Parm) { |
| 3552 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3553 | return; |
| 3554 | } |
| 3555 | |
| 3556 | D->addAttr(::new (S.Context) |
| 3557 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3558 | Attr.getAttributeSpellingListIndex())); |
| 3559 | } |
| 3560 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3561 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3562 | const AttributeList &Attr) { |
| 3563 | IdentifierInfo *RelatedClass = |
| 3564 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0; |
| 3565 | if (!RelatedClass) { |
| 3566 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3567 | return; |
| 3568 | } |
| 3569 | IdentifierInfo *ClassMethod = |
| 3570 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0; |
| 3571 | IdentifierInfo *InstanceMethod = |
| 3572 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0; |
| 3573 | D->addAttr(::new (S.Context) |
| 3574 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3575 | ClassMethod, InstanceMethod, |
| 3576 | Attr.getAttributeSpellingListIndex())); |
| 3577 | } |
| 3578 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3579 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3580 | const AttributeList &Attr) { |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3581 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3582 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3583 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3584 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3585 | Attr.getAttributeSpellingListIndex())); |
| 3586 | } |
| 3587 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3588 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3589 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3590 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3591 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3592 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3593 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3596 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3597 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3598 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3599 | QualType type = vd->getType(); |
| 3600 | |
| 3601 | if (!type->isDependentType() && |
| 3602 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3603 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3604 | << type; |
| 3605 | return; |
| 3606 | } |
| 3607 | |
| 3608 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3609 | |
| 3610 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3611 | // going to infer. |
| 3612 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3613 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3614 | |
| 3615 | switch (lifetime) { |
| 3616 | case Qualifiers::OCL_None: |
| 3617 | assert(type->isDependentType() && |
| 3618 | "didn't infer lifetime for non-dependent type?"); |
| 3619 | break; |
| 3620 | |
| 3621 | case Qualifiers::OCL_Weak: // meaningful |
| 3622 | case Qualifiers::OCL_Strong: // meaningful |
| 3623 | break; |
| 3624 | |
| 3625 | case Qualifiers::OCL_ExplicitNone: |
| 3626 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3627 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3628 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3629 | break; |
| 3630 | } |
| 3631 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3632 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3633 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3634 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3637 | //===----------------------------------------------------------------------===// |
| 3638 | // Microsoft specific attribute handlers. |
| 3639 | //===----------------------------------------------------------------------===// |
| 3640 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3641 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3642 | if (!S.LangOpts.CPlusPlus) { |
| 3643 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3644 | << Attr.getName() << AttributeLangSupport::C; |
| 3645 | return; |
| 3646 | } |
| 3647 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3648 | if (!isa<CXXRecordDecl>(D)) { |
| 3649 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3650 | << Attr.getName() << ExpectedClass; |
| 3651 | return; |
| 3652 | } |
| 3653 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3654 | StringRef StrRef; |
| 3655 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3656 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3657 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3658 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3659 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3660 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3661 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3662 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3663 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3664 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3665 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3666 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3667 | return; |
| 3668 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3669 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3670 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3671 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3672 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3673 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3674 | return; |
| 3675 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3676 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3677 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3678 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3679 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3680 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3681 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3682 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3683 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3686 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 3687 | const AttributeList &Attr) { |
| 3688 | // Check the attribute arguments. |
| 3689 | if (Attr.getNumArgs() > 1) { |
| 3690 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3691 | << Attr.getName() << 1; |
| 3692 | return; |
| 3693 | } |
| 3694 | |
| 3695 | StringRef Str; |
| 3696 | SourceLocation ArgLoc; |
| 3697 | |
| 3698 | if (Attr.getNumArgs() == 0) |
| 3699 | Str = ""; |
| 3700 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 3701 | return; |
| 3702 | |
| 3703 | ARMInterruptAttr::InterruptType Kind; |
| 3704 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 3705 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3706 | << Attr.getName() << Str << ArgLoc; |
| 3707 | return; |
| 3708 | } |
| 3709 | |
| 3710 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3711 | D->addAttr(::new (S.Context) |
| 3712 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 3713 | } |
| 3714 | |
| 3715 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 3716 | const AttributeList &Attr) { |
| 3717 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3718 | return; |
| 3719 | |
| 3720 | if (!Attr.isArgExpr(0)) { |
| 3721 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3722 | << AANT_ArgumentIntegerConstant; |
| 3723 | return; |
| 3724 | } |
| 3725 | |
| 3726 | // FIXME: Check for decl - it should be void ()(void). |
| 3727 | |
| 3728 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 3729 | llvm::APSInt NumParams(32); |
| 3730 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 3731 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3732 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3733 | << NumParamsExpr->getSourceRange(); |
| 3734 | return; |
| 3735 | } |
| 3736 | |
| 3737 | unsigned Num = NumParams.getLimitedValue(255); |
| 3738 | if ((Num & 1) || Num > 30) { |
| 3739 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3740 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 3741 | << NumParamsExpr->getSourceRange(); |
| 3742 | return; |
| 3743 | } |
| 3744 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3745 | D->addAttr(::new (S.Context) |
| 3746 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 3747 | Attr.getAttributeSpellingListIndex())); |
| 3748 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3752 | // Dispatch the interrupt attribute based on the current target. |
| 3753 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 3754 | handleMSP430InterruptAttr(S, D, Attr); |
| 3755 | else |
| 3756 | handleARMInterruptAttr(S, D, Attr); |
| 3757 | } |
| 3758 | |
| 3759 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 3760 | const AttributeList& Attr) { |
| 3761 | // If we try to apply it to a function pointer, don't warn, but don't |
| 3762 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 3763 | // special about calling a force_align_arg_pointer function. |
| 3764 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3765 | if (VD && VD->getType()->isFunctionPointerType()) |
| 3766 | return; |
| 3767 | // Also don't warn on function pointer typedefs. |
| 3768 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 3769 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 3770 | TD->getUnderlyingType()->isFunctionType())) |
| 3771 | return; |
| 3772 | // Attribute can only be applied to function types. |
| 3773 | if (!isa<FunctionDecl>(D)) { |
| 3774 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3775 | << Attr.getName() << /* function */0; |
| 3776 | return; |
| 3777 | } |
| 3778 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3779 | D->addAttr(::new (S.Context) |
| 3780 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 3781 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 3785 | unsigned AttrSpellingListIndex) { |
| 3786 | if (D->hasAttr<DLLExportAttr>()) { |
| 3787 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport"; |
| 3788 | return NULL; |
| 3789 | } |
| 3790 | |
| 3791 | if (D->hasAttr<DLLImportAttr>()) |
| 3792 | return NULL; |
| 3793 | |
| 3794 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3795 | if (VD->hasDefinition()) { |
| 3796 | // dllimport cannot be applied to definitions. |
| 3797 | Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition) |
| 3798 | << "dllimport"; |
| 3799 | return NULL; |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | return ::new (Context)DLLImportAttr(Range, Context, |
| 3804 | AttrSpellingListIndex); |
| 3805 | } |
| 3806 | |
| 3807 | static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3808 | // Attribute can be applied only to functions or variables. |
| 3809 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3810 | if (!FD && !isa<VarDecl>(D)) { |
| 3811 | // Apparently Visual C++ thinks it is okay to not emit a warning |
| 3812 | // in this case, so only emit a warning when -fms-extensions is not |
| 3813 | // specified. |
| 3814 | if (!S.getLangOpts().MicrosoftExt) |
| 3815 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3816 | << Attr.getName() << 2 /*variable and function*/; |
| 3817 | return; |
| 3818 | } |
| 3819 | |
| 3820 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 3821 | // Warning is emitted. |
| 3822 | if (FD && FD->isInlineSpecified()) { |
| 3823 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3824 | return; |
| 3825 | } |
| 3826 | |
| 3827 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3828 | DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); |
| 3829 | if (NewAttr) |
| 3830 | D->addAttr(NewAttr); |
| 3831 | } |
| 3832 | |
| 3833 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 3834 | unsigned AttrSpellingListIndex) { |
| 3835 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
| 3836 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport"; |
| 3837 | D->dropAttr<DLLImportAttr>(); |
| 3838 | } |
| 3839 | |
| 3840 | if (D->hasAttr<DLLExportAttr>()) |
| 3841 | return NULL; |
| 3842 | |
| 3843 | return ::new (Context)DLLExportAttr(Range, Context, |
| 3844 | AttrSpellingListIndex); |
| 3845 | } |
| 3846 | |
| 3847 | static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3848 | // Currently, the dllexport attribute is ignored for inlined functions, unless |
| 3849 | // the -fkeep-inline-functions flag has been used. Warning is emitted; |
| 3850 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) { |
| 3851 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 3852 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3853 | return; |
| 3854 | } |
| 3855 | |
| 3856 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3857 | DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); |
| 3858 | if (NewAttr) |
| 3859 | D->addAttr(NewAttr); |
| 3860 | } |
| 3861 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3862 | /// Handles semantic checking for features that are common to all attributes, |
| 3863 | /// such as checking whether a parameter was properly specified, or the correct |
| 3864 | /// number of arguments were passed, etc. |
| 3865 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 3866 | const AttributeList &Attr) { |
| 3867 | // Several attributes carry different semantics than the parsing requires, so |
| 3868 | // those are opted out of the common handling. |
| 3869 | // |
| 3870 | // We also bail on unknown and ignored attributes because those are handled |
| 3871 | // as part of the target-specific handling logic. |
| 3872 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3873 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3874 | return false; |
| 3875 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3876 | // Check whether the attribute requires specific language extensions to be |
| 3877 | // enabled. |
| 3878 | if (!Attr.diagnoseLangOpts(S)) |
| 3879 | return true; |
| 3880 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3881 | // If there are no optional arguments, then checking for the argument count |
| 3882 | // is trivial. |
| 3883 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 3884 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 3885 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3886 | |
| 3887 | // Check whether the attribute appertains to the given subject. |
| 3888 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 3889 | return true; |
| 3890 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3891 | return false; |
| 3892 | } |
| 3893 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3894 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3895 | // Top Level Sema Entry Points |
| 3896 | //===----------------------------------------------------------------------===// |
| 3897 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3898 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3899 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3900 | /// silently ignore it if a GNU attribute. |
| 3901 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3902 | const AttributeList &Attr, |
| 3903 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3904 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3905 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3906 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3907 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 3908 | // instead. |
| 3909 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 3910 | return; |
| 3911 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3912 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 3913 | // which do not apply to the current target architecture are treated as |
| 3914 | // though they were unknown attributes. |
| 3915 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
| 3916 | !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) { |
| 3917 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 3918 | diag::warn_unhandled_ms_attribute_ignored : |
| 3919 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
| 3920 | return; |
| 3921 | } |
| 3922 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3923 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 3924 | return; |
| 3925 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3926 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3927 | default: |
| 3928 | // Type attributes are handled elsewhere; silently move on. |
| 3929 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 3930 | break; |
| 3931 | case AttributeList::AT_Interrupt: |
| 3932 | handleInterruptAttr(S, D, Attr); break; |
| 3933 | case AttributeList::AT_X86ForceAlignArgPointer: |
| 3934 | handleX86ForceAlignArgPointerAttr(S, D, Attr); break; |
| 3935 | case AttributeList::AT_DLLExport: |
| 3936 | handleDLLExportAttr(S, D, Attr); break; |
| 3937 | case AttributeList::AT_DLLImport: |
| 3938 | handleDLLImportAttr(S, D, Attr); break; |
| 3939 | case AttributeList::AT_Mips16: |
| 3940 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); break; |
| 3941 | case AttributeList::AT_NoMips16: |
| 3942 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 3943 | case AttributeList::AT_IBAction: |
| 3944 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3945 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 3946 | case AttributeList::AT_IBOutletCollection: |
| 3947 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3948 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 3949 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3950 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3951 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3952 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3953 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 3954 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3955 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 3956 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 3957 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 3958 | handleDependencyAttr(S, scope, D, Attr); |
| 3959 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3960 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3961 | case AttributeList::AT_CUDAConstant: |
| 3962 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3963 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3964 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 3965 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3966 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 3967 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 3968 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3969 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 3970 | case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3971 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3972 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3973 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 3974 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3975 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 3976 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3977 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 3978 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 3979 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 3980 | case AttributeList::AT_CUDADevice: |
| 3981 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3982 | case AttributeList::AT_CUDAHost: |
| 3983 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3984 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 3985 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3986 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3987 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3988 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3989 | case AttributeList::AT_MayAlias: |
| 3990 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3991 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3992 | case AttributeList::AT_NoCommon: |
| 3993 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 3994 | case AttributeList::AT_NonNull: |
| 3995 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 3996 | handleNonNullAttrParameter(S, PVD, Attr); |
| 3997 | else |
| 3998 | handleNonNullAttr(S, D, Attr); |
| 3999 | break; |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame^] | 4000 | case AttributeList::AT_ReturnsNonNull: |
| 4001 | handleReturnsNonNullAttr(S, D, Attr); |
| 4002 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4003 | case AttributeList::AT_Overloadable: |
| 4004 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 4005 | case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4006 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4007 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4008 | case AttributeList::AT_Naked: |
| 4009 | handleSimpleAttribute<NakedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4010 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4011 | case AttributeList::AT_NoThrow: |
| 4012 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4013 | case AttributeList::AT_CUDAShared: |
| 4014 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4015 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4016 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4017 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4018 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4019 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4020 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4021 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4022 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4023 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4024 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4025 | case AttributeList::AT_ObjCRequiresSuper: |
| 4026 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4027 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4028 | case AttributeList::AT_ObjCBridge: |
| 4029 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4030 | |
| 4031 | case AttributeList::AT_ObjCBridgeMutable: |
| 4032 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4033 | |
| 4034 | case AttributeList::AT_ObjCBridgeRelated: |
| 4035 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4036 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4037 | case AttributeList::AT_ObjCDesignatedInitializer: |
| 4038 | handleObjCDesignatedInitializer(S, D, Attr); break; |
| 4039 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4040 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4041 | handleCFAuditedTransferAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4042 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4043 | handleCFUnknownTransferAttr(S, D, Attr); break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4044 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4045 | case AttributeList::AT_CFConsumed: |
| 4046 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4047 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4048 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4049 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4050 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4051 | case AttributeList::AT_NSReturnsNotRetained: |
| 4052 | case AttributeList::AT_CFReturnsNotRetained: |
| 4053 | case AttributeList::AT_NSReturnsRetained: |
| 4054 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4055 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4056 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4057 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4058 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4059 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4060 | case AttributeList::AT_VecTypeHint: |
| 4061 | handleVecTypeHint(S, D, Attr); break; |
| 4062 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4063 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4064 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4065 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4066 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4067 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4068 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4069 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4070 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4071 | case AttributeList::AT_ArcWeakrefUnavailable: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4072 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4073 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4074 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4075 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4076 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 4077 | break; |
| 4078 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4079 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 4080 | case AttributeList::AT_Unused: |
| 4081 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4082 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4083 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4084 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4085 | case AttributeList::AT_Visibility: |
| 4086 | handleVisibilityAttr(S, D, Attr, false); |
| 4087 | break; |
| 4088 | case AttributeList::AT_TypeVisibility: |
| 4089 | handleVisibilityAttr(S, D, Attr, true); |
| 4090 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4091 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 6a42b5a | 2013-11-27 16:59:17 +0000 | [diff] [blame] | 4092 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4093 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4094 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4095 | case AttributeList::AT_Weak: |
| 4096 | handleSimpleAttribute<WeakAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4097 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4098 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4099 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4100 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4101 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4102 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4103 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4104 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4105 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4106 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4107 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4108 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4109 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4110 | case AttributeList::AT_Const: |
| 4111 | handleSimpleAttribute<ConstAttr>(S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4112 | case AttributeList::AT_Pure: |
| 4113 | handleSimpleAttribute<PureAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4114 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4115 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4116 | case AttributeList::AT_NoInline: |
| 4117 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4118 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4119 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4120 | case AttributeList::AT_StdCall: |
| 4121 | case AttributeList::AT_CDecl: |
| 4122 | case AttributeList::AT_FastCall: |
| 4123 | case AttributeList::AT_ThisCall: |
| 4124 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4125 | case AttributeList::AT_MSABI: |
| 4126 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4127 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4128 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4129 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4130 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4131 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4132 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4133 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4134 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 2689133 | 2014-01-14 17:41:53 +0000 | [diff] [blame] | 4135 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4136 | |
| 4137 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4138 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4139 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4140 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4141 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4142 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4143 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4144 | case AttributeList::AT_MSInheritance: |
| 4145 | handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4146 | case AttributeList::AT_ForceInline: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4147 | handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4148 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4149 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4150 | |
| 4151 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4152 | case AttributeList::AT_AssertExclusiveLock: |
| 4153 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4154 | break; |
| 4155 | case AttributeList::AT_AssertSharedLock: |
| 4156 | handleAssertSharedLockAttr(S, D, Attr); |
| 4157 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4158 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 4159 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4160 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4161 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4162 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4163 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4164 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4165 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4166 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4167 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4168 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4169 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4170 | break; |
| 4171 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4172 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4173 | break; |
| 4174 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4175 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4176 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4177 | case AttributeList::AT_Lockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4178 | handleSimpleAttribute<LockableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4179 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4180 | handleGuardedByAttr(S, D, Attr); |
| 4181 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4182 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4183 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4184 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4185 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4186 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4187 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4188 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4189 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4190 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4191 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4192 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4193 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4194 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4195 | handleLockReturnedAttr(S, D, Attr); |
| 4196 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4197 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4198 | handleLocksExcludedAttr(S, D, Attr); |
| 4199 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4200 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4201 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4202 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4203 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4204 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4205 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4206 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4207 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4208 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4209 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4210 | handleUnlockFunAttr(S, D, Attr); |
| 4211 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4212 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4213 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4214 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4215 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4216 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4217 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4218 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4219 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4220 | case AttributeList::AT_Consumable: |
| 4221 | handleConsumableAttr(S, D, Attr); |
| 4222 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4223 | case AttributeList::AT_ConsumableAutoCast: |
| 4224 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break; |
| 4225 | break; |
| 4226 | case AttributeList::AT_ConsumableSetOnRead: |
| 4227 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break; |
| 4228 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4229 | case AttributeList::AT_CallableWhen: |
| 4230 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4231 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4232 | case AttributeList::AT_ParamTypestate: |
| 4233 | handleParamTypestateAttr(S, D, Attr); |
| 4234 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4235 | case AttributeList::AT_ReturnTypestate: |
| 4236 | handleReturnTypestateAttr(S, D, Attr); |
| 4237 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4238 | case AttributeList::AT_SetTypestate: |
| 4239 | handleSetTypestateAttr(S, D, Attr); |
| 4240 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4241 | case AttributeList::AT_TestTypestate: |
| 4242 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4243 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4244 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4245 | // Type safety attributes. |
| 4246 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4247 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4248 | break; |
| 4249 | case AttributeList::AT_TypeTagForDatatype: |
| 4250 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4251 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4252 | } |
| 4253 | } |
| 4254 | |
| 4255 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4256 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4257 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4258 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4259 | bool IncludeCXX11Attributes) { |
| 4260 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4261 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4262 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4263 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4264 | // GCC accepts |
| 4265 | // static int a9 __attribute__((weakref)); |
| 4266 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4267 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 4268 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 4269 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4270 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4271 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4272 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4273 | |
| 4274 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4275 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4276 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 4277 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4278 | D->setInvalidDecl(); |
| 4279 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4280 | if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
| 4281 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4282 | D->setInvalidDecl(); |
| 4283 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4284 | if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
| 4285 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4286 | D->setInvalidDecl(); |
| 4287 | } |
| 4288 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4291 | // Annotation attributes are the only attributes allowed after an access |
| 4292 | // specifier. |
| 4293 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4294 | const AttributeList *AttrList) { |
| 4295 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4296 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4297 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4298 | } else { |
| 4299 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4300 | return true; |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | return false; |
| 4305 | } |
| 4306 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4307 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4308 | /// contains any decl attributes that we should warn about. |
| 4309 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4310 | for ( ; A; A = A->getNext()) { |
| 4311 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4312 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4313 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4314 | |
| 4315 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4316 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4317 | << A->getName() << A->getRange(); |
| 4318 | } else { |
| 4319 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4320 | << A->getName() << A->getRange(); |
| 4321 | } |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4326 | /// used to build a declaration, complain about any decl attributes |
| 4327 | /// which might be lying around on it. |
| 4328 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4329 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4330 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4331 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4332 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4333 | } |
| 4334 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4335 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4336 | /// \#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] | 4337 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4338 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4339 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4340 | NamedDecl *NewD = 0; |
| 4341 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4342 | FunctionDecl *NewFD; |
| 4343 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4344 | // FIXME: Mangling? |
| 4345 | // FIXME: Is the qualifier info correct? |
| 4346 | // FIXME: Is the DeclContext correct? |
| 4347 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4348 | Loc, Loc, DeclarationName(II), |
| 4349 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4350 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4351 | FD->hasPrototype(), |
| 4352 | false/*isConstexprSpecified*/); |
| 4353 | NewD = NewFD; |
| 4354 | |
| 4355 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4356 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4357 | |
| 4358 | // Fake up parameter variables; they are declared as if this were |
| 4359 | // a typedef. |
| 4360 | QualType FDTy = FD->getType(); |
| 4361 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4362 | SmallVector<ParmVarDecl*, 16> Params; |
| 4363 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4364 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4365 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4366 | Param->setScopeInfo(0, Params.size()); |
| 4367 | Params.push_back(Param); |
| 4368 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4369 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4370 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4371 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4372 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4373 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4374 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4375 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4376 | if (VD->getQualifier()) { |
| 4377 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4378 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4379 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4380 | } |
| 4381 | return NewD; |
| 4382 | } |
| 4383 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4384 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4385 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4386 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4387 | if (W.getUsed()) return; // only do this once |
| 4388 | W.setUsed(true); |
| 4389 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4390 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4391 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4392 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 4393 | W.getLocation())); |
| 4394 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4395 | WeakTopLevelDecl.push_back(NewD); |
| 4396 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4397 | // to insert Decl at TU scope, sorry. |
| 4398 | DeclContext *SavedContext = CurContext; |
| 4399 | CurContext = Context.getTranslationUnitDecl(); |
| 4400 | PushOnScopeChains(NewD, S); |
| 4401 | CurContext = SavedContext; |
| 4402 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4403 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4407 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4408 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4409 | // have to do this. |
| 4410 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4411 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4412 | NamedDecl *ND = NULL; |
| 4413 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4414 | if (VD->isExternC()) |
| 4415 | ND = VD; |
| 4416 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4417 | if (FD->isExternC()) |
| 4418 | ND = FD; |
| 4419 | if (ND) { |
| 4420 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4421 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4422 | = WeakUndeclaredIdentifiers.find(Id); |
| 4423 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4424 | WeakInfo W = I->second; |
| 4425 | DeclApplyPragmaWeak(S, ND, W); |
| 4426 | WeakUndeclaredIdentifiers[Id] = W; |
| 4427 | } |
| 4428 | } |
| 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4433 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4434 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4435 | /// 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] | 4436 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4437 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4438 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4439 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4440 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4441 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4442 | // position to the decl itself. This handles cases like: |
| 4443 | // int *__attr__(x)** D; |
| 4444 | // when X is a decl attribute. |
| 4445 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4446 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4447 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4448 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4449 | // Finally, apply any attributes on the decl itself. |
| 4450 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4451 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4452 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4453 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4454 | /// Is the given declaration allowed to use a forbidden type? |
| 4455 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4456 | // Private ivars are always okay. Unfortunately, people don't |
| 4457 | // always properly make their ivars private, even in system headers. |
| 4458 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4459 | // Function declarations in sys headers will be marked unavailable. |
| 4460 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4461 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4462 | return false; |
| 4463 | |
| 4464 | // Require it to be declared in a system header. |
| 4465 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4466 | } |
| 4467 | |
| 4468 | /// Handle a delayed forbidden-type diagnostic. |
| 4469 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4470 | Decl *decl) { |
| 4471 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4472 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, |
| 4473 | "this system declaration uses an unsupported type", |
| 4474 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4475 | return; |
| 4476 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4477 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4478 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4479 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4480 | // kind of forbidden type messages on unavailable functions. |
| 4481 | if (FD->hasAttr<UnavailableAttr>() && |
| 4482 | diag.getForbiddenTypeDiagnostic() == |
| 4483 | diag::err_arc_array_param_no_ownership) { |
| 4484 | diag.Triggered = true; |
| 4485 | return; |
| 4486 | } |
| 4487 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4488 | |
| 4489 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4490 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4491 | diag.Triggered = true; |
| 4492 | } |
| 4493 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4494 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4495 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4496 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4497 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4498 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4499 | // When delaying diagnostics to run in the context of a parsed |
| 4500 | // declaration, we only want to actually emit anything if parsing |
| 4501 | // succeeds. |
| 4502 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4503 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4504 | // We emit all the active diagnostics in this pool or any of its |
| 4505 | // parents. In general, we'll get one pool for the decl spec |
| 4506 | // and a child pool for each declarator; in a decl group like: |
| 4507 | // deprecated_typedef foo, *bar, baz(); |
| 4508 | // only the declarator pops will be passed decls. This is correct; |
| 4509 | // we really do need to consider delayed diagnostics from the decl spec |
| 4510 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4511 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4512 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4513 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4514 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4515 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4516 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4517 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4518 | continue; |
| 4519 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4520 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4521 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4522 | case DelayedDiagnostic::Unavailable: |
| 4523 | // Don't bother giving deprecation/unavailable diagnostics if |
| 4524 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4525 | if (!decl->isInvalidDecl()) |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4526 | HandleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4527 | break; |
| 4528 | |
| 4529 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4530 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4531 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4532 | |
| 4533 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4534 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4535 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4536 | } |
| 4537 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4538 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4541 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4542 | /// been delayed in the current context instead of in the given pool. |
| 4543 | /// Essentially, this just moves them to the current pool. |
| 4544 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4545 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4546 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4547 | curPool->steal(pool); |
| 4548 | } |
| 4549 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4550 | static bool isDeclDeprecated(Decl *D) { |
| 4551 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4552 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4553 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4554 | // A category implicitly has the availability of the interface. |
| 4555 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4556 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4557 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4558 | return false; |
| 4559 | } |
| 4560 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4561 | static bool isDeclUnavailable(Decl *D) { |
| 4562 | do { |
| 4563 | if (D->isUnavailable()) |
| 4564 | return true; |
| 4565 | // A category implicitly has the availability of the interface. |
| 4566 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4567 | return CatD->getClassInterface()->isUnavailable(); |
| 4568 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4569 | return false; |
| 4570 | } |
| 4571 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4572 | static void |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4573 | DoEmitAvailabilityWarning(Sema &S, |
| 4574 | DelayedDiagnostic::DDKind K, |
| 4575 | Decl *Ctx, |
| 4576 | const NamedDecl *D, |
| 4577 | StringRef Message, |
| 4578 | SourceLocation Loc, |
| 4579 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4580 | const ObjCPropertyDecl *ObjCProperty) { |
| 4581 | |
| 4582 | // Diagnostics for deprecated or unavailable. |
| 4583 | unsigned diag, diag_message, diag_fwdclass_message; |
| 4584 | |
| 4585 | // Matches 'diag::note_property_attribute' options. |
| 4586 | unsigned property_note_select; |
| 4587 | |
| 4588 | // Matches diag::note_availability_specified_here. |
| 4589 | unsigned available_here_select_kind; |
| 4590 | |
| 4591 | // Don't warn if our current context is deprecated or unavailable. |
| 4592 | switch (K) { |
| 4593 | case DelayedDiagnostic::Deprecation: |
| 4594 | if (isDeclDeprecated(Ctx)) |
| 4595 | return; |
| 4596 | diag = diag::warn_deprecated; |
| 4597 | diag_message = diag::warn_deprecated_message; |
| 4598 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 4599 | property_note_select = /* deprecated */ 0; |
| 4600 | available_here_select_kind = /* deprecated */ 2; |
| 4601 | break; |
| 4602 | |
| 4603 | case DelayedDiagnostic::Unavailable: |
| 4604 | if (isDeclUnavailable(Ctx)) |
| 4605 | return; |
| 4606 | diag = diag::err_unavailable; |
| 4607 | diag_message = diag::err_unavailable_message; |
| 4608 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 4609 | property_note_select = /* unavailable */ 1; |
| 4610 | available_here_select_kind = /* unavailable */ 0; |
| 4611 | break; |
| 4612 | |
| 4613 | default: |
| 4614 | llvm_unreachable("Neither a deprecation or unavailable kind"); |
| 4615 | } |
| 4616 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4617 | DeclarationName Name = D->getDeclName(); |
| 4618 | if (!Message.empty()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4619 | S.Diag(Loc, diag_message) << Name << Message; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4620 | if (ObjCProperty) |
| 4621 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4622 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4623 | } else if (!UnknownObjCClass) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4624 | S.Diag(Loc, diag) << Name; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4625 | if (ObjCProperty) |
| 4626 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4627 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4628 | } else { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4629 | S.Diag(Loc, diag_fwdclass_message) << Name; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4630 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4631 | } |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4632 | |
| 4633 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 4634 | << D << available_here_select_kind; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4637 | void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD, |
| 4638 | Decl *Ctx) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4639 | DD.Triggered = true; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4640 | DoEmitAvailabilityWarning(*this, |
| 4641 | (DelayedDiagnostic::DDKind) DD.Kind, |
| 4642 | Ctx, |
| 4643 | DD.getDeprecationDecl(), |
| 4644 | DD.getDeprecationMessage(), |
| 4645 | DD.Loc, |
| 4646 | DD.getUnknownObjCClass(), |
| 4647 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4650 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 4651 | NamedDecl *D, StringRef Message, |
| 4652 | SourceLocation Loc, |
| 4653 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4654 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4655 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4656 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4657 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D, |
| 4658 | UnknownObjCClass, |
| 4659 | ObjCProperty, |
| 4660 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4661 | return; |
| 4662 | } |
| 4663 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4664 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
| 4665 | DelayedDiagnostic::DDKind K; |
| 4666 | switch (AD) { |
| 4667 | case AD_Deprecation: |
| 4668 | K = DelayedDiagnostic::Deprecation; |
| 4669 | break; |
| 4670 | case AD_Unavailable: |
| 4671 | K = DelayedDiagnostic::Unavailable; |
| 4672 | break; |
| 4673 | } |
| 4674 | |
| 4675 | DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc, |
| 4676 | UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4677 | } |