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 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1159 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1160 | SmallVector<unsigned, 8> NonNullArgs; |
| 1161 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1162 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1163 | uint64_t Idx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 1164 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1165 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1166 | |
| 1167 | // Is the function argument a pointer type? |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1168 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1169 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1170 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1171 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1172 | // FIXME: Should also highlight argument in decl. |
Aaron Ballman | cedaaea | 2013-12-26 17:07:49 +0000 | [diff] [blame] | 1173 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1174 | << Attr.getName() << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1175 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1176 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1177 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1178 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1179 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1180 | |
| 1181 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1182 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1183 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1184 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1185 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1186 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1187 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1188 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1189 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1190 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1191 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1192 | if (NonNullArgs.empty()) { |
| 1193 | // Warn the trivial case only if attribute is not coming from a |
| 1194 | // macro instantiation. |
| 1195 | if (Attr.getLoc().isFileID()) |
| 1196 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1197 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1198 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1199 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1200 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1201 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1202 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1203 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1204 | D->addAttr(::new (S.Context) |
| 1205 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1206 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1209 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1210 | switch (K) { |
| 1211 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1212 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1213 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1214 | } |
| 1215 | llvm_unreachable("unknown ownership"); |
| 1216 | } |
| 1217 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1218 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1219 | // This attribute must be applied to a function declaration. The first |
| 1220 | // argument to the attribute must be an identifier, the name of the resource, |
| 1221 | // for example: malloc. The following arguments must be argument indexes, the |
| 1222 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1223 | // 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] | 1224 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1225 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1226 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1227 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1228 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1229 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1230 | return; |
| 1231 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1232 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1233 | // Figure out our Kind. |
| 1234 | OwnershipAttr::OwnershipKind K = |
| 1235 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1236 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1237 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1238 | // Check arguments. |
| 1239 | switch (K) { |
| 1240 | case OwnershipAttr::Takes: |
| 1241 | case OwnershipAttr::Holds: |
| 1242 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1243 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1244 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | } |
| 1247 | break; |
| 1248 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1249 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1250 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1251 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1252 | return; |
| 1253 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1254 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1257 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1260 | StringRef ModuleName = Module->getName(); |
| 1261 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1262 | ModuleName.size() > 4) { |
| 1263 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1264 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1265 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1266 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1267 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1268 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1269 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1270 | uint64_t Idx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 1271 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1272 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1273 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1274 | // Is the function argument a pointer type? |
| 1275 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1276 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1277 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1278 | case OwnershipAttr::Takes: |
| 1279 | case OwnershipAttr::Holds: |
| 1280 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1281 | Err = 0; |
| 1282 | break; |
| 1283 | case OwnershipAttr::Returns: |
| 1284 | if (!T->isIntegerType()) |
| 1285 | Err = 1; |
| 1286 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1287 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1288 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1289 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1290 | << Ex->getSourceRange(); |
| 1291 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1292 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1293 | |
| 1294 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1295 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1296 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1297 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1298 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1299 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1300 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1301 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1302 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1303 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1304 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1307 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | unsigned* start = OwnershipArgs.data(); |
| 1311 | unsigned size = OwnershipArgs.size(); |
| 1312 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1313 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1314 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1315 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1316 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1319 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1320 | // Check the attribute arguments. |
| 1321 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1322 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1323 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1324 | return; |
| 1325 | } |
| 1326 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1327 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1328 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1329 | // gcc rejects |
| 1330 | // class c { |
| 1331 | // static int a __attribute__((weakref ("v2"))); |
| 1332 | // static int b() __attribute__((weakref ("f3"))); |
| 1333 | // }; |
| 1334 | // and ignores the attributes of |
| 1335 | // void f(void) { |
| 1336 | // static int a __attribute__((weakref ("v2"))); |
| 1337 | // } |
| 1338 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1339 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1340 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1341 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1342 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1343 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | // The GCC manual says |
| 1347 | // |
| 1348 | // At present, a declaration to which `weakref' is attached can only |
| 1349 | // be `static'. |
| 1350 | // |
| 1351 | // It also says |
| 1352 | // |
| 1353 | // Without a TARGET, |
| 1354 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1355 | // equivalent to `weak'. |
| 1356 | // |
| 1357 | // gcc 4.4.1 will accept |
| 1358 | // int a7 __attribute__((weakref)); |
| 1359 | // as |
| 1360 | // int a7 __attribute__((weak)); |
| 1361 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1362 | // it if this behaviour is actually used. |
| 1363 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1364 | // GCC rejects |
| 1365 | // static ((alias ("y"), weakref)). |
| 1366 | // Should we? How to check that weakref is before or after alias? |
| 1367 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1368 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1369 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1370 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1371 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1372 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1373 | // GCC will accept anything as the argument of weakref. Should we |
| 1374 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1375 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1376 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1377 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1378 | D->addAttr(::new (S.Context) |
| 1379 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1380 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1383 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1384 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1385 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1386 | return; |
| 1387 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1388 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1389 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1390 | return; |
| 1391 | } |
| 1392 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1393 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1394 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1395 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1396 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1399 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1400 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1401 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1402 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1403 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1404 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1408 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1409 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1410 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1411 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1412 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1415 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1416 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1417 | StringRef Model; |
| 1418 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1419 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1420 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1421 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1422 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1423 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1424 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1425 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1426 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1427 | return; |
| 1428 | } |
| 1429 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1430 | D->addAttr(::new (S.Context) |
| 1431 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1432 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1435 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1436 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1438 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1439 | D->addAttr(::new (S.Context) |
| 1440 | MallocAttr(Attr.getRange(), S.Context, |
| 1441 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1442 | return; |
| 1443 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1446 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1449 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1450 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1451 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1452 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1453 | return; |
| 1454 | } |
| 1455 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1456 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1457 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1460 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1461 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (S.CheckNoReturnAttr(attr)) return; |
| 1464 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1465 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1466 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1467 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1468 | return; |
| 1469 | } |
| 1470 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1471 | D->addAttr(::new (S.Context) |
| 1472 | NoReturnAttr(attr.getRange(), S.Context, |
| 1473 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1477 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1478 | attr.setInvalid(); |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
| 1482 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1485 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1486 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1487 | |
| 1488 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1489 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1490 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1491 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1492 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1493 | && !VD->getType()->isFunctionPointerType())) { |
| 1494 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1495 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1496 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1497 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1498 | return; |
| 1499 | } |
| 1500 | } |
| 1501 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1502 | D->addAttr(::new (S.Context) |
| 1503 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1504 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1507 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1508 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1509 | /* |
| 1510 | Returning a Vector Class in Registers |
| 1511 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1512 | According to the PPU ABI specifications, a class with a single member of |
| 1513 | vector type is returned in memory when used as the return value of a function. |
| 1514 | This results in inefficient code when implementing vector classes. To return |
| 1515 | the value in a single vector register, add the vecreturn attribute to the |
| 1516 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1517 | |
| 1518 | Example: |
| 1519 | |
| 1520 | struct Vector |
| 1521 | { |
| 1522 | __vector float xyzw; |
| 1523 | } __attribute__((vecreturn)); |
| 1524 | |
| 1525 | Vector Add(Vector lhs, Vector rhs) |
| 1526 | { |
| 1527 | Vector result; |
| 1528 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1529 | return result; // This will be returned in a register |
| 1530 | } |
| 1531 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1532 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1533 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1534 | return; |
| 1535 | } |
| 1536 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1537 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1538 | int count = 0; |
| 1539 | |
| 1540 | if (!isa<CXXRecordDecl>(record)) { |
| 1541 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1542 | return; |
| 1543 | } |
| 1544 | |
| 1545 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1546 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1547 | return; |
| 1548 | } |
| 1549 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1550 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1551 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1552 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1553 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1554 | return; |
| 1555 | } |
| 1556 | count++; |
| 1557 | } |
| 1558 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1559 | D->addAttr(::new (S.Context) |
| 1560 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1561 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1564 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1565 | const AttributeList &Attr) { |
| 1566 | if (isa<ParmVarDecl>(D)) { |
| 1567 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1568 | // parameter of a function declaration or lambda. |
| 1569 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1570 | S.Diag(Attr.getLoc(), |
| 1571 | diag::err_carries_dependency_param_not_function_decl); |
| 1572 | return; |
| 1573 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1574 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1575 | |
| 1576 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1577 | Attr.getRange(), S.Context, |
| 1578 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1581 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1582 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1583 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1584 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1585 | return; |
| 1586 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1587 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1588 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1589 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1590 | return; |
| 1591 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1592 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1593 | D->addAttr(::new (S.Context) |
| 1594 | UsedAttr(Attr.getRange(), S.Context, |
| 1595 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1598 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1599 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1600 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1601 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1602 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1603 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1604 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1605 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1606 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1607 | if (Attr.getNumArgs() > 0 && |
| 1608 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1609 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1610 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1611 | D->addAttr(::new (S.Context) |
| 1612 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1613 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1616 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1617 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1618 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1619 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1620 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1621 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1622 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1623 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1624 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1625 | if (Attr.getNumArgs() > 0 && |
| 1626 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1627 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1628 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1629 | D->addAttr(::new (S.Context) |
| 1630 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1631 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1634 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1635 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1636 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1637 | unsigned NumArgs = Attr.getNumArgs(); |
| 1638 | if (NumArgs > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1639 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1640 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1641 | return; |
| 1642 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1643 | |
| 1644 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1645 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1646 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1647 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1648 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1649 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1650 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1653 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 1654 | const AttributeList &Attr) { |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1655 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1656 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1657 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1660 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1661 | IdentifierInfo *Platform, |
| 1662 | VersionTuple Introduced, |
| 1663 | VersionTuple Deprecated, |
| 1664 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1665 | StringRef PlatformName |
| 1666 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1667 | if (PlatformName.empty()) |
| 1668 | PlatformName = Platform->getName(); |
| 1669 | |
| 1670 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1671 | // of these steps are needed). |
| 1672 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1673 | !(Introduced <= Deprecated)) { |
| 1674 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1675 | << 1 << PlatformName << Deprecated.getAsString() |
| 1676 | << 0 << Introduced.getAsString(); |
| 1677 | return true; |
| 1678 | } |
| 1679 | |
| 1680 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1681 | !(Introduced <= Obsoleted)) { |
| 1682 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1683 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1684 | << 0 << Introduced.getAsString(); |
| 1685 | return true; |
| 1686 | } |
| 1687 | |
| 1688 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1689 | !(Deprecated <= Obsoleted)) { |
| 1690 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1691 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1692 | << 1 << Deprecated.getAsString(); |
| 1693 | return true; |
| 1694 | } |
| 1695 | |
| 1696 | return false; |
| 1697 | } |
| 1698 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1699 | /// \brief Check whether the two versions match. |
| 1700 | /// |
| 1701 | /// If either version tuple is empty, then they are assumed to match. If |
| 1702 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1703 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1704 | bool BeforeIsOkay) { |
| 1705 | if (X.empty() || Y.empty()) |
| 1706 | return true; |
| 1707 | |
| 1708 | if (X == Y) |
| 1709 | return true; |
| 1710 | |
| 1711 | if (BeforeIsOkay && X < Y) |
| 1712 | return true; |
| 1713 | |
| 1714 | return false; |
| 1715 | } |
| 1716 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1717 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1718 | IdentifierInfo *Platform, |
| 1719 | VersionTuple Introduced, |
| 1720 | VersionTuple Deprecated, |
| 1721 | VersionTuple Obsoleted, |
| 1722 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1723 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1724 | bool Override, |
| 1725 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1726 | VersionTuple MergedIntroduced = Introduced; |
| 1727 | VersionTuple MergedDeprecated = Deprecated; |
| 1728 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1729 | bool FoundAny = false; |
| 1730 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1731 | if (D->hasAttrs()) { |
| 1732 | AttrVec &Attrs = D->getAttrs(); |
| 1733 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1734 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1735 | if (!OldAA) { |
| 1736 | ++i; |
| 1737 | continue; |
| 1738 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1739 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1740 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1741 | if (OldPlatform != Platform) { |
| 1742 | ++i; |
| 1743 | continue; |
| 1744 | } |
| 1745 | |
| 1746 | FoundAny = true; |
| 1747 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1748 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1749 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1750 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1751 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1752 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1753 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1754 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1755 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1756 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1757 | if (Override) { |
| 1758 | int Which = -1; |
| 1759 | VersionTuple FirstVersion; |
| 1760 | VersionTuple SecondVersion; |
| 1761 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1762 | Which = 0; |
| 1763 | FirstVersion = OldIntroduced; |
| 1764 | SecondVersion = Introduced; |
| 1765 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1766 | Which = 1; |
| 1767 | FirstVersion = Deprecated; |
| 1768 | SecondVersion = OldDeprecated; |
| 1769 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1770 | Which = 2; |
| 1771 | FirstVersion = Obsoleted; |
| 1772 | SecondVersion = OldObsoleted; |
| 1773 | } |
| 1774 | |
| 1775 | if (Which == -1) { |
| 1776 | Diag(OldAA->getLocation(), |
| 1777 | diag::warn_mismatched_availability_override_unavail) |
| 1778 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1779 | } else { |
| 1780 | Diag(OldAA->getLocation(), |
| 1781 | diag::warn_mismatched_availability_override) |
| 1782 | << Which |
| 1783 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1784 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1785 | } |
| 1786 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1787 | } else { |
| 1788 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1789 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1790 | } |
| 1791 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1792 | Attrs.erase(Attrs.begin() + i); |
| 1793 | --e; |
| 1794 | continue; |
| 1795 | } |
| 1796 | |
| 1797 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1798 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1799 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1800 | |
| 1801 | if (MergedIntroduced2.empty()) |
| 1802 | MergedIntroduced2 = OldIntroduced; |
| 1803 | if (MergedDeprecated2.empty()) |
| 1804 | MergedDeprecated2 = OldDeprecated; |
| 1805 | if (MergedObsoleted2.empty()) |
| 1806 | MergedObsoleted2 = OldObsoleted; |
| 1807 | |
| 1808 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1809 | MergedIntroduced2, MergedDeprecated2, |
| 1810 | MergedObsoleted2)) { |
| 1811 | Attrs.erase(Attrs.begin() + i); |
| 1812 | --e; |
| 1813 | continue; |
| 1814 | } |
| 1815 | |
| 1816 | MergedIntroduced = MergedIntroduced2; |
| 1817 | MergedDeprecated = MergedDeprecated2; |
| 1818 | MergedObsoleted = MergedObsoleted2; |
| 1819 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1820 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | if (FoundAny && |
| 1824 | MergedIntroduced == Introduced && |
| 1825 | MergedDeprecated == Deprecated && |
| 1826 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1827 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1828 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1829 | // Only create a new attribute if !Override, but we want to do |
| 1830 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1831 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1832 | MergedDeprecated, MergedObsoleted) && |
| 1833 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1834 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1835 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1836 | Obsoleted, IsUnavailable, Message, |
| 1837 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1838 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1839 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1842 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1843 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1844 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1845 | return; |
| 1846 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1847 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1848 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1849 | IdentifierInfo *II = Platform->Ident; |
| 1850 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1851 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1852 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1853 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1854 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1855 | if (!ND) { |
| 1856 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1857 | return; |
| 1858 | } |
| 1859 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1860 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1861 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1862 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1863 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1864 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1865 | if (const StringLiteral *SE = |
| 1866 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1867 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1868 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1869 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1870 | Introduced.Version, |
| 1871 | Deprecated.Version, |
| 1872 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1873 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1874 | /*Override=*/false, |
| 1875 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1876 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1877 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1880 | template <class T> |
| 1881 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1882 | typename T::VisibilityType value, |
| 1883 | unsigned attrSpellingListIndex) { |
| 1884 | T *existingAttr = D->getAttr<T>(); |
| 1885 | if (existingAttr) { |
| 1886 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1887 | if (existingValue == value) |
| 1888 | return NULL; |
| 1889 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1890 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1891 | D->dropAttr<T>(); |
| 1892 | } |
| 1893 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1894 | } |
| 1895 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1896 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1897 | VisibilityAttr::VisibilityType Vis, |
| 1898 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1899 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1900 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1903 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1904 | TypeVisibilityAttr::VisibilityType Vis, |
| 1905 | unsigned AttrSpellingListIndex) { |
| 1906 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1907 | AttrSpellingListIndex); |
| 1908 | } |
| 1909 | |
| 1910 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1911 | bool isTypeVisibility) { |
| 1912 | // Visibility attributes don't mean anything on a typedef. |
| 1913 | if (isa<TypedefNameDecl>(D)) { |
| 1914 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 1915 | << Attr.getName(); |
| 1916 | return; |
| 1917 | } |
| 1918 | |
| 1919 | // 'type_visibility' can only go on a type or namespace. |
| 1920 | if (isTypeVisibility && |
| 1921 | !(isa<TagDecl>(D) || |
| 1922 | isa<ObjCInterfaceDecl>(D) || |
| 1923 | isa<NamespaceDecl>(D))) { |
| 1924 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 1925 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 1926 | return; |
| 1927 | } |
| 1928 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 1929 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1930 | StringRef TypeStr; |
| 1931 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1932 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1933 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1934 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1935 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1936 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1937 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1938 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1939 | return; |
| 1940 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1941 | |
| 1942 | // Complain about attempts to use protected visibility on targets |
| 1943 | // (like Darwin) that don't support it. |
| 1944 | if (type == VisibilityAttr::Protected && |
| 1945 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 1946 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 1947 | type = VisibilityAttr::Default; |
| 1948 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1949 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1950 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1951 | clang::Attr *newAttr; |
| 1952 | if (isTypeVisibility) { |
| 1953 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 1954 | (TypeVisibilityAttr::VisibilityType) type, |
| 1955 | Index); |
| 1956 | } else { |
| 1957 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 1958 | } |
| 1959 | if (newAttr) |
| 1960 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1963 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 1964 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1965 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1966 | if (!Attr.isArgIdent(0)) { |
| 1967 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 1968 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1969 | return; |
| 1970 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1971 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1972 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1973 | ObjCMethodFamilyAttr::FamilyKind F; |
| 1974 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 1975 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 1976 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1977 | return; |
| 1978 | } |
| 1979 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1980 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1981 | !method->getResultType()->isObjCObjectPointerType()) { |
| 1982 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 1983 | << method->getResultType(); |
| 1984 | // Ignore the attribute. |
| 1985 | return; |
| 1986 | } |
| 1987 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1988 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1989 | S.Context, F, |
| 1990 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1993 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1994 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1995 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 1996 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1997 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1998 | return; |
| 1999 | } |
| 2000 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2001 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2002 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2003 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2004 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2005 | return; |
| 2006 | } |
| 2007 | } |
| 2008 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2009 | // It is okay to include this attribute on properties, e.g.: |
| 2010 | // |
| 2011 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2012 | // |
| 2013 | // In this case it follows tradition and suppresses an error in the above |
| 2014 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2015 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2016 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2017 | D->addAttr(::new (S.Context) |
| 2018 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2019 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2022 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2023 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2024 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2025 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2026 | return; |
| 2027 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2028 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2029 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2030 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2031 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2032 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2033 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2034 | return; |
| 2035 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2036 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2037 | D->addAttr(::new (S.Context) |
| 2038 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2039 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2042 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2043 | // check the attribute arguments. |
| 2044 | if (Attr.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 2045 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 2046 | << Attr.getName() << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2047 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2050 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2051 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2052 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2053 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2054 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2055 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2056 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2057 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2058 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2059 | return; |
| 2060 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2061 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2062 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2063 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2064 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2065 | return; |
| 2066 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2067 | |
| 2068 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2071 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2072 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2073 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2074 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2075 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2076 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2077 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2078 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2079 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2080 | return; |
| 2081 | } |
| 2082 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2083 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2084 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2085 | // FIXME: This error message could be improved, it would be nice |
| 2086 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2087 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2088 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2089 | return; |
| 2090 | } |
| 2091 | } |
| 2092 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2093 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2094 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2095 | if (isa<FunctionNoProtoType>(FT)) { |
| 2096 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2097 | return; |
| 2098 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2099 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2100 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2101 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2102 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2103 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2104 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2105 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2106 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2107 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2108 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2109 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2110 | if (!BD->isVariadic()) { |
| 2111 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2112 | return; |
| 2113 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2114 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2115 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2116 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame^] | 2117 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2118 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2119 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2120 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2121 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2122 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2123 | return; |
| 2124 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2125 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2126 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2127 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2128 | return; |
| 2129 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2130 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2131 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2132 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2133 | return; |
| 2134 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2135 | D->addAttr(::new (S.Context) |
| 2136 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2137 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2140 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame^] | 2141 | if (D->getFunctionType() && D->getFunctionType()->getResultType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2142 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2143 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2144 | return; |
| 2145 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2146 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2147 | if (MD->getResultType()->isVoidType()) { |
| 2148 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2149 | << Attr.getName() << 1; |
| 2150 | return; |
| 2151 | } |
| 2152 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2153 | D->addAttr(::new (S.Context) |
| 2154 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2155 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2158 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2159 | // weak_import only applies to variable & function declarations. |
| 2160 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2161 | if (!D->canBeWeakImported(isDef)) { |
| 2162 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2163 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2164 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2165 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2166 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2167 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2168 | // Nothing to warn about here. |
| 2169 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +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() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2172 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2173 | return; |
| 2174 | } |
| 2175 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2176 | D->addAttr(::new (S.Context) |
| 2177 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2178 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2181 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2182 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2183 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2184 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2185 | uint32_t WGSize[3]; |
| 2186 | for (unsigned i = 0; i < 3; ++i) |
| 2187 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2188 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2189 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2190 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2191 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2192 | Existing->getYDim() == WGSize[1] && |
| 2193 | Existing->getZDim() == WGSize[2])) |
| 2194 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2195 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2196 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2197 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2198 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2201 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2202 | if (!Attr.hasParsedType()) { |
| 2203 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2204 | << Attr.getName() << 1; |
| 2205 | return; |
| 2206 | } |
| 2207 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2208 | TypeSourceInfo *ParmTSI = 0; |
| 2209 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2210 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2211 | |
| 2212 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2213 | (ParmType->isBooleanType() || |
| 2214 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2215 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2216 | << ParmType; |
| 2217 | return; |
| 2218 | } |
| 2219 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2220 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2221 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2222 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2223 | return; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2228 | ParmTSI, |
| 2229 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2232 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2233 | StringRef Name, |
| 2234 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2235 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2236 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2237 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2238 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2239 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2240 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2241 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2242 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2243 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2246 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2247 | // Make sure that there is a string literal as the sections's single |
| 2248 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2249 | StringRef Str; |
| 2250 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2251 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2252 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2254 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2255 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2256 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2257 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2258 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2259 | return; |
| 2260 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2262 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2263 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2264 | if (NewAttr) |
| 2265 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2268 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2269 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2270 | VarDecl *VD = cast<VarDecl>(D); |
| 2271 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2272 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2273 | return; |
| 2274 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2275 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2276 | Expr *E = Attr.getArgAsExpr(0); |
| 2277 | SourceLocation Loc = E->getExprLoc(); |
| 2278 | FunctionDecl *FD = 0; |
| 2279 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2280 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2281 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2282 | // will warn the user. |
| 2283 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2284 | if (DRE->hasQualifier()) |
| 2285 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2286 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2287 | NI = DRE->getNameInfo(); |
| 2288 | if (!FD) { |
| 2289 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2290 | << NI.getName(); |
| 2291 | return; |
| 2292 | } |
| 2293 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2294 | if (ULE->hasExplicitTemplateArgs()) |
| 2295 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2296 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2297 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2298 | if (!FD) { |
| 2299 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2300 | << NI.getName(); |
| 2301 | if (ULE->getType() == S.Context.OverloadTy) |
| 2302 | S.NoteAllOverloadCandidates(ULE); |
| 2303 | return; |
| 2304 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2305 | } else { |
| 2306 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2307 | return; |
| 2308 | } |
| 2309 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2310 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2311 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2312 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2313 | return; |
| 2314 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2315 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2316 | // We're currently more strict than GCC about what function types we accept. |
| 2317 | // If this ever proves to be a problem it should be easy to fix. |
| 2318 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2319 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2320 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2321 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2322 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2323 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2324 | return; |
| 2325 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2326 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2327 | D->addAttr(::new (S.Context) |
| 2328 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2329 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2332 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2333 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2334 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2335 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2336 | uint64_t ArgIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2337 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2338 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2339 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2340 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2341 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2342 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2343 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2344 | if (not_nsstring_type && |
| 2345 | !isCFStringType(Ty, S.Context) && |
| 2346 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2347 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2348 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2349 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2350 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2351 | << IdxExpr->getSourceRange(); |
| 2352 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2353 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2354 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2355 | if (!isNSStringType(Ty, S.Context) && |
| 2356 | !isCFStringType(Ty, S.Context) && |
| 2357 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2358 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2359 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2360 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2361 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2362 | << IdxExpr->getSourceRange(); |
| 2363 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2366 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2367 | // because that has corrected for the implicit this parameter, and is zero- |
| 2368 | // based. The attribute expects what the user wrote explicitly. |
| 2369 | llvm::APSInt Val; |
| 2370 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2371 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2372 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2373 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2374 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2377 | enum FormatAttrKind { |
| 2378 | CFStringFormat, |
| 2379 | NSStringFormat, |
| 2380 | StrftimeFormat, |
| 2381 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2382 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2383 | InvalidFormat |
| 2384 | }; |
| 2385 | |
| 2386 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2387 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2388 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2389 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2390 | // Check for formats that get handled specially. |
| 2391 | .Case("NSString", NSStringFormat) |
| 2392 | .Case("CFString", CFStringFormat) |
| 2393 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2394 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2395 | // Otherwise, check for supported formats. |
| 2396 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2397 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2398 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2399 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2400 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2401 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2404 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2405 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2406 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2407 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2408 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2409 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2410 | return; |
| 2411 | } |
| 2412 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2413 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2414 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2415 | Attr.setInvalid(); |
| 2416 | return; |
| 2417 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2418 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2419 | if (S.Context.getAsArrayType(T)) |
| 2420 | T = S.Context.getBaseElementType(T); |
| 2421 | if (!T->getAs<RecordType>()) { |
| 2422 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2423 | Attr.setInvalid(); |
| 2424 | return; |
| 2425 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2426 | |
| 2427 | Expr *E = Attr.getArgAsExpr(0); |
| 2428 | uint32_t prioritynum; |
| 2429 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2430 | Attr.setInvalid(); |
| 2431 | return; |
| 2432 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2433 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2434 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2435 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2436 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2437 | Attr.setInvalid(); |
| 2438 | return; |
| 2439 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2440 | D->addAttr(::new (S.Context) |
| 2441 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2442 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2445 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2446 | IdentifierInfo *Format, int FormatIdx, |
| 2447 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2448 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2449 | // Check whether we already have an equivalent format attribute. |
| 2450 | for (specific_attr_iterator<FormatAttr> |
| 2451 | i = D->specific_attr_begin<FormatAttr>(), |
| 2452 | e = D->specific_attr_end<FormatAttr>(); |
| 2453 | i != e ; ++i) { |
| 2454 | FormatAttr *f = *i; |
| 2455 | if (f->getType() == Format && |
| 2456 | f->getFormatIdx() == FormatIdx && |
| 2457 | f->getFirstArg() == FirstArg) { |
| 2458 | // If we don't have a valid location for this attribute, adopt the |
| 2459 | // location. |
| 2460 | if (f->getLocation().isInvalid()) |
| 2461 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2462 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2463 | } |
| 2464 | } |
| 2465 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2466 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2467 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2470 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2471 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2472 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2473 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2474 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2475 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2476 | return; |
| 2477 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2478 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2479 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2480 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2481 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2482 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2483 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2484 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2485 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2486 | |
| 2487 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2488 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2489 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2490 | // If we've modified the string name, we need a new identifier for it. |
| 2491 | II = &S.Context.Idents.get(Format); |
| 2492 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2493 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2494 | // Check for supported formats. |
| 2495 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2496 | |
| 2497 | if (Kind == IgnoredFormat) |
| 2498 | return; |
| 2499 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2500 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2501 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2502 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2503 | return; |
| 2504 | } |
| 2505 | |
| 2506 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2507 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2508 | uint32_t Idx; |
| 2509 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2510 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2511 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2512 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2513 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2514 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2515 | return; |
| 2516 | } |
| 2517 | |
| 2518 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2519 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2520 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2521 | if (HasImplicitThisParam) { |
| 2522 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2523 | S.Diag(Attr.getLoc(), |
| 2524 | diag::err_format_attribute_implicit_this_format_string) |
| 2525 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2526 | return; |
| 2527 | } |
| 2528 | ArgIdx--; |
| 2529 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2531 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2532 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2533 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2534 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2535 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2536 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2537 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2538 | return; |
| 2539 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2540 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2541 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2542 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2543 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2544 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2545 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2546 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2547 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2548 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2549 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2550 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2551 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2552 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2553 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2554 | return; |
| 2555 | } |
| 2556 | |
| 2557 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2558 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2559 | uint32_t FirstArg; |
| 2560 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2561 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2562 | |
| 2563 | // check if the function is variadic if the 3rd argument non-zero |
| 2564 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2565 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2566 | ++NumArgs; // +1 for ... |
| 2567 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2568 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2569 | return; |
| 2570 | } |
| 2571 | } |
| 2572 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2573 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2574 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2575 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2576 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2577 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2578 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2582 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2583 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2584 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2585 | return; |
| 2586 | } |
| 2587 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2588 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2589 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2590 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2591 | if (NewAttr) |
| 2592 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2595 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2596 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2597 | // Try to find the underlying union declaration. |
| 2598 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2599 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2600 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2601 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2602 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2603 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2604 | |
| 2605 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2606 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2607 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2608 | return; |
| 2609 | } |
| 2610 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2611 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2612 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2613 | diag::warn_transparent_union_attribute_not_definition); |
| 2614 | return; |
| 2615 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2616 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2617 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2618 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2619 | if (Field == FieldEnd) { |
| 2620 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2621 | return; |
| 2622 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2623 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2624 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2625 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2626 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2627 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2628 | diag::warn_transparent_union_attribute_floating) |
| 2629 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2630 | return; |
| 2631 | } |
| 2632 | |
| 2633 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2634 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2635 | for (; Field != FieldEnd; ++Field) { |
| 2636 | QualType FieldType = Field->getType(); |
| 2637 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2638 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2639 | // Warn if we drop the attribute. |
| 2640 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2641 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2642 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2643 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2644 | diag::warn_transparent_union_attribute_field_size_align) |
| 2645 | << isSize << Field->getDeclName() << FieldBits; |
| 2646 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2647 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2648 | diag::note_transparent_union_first_field_size_align) |
| 2649 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2650 | return; |
| 2651 | } |
| 2652 | } |
| 2653 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2654 | RD->addAttr(::new (S.Context) |
| 2655 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2656 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2659 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2660 | // Make sure that there is a string literal as the annotation's single |
| 2661 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2662 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2663 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2664 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2665 | |
| 2666 | // Don't duplicate annotations that are already set. |
| 2667 | for (specific_attr_iterator<AnnotateAttr> |
| 2668 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2669 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2670 | if ((*i)->getAnnotation() == Str) |
| 2671 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2672 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2673 | |
| 2674 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2675 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2676 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2679 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2680 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2681 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2682 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2683 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2684 | return; |
| 2685 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2686 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2687 | if (Attr.getNumArgs() == 0) { |
| 2688 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2689 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2690 | return; |
| 2691 | } |
| 2692 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2693 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2694 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2695 | S.Diag(Attr.getEllipsisLoc(), |
| 2696 | diag::err_pack_expansion_without_parameter_packs); |
| 2697 | return; |
| 2698 | } |
| 2699 | |
| 2700 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2701 | return; |
| 2702 | |
| 2703 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2704 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2708 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2709 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2710 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2711 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2712 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2713 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2714 | // C++11 [dcl.align]p1: |
| 2715 | // An alignment-specifier may be applied to a variable or to a class |
| 2716 | // data member, but it shall not be applied to a bit-field, a function |
| 2717 | // parameter, the formal parameter of a catch clause, or a variable |
| 2718 | // declared with the register storage class specifier. An |
| 2719 | // alignment-specifier may also be applied to the declaration of a class |
| 2720 | // or enumeration type. |
| 2721 | // C11 6.7.5/2: |
| 2722 | // An alignment attribute shall not be specified in a declaration of |
| 2723 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2724 | // object declared with the register storage-class specifier. |
| 2725 | int DiagKind = -1; |
| 2726 | if (isa<ParmVarDecl>(D)) { |
| 2727 | DiagKind = 0; |
| 2728 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2729 | if (VD->getStorageClass() == SC_Register) |
| 2730 | DiagKind = 1; |
| 2731 | if (VD->isExceptionVariable()) |
| 2732 | DiagKind = 2; |
| 2733 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2734 | if (FD->isBitField()) |
| 2735 | DiagKind = 3; |
| 2736 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2737 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2738 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2739 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
| 2742 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2743 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2744 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2745 | return; |
| 2746 | } |
| 2747 | } |
| 2748 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2749 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2750 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2751 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2752 | AA->setPackExpansion(IsPackExpansion); |
| 2753 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2754 | return; |
| 2755 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2756 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2757 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2758 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2759 | ExprResult ICE |
| 2760 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2761 | diag::err_aligned_attribute_argument_not_int, |
| 2762 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2763 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2764 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2765 | |
| 2766 | // C++11 [dcl.align]p2: |
| 2767 | // -- if the constant expression evaluates to zero, the alignment |
| 2768 | // specifier shall have no effect |
| 2769 | // C11 6.7.5p6: |
| 2770 | // An alignment specification of zero has no effect. |
| 2771 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2772 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2773 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2774 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2775 | return; |
| 2776 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2777 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2778 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2779 | // We've already verified it's a power of 2, now let's make sure it's |
| 2780 | // 8192 or less. |
| 2781 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2782 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2783 | << E->getSourceRange(); |
| 2784 | return; |
| 2785 | } |
| 2786 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2787 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2788 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 2789 | ICE.take(), SpellingListIndex); |
| 2790 | AA->setPackExpansion(IsPackExpansion); |
| 2791 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2794 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2795 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2796 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2797 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2798 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2799 | SpellingListIndex); |
| 2800 | AA->setPackExpansion(IsPackExpansion); |
| 2801 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2802 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2803 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2804 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2805 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2806 | |
| 2807 | QualType Ty; |
| 2808 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2809 | Ty = VD->getType(); |
| 2810 | else |
| 2811 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 2812 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2813 | return; |
| 2814 | |
| 2815 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2816 | // The combined effect of all alignment attributes in a declaration shall |
| 2817 | // not specify an alignment that is less strict than the alignment that |
| 2818 | // would otherwise be required for the entity being declared. |
| 2819 | AlignedAttr *AlignasAttr = 0; |
| 2820 | unsigned Align = 0; |
| 2821 | for (specific_attr_iterator<AlignedAttr> |
| 2822 | I = D->specific_attr_begin<AlignedAttr>(), |
| 2823 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 2824 | if (I->isAlignmentDependent()) |
| 2825 | return; |
| 2826 | if (I->isAlignas()) |
| 2827 | AlignasAttr = *I; |
| 2828 | Align = std::max(Align, I->getAlignment(Context)); |
| 2829 | } |
| 2830 | |
| 2831 | if (AlignasAttr && Align) { |
| 2832 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 2833 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 2834 | if (NaturalAlign > RequestedAlign) |
| 2835 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 2836 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 2837 | } |
| 2838 | } |
| 2839 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2840 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2841 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2842 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2843 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2844 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2845 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2846 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2847 | // This attribute isn't documented, but glibc uses it. It changes |
| 2848 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2849 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 2850 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 2851 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2852 | return; |
| 2853 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2854 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2855 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 2856 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2857 | |
| 2858 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2859 | if (Str.startswith("__") && Str.endswith("__")) |
| 2860 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2861 | |
| 2862 | unsigned DestWidth = 0; |
| 2863 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2864 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2865 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2866 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2867 | switch (Str[0]) { |
| 2868 | case 'Q': DestWidth = 8; break; |
| 2869 | case 'H': DestWidth = 16; break; |
| 2870 | case 'S': DestWidth = 32; break; |
| 2871 | case 'D': DestWidth = 64; break; |
| 2872 | case 'X': DestWidth = 96; break; |
| 2873 | case 'T': DestWidth = 128; break; |
| 2874 | } |
| 2875 | if (Str[1] == 'F') { |
| 2876 | IntegerMode = false; |
| 2877 | } else if (Str[1] == 'C') { |
| 2878 | IntegerMode = false; |
| 2879 | ComplexMode = true; |
| 2880 | } else if (Str[1] != 'I') { |
| 2881 | DestWidth = 0; |
| 2882 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2883 | break; |
| 2884 | case 4: |
| 2885 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2886 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2887 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2888 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2889 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2890 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2891 | break; |
| 2892 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2893 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2894 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2895 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2896 | case 11: |
| 2897 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 2898 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2899 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2903 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2904 | OldTy = TD->getUnderlyingType(); |
| 2905 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2906 | OldTy = VD->getType(); |
| 2907 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2908 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 2909 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2910 | return; |
| 2911 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2912 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2913 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2914 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2915 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2916 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2917 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2918 | } else if (ComplexMode) { |
| 2919 | if (!OldTy->isComplexType()) |
| 2920 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2921 | } else { |
| 2922 | if (!OldTy->isFloatingType()) |
| 2923 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2924 | } |
| 2925 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2926 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2927 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2928 | // FIXME: Make sure floating-point mappings are accurate |
| 2929 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2930 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2931 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2932 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | QualType NewTy; |
| 2936 | |
| 2937 | if (IntegerMode) |
| 2938 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 2939 | OldTy->isSignedIntegerType()); |
| 2940 | else |
| 2941 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 2942 | |
| 2943 | if (NewTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2944 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2945 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2948 | if (ComplexMode) { |
| 2949 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 2953 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 2954 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 2955 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2956 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 2957 | |
| 2958 | D->addAttr(::new (S.Context) |
| 2959 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 2960 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2961 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2962 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2963 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 2964 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2965 | if (!VD->hasGlobalStorage()) |
| 2966 | S.Diag(Attr.getLoc(), |
| 2967 | diag::warn_attribute_requires_functions_or_static_globals) |
| 2968 | << Attr.getName(); |
| 2969 | } else if (!isFunctionOrMethod(D)) { |
| 2970 | S.Diag(Attr.getLoc(), |
| 2971 | diag::warn_attribute_requires_functions_or_static_globals) |
| 2972 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2973 | return; |
| 2974 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2975 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2976 | D->addAttr(::new (S.Context) |
| 2977 | NoDebugAttr(Attr.getRange(), S.Context, |
| 2978 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2981 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 2982 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 2983 | if (!FD->getResultType()->isVoidType()) { |
| 2984 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| 2985 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
| 2986 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2987 | << FD->getType() |
| 2988 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
| 2989 | "void"); |
| 2990 | } else { |
| 2991 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2992 | << FD->getType(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2993 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 2994 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2995 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2996 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 2997 | D->addAttr(::new (S.Context) |
| 2998 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2999 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3002 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3003 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3004 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3005 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3006 | return; |
| 3007 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3008 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3009 | D->addAttr(::new (S.Context) |
| 3010 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3011 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3014 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3015 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3016 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3017 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3018 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3019 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3020 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3021 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3022 | return; |
| 3023 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3024 | if (!isa<ObjCMethodDecl>(D)) { |
| 3025 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3026 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3027 | return; |
| 3028 | } |
| 3029 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3030 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3031 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3032 | D->addAttr(::new (S.Context) |
| 3033 | FastCallAttr(Attr.getRange(), S.Context, |
| 3034 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3035 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3036 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3037 | D->addAttr(::new (S.Context) |
| 3038 | StdCallAttr(Attr.getRange(), S.Context, |
| 3039 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3040 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3041 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3042 | D->addAttr(::new (S.Context) |
| 3043 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3044 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3045 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3046 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3047 | D->addAttr(::new (S.Context) |
| 3048 | CDeclAttr(Attr.getRange(), S.Context, |
| 3049 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3050 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3051 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3052 | D->addAttr(::new (S.Context) |
| 3053 | PascalAttr(Attr.getRange(), S.Context, |
| 3054 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3055 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3056 | case AttributeList::AT_MSABI: |
| 3057 | D->addAttr(::new (S.Context) |
| 3058 | MSABIAttr(Attr.getRange(), S.Context, |
| 3059 | Attr.getAttributeSpellingListIndex())); |
| 3060 | return; |
| 3061 | case AttributeList::AT_SysVABI: |
| 3062 | D->addAttr(::new (S.Context) |
| 3063 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3064 | Attr.getAttributeSpellingListIndex())); |
| 3065 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3066 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3067 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3068 | switch (CC) { |
| 3069 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3070 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3071 | break; |
| 3072 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3073 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3074 | break; |
| 3075 | default: |
| 3076 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3079 | D->addAttr(::new (S.Context) |
| 3080 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3081 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3082 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3083 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3084 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3085 | D->addAttr(::new (S.Context) |
| 3086 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3087 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3088 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3089 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3090 | D->addAttr(::new (S.Context) |
| 3091 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3092 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3093 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3094 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3095 | default: |
| 3096 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3100 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3101 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3102 | if (attr.isInvalid()) |
| 3103 | return true; |
| 3104 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3105 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3106 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3107 | attr.setInvalid(); |
| 3108 | return true; |
| 3109 | } |
| 3110 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3111 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3112 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3113 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3114 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3115 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3116 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3117 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3118 | case AttributeList::AT_MSABI: |
| 3119 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3120 | CC_X86_64Win64; |
| 3121 | break; |
| 3122 | case AttributeList::AT_SysVABI: |
| 3123 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3124 | CC_C; |
| 3125 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3126 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3127 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3128 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3129 | attr.setInvalid(); |
| 3130 | return true; |
| 3131 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3132 | if (StrRef == "aapcs") { |
| 3133 | CC = CC_AAPCS; |
| 3134 | break; |
| 3135 | } else if (StrRef == "aapcs-vfp") { |
| 3136 | CC = CC_AAPCS_VFP; |
| 3137 | break; |
| 3138 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3139 | |
| 3140 | attr.setInvalid(); |
| 3141 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3142 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3143 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3144 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3145 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3146 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3149 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3150 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3151 | if (A == TargetInfo::CCCR_Warning) { |
| 3152 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3153 | |
| 3154 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3155 | if (FD) |
| 3156 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3157 | TargetInfo::CCMT_NonMember; |
| 3158 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3161 | return false; |
| 3162 | } |
| 3163 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3164 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3165 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3166 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3167 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3168 | return true; |
| 3169 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3170 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3171 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3172 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3173 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3174 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3175 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3176 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3177 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3178 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3179 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3182 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3183 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3184 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3185 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3186 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3189 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3190 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3191 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3192 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3193 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3194 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3197 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3200 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3201 | const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3202 | // check the attribute arguments. |
| 3203 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| 3204 | // FIXME: 0 is not okay. |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 3205 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3206 | << Attr.getName() << 2; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3207 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3208 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3209 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3210 | uint32_t MaxThreads, MinBlocks = 0; |
| 3211 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3212 | return; |
| 3213 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3214 | Attr.getArgAsExpr(1), |
| 3215 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3216 | return; |
| 3217 | |
| 3218 | D->addAttr(::new (S.Context) |
| 3219 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3220 | MaxThreads, MinBlocks, |
| 3221 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3224 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3225 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3226 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3227 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3228 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3229 | return; |
| 3230 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3231 | |
| 3232 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3233 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3234 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3235 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3236 | |
| 3237 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3238 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3239 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3240 | return; |
| 3241 | } |
| 3242 | |
| 3243 | uint64_t ArgumentIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3244 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3245 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3246 | return; |
| 3247 | |
| 3248 | uint64_t TypeTagIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3249 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3250 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3251 | return; |
| 3252 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3253 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3254 | if (IsPointer) { |
| 3255 | // Ensure that buffer has a pointer type. |
| 3256 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3257 | if (!BufferTy->isPointerType()) { |
| 3258 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3259 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3260 | } |
| 3261 | } |
| 3262 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3263 | D->addAttr(::new (S.Context) |
| 3264 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3265 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3266 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3270 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3271 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3272 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3273 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3274 | return; |
| 3275 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3276 | |
| 3277 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3278 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3279 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3280 | if (!isa<VarDecl>(D)) { |
| 3281 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3282 | << Attr.getName() << ExpectedVariable; |
| 3283 | return; |
| 3284 | } |
| 3285 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3286 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3287 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3288 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3289 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3290 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3291 | D->addAttr(::new (S.Context) |
| 3292 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3293 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3294 | Attr.getLayoutCompatible(), |
| 3295 | Attr.getMustBeNull(), |
| 3296 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3299 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3300 | // Checker-specific attribute handlers. |
| 3301 | //===----------------------------------------------------------------------===// |
| 3302 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3303 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3304 | return type->isDependentType() || |
| 3305 | type->isObjCObjectPointerType() || |
| 3306 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3307 | } |
| 3308 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3309 | return type->isDependentType() || |
| 3310 | type->isPointerType() || |
| 3311 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3314 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3315 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3316 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3317 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3318 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3319 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3320 | cf = false; |
| 3321 | } else { |
| 3322 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3323 | cf = true; |
| 3324 | } |
| 3325 | |
| 3326 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3327 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3328 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3329 | return; |
| 3330 | } |
| 3331 | |
| 3332 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3333 | param->addAttr(::new (S.Context) |
| 3334 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3335 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3336 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3337 | param->addAttr(::new (S.Context) |
| 3338 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3339 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3342 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3343 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3344 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3345 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3346 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3347 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3348 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3349 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3350 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3351 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3352 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3353 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3354 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3355 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3356 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3357 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3358 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3359 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3360 | return; |
| 3361 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3362 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3363 | bool typeOK; |
| 3364 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3365 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3366 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3367 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3368 | case AttributeList::AT_NSReturnsRetained: |
| 3369 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3370 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3371 | cf = false; |
| 3372 | break; |
| 3373 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3374 | case AttributeList::AT_CFReturnsRetained: |
| 3375 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3376 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3377 | cf = true; |
| 3378 | break; |
| 3379 | } |
| 3380 | |
| 3381 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3382 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3383 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3384 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3385 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3386 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3387 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3388 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3389 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3390 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3391 | D->addAttr(::new (S.Context) |
| 3392 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3393 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3394 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3395 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3396 | D->addAttr(::new (S.Context) |
| 3397 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3398 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3399 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3400 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3401 | D->addAttr(::new (S.Context) |
| 3402 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3403 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3404 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3405 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3406 | D->addAttr(::new (S.Context) |
| 3407 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3408 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3409 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3410 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3411 | D->addAttr(::new (S.Context) |
| 3412 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3413 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3414 | return; |
| 3415 | }; |
| 3416 | } |
| 3417 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3418 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3419 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3420 | const int EP_ObjCMethod = 1; |
| 3421 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3422 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3423 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3424 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3425 | if (isa<ObjCMethodDecl>(D)) |
| 3426 | resultType = cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3427 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3428 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3429 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3430 | if (!resultType->isReferenceType() && |
| 3431 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3432 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3433 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3434 | << attr.getName() |
| 3435 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3436 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3437 | |
| 3438 | // Drop the attribute. |
| 3439 | return; |
| 3440 | } |
| 3441 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3442 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3443 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3444 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3447 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3448 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3449 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3450 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3451 | DeclContext *DC = method->getDeclContext(); |
| 3452 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3453 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3454 | << attr.getName() << 0; |
| 3455 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3456 | return; |
| 3457 | } |
| 3458 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3459 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3460 | << attr.getName() << 1; |
| 3461 | return; |
| 3462 | } |
| 3463 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3464 | method->addAttr(::new (S.Context) |
| 3465 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3466 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3469 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3470 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3471 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3472 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3473 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3474 | D->addAttr(::new (S.Context) |
| 3475 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3476 | Attr.getAttributeSpellingListIndex())); |
| 3477 | } |
| 3478 | |
| 3479 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3480 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3481 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3482 | return; |
| 3483 | |
| 3484 | D->addAttr(::new (S.Context) |
| 3485 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3486 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3489 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3490 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3491 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3492 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3493 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3494 | 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] | 3495 | return; |
| 3496 | } |
| 3497 | |
| 3498 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3499 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3500 | Attr.getAttributeSpellingListIndex())); |
| 3501 | } |
| 3502 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3503 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3504 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3505 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3506 | |
| 3507 | if (!Parm) { |
| 3508 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3509 | return; |
| 3510 | } |
| 3511 | |
| 3512 | D->addAttr(::new (S.Context) |
| 3513 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3514 | Attr.getAttributeSpellingListIndex())); |
| 3515 | } |
| 3516 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3517 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3518 | const AttributeList &Attr) { |
| 3519 | IdentifierInfo *RelatedClass = |
| 3520 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0; |
| 3521 | if (!RelatedClass) { |
| 3522 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3523 | return; |
| 3524 | } |
| 3525 | IdentifierInfo *ClassMethod = |
| 3526 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0; |
| 3527 | IdentifierInfo *InstanceMethod = |
| 3528 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0; |
| 3529 | D->addAttr(::new (S.Context) |
| 3530 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3531 | ClassMethod, InstanceMethod, |
| 3532 | Attr.getAttributeSpellingListIndex())); |
| 3533 | } |
| 3534 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3535 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3536 | const AttributeList &Attr) { |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3537 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3538 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3539 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3540 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3541 | Attr.getAttributeSpellingListIndex())); |
| 3542 | } |
| 3543 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3544 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3545 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3546 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3547 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3548 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3549 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3552 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3553 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3554 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3555 | QualType type = vd->getType(); |
| 3556 | |
| 3557 | if (!type->isDependentType() && |
| 3558 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3559 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3560 | << type; |
| 3561 | return; |
| 3562 | } |
| 3563 | |
| 3564 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3565 | |
| 3566 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3567 | // going to infer. |
| 3568 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3569 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3570 | |
| 3571 | switch (lifetime) { |
| 3572 | case Qualifiers::OCL_None: |
| 3573 | assert(type->isDependentType() && |
| 3574 | "didn't infer lifetime for non-dependent type?"); |
| 3575 | break; |
| 3576 | |
| 3577 | case Qualifiers::OCL_Weak: // meaningful |
| 3578 | case Qualifiers::OCL_Strong: // meaningful |
| 3579 | break; |
| 3580 | |
| 3581 | case Qualifiers::OCL_ExplicitNone: |
| 3582 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3583 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3584 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3585 | break; |
| 3586 | } |
| 3587 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3588 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3589 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3590 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3593 | //===----------------------------------------------------------------------===// |
| 3594 | // Microsoft specific attribute handlers. |
| 3595 | //===----------------------------------------------------------------------===// |
| 3596 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3597 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3598 | if (!S.LangOpts.CPlusPlus) { |
| 3599 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3600 | << Attr.getName() << AttributeLangSupport::C; |
| 3601 | return; |
| 3602 | } |
| 3603 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3604 | if (!isa<CXXRecordDecl>(D)) { |
| 3605 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3606 | << Attr.getName() << ExpectedClass; |
| 3607 | return; |
| 3608 | } |
| 3609 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3610 | StringRef StrRef; |
| 3611 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3612 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3613 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3614 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3615 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3616 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3617 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3618 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3619 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3620 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3621 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3622 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3623 | return; |
| 3624 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3625 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3626 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3627 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3628 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3629 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3630 | return; |
| 3631 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3632 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3633 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3634 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3635 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3636 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3637 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3638 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3639 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3642 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 3643 | const AttributeList &Attr) { |
| 3644 | // Check the attribute arguments. |
| 3645 | if (Attr.getNumArgs() > 1) { |
| 3646 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3647 | << Attr.getName() << 1; |
| 3648 | return; |
| 3649 | } |
| 3650 | |
| 3651 | StringRef Str; |
| 3652 | SourceLocation ArgLoc; |
| 3653 | |
| 3654 | if (Attr.getNumArgs() == 0) |
| 3655 | Str = ""; |
| 3656 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 3657 | return; |
| 3658 | |
| 3659 | ARMInterruptAttr::InterruptType Kind; |
| 3660 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 3661 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3662 | << Attr.getName() << Str << ArgLoc; |
| 3663 | return; |
| 3664 | } |
| 3665 | |
| 3666 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3667 | D->addAttr(::new (S.Context) |
| 3668 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 3669 | } |
| 3670 | |
| 3671 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 3672 | const AttributeList &Attr) { |
| 3673 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3674 | return; |
| 3675 | |
| 3676 | if (!Attr.isArgExpr(0)) { |
| 3677 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3678 | << AANT_ArgumentIntegerConstant; |
| 3679 | return; |
| 3680 | } |
| 3681 | |
| 3682 | // FIXME: Check for decl - it should be void ()(void). |
| 3683 | |
| 3684 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 3685 | llvm::APSInt NumParams(32); |
| 3686 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 3687 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3688 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3689 | << NumParamsExpr->getSourceRange(); |
| 3690 | return; |
| 3691 | } |
| 3692 | |
| 3693 | unsigned Num = NumParams.getLimitedValue(255); |
| 3694 | if ((Num & 1) || Num > 30) { |
| 3695 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3696 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 3697 | << NumParamsExpr->getSourceRange(); |
| 3698 | return; |
| 3699 | } |
| 3700 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3701 | D->addAttr(::new (S.Context) |
| 3702 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 3703 | Attr.getAttributeSpellingListIndex())); |
| 3704 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3708 | // Dispatch the interrupt attribute based on the current target. |
| 3709 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 3710 | handleMSP430InterruptAttr(S, D, Attr); |
| 3711 | else |
| 3712 | handleARMInterruptAttr(S, D, Attr); |
| 3713 | } |
| 3714 | |
| 3715 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 3716 | const AttributeList& Attr) { |
| 3717 | // If we try to apply it to a function pointer, don't warn, but don't |
| 3718 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 3719 | // special about calling a force_align_arg_pointer function. |
| 3720 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3721 | if (VD && VD->getType()->isFunctionPointerType()) |
| 3722 | return; |
| 3723 | // Also don't warn on function pointer typedefs. |
| 3724 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 3725 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 3726 | TD->getUnderlyingType()->isFunctionType())) |
| 3727 | return; |
| 3728 | // Attribute can only be applied to function types. |
| 3729 | if (!isa<FunctionDecl>(D)) { |
| 3730 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3731 | << Attr.getName() << /* function */0; |
| 3732 | return; |
| 3733 | } |
| 3734 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3735 | D->addAttr(::new (S.Context) |
| 3736 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 3737 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 3741 | unsigned AttrSpellingListIndex) { |
| 3742 | if (D->hasAttr<DLLExportAttr>()) { |
| 3743 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport"; |
| 3744 | return NULL; |
| 3745 | } |
| 3746 | |
| 3747 | if (D->hasAttr<DLLImportAttr>()) |
| 3748 | return NULL; |
| 3749 | |
| 3750 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3751 | if (VD->hasDefinition()) { |
| 3752 | // dllimport cannot be applied to definitions. |
| 3753 | Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition) |
| 3754 | << "dllimport"; |
| 3755 | return NULL; |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | return ::new (Context)DLLImportAttr(Range, Context, |
| 3760 | AttrSpellingListIndex); |
| 3761 | } |
| 3762 | |
| 3763 | static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3764 | // Attribute can be applied only to functions or variables. |
| 3765 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3766 | if (!FD && !isa<VarDecl>(D)) { |
| 3767 | // Apparently Visual C++ thinks it is okay to not emit a warning |
| 3768 | // in this case, so only emit a warning when -fms-extensions is not |
| 3769 | // specified. |
| 3770 | if (!S.getLangOpts().MicrosoftExt) |
| 3771 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3772 | << Attr.getName() << 2 /*variable and function*/; |
| 3773 | return; |
| 3774 | } |
| 3775 | |
| 3776 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 3777 | // Warning is emitted. |
| 3778 | if (FD && FD->isInlineSpecified()) { |
| 3779 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3780 | return; |
| 3781 | } |
| 3782 | |
| 3783 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3784 | DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); |
| 3785 | if (NewAttr) |
| 3786 | D->addAttr(NewAttr); |
| 3787 | } |
| 3788 | |
| 3789 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 3790 | unsigned AttrSpellingListIndex) { |
| 3791 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
| 3792 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport"; |
| 3793 | D->dropAttr<DLLImportAttr>(); |
| 3794 | } |
| 3795 | |
| 3796 | if (D->hasAttr<DLLExportAttr>()) |
| 3797 | return NULL; |
| 3798 | |
| 3799 | return ::new (Context)DLLExportAttr(Range, Context, |
| 3800 | AttrSpellingListIndex); |
| 3801 | } |
| 3802 | |
| 3803 | static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3804 | // Currently, the dllexport attribute is ignored for inlined functions, unless |
| 3805 | // the -fkeep-inline-functions flag has been used. Warning is emitted; |
| 3806 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) { |
| 3807 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 3808 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3809 | return; |
| 3810 | } |
| 3811 | |
| 3812 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3813 | DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); |
| 3814 | if (NewAttr) |
| 3815 | D->addAttr(NewAttr); |
| 3816 | } |
| 3817 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3818 | /// Handles semantic checking for features that are common to all attributes, |
| 3819 | /// such as checking whether a parameter was properly specified, or the correct |
| 3820 | /// number of arguments were passed, etc. |
| 3821 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 3822 | const AttributeList &Attr) { |
| 3823 | // Several attributes carry different semantics than the parsing requires, so |
| 3824 | // those are opted out of the common handling. |
| 3825 | // |
| 3826 | // We also bail on unknown and ignored attributes because those are handled |
| 3827 | // as part of the target-specific handling logic. |
| 3828 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3829 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3830 | return false; |
| 3831 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3832 | // Check whether the attribute requires specific language extensions to be |
| 3833 | // enabled. |
| 3834 | if (!Attr.diagnoseLangOpts(S)) |
| 3835 | return true; |
| 3836 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3837 | // If there are no optional arguments, then checking for the argument count |
| 3838 | // is trivial. |
| 3839 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 3840 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 3841 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3842 | |
| 3843 | // Check whether the attribute appertains to the given subject. |
| 3844 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 3845 | return true; |
| 3846 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3847 | return false; |
| 3848 | } |
| 3849 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3850 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3851 | // Top Level Sema Entry Points |
| 3852 | //===----------------------------------------------------------------------===// |
| 3853 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3854 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3855 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3856 | /// silently ignore it if a GNU attribute. |
| 3857 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3858 | const AttributeList &Attr, |
| 3859 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3860 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3861 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3862 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3863 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 3864 | // instead. |
| 3865 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 3866 | return; |
| 3867 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3868 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 3869 | // which do not apply to the current target architecture are treated as |
| 3870 | // though they were unknown attributes. |
| 3871 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
| 3872 | !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) { |
| 3873 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 3874 | diag::warn_unhandled_ms_attribute_ignored : |
| 3875 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
| 3876 | return; |
| 3877 | } |
| 3878 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3879 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 3880 | return; |
| 3881 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3882 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3883 | default: |
| 3884 | // Type attributes are handled elsewhere; silently move on. |
| 3885 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 3886 | break; |
| 3887 | case AttributeList::AT_Interrupt: |
| 3888 | handleInterruptAttr(S, D, Attr); break; |
| 3889 | case AttributeList::AT_X86ForceAlignArgPointer: |
| 3890 | handleX86ForceAlignArgPointerAttr(S, D, Attr); break; |
| 3891 | case AttributeList::AT_DLLExport: |
| 3892 | handleDLLExportAttr(S, D, Attr); break; |
| 3893 | case AttributeList::AT_DLLImport: |
| 3894 | handleDLLImportAttr(S, D, Attr); break; |
| 3895 | case AttributeList::AT_Mips16: |
| 3896 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); break; |
| 3897 | case AttributeList::AT_NoMips16: |
| 3898 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 3899 | case AttributeList::AT_IBAction: |
| 3900 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3901 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 3902 | case AttributeList::AT_IBOutletCollection: |
| 3903 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3904 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 3905 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3906 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3907 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3908 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3909 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 3910 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3911 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 3912 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 3913 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 3914 | handleDependencyAttr(S, scope, D, Attr); |
| 3915 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3916 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3917 | case AttributeList::AT_CUDAConstant: |
| 3918 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3919 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3920 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 3921 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3922 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 3923 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 3924 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3925 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 3926 | case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3927 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3928 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3929 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 3930 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3931 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 3932 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3933 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 3934 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 3935 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 3936 | case AttributeList::AT_CUDADevice: |
| 3937 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3938 | case AttributeList::AT_CUDAHost: |
| 3939 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3940 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 3941 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3942 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3943 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3944 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3945 | case AttributeList::AT_MayAlias: |
| 3946 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3947 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3948 | case AttributeList::AT_NoCommon: |
| 3949 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3950 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3951 | case AttributeList::AT_Overloadable: |
| 3952 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 3953 | case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3954 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 3955 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3956 | case AttributeList::AT_Naked: |
| 3957 | handleSimpleAttribute<NakedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3958 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 3959 | case AttributeList::AT_NoThrow: |
| 3960 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3961 | case AttributeList::AT_CUDAShared: |
| 3962 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3963 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3964 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3965 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3966 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3967 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3968 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3969 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3970 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3971 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 3972 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3973 | case AttributeList::AT_ObjCRequiresSuper: |
| 3974 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 3975 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3976 | case AttributeList::AT_ObjCBridge: |
| 3977 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3978 | |
| 3979 | case AttributeList::AT_ObjCBridgeMutable: |
| 3980 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3981 | |
| 3982 | case AttributeList::AT_ObjCBridgeRelated: |
| 3983 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3984 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3985 | case AttributeList::AT_ObjCDesignatedInitializer: |
| 3986 | handleObjCDesignatedInitializer(S, D, Attr); break; |
| 3987 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3988 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3989 | handleCFAuditedTransferAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3990 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3991 | handleCFUnknownTransferAttr(S, D, Attr); break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3992 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3993 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3994 | case AttributeList::AT_CFConsumed: |
| 3995 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 3996 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3997 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3998 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3999 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4000 | case AttributeList::AT_NSReturnsNotRetained: |
| 4001 | case AttributeList::AT_CFReturnsNotRetained: |
| 4002 | case AttributeList::AT_NSReturnsRetained: |
| 4003 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4004 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4005 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4006 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4007 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4008 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4009 | case AttributeList::AT_VecTypeHint: |
| 4010 | handleVecTypeHint(S, D, Attr); break; |
| 4011 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4012 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4013 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4014 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4015 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4016 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4017 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4018 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4019 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4020 | case AttributeList::AT_ArcWeakrefUnavailable: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4021 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4022 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4023 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4024 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4025 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 4026 | break; |
| 4027 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4028 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame^] | 4029 | case AttributeList::AT_Unused: |
| 4030 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4031 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4032 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4033 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4034 | case AttributeList::AT_Visibility: |
| 4035 | handleVisibilityAttr(S, D, Attr, false); |
| 4036 | break; |
| 4037 | case AttributeList::AT_TypeVisibility: |
| 4038 | handleVisibilityAttr(S, D, Attr, true); |
| 4039 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4040 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 6a42b5a | 2013-11-27 16:59:17 +0000 | [diff] [blame] | 4041 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4042 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4043 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4044 | case AttributeList::AT_Weak: |
| 4045 | handleSimpleAttribute<WeakAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4046 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4047 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4048 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4049 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4050 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4051 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4052 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4053 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4054 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4055 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4056 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4057 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4058 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4059 | case AttributeList::AT_Const: |
| 4060 | handleSimpleAttribute<ConstAttr>(S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4061 | case AttributeList::AT_Pure: |
| 4062 | handleSimpleAttribute<PureAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4063 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4064 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4065 | case AttributeList::AT_NoInline: |
| 4066 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4067 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4068 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4069 | case AttributeList::AT_StdCall: |
| 4070 | case AttributeList::AT_CDecl: |
| 4071 | case AttributeList::AT_FastCall: |
| 4072 | case AttributeList::AT_ThisCall: |
| 4073 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4074 | case AttributeList::AT_MSABI: |
| 4075 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4076 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4077 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4078 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4079 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4080 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4081 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4082 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4083 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 2689133 | 2014-01-14 17:41:53 +0000 | [diff] [blame] | 4084 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4085 | |
| 4086 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4087 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4088 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4089 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4090 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4091 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4092 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4093 | case AttributeList::AT_MSInheritance: |
| 4094 | handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4095 | case AttributeList::AT_ForceInline: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4096 | handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4097 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4098 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4099 | |
| 4100 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4101 | case AttributeList::AT_AssertExclusiveLock: |
| 4102 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4103 | break; |
| 4104 | case AttributeList::AT_AssertSharedLock: |
| 4105 | handleAssertSharedLockAttr(S, D, Attr); |
| 4106 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4107 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 4108 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4109 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4110 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4111 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4112 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4113 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4114 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4115 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4116 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4117 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4118 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4119 | break; |
| 4120 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4121 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4122 | break; |
| 4123 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4124 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4125 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4126 | case AttributeList::AT_Lockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4127 | handleSimpleAttribute<LockableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4128 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4129 | handleGuardedByAttr(S, D, Attr); |
| 4130 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4131 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4132 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4133 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4134 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4135 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4136 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4137 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4138 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4139 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4140 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4141 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4142 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4143 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4144 | handleLockReturnedAttr(S, D, Attr); |
| 4145 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4146 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4147 | handleLocksExcludedAttr(S, D, Attr); |
| 4148 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4149 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4150 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4151 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4152 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4153 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4154 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4155 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4156 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4157 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4158 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4159 | handleUnlockFunAttr(S, D, Attr); |
| 4160 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4161 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4162 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4163 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4164 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4165 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4166 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4167 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4168 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4169 | case AttributeList::AT_Consumable: |
| 4170 | handleConsumableAttr(S, D, Attr); |
| 4171 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4172 | case AttributeList::AT_ConsumableAutoCast: |
| 4173 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break; |
| 4174 | break; |
| 4175 | case AttributeList::AT_ConsumableSetOnRead: |
| 4176 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break; |
| 4177 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4178 | case AttributeList::AT_CallableWhen: |
| 4179 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4180 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4181 | case AttributeList::AT_ParamTypestate: |
| 4182 | handleParamTypestateAttr(S, D, Attr); |
| 4183 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4184 | case AttributeList::AT_ReturnTypestate: |
| 4185 | handleReturnTypestateAttr(S, D, Attr); |
| 4186 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4187 | case AttributeList::AT_SetTypestate: |
| 4188 | handleSetTypestateAttr(S, D, Attr); |
| 4189 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4190 | case AttributeList::AT_TestTypestate: |
| 4191 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4192 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4193 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4194 | // Type safety attributes. |
| 4195 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4196 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4197 | break; |
| 4198 | case AttributeList::AT_TypeTagForDatatype: |
| 4199 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4200 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4205 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4206 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4207 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4208 | bool IncludeCXX11Attributes) { |
| 4209 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4210 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4211 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4212 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4213 | // GCC accepts |
| 4214 | // static int a9 __attribute__((weakref)); |
| 4215 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4216 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 4217 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 4218 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4219 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4220 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4221 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4222 | |
| 4223 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4224 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4225 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 4226 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4227 | D->setInvalidDecl(); |
| 4228 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4229 | if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
| 4230 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4231 | D->setInvalidDecl(); |
| 4232 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4233 | if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
| 4234 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4235 | D->setInvalidDecl(); |
| 4236 | } |
| 4237 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4240 | // Annotation attributes are the only attributes allowed after an access |
| 4241 | // specifier. |
| 4242 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4243 | const AttributeList *AttrList) { |
| 4244 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4245 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4246 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4247 | } else { |
| 4248 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4249 | return true; |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | return false; |
| 4254 | } |
| 4255 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4256 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4257 | /// contains any decl attributes that we should warn about. |
| 4258 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4259 | for ( ; A; A = A->getNext()) { |
| 4260 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4261 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4262 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4263 | |
| 4264 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4265 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4266 | << A->getName() << A->getRange(); |
| 4267 | } else { |
| 4268 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4269 | << A->getName() << A->getRange(); |
| 4270 | } |
| 4271 | } |
| 4272 | } |
| 4273 | |
| 4274 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4275 | /// used to build a declaration, complain about any decl attributes |
| 4276 | /// which might be lying around on it. |
| 4277 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4278 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4279 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4280 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4281 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4282 | } |
| 4283 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4284 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4285 | /// \#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] | 4286 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4287 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4288 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4289 | NamedDecl *NewD = 0; |
| 4290 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4291 | FunctionDecl *NewFD; |
| 4292 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4293 | // FIXME: Mangling? |
| 4294 | // FIXME: Is the qualifier info correct? |
| 4295 | // FIXME: Is the DeclContext correct? |
| 4296 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4297 | Loc, Loc, DeclarationName(II), |
| 4298 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4299 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4300 | FD->hasPrototype(), |
| 4301 | false/*isConstexprSpecified*/); |
| 4302 | NewD = NewFD; |
| 4303 | |
| 4304 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4305 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4306 | |
| 4307 | // Fake up parameter variables; they are declared as if this were |
| 4308 | // a typedef. |
| 4309 | QualType FDTy = FD->getType(); |
| 4310 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4311 | SmallVector<ParmVarDecl*, 16> Params; |
| 4312 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4313 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4314 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4315 | Param->setScopeInfo(0, Params.size()); |
| 4316 | Params.push_back(Param); |
| 4317 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4318 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4319 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4320 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4321 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4322 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4323 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4324 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4325 | if (VD->getQualifier()) { |
| 4326 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4327 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4328 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4329 | } |
| 4330 | return NewD; |
| 4331 | } |
| 4332 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4333 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4334 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4335 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4336 | if (W.getUsed()) return; // only do this once |
| 4337 | W.setUsed(true); |
| 4338 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4339 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4340 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4341 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 4342 | W.getLocation())); |
| 4343 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4344 | WeakTopLevelDecl.push_back(NewD); |
| 4345 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4346 | // to insert Decl at TU scope, sorry. |
| 4347 | DeclContext *SavedContext = CurContext; |
| 4348 | CurContext = Context.getTranslationUnitDecl(); |
| 4349 | PushOnScopeChains(NewD, S); |
| 4350 | CurContext = SavedContext; |
| 4351 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4352 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4353 | } |
| 4354 | } |
| 4355 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4356 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4357 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4358 | // have to do this. |
| 4359 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4360 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4361 | NamedDecl *ND = NULL; |
| 4362 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4363 | if (VD->isExternC()) |
| 4364 | ND = VD; |
| 4365 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4366 | if (FD->isExternC()) |
| 4367 | ND = FD; |
| 4368 | if (ND) { |
| 4369 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4370 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4371 | = WeakUndeclaredIdentifiers.find(Id); |
| 4372 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4373 | WeakInfo W = I->second; |
| 4374 | DeclApplyPragmaWeak(S, ND, W); |
| 4375 | WeakUndeclaredIdentifiers[Id] = W; |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4382 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4383 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4384 | /// 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] | 4385 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4386 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4387 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4388 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4389 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4390 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4391 | // position to the decl itself. This handles cases like: |
| 4392 | // int *__attr__(x)** D; |
| 4393 | // when X is a decl attribute. |
| 4394 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4395 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4396 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4397 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4398 | // Finally, apply any attributes on the decl itself. |
| 4399 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4400 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4401 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4402 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4403 | /// Is the given declaration allowed to use a forbidden type? |
| 4404 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4405 | // Private ivars are always okay. Unfortunately, people don't |
| 4406 | // always properly make their ivars private, even in system headers. |
| 4407 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4408 | // Function declarations in sys headers will be marked unavailable. |
| 4409 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4410 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4411 | return false; |
| 4412 | |
| 4413 | // Require it to be declared in a system header. |
| 4414 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4415 | } |
| 4416 | |
| 4417 | /// Handle a delayed forbidden-type diagnostic. |
| 4418 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4419 | Decl *decl) { |
| 4420 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4421 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, |
| 4422 | "this system declaration uses an unsupported type", |
| 4423 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4424 | return; |
| 4425 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4426 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4427 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4428 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4429 | // kind of forbidden type messages on unavailable functions. |
| 4430 | if (FD->hasAttr<UnavailableAttr>() && |
| 4431 | diag.getForbiddenTypeDiagnostic() == |
| 4432 | diag::err_arc_array_param_no_ownership) { |
| 4433 | diag.Triggered = true; |
| 4434 | return; |
| 4435 | } |
| 4436 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4437 | |
| 4438 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4439 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4440 | diag.Triggered = true; |
| 4441 | } |
| 4442 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4443 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4444 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4445 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4446 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4447 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4448 | // When delaying diagnostics to run in the context of a parsed |
| 4449 | // declaration, we only want to actually emit anything if parsing |
| 4450 | // succeeds. |
| 4451 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4452 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4453 | // We emit all the active diagnostics in this pool or any of its |
| 4454 | // parents. In general, we'll get one pool for the decl spec |
| 4455 | // and a child pool for each declarator; in a decl group like: |
| 4456 | // deprecated_typedef foo, *bar, baz(); |
| 4457 | // only the declarator pops will be passed decls. This is correct; |
| 4458 | // we really do need to consider delayed diagnostics from the decl spec |
| 4459 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4460 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4461 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4462 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4463 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4464 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4465 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4466 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4467 | continue; |
| 4468 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4469 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4470 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4471 | case DelayedDiagnostic::Unavailable: |
| 4472 | // Don't bother giving deprecation/unavailable diagnostics if |
| 4473 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4474 | if (!decl->isInvalidDecl()) |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4475 | HandleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4476 | break; |
| 4477 | |
| 4478 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4479 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4480 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4481 | |
| 4482 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4483 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4484 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4485 | } |
| 4486 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4487 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4490 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4491 | /// been delayed in the current context instead of in the given pool. |
| 4492 | /// Essentially, this just moves them to the current pool. |
| 4493 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4494 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4495 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4496 | curPool->steal(pool); |
| 4497 | } |
| 4498 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4499 | static bool isDeclDeprecated(Decl *D) { |
| 4500 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4501 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4502 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4503 | // A category implicitly has the availability of the interface. |
| 4504 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4505 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4506 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4507 | return false; |
| 4508 | } |
| 4509 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4510 | static bool isDeclUnavailable(Decl *D) { |
| 4511 | do { |
| 4512 | if (D->isUnavailable()) |
| 4513 | return true; |
| 4514 | // A category implicitly has the availability of the interface. |
| 4515 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4516 | return CatD->getClassInterface()->isUnavailable(); |
| 4517 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4518 | return false; |
| 4519 | } |
| 4520 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4521 | static void |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4522 | DoEmitAvailabilityWarning(Sema &S, |
| 4523 | DelayedDiagnostic::DDKind K, |
| 4524 | Decl *Ctx, |
| 4525 | const NamedDecl *D, |
| 4526 | StringRef Message, |
| 4527 | SourceLocation Loc, |
| 4528 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4529 | const ObjCPropertyDecl *ObjCProperty) { |
| 4530 | |
| 4531 | // Diagnostics for deprecated or unavailable. |
| 4532 | unsigned diag, diag_message, diag_fwdclass_message; |
| 4533 | |
| 4534 | // Matches 'diag::note_property_attribute' options. |
| 4535 | unsigned property_note_select; |
| 4536 | |
| 4537 | // Matches diag::note_availability_specified_here. |
| 4538 | unsigned available_here_select_kind; |
| 4539 | |
| 4540 | // Don't warn if our current context is deprecated or unavailable. |
| 4541 | switch (K) { |
| 4542 | case DelayedDiagnostic::Deprecation: |
| 4543 | if (isDeclDeprecated(Ctx)) |
| 4544 | return; |
| 4545 | diag = diag::warn_deprecated; |
| 4546 | diag_message = diag::warn_deprecated_message; |
| 4547 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 4548 | property_note_select = /* deprecated */ 0; |
| 4549 | available_here_select_kind = /* deprecated */ 2; |
| 4550 | break; |
| 4551 | |
| 4552 | case DelayedDiagnostic::Unavailable: |
| 4553 | if (isDeclUnavailable(Ctx)) |
| 4554 | return; |
| 4555 | diag = diag::err_unavailable; |
| 4556 | diag_message = diag::err_unavailable_message; |
| 4557 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 4558 | property_note_select = /* unavailable */ 1; |
| 4559 | available_here_select_kind = /* unavailable */ 0; |
| 4560 | break; |
| 4561 | |
| 4562 | default: |
| 4563 | llvm_unreachable("Neither a deprecation or unavailable kind"); |
| 4564 | } |
| 4565 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4566 | DeclarationName Name = D->getDeclName(); |
| 4567 | if (!Message.empty()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4568 | S.Diag(Loc, diag_message) << Name << Message; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4569 | if (ObjCProperty) |
| 4570 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4571 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4572 | } else if (!UnknownObjCClass) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4573 | S.Diag(Loc, diag) << Name; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4574 | if (ObjCProperty) |
| 4575 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4576 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4577 | } else { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4578 | S.Diag(Loc, diag_fwdclass_message) << Name; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4579 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4580 | } |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4581 | |
| 4582 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 4583 | << D << available_here_select_kind; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4586 | void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD, |
| 4587 | Decl *Ctx) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4588 | DD.Triggered = true; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4589 | DoEmitAvailabilityWarning(*this, |
| 4590 | (DelayedDiagnostic::DDKind) DD.Kind, |
| 4591 | Ctx, |
| 4592 | DD.getDeprecationDecl(), |
| 4593 | DD.getDeprecationMessage(), |
| 4594 | DD.Loc, |
| 4595 | DD.getUnknownObjCClass(), |
| 4596 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4599 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 4600 | NamedDecl *D, StringRef Message, |
| 4601 | SourceLocation Loc, |
| 4602 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4603 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4604 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4605 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4606 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D, |
| 4607 | UnknownObjCClass, |
| 4608 | ObjCProperty, |
| 4609 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4610 | return; |
| 4611 | } |
| 4612 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4613 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
| 4614 | DelayedDiagnostic::DDKind K; |
| 4615 | switch (AD) { |
| 4616 | case AD_Deprecation: |
| 4617 | K = DelayedDiagnostic::Deprecation; |
| 4618 | break; |
| 4619 | case AD_Unavailable: |
| 4620 | K = DelayedDiagnostic::Unavailable; |
| 4621 | break; |
| 4622 | } |
| 4623 | |
| 4624 | DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc, |
| 4625 | UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4626 | } |