Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 21 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 22 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 34 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 35 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 36 | C, |
| 37 | Cpp, |
| 38 | ObjC |
| 39 | }; |
| 40 | } |
| 41 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // Helper functions |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 46 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 47 | /// type (function or function-typed variable) or an Objective-C |
| 48 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 49 | static bool isFunctionOrMethod(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 50 | return (D->getFunctionType() != NULL) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 51 | } |
| 52 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 53 | /// Return true if the given decl has a declarator that should have |
| 54 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 55 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 56 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 58 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 61 | /// hasFunctionProto - Return true if the given decl has a argument |
| 62 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 63 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 64 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 65 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 66 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 67 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 70 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 71 | /// parameters. It is an error to call this on a K&R function (use |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 72 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 73 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 74 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 75 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 76 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 77 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 81 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 82 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 83 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 84 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 85 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 87 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 90 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 91 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 92 | return cast<FunctionProtoType>(FnTy)->getReturnType(); |
| 93 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 96 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 97 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 98 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 99 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 100 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 101 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 102 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 103 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 107 | static bool isInstanceMethod(const Decl *D) { |
| 108 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 109 | return MethodDecl->isInstance(); |
| 110 | return false; |
| 111 | } |
| 112 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 113 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 114 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 115 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 116 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 117 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 118 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 119 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 120 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 121 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 122 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 124 | // FIXME: Should we walk the chain of classes? |
| 125 | return ClsName == &Ctx.Idents.get("NSString") || |
| 126 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 127 | } |
| 128 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 129 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 130 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 131 | if (!PT) |
| 132 | return false; |
| 133 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 134 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 135 | if (!RT) |
| 136 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 137 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 138 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 139 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 140 | return false; |
| 141 | |
| 142 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 143 | } |
| 144 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 145 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 146 | // FIXME: Include the type in the argument list. |
| 147 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 148 | } |
| 149 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 150 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 151 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 152 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 153 | unsigned Num) { |
| 154 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 155 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 156 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 163 | /// \brief Check if the attribute has at least as many args as Num. May |
| 164 | /// output an error. |
| 165 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 166 | unsigned Num) { |
| 167 | if (getNumAttributeArgs(Attr) < Num) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 168 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) |
| 169 | << Attr.getName() << Num; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 176 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 177 | /// expression and return success or failure. May output an error. |
| 178 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 179 | const Expr *Expr, uint32_t &Val, |
| 180 | unsigned Idx = UINT_MAX) { |
| 181 | llvm::APSInt I(32); |
| 182 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 183 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 184 | if (Idx != UINT_MAX) |
| 185 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 186 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 187 | << Expr->getSourceRange(); |
| 188 | else |
| 189 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 190 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 191 | << Expr->getSourceRange(); |
| 192 | return false; |
| 193 | } |
| 194 | Val = (uint32_t)I.getZExtValue(); |
| 195 | return true; |
| 196 | } |
| 197 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 198 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 199 | /// declaration. Returns true if diagnosed. |
| 200 | template <typename AttrTy> |
| 201 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 202 | const AttributeList &Attr) { |
| 203 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 204 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 205 | << Attr.getName() << A; |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 211 | /// \brief Check if IdxExpr is a valid parameter index for a function or |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 212 | /// instance method D. May output an error. |
| 213 | /// |
| 214 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 215 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 216 | const AttributeList &Attr, |
| 217 | unsigned AttrArgNum, |
| 218 | const Expr *IdxExpr, |
| 219 | uint64_t &Idx) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 220 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 221 | |
| 222 | // In C++ the implicit 'this' function parameter also counts. |
| 223 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 224 | bool HP = hasFunctionProto(D); |
| 225 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 226 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 227 | unsigned NumParams = |
| 228 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 229 | |
| 230 | llvm::APSInt IdxInt; |
| 231 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 232 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 233 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 234 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 235 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | |
| 239 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 240 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 241 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 242 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | Idx--; // Convert to zero-based. |
| 246 | if (HasImplicitThisParam) { |
| 247 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 248 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 249 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 250 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | --Idx; |
| 254 | } |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 259 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 260 | /// If not emit an error and return false. If the argument is an identifier it |
| 261 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 262 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 263 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 264 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 265 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 266 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 267 | if (Attr.isArgIdent(ArgNum)) { |
| 268 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 269 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 270 | << Attr.getName() << AANT_ArgumentString |
| 271 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 272 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 273 | Str = Loc->Ident->getName(); |
| 274 | if (ArgLocation) |
| 275 | *ArgLocation = Loc->Loc; |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | // Now check for an actual string literal. |
| 280 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 281 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 282 | if (ArgLocation) |
| 283 | *ArgLocation = ArgExpr->getLocStart(); |
| 284 | |
| 285 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 286 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 287 | << Attr.getName() << AANT_ArgumentString; |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | Str = Literal->getString(); |
| 292 | return true; |
| 293 | } |
| 294 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 295 | /// \brief Applies the given attribute to the Decl without performing any |
| 296 | /// additional semantic checking. |
| 297 | template <typename AttrType> |
| 298 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 299 | const AttributeList &Attr) { |
| 300 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 301 | Attr.getAttributeSpellingListIndex())); |
| 302 | } |
| 303 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 304 | /// \brief Check if the passed-in expression is of type int or bool. |
| 305 | static bool isIntOrBool(Expr *Exp) { |
| 306 | QualType QT = Exp->getType(); |
| 307 | return QT->isBooleanType() || QT->isIntegerType(); |
| 308 | } |
| 309 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 310 | |
| 311 | // Check to see if the type is a smart pointer of some kind. We assume |
| 312 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 313 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 314 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 315 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 316 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 317 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 318 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 319 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 320 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 321 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 322 | return false; |
| 323 | |
| 324 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 327 | /// \brief Check if passed in Decl is a pointer type. |
| 328 | /// Note that this function may produce an error message. |
| 329 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 330 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 331 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 332 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 333 | QualType QT = vd->getType(); |
| 334 | if (QT->isAnyPointerType()) |
| 335 | return true; |
| 336 | |
| 337 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 338 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 339 | // (We don't want to force template instantiation if we can avoid it, |
| 340 | // since that would alter the order in which templates are instantiated.) |
| 341 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 342 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 343 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 344 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 345 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 346 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 347 | |
| 348 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 349 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 353 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 354 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 355 | static const RecordType *getRecordType(QualType QT) { |
| 356 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 357 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 358 | |
| 359 | // Now check if we point to record type. |
| 360 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 361 | return PT->getPointeeType()->getAs<RecordType>(); |
| 362 | |
| 363 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 364 | } |
| 365 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 366 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 367 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 368 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 369 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 370 | return RT->getDecl()->hasAttr<LockableAttr>(); |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 374 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 375 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 376 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 377 | QualType Ty) { |
| 378 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 379 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 380 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 381 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 382 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 383 | << Attr.getName() << Ty; |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 384 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 385 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 386 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 387 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 388 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 389 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 390 | |
| 391 | // Allow smart pointers to be used as lockable objects. |
| 392 | // FIXME -- Check the type that the smart pointer points to. |
| 393 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 394 | return; |
| 395 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 396 | // Check if the type is lockable. |
| 397 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 398 | if (RD->hasAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 399 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 400 | |
| 401 | // Else check if any base classes are lockable. |
| 402 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 403 | CXXBasePaths BPaths(false, false); |
| 404 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 405 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 406 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 407 | |
| 408 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 409 | << Attr.getName() << Ty; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 412 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 413 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 414 | /// \param Sidx The attribute argument index to start checking with. |
| 415 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 416 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 417 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 418 | const AttributeList &Attr, |
| 419 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 420 | int Sidx = 0, |
| 421 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 422 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 423 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 424 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 425 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 426 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 427 | Args.push_back(ArgExp); |
| 428 | continue; |
| 429 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 430 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 431 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 432 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 433 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 434 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 435 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 436 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 437 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 438 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 439 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 440 | // We allow constant strings to be used as a placeholder for expressions |
| 441 | // that are not valid C++ syntax, but warn that they are ignored. |
| 442 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 443 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 444 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 445 | continue; |
| 446 | } |
| 447 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 448 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 449 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 450 | // A pointer to member expression of the form &MyClass::mu is treated |
| 451 | // specially -- we need to look at the type of the member. |
| 452 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 453 | if (UOp->getOpcode() == UO_AddrOf) |
| 454 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 455 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 456 | ArgTy = DRE->getDecl()->getType(); |
| 457 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 458 | // First see if we can just cast to record type, or point to record type. |
| 459 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 460 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 461 | // Now check if we index into a record type function param. |
| 462 | if(!RT && ParamIdxOk) { |
| 463 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 464 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 465 | if(FD && IL) { |
| 466 | unsigned int NumParams = FD->getNumParams(); |
| 467 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 468 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 469 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 470 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 471 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 472 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 473 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 474 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 475 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 479 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 480 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 481 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 482 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 485 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 486 | // Attribute Implementations |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 489 | // FIXME: All this manual attribute parsing code is gross. At the |
| 490 | // least add some helper functions to check most argument patterns (# |
| 491 | // and types of args). |
| 492 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 493 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 494 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 495 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 496 | return; |
| 497 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 498 | D->addAttr(::new (S.Context) |
| 499 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 500 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 503 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 504 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 505 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 506 | SmallVector<Expr*, 1> Args; |
| 507 | // check that all arguments are lockable objects |
| 508 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 509 | unsigned Size = Args.size(); |
| 510 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 511 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 512 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 513 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 514 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 515 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 518 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 519 | Expr *Arg = 0; |
| 520 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 521 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 522 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 523 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 524 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 527 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 528 | const AttributeList &Attr) { |
| 529 | Expr *Arg = 0; |
| 530 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 531 | return; |
| 532 | |
| 533 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 534 | return; |
| 535 | |
| 536 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 537 | S.Context, Arg, |
| 538 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 541 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 542 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 543 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 544 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 545 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 546 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 547 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 548 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 549 | if (!QT->isDependentType()) { |
| 550 | const RecordType *RT = getRecordType(QT); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 551 | if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 552 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | << Attr.getName(); |
| 554 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 558 | // Check that all arguments are lockable objects. |
| 559 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 560 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 561 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 562 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 563 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 566 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 567 | const AttributeList &Attr) { |
| 568 | SmallVector<Expr*, 1> Args; |
| 569 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 570 | return; |
| 571 | |
| 572 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 573 | D->addAttr(::new (S.Context) |
| 574 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 575 | StartArg, Args.size(), |
| 576 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 579 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 580 | const AttributeList &Attr) { |
| 581 | SmallVector<Expr*, 1> Args; |
| 582 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 583 | return; |
| 584 | |
| 585 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 586 | D->addAttr(::new (S.Context) |
| 587 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 588 | StartArg, Args.size(), |
| 589 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 592 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 593 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 594 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 595 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 596 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 597 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 598 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 602 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 603 | const AttributeList &Attr) { |
| 604 | SmallVector<Expr*, 1> Args; |
| 605 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 606 | return; |
| 607 | |
| 608 | unsigned Size = Args.size(); |
| 609 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 610 | D->addAttr(::new (S.Context) |
| 611 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 612 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 615 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 616 | const AttributeList &Attr) { |
| 617 | SmallVector<Expr*, 1> Args; |
| 618 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 619 | return; |
| 620 | |
| 621 | unsigned Size = Args.size(); |
| 622 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 623 | D->addAttr(::new (S.Context) |
| 624 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 625 | StartArg, Size, |
| 626 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 627 | } |
| 628 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 629 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 630 | const AttributeList &Attr) { |
| 631 | SmallVector<Expr*, 1> Args; |
| 632 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 633 | return; |
| 634 | |
| 635 | unsigned Size = Args.size(); |
| 636 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 637 | D->addAttr(::new (S.Context) |
| 638 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 639 | Attr.getAttributeSpellingListIndex())); |
| 640 | } |
| 641 | |
| 642 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 643 | const AttributeList &Attr) { |
| 644 | SmallVector<Expr*, 1> Args; |
| 645 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 646 | return; |
| 647 | |
| 648 | unsigned Size = Args.size(); |
| 649 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 650 | D->addAttr(::new (S.Context) |
| 651 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 652 | StartArg, Size, |
| 653 | Attr.getAttributeSpellingListIndex())); |
| 654 | } |
| 655 | |
| 656 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 657 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 658 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 659 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 661 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 662 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 663 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 664 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 665 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 666 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 670 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 671 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 672 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 675 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 676 | const AttributeList &Attr) { |
| 677 | SmallVector<Expr*, 2> Args; |
| 678 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 679 | return; |
| 680 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 681 | D->addAttr(::new (S.Context) |
| 682 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 683 | Attr.getArgAsExpr(0), |
| 684 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 685 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 688 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 689 | const AttributeList &Attr) { |
| 690 | SmallVector<Expr*, 2> Args; |
| 691 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 692 | return; |
| 693 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 694 | D->addAttr(::new (S.Context) |
| 695 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 696 | Attr.getArgAsExpr(0), |
| 697 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 698 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 701 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 702 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 703 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 704 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 705 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 706 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 707 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 708 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 709 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 710 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 711 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 712 | return true; |
| 713 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 714 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 715 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 716 | const AttributeList &Attr) { |
| 717 | SmallVector<Expr*, 1> Args; |
| 718 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 719 | return; |
| 720 | |
| 721 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 722 | D->addAttr(::new (S.Context) |
| 723 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 724 | StartArg, Args.size(), |
| 725 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 728 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 729 | const AttributeList &Attr) { |
| 730 | SmallVector<Expr*, 1> Args; |
| 731 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 732 | return; |
| 733 | |
| 734 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 735 | D->addAttr(::new (S.Context) |
| 736 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 737 | StartArg, Args.size(), |
| 738 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 741 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 742 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 743 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 744 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 745 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 746 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 747 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 748 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 749 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 750 | D->addAttr(::new (S.Context) |
| 751 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 752 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 756 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 757 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 758 | SmallVector<Expr*, 1> Args; |
| 759 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 760 | unsigned Size = Args.size(); |
| 761 | if (Size == 0) |
| 762 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 763 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 764 | D->addAttr(::new (S.Context) |
| 765 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 766 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 770 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 771 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 772 | return; |
| 773 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 774 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 775 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 776 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 777 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 778 | if (Size == 0) |
| 779 | return; |
| 780 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 781 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 782 | D->addAttr(::new (S.Context) |
| 783 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 784 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 787 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 788 | Expr *Cond = Attr.getArgAsExpr(0); |
| 789 | if (!Cond->isTypeDependent()) { |
| 790 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 791 | if (Converted.isInvalid()) |
| 792 | return; |
| 793 | Cond = Converted.take(); |
| 794 | } |
| 795 | |
| 796 | StringRef Msg; |
| 797 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 798 | return; |
| 799 | |
| 800 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 801 | if (!Cond->isValueDependent() && |
| 802 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 803 | Diags)) { |
| 804 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 805 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 806 | S.Diag(Diags[I].first, Diags[I].second); |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | D->addAttr(::new (S.Context) |
| 811 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 812 | Attr.getAttributeSpellingListIndex())); |
| 813 | } |
| 814 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 815 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 816 | ConsumableAttr::ConsumedState DefaultState; |
| 817 | |
| 818 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 819 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 820 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 821 | DefaultState)) { |
| 822 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 823 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 824 | return; |
| 825 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 826 | } else { |
| 827 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 828 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 829 | return; |
| 830 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 831 | |
| 832 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 833 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 834 | Attr.getAttributeSpellingListIndex())); |
| 835 | } |
| 836 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 837 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 838 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 839 | const AttributeList &Attr) { |
| 840 | ASTContext &CurrContext = S.getASTContext(); |
| 841 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 842 | |
| 843 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 844 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 845 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 846 | RD->getNameAsString(); |
| 847 | |
| 848 | return false; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | return true; |
| 853 | } |
| 854 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 855 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 856 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 857 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 858 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 859 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 860 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 861 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 862 | return; |
| 863 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 864 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 865 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 866 | CallableWhenAttr::ConsumedState CallableState; |
| 867 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 868 | StringRef StateString; |
| 869 | SourceLocation Loc; |
| 870 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 871 | return; |
| 872 | |
| 873 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 874 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 875 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 876 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 877 | return; |
| 878 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 879 | |
| 880 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 881 | } |
| 882 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 883 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 884 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 885 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 886 | } |
| 887 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 888 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 889 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 890 | const AttributeList &Attr) { |
| 891 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 892 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 893 | ParamTypestateAttr::ConsumedState ParamState; |
| 894 | |
| 895 | if (Attr.isArgIdent(0)) { |
| 896 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 897 | StringRef StateString = Ident->Ident->getName(); |
| 898 | |
| 899 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 900 | ParamState)) { |
| 901 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 902 | << Attr.getName() << StateString; |
| 903 | return; |
| 904 | } |
| 905 | } else { |
| 906 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 907 | Attr.getName() << AANT_ArgumentIdentifier; |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | // FIXME: This check is currently being done in the analysis. It can be |
| 912 | // enabled here only after the parser propagates attributes at |
| 913 | // template specialization definition, not declaration. |
| 914 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 915 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 916 | // |
| 917 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 918 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 919 | // ReturnType.getAsString(); |
| 920 | // return; |
| 921 | //} |
| 922 | |
| 923 | D->addAttr(::new (S.Context) |
| 924 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 925 | Attr.getAttributeSpellingListIndex())); |
| 926 | } |
| 927 | |
| 928 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 929 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 930 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 931 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 932 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 933 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 934 | |
| 935 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 936 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 937 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 938 | ReturnState)) { |
| 939 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 940 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 941 | return; |
| 942 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 943 | } else { |
| 944 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 945 | Attr.getName() << AANT_ArgumentIdentifier; |
| 946 | return; |
| 947 | } |
| 948 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 949 | // FIXME: This check is currently being done in the analysis. It can be |
| 950 | // enabled here only after the parser propagates attributes at |
| 951 | // template specialization definition, not declaration. |
| 952 | //QualType ReturnType; |
| 953 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 954 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 955 | // ReturnType = Param->getType(); |
| 956 | // |
| 957 | //} else if (const CXXConstructorDecl *Constructor = |
| 958 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 959 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 960 | // |
| 961 | //} else { |
| 962 | // |
| 963 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 964 | //} |
| 965 | // |
| 966 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 967 | // |
| 968 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 969 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 970 | // ReturnType.getAsString(); |
| 971 | // return; |
| 972 | //} |
| 973 | |
| 974 | D->addAttr(::new (S.Context) |
| 975 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 976 | Attr.getAttributeSpellingListIndex())); |
| 977 | } |
| 978 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 979 | |
| 980 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 981 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 982 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 983 | |
| 984 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 985 | return; |
| 986 | |
| 987 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 988 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 989 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 990 | StringRef Param = Ident->Ident->getName(); |
| 991 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 992 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 993 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 994 | return; |
| 995 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 996 | } else { |
| 997 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 998 | Attr.getName() << AANT_ArgumentIdentifier; |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | D->addAttr(::new (S.Context) |
| 1003 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1004 | Attr.getAttributeSpellingListIndex())); |
| 1005 | } |
| 1006 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1007 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1008 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1009 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1010 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1011 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1012 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1013 | return; |
| 1014 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1015 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1016 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1017 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1018 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1019 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1020 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1021 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1022 | return; |
| 1023 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1024 | } else { |
| 1025 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1026 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1031 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1032 | Attr.getAttributeSpellingListIndex())); |
| 1033 | } |
| 1034 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1035 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1036 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1037 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1038 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1041 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1042 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1043 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1044 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1045 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1046 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1047 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1048 | if (!FD->getType()->isDependentType() && |
| 1049 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1050 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1051 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1052 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1053 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1054 | FD->addAttr(::new (S.Context) |
| 1055 | PackedAttr(Attr.getRange(), S.Context, |
| 1056 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1057 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1058 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1061 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1062 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1063 | // variables or properties of Objective-C classes. The outlet must also |
| 1064 | // have an object reference type. |
| 1065 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1066 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1067 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1068 | << Attr.getName() << VD->getType() << 0; |
| 1069 | return false; |
| 1070 | } |
| 1071 | } |
| 1072 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1073 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1074 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1075 | << Attr.getName() << PD->getType() << 1; |
| 1076 | return false; |
| 1077 | } |
| 1078 | } |
| 1079 | else { |
| 1080 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1081 | return false; |
| 1082 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1083 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1084 | return true; |
| 1085 | } |
| 1086 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1087 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1088 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1089 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1090 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1091 | D->addAttr(::new (S.Context) |
| 1092 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1093 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1096 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1097 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1098 | |
| 1099 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1100 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1101 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1102 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1103 | return; |
| 1104 | } |
| 1105 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1106 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1107 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1108 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1109 | ParsedType PT; |
| 1110 | |
| 1111 | if (Attr.hasParsedType()) |
| 1112 | PT = Attr.getTypeArg(); |
| 1113 | else { |
| 1114 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1115 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1116 | if (!PT) { |
| 1117 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1118 | return; |
| 1119 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1120 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1121 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1122 | TypeSourceInfo *QTLoc = 0; |
| 1123 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1124 | if (!QTLoc) |
| 1125 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1126 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1127 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1128 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1129 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1130 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1131 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1132 | S.Diag(Attr.getLoc(), |
| 1133 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1134 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1135 | return; |
| 1136 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1137 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1138 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1139 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1140 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1143 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1144 | if (const RecordType *UT = T->getAsUnionType()) |
| 1145 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1146 | RecordDecl *UD = UT->getDecl(); |
| 1147 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1148 | itend = UD->field_end(); it != itend; ++it) { |
| 1149 | QualType QT = it->getType(); |
| 1150 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1151 | T = QT; |
| 1152 | return; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1158 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1159 | SourceRange R, bool isReturnValue = false) { |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1160 | T = T.getNonReferenceType(); |
| 1161 | possibleTransparentUnionPointerType(T); |
| 1162 | |
| 1163 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1164 | S.Diag(Attr.getLoc(), |
| 1165 | isReturnValue ? diag::warn_attribute_return_pointers_only |
| 1166 | : diag::warn_attribute_pointers_only) |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1167 | << Attr.getName() << R; |
| 1168 | return false; |
| 1169 | } |
| 1170 | return true; |
| 1171 | } |
| 1172 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1173 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1174 | SmallVector<unsigned, 8> NonNullArgs; |
| 1175 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1176 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1177 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1178 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1179 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Is the function argument a pointer type? |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1182 | // FIXME: Should also highlight argument in decl in the diagnostic. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1183 | if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
| 1184 | Ex->getSourceRange())) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1185 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1186 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1187 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1188 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1189 | |
| 1190 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1191 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1192 | if (NonNullArgs.empty()) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1193 | for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) { |
| 1194 | QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1195 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1196 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1197 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1198 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1199 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1200 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1201 | if (NonNullArgs.empty()) { |
| 1202 | // Warn the trivial case only if attribute is not coming from a |
| 1203 | // macro instantiation. |
| 1204 | if (Attr.getLoc().isFileID()) |
| 1205 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1206 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1207 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1208 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1209 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1210 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1211 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1212 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1213 | D->addAttr(::new (S.Context) |
| 1214 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1215 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1218 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1219 | const AttributeList &Attr) { |
| 1220 | if (Attr.getNumArgs() > 0) { |
| 1221 | if (D->getFunctionType()) { |
| 1222 | handleNonNullAttr(S, D, Attr); |
| 1223 | } else { |
| 1224 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1225 | << D->getSourceRange(); |
| 1226 | } |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | // Is the argument a pointer type? |
| 1231 | if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange())) |
| 1232 | return; |
| 1233 | |
| 1234 | D->addAttr(::new (S.Context) |
| 1235 | NonNullAttr(Attr.getRange(), S.Context, 0, 0, |
| 1236 | Attr.getAttributeSpellingListIndex())); |
| 1237 | } |
| 1238 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1239 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1240 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1241 | QualType ResultType = getFunctionOrMethodResultType(D); |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1242 | if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(), |
| 1243 | /* isReturnValue */ true)) |
| 1244 | return; |
| 1245 | |
| 1246 | D->addAttr(::new (S.Context) |
| 1247 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1248 | Attr.getAttributeSpellingListIndex())); |
| 1249 | } |
| 1250 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1251 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1252 | // This attribute must be applied to a function declaration. The first |
| 1253 | // argument to the attribute must be an identifier, the name of the resource, |
| 1254 | // for example: malloc. The following arguments must be argument indexes, the |
| 1255 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1256 | // 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] | 1257 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1258 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1259 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1260 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1261 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1262 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1263 | return; |
| 1264 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1265 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1266 | // Figure out our Kind. |
| 1267 | OwnershipAttr::OwnershipKind K = |
| 1268 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1269 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1270 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1271 | // Check arguments. |
| 1272 | switch (K) { |
| 1273 | case OwnershipAttr::Takes: |
| 1274 | case OwnershipAttr::Holds: |
| 1275 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1276 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1277 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1278 | return; |
| 1279 | } |
| 1280 | break; |
| 1281 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1282 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1283 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1284 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1285 | return; |
| 1286 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1287 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1290 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1293 | StringRef ModuleName = Module->getName(); |
| 1294 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1295 | ModuleName.size() > 4) { |
| 1296 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1297 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1298 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1299 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1300 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1301 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1302 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1303 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1304 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1305 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1306 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1307 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1308 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1309 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1310 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1311 | case OwnershipAttr::Takes: |
| 1312 | case OwnershipAttr::Holds: |
| 1313 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1314 | Err = 0; |
| 1315 | break; |
| 1316 | case OwnershipAttr::Returns: |
| 1317 | if (!T->isIntegerType()) |
| 1318 | Err = 1; |
| 1319 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1320 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1321 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1322 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1323 | << Ex->getSourceRange(); |
| 1324 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1325 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1328 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1329 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1330 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1331 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1332 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1333 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1334 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1335 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 05a6378 | 2014-01-20 15:06:09 +0000 | [diff] [blame] | 1336 | << AL.getName() << *i; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1337 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1340 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | unsigned* start = OwnershipArgs.data(); |
| 1344 | unsigned size = OwnershipArgs.size(); |
| 1345 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1346 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1347 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1348 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1349 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1352 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1353 | // Check the attribute arguments. |
| 1354 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1355 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1356 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1357 | return; |
| 1358 | } |
| 1359 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1360 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1361 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1362 | // gcc rejects |
| 1363 | // class c { |
| 1364 | // static int a __attribute__((weakref ("v2"))); |
| 1365 | // static int b() __attribute__((weakref ("f3"))); |
| 1366 | // }; |
| 1367 | // and ignores the attributes of |
| 1368 | // void f(void) { |
| 1369 | // static int a __attribute__((weakref ("v2"))); |
| 1370 | // } |
| 1371 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1372 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1373 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1374 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1375 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1376 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | // The GCC manual says |
| 1380 | // |
| 1381 | // At present, a declaration to which `weakref' is attached can only |
| 1382 | // be `static'. |
| 1383 | // |
| 1384 | // It also says |
| 1385 | // |
| 1386 | // Without a TARGET, |
| 1387 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1388 | // equivalent to `weak'. |
| 1389 | // |
| 1390 | // gcc 4.4.1 will accept |
| 1391 | // int a7 __attribute__((weakref)); |
| 1392 | // as |
| 1393 | // int a7 __attribute__((weak)); |
| 1394 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1395 | // it if this behaviour is actually used. |
| 1396 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1397 | // GCC rejects |
| 1398 | // static ((alias ("y"), weakref)). |
| 1399 | // Should we? How to check that weakref is before or after alias? |
| 1400 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1401 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1402 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1403 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1404 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1405 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1406 | // GCC will accept anything as the argument of weakref. Should we |
| 1407 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1408 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1409 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1410 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1411 | D->addAttr(::new (S.Context) |
| 1412 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1413 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1416 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1417 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1418 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1421 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1422 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1423 | return; |
| 1424 | } |
| 1425 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1426 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1427 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1428 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1429 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1432 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1433 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1434 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1435 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1436 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1437 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1441 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1442 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1443 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1444 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1445 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1448 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1449 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1450 | StringRef Model; |
| 1451 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1452 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1453 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1454 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1455 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1456 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1457 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1458 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1459 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1463 | D->addAttr(::new (S.Context) |
| 1464 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1465 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1468 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1469 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1470 | QualType RetTy = FD->getReturnType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1471 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1472 | D->addAttr(::new (S.Context) |
| 1473 | MallocAttr(Attr.getRange(), S.Context, |
| 1474 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1475 | return; |
| 1476 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1479 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1482 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1483 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1484 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1485 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1486 | return; |
| 1487 | } |
| 1488 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1489 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1490 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1493 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1494 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1495 | |
| 1496 | if (S.CheckNoReturnAttr(attr)) return; |
| 1497 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1498 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1499 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1500 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1501 | return; |
| 1502 | } |
| 1503 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1504 | D->addAttr(::new (S.Context) |
| 1505 | NoReturnAttr(attr.getRange(), S.Context, |
| 1506 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1510 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1511 | attr.setInvalid(); |
| 1512 | return true; |
| 1513 | } |
| 1514 | |
| 1515 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1518 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1519 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1520 | |
| 1521 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1522 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1523 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1524 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1525 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1526 | && !VD->getType()->isFunctionPointerType())) { |
| 1527 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1528 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1529 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1530 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1531 | return; |
| 1532 | } |
| 1533 | } |
| 1534 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1535 | D->addAttr(::new (S.Context) |
| 1536 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1537 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1540 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1541 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1542 | /* |
| 1543 | Returning a Vector Class in Registers |
| 1544 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1545 | According to the PPU ABI specifications, a class with a single member of |
| 1546 | vector type is returned in memory when used as the return value of a function. |
| 1547 | This results in inefficient code when implementing vector classes. To return |
| 1548 | the value in a single vector register, add the vecreturn attribute to the |
| 1549 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1550 | |
| 1551 | Example: |
| 1552 | |
| 1553 | struct Vector |
| 1554 | { |
| 1555 | __vector float xyzw; |
| 1556 | } __attribute__((vecreturn)); |
| 1557 | |
| 1558 | Vector Add(Vector lhs, Vector rhs) |
| 1559 | { |
| 1560 | Vector result; |
| 1561 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1562 | return result; // This will be returned in a register |
| 1563 | } |
| 1564 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1565 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1566 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1567 | return; |
| 1568 | } |
| 1569 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1570 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1571 | int count = 0; |
| 1572 | |
| 1573 | if (!isa<CXXRecordDecl>(record)) { |
| 1574 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1579 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1580 | return; |
| 1581 | } |
| 1582 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1583 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1584 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1585 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1586 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1587 | return; |
| 1588 | } |
| 1589 | count++; |
| 1590 | } |
| 1591 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1592 | D->addAttr(::new (S.Context) |
| 1593 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1594 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1597 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1598 | const AttributeList &Attr) { |
| 1599 | if (isa<ParmVarDecl>(D)) { |
| 1600 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1601 | // parameter of a function declaration or lambda. |
| 1602 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1603 | S.Diag(Attr.getLoc(), |
| 1604 | diag::err_carries_dependency_param_not_function_decl); |
| 1605 | return; |
| 1606 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1607 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1608 | |
| 1609 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1610 | Attr.getRange(), S.Context, |
| 1611 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1614 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1615 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1616 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1617 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1618 | return; |
| 1619 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1620 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1621 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1622 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1623 | return; |
| 1624 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1625 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1626 | D->addAttr(::new (S.Context) |
| 1627 | UsedAttr(Attr.getRange(), S.Context, |
| 1628 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1631 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1632 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1633 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1634 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1635 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1636 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1637 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1638 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1639 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1640 | if (Attr.getNumArgs() > 0 && |
| 1641 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1642 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1643 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1644 | D->addAttr(::new (S.Context) |
| 1645 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1646 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1649 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1650 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1651 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1652 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1653 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1654 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1655 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1656 | |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1657 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1658 | if (Attr.getNumArgs() > 0 && |
| 1659 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1660 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1661 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1662 | D->addAttr(::new (S.Context) |
| 1663 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1664 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1667 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1668 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1669 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1670 | unsigned NumArgs = Attr.getNumArgs(); |
| 1671 | if (NumArgs > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1672 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1673 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1674 | return; |
| 1675 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1676 | |
| 1677 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1678 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1679 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1680 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1681 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1682 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1683 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1686 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 1687 | const AttributeList &Attr) { |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1688 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1689 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1690 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1693 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1694 | IdentifierInfo *Platform, |
| 1695 | VersionTuple Introduced, |
| 1696 | VersionTuple Deprecated, |
| 1697 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1698 | StringRef PlatformName |
| 1699 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1700 | if (PlatformName.empty()) |
| 1701 | PlatformName = Platform->getName(); |
| 1702 | |
| 1703 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1704 | // of these steps are needed). |
| 1705 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1706 | !(Introduced <= Deprecated)) { |
| 1707 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1708 | << 1 << PlatformName << Deprecated.getAsString() |
| 1709 | << 0 << Introduced.getAsString(); |
| 1710 | return true; |
| 1711 | } |
| 1712 | |
| 1713 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1714 | !(Introduced <= Obsoleted)) { |
| 1715 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1716 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1717 | << 0 << Introduced.getAsString(); |
| 1718 | return true; |
| 1719 | } |
| 1720 | |
| 1721 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1722 | !(Deprecated <= Obsoleted)) { |
| 1723 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1724 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1725 | << 1 << Deprecated.getAsString(); |
| 1726 | return true; |
| 1727 | } |
| 1728 | |
| 1729 | return false; |
| 1730 | } |
| 1731 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1732 | /// \brief Check whether the two versions match. |
| 1733 | /// |
| 1734 | /// If either version tuple is empty, then they are assumed to match. If |
| 1735 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1736 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1737 | bool BeforeIsOkay) { |
| 1738 | if (X.empty() || Y.empty()) |
| 1739 | return true; |
| 1740 | |
| 1741 | if (X == Y) |
| 1742 | return true; |
| 1743 | |
| 1744 | if (BeforeIsOkay && X < Y) |
| 1745 | return true; |
| 1746 | |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1750 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1751 | IdentifierInfo *Platform, |
| 1752 | VersionTuple Introduced, |
| 1753 | VersionTuple Deprecated, |
| 1754 | VersionTuple Obsoleted, |
| 1755 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1756 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1757 | bool Override, |
| 1758 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1759 | VersionTuple MergedIntroduced = Introduced; |
| 1760 | VersionTuple MergedDeprecated = Deprecated; |
| 1761 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1762 | bool FoundAny = false; |
| 1763 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1764 | if (D->hasAttrs()) { |
| 1765 | AttrVec &Attrs = D->getAttrs(); |
| 1766 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1767 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1768 | if (!OldAA) { |
| 1769 | ++i; |
| 1770 | continue; |
| 1771 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1772 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1773 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1774 | if (OldPlatform != Platform) { |
| 1775 | ++i; |
| 1776 | continue; |
| 1777 | } |
| 1778 | |
| 1779 | FoundAny = true; |
| 1780 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1781 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1782 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1783 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1784 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1785 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1786 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1787 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1788 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1789 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1790 | if (Override) { |
| 1791 | int Which = -1; |
| 1792 | VersionTuple FirstVersion; |
| 1793 | VersionTuple SecondVersion; |
| 1794 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1795 | Which = 0; |
| 1796 | FirstVersion = OldIntroduced; |
| 1797 | SecondVersion = Introduced; |
| 1798 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1799 | Which = 1; |
| 1800 | FirstVersion = Deprecated; |
| 1801 | SecondVersion = OldDeprecated; |
| 1802 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1803 | Which = 2; |
| 1804 | FirstVersion = Obsoleted; |
| 1805 | SecondVersion = OldObsoleted; |
| 1806 | } |
| 1807 | |
| 1808 | if (Which == -1) { |
| 1809 | Diag(OldAA->getLocation(), |
| 1810 | diag::warn_mismatched_availability_override_unavail) |
| 1811 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1812 | } else { |
| 1813 | Diag(OldAA->getLocation(), |
| 1814 | diag::warn_mismatched_availability_override) |
| 1815 | << Which |
| 1816 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1817 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1818 | } |
| 1819 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1820 | } else { |
| 1821 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1822 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1823 | } |
| 1824 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1825 | Attrs.erase(Attrs.begin() + i); |
| 1826 | --e; |
| 1827 | continue; |
| 1828 | } |
| 1829 | |
| 1830 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1831 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1832 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1833 | |
| 1834 | if (MergedIntroduced2.empty()) |
| 1835 | MergedIntroduced2 = OldIntroduced; |
| 1836 | if (MergedDeprecated2.empty()) |
| 1837 | MergedDeprecated2 = OldDeprecated; |
| 1838 | if (MergedObsoleted2.empty()) |
| 1839 | MergedObsoleted2 = OldObsoleted; |
| 1840 | |
| 1841 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1842 | MergedIntroduced2, MergedDeprecated2, |
| 1843 | MergedObsoleted2)) { |
| 1844 | Attrs.erase(Attrs.begin() + i); |
| 1845 | --e; |
| 1846 | continue; |
| 1847 | } |
| 1848 | |
| 1849 | MergedIntroduced = MergedIntroduced2; |
| 1850 | MergedDeprecated = MergedDeprecated2; |
| 1851 | MergedObsoleted = MergedObsoleted2; |
| 1852 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1853 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | if (FoundAny && |
| 1857 | MergedIntroduced == Introduced && |
| 1858 | MergedDeprecated == Deprecated && |
| 1859 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1860 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1861 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1862 | // Only create a new attribute if !Override, but we want to do |
| 1863 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1864 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1865 | MergedDeprecated, MergedObsoleted) && |
| 1866 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1867 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1868 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1869 | Obsoleted, IsUnavailable, Message, |
| 1870 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1871 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1872 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1875 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1876 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1877 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1878 | return; |
| 1879 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1880 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1881 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1882 | IdentifierInfo *II = Platform->Ident; |
| 1883 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1884 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1885 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1886 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1887 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1888 | if (!ND) { |
| 1889 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1893 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1894 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1895 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1896 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1897 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1898 | if (const StringLiteral *SE = |
| 1899 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1900 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1901 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1902 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1903 | Introduced.Version, |
| 1904 | Deprecated.Version, |
| 1905 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1906 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1907 | /*Override=*/false, |
| 1908 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1909 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1910 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1913 | template <class T> |
| 1914 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1915 | typename T::VisibilityType value, |
| 1916 | unsigned attrSpellingListIndex) { |
| 1917 | T *existingAttr = D->getAttr<T>(); |
| 1918 | if (existingAttr) { |
| 1919 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1920 | if (existingValue == value) |
| 1921 | return NULL; |
| 1922 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1923 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1924 | D->dropAttr<T>(); |
| 1925 | } |
| 1926 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1927 | } |
| 1928 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1929 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1930 | VisibilityAttr::VisibilityType Vis, |
| 1931 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1932 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1933 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1936 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1937 | TypeVisibilityAttr::VisibilityType Vis, |
| 1938 | unsigned AttrSpellingListIndex) { |
| 1939 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1940 | AttrSpellingListIndex); |
| 1941 | } |
| 1942 | |
| 1943 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1944 | bool isTypeVisibility) { |
| 1945 | // Visibility attributes don't mean anything on a typedef. |
| 1946 | if (isa<TypedefNameDecl>(D)) { |
| 1947 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 1948 | << Attr.getName(); |
| 1949 | return; |
| 1950 | } |
| 1951 | |
| 1952 | // 'type_visibility' can only go on a type or namespace. |
| 1953 | if (isTypeVisibility && |
| 1954 | !(isa<TagDecl>(D) || |
| 1955 | isa<ObjCInterfaceDecl>(D) || |
| 1956 | isa<NamespaceDecl>(D))) { |
| 1957 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 1958 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 1959 | return; |
| 1960 | } |
| 1961 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 1962 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1963 | StringRef TypeStr; |
| 1964 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1965 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1966 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1967 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1968 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1969 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1970 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1971 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1972 | return; |
| 1973 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1974 | |
| 1975 | // Complain about attempts to use protected visibility on targets |
| 1976 | // (like Darwin) that don't support it. |
| 1977 | if (type == VisibilityAttr::Protected && |
| 1978 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 1979 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 1980 | type = VisibilityAttr::Default; |
| 1981 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1982 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1983 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1984 | clang::Attr *newAttr; |
| 1985 | if (isTypeVisibility) { |
| 1986 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 1987 | (TypeVisibilityAttr::VisibilityType) type, |
| 1988 | Index); |
| 1989 | } else { |
| 1990 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 1991 | } |
| 1992 | if (newAttr) |
| 1993 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1996 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 1997 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1998 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1999 | if (!Attr.isArgIdent(0)) { |
| 2000 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2001 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2002 | return; |
| 2003 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2004 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2005 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2006 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2007 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2008 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2009 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2010 | return; |
| 2011 | } |
| 2012 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2013 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2014 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2015 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2016 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2017 | // Ignore the attribute. |
| 2018 | return; |
| 2019 | } |
| 2020 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2021 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2022 | S.Context, F, |
| 2023 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2026 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2027 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2028 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2029 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2030 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2031 | return; |
| 2032 | } |
| 2033 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2034 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2035 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2036 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2037 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2038 | return; |
| 2039 | } |
| 2040 | } |
| 2041 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2042 | // It is okay to include this attribute on properties, e.g.: |
| 2043 | // |
| 2044 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2045 | // |
| 2046 | // In this case it follows tradition and suppresses an error in the above |
| 2047 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2048 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2049 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2050 | D->addAttr(::new (S.Context) |
| 2051 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2052 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2055 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2056 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2057 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2058 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2059 | return; |
| 2060 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2061 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2062 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2063 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2064 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2065 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2066 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2067 | return; |
| 2068 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2069 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2070 | D->addAttr(::new (S.Context) |
| 2071 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2072 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2075 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2076 | // check the attribute arguments. |
| 2077 | if (Attr.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 2078 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 2079 | << Attr.getName() << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2080 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2083 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2084 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2085 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2086 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2087 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2088 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2089 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2090 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2091 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2092 | return; |
| 2093 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2094 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2095 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2096 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2097 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2098 | return; |
| 2099 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2100 | |
| 2101 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2104 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2105 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2106 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2107 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2108 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2109 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2110 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2111 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2112 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2113 | return; |
| 2114 | } |
| 2115 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2116 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2117 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2118 | // FIXME: This error message could be improved, it would be nice |
| 2119 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2120 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2121 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2122 | return; |
| 2123 | } |
| 2124 | } |
| 2125 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2126 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2127 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2128 | if (isa<FunctionNoProtoType>(FT)) { |
| 2129 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2130 | return; |
| 2131 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2132 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2133 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2134 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2135 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2136 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2137 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2138 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2139 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2140 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2141 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2142 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2143 | if (!BD->isVariadic()) { |
| 2144 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2145 | return; |
| 2146 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2147 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2148 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2149 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2150 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2151 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2152 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2153 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2154 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2155 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2156 | return; |
| 2157 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2158 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2159 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2160 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2161 | return; |
| 2162 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2163 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2164 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2165 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2166 | return; |
| 2167 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2168 | D->addAttr(::new (S.Context) |
| 2169 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2170 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2173 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2174 | if (D->getFunctionType() && |
| 2175 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2176 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2177 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2178 | return; |
| 2179 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2180 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2181 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2182 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2183 | << Attr.getName() << 1; |
| 2184 | return; |
| 2185 | } |
| 2186 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2187 | D->addAttr(::new (S.Context) |
| 2188 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2189 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2192 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2193 | // weak_import only applies to variable & function declarations. |
| 2194 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2195 | if (!D->canBeWeakImported(isDef)) { |
| 2196 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2197 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2198 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2199 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2200 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2201 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2202 | // Nothing to warn about here. |
| 2203 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2204 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2205 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2206 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2207 | return; |
| 2208 | } |
| 2209 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2210 | D->addAttr(::new (S.Context) |
| 2211 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2212 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2215 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2216 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2217 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2218 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2219 | uint32_t WGSize[3]; |
| 2220 | for (unsigned i = 0; i < 3; ++i) |
| 2221 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2222 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2223 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2224 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2225 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2226 | Existing->getYDim() == WGSize[1] && |
| 2227 | Existing->getZDim() == WGSize[2])) |
| 2228 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2229 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2230 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2231 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2232 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2235 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2236 | if (!Attr.hasParsedType()) { |
| 2237 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2238 | << Attr.getName() << 1; |
| 2239 | return; |
| 2240 | } |
| 2241 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2242 | TypeSourceInfo *ParmTSI = 0; |
| 2243 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2244 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2245 | |
| 2246 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2247 | (ParmType->isBooleanType() || |
| 2248 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2249 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2250 | << ParmType; |
| 2251 | return; |
| 2252 | } |
| 2253 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2254 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2255 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2256 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2257 | return; |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2262 | ParmTSI, |
| 2263 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2266 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2267 | StringRef Name, |
| 2268 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2269 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2270 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2271 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2272 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2273 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2274 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2275 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2276 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2277 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2280 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2281 | // Make sure that there is a string literal as the sections's single |
| 2282 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2283 | StringRef Str; |
| 2284 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2285 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2286 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2288 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2289 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2290 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2291 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2292 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2293 | return; |
| 2294 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2296 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2297 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2298 | if (NewAttr) |
| 2299 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2302 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2303 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2304 | VarDecl *VD = cast<VarDecl>(D); |
| 2305 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2306 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2307 | return; |
| 2308 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2309 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2310 | Expr *E = Attr.getArgAsExpr(0); |
| 2311 | SourceLocation Loc = E->getExprLoc(); |
| 2312 | FunctionDecl *FD = 0; |
| 2313 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2314 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2315 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2316 | // will warn the user. |
| 2317 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2318 | if (DRE->hasQualifier()) |
| 2319 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2320 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2321 | NI = DRE->getNameInfo(); |
| 2322 | if (!FD) { |
| 2323 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2324 | << NI.getName(); |
| 2325 | return; |
| 2326 | } |
| 2327 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2328 | if (ULE->hasExplicitTemplateArgs()) |
| 2329 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2330 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2331 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2332 | if (!FD) { |
| 2333 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2334 | << NI.getName(); |
| 2335 | if (ULE->getType() == S.Context.OverloadTy) |
| 2336 | S.NoteAllOverloadCandidates(ULE); |
| 2337 | return; |
| 2338 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2339 | } else { |
| 2340 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2341 | return; |
| 2342 | } |
| 2343 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2344 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2345 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2346 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2347 | return; |
| 2348 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2349 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2350 | // We're currently more strict than GCC about what function types we accept. |
| 2351 | // If this ever proves to be a problem it should be easy to fix. |
| 2352 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2353 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2354 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2355 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2356 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2357 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2358 | return; |
| 2359 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2360 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2361 | D->addAttr(::new (S.Context) |
| 2362 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2363 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2366 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2367 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2368 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2369 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2370 | uint64_t Idx; |
| 2371 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2372 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2373 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2374 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2375 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2376 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2377 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2378 | if (not_nsstring_type && |
| 2379 | !isCFStringType(Ty, S.Context) && |
| 2380 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2381 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2382 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2383 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2384 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2385 | << IdxExpr->getSourceRange(); |
| 2386 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2387 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2388 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2389 | if (!isNSStringType(Ty, S.Context) && |
| 2390 | !isCFStringType(Ty, S.Context) && |
| 2391 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2392 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2393 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2394 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2395 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2396 | << IdxExpr->getSourceRange(); |
| 2397 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2400 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2401 | // because that has corrected for the implicit this parameter, and is zero- |
| 2402 | // based. The attribute expects what the user wrote explicitly. |
| 2403 | llvm::APSInt Val; |
| 2404 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2405 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2406 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2407 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2408 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2411 | enum FormatAttrKind { |
| 2412 | CFStringFormat, |
| 2413 | NSStringFormat, |
| 2414 | StrftimeFormat, |
| 2415 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2416 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2417 | InvalidFormat |
| 2418 | }; |
| 2419 | |
| 2420 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2421 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2422 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2423 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2424 | // Check for formats that get handled specially. |
| 2425 | .Case("NSString", NSStringFormat) |
| 2426 | .Case("CFString", CFStringFormat) |
| 2427 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2428 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2429 | // Otherwise, check for supported formats. |
| 2430 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2431 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2432 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2433 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2434 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2435 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2438 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2439 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2440 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2441 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2442 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2443 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2444 | return; |
| 2445 | } |
| 2446 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2447 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2448 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2449 | Attr.setInvalid(); |
| 2450 | return; |
| 2451 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2452 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2453 | if (S.Context.getAsArrayType(T)) |
| 2454 | T = S.Context.getBaseElementType(T); |
| 2455 | if (!T->getAs<RecordType>()) { |
| 2456 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2457 | Attr.setInvalid(); |
| 2458 | return; |
| 2459 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2460 | |
| 2461 | Expr *E = Attr.getArgAsExpr(0); |
| 2462 | uint32_t prioritynum; |
| 2463 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2464 | Attr.setInvalid(); |
| 2465 | return; |
| 2466 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2467 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2468 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2469 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2470 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2471 | Attr.setInvalid(); |
| 2472 | return; |
| 2473 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2474 | D->addAttr(::new (S.Context) |
| 2475 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2476 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2479 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2480 | IdentifierInfo *Format, int FormatIdx, |
| 2481 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2482 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2483 | // Check whether we already have an equivalent format attribute. |
| 2484 | for (specific_attr_iterator<FormatAttr> |
| 2485 | i = D->specific_attr_begin<FormatAttr>(), |
| 2486 | e = D->specific_attr_end<FormatAttr>(); |
| 2487 | i != e ; ++i) { |
| 2488 | FormatAttr *f = *i; |
| 2489 | if (f->getType() == Format && |
| 2490 | f->getFormatIdx() == FormatIdx && |
| 2491 | f->getFirstArg() == FirstArg) { |
| 2492 | // If we don't have a valid location for this attribute, adopt the |
| 2493 | // location. |
| 2494 | if (f->getLocation().isInvalid()) |
| 2495 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2496 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2497 | } |
| 2498 | } |
| 2499 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2500 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2501 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2504 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2505 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2506 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2507 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2508 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2509 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2510 | return; |
| 2511 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2512 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2513 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2514 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2515 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2516 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2517 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2518 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2519 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2520 | |
| 2521 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2522 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2523 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2524 | // If we've modified the string name, we need a new identifier for it. |
| 2525 | II = &S.Context.Idents.get(Format); |
| 2526 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2527 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2528 | // Check for supported formats. |
| 2529 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2530 | |
| 2531 | if (Kind == IgnoredFormat) |
| 2532 | return; |
| 2533 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2534 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2535 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2536 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2537 | return; |
| 2538 | } |
| 2539 | |
| 2540 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2541 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2542 | uint32_t Idx; |
| 2543 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2544 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2545 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2546 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2547 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2548 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2549 | return; |
| 2550 | } |
| 2551 | |
| 2552 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2553 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2554 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2555 | if (HasImplicitThisParam) { |
| 2556 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2557 | S.Diag(Attr.getLoc(), |
| 2558 | diag::err_format_attribute_implicit_this_format_string) |
| 2559 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2560 | return; |
| 2561 | } |
| 2562 | ArgIdx--; |
| 2563 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2565 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2566 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2567 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2568 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2569 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2570 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2571 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2574 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2575 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2576 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2577 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2578 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2579 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2580 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2581 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2582 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2583 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2584 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2585 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2586 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2587 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2588 | return; |
| 2589 | } |
| 2590 | |
| 2591 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2592 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2593 | uint32_t FirstArg; |
| 2594 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2595 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2596 | |
| 2597 | // check if the function is variadic if the 3rd argument non-zero |
| 2598 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2599 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2600 | ++NumArgs; // +1 for ... |
| 2601 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2602 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2603 | return; |
| 2604 | } |
| 2605 | } |
| 2606 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2607 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2608 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2609 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2610 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2611 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2612 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
| 2615 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2616 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2617 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2618 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2619 | return; |
| 2620 | } |
| 2621 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2622 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2623 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2624 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2625 | if (NewAttr) |
| 2626 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2629 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2630 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2631 | // Try to find the underlying union declaration. |
| 2632 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2633 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2634 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2635 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2636 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2637 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2638 | |
| 2639 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2640 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2641 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
| 2644 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2645 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2646 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2647 | diag::warn_transparent_union_attribute_not_definition); |
| 2648 | return; |
| 2649 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2650 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2651 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2652 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2653 | if (Field == FieldEnd) { |
| 2654 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2655 | return; |
| 2656 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2657 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2658 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2659 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2660 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2661 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2662 | diag::warn_transparent_union_attribute_floating) |
| 2663 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2668 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2669 | for (; Field != FieldEnd; ++Field) { |
| 2670 | QualType FieldType = Field->getType(); |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2671 | // FIXME: this isn't fully correct; we also need to test whether the |
| 2672 | // members of the union would all have the same calling convention as the |
| 2673 | // first member of the union. Checking just the size and alignment isn't |
| 2674 | // sufficient (consider structs passed on the stack instead of in registers |
| 2675 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2676 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 2677 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2678 | // Warn if we drop the attribute. |
| 2679 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2680 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2681 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2682 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2683 | diag::warn_transparent_union_attribute_field_size_align) |
| 2684 | << isSize << Field->getDeclName() << FieldBits; |
| 2685 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2686 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2687 | diag::note_transparent_union_first_field_size_align) |
| 2688 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2689 | return; |
| 2690 | } |
| 2691 | } |
| 2692 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2693 | RD->addAttr(::new (S.Context) |
| 2694 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2695 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2698 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2699 | // Make sure that there is a string literal as the annotation's single |
| 2700 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2701 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2702 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2703 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2704 | |
| 2705 | // Don't duplicate annotations that are already set. |
| 2706 | for (specific_attr_iterator<AnnotateAttr> |
| 2707 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2708 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2709 | if ((*i)->getAnnotation() == Str) |
| 2710 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2711 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2712 | |
| 2713 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2714 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2715 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2718 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2719 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2720 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2721 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2722 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2723 | return; |
| 2724 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2725 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2726 | if (Attr.getNumArgs() == 0) { |
| 2727 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2728 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2729 | return; |
| 2730 | } |
| 2731 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2732 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2733 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2734 | S.Diag(Attr.getEllipsisLoc(), |
| 2735 | diag::err_pack_expansion_without_parameter_packs); |
| 2736 | return; |
| 2737 | } |
| 2738 | |
| 2739 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2740 | return; |
| 2741 | |
| 2742 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2743 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2747 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2748 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2749 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2750 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2751 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2752 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2753 | // C++11 [dcl.align]p1: |
| 2754 | // An alignment-specifier may be applied to a variable or to a class |
| 2755 | // data member, but it shall not be applied to a bit-field, a function |
| 2756 | // parameter, the formal parameter of a catch clause, or a variable |
| 2757 | // declared with the register storage class specifier. An |
| 2758 | // alignment-specifier may also be applied to the declaration of a class |
| 2759 | // or enumeration type. |
| 2760 | // C11 6.7.5/2: |
| 2761 | // An alignment attribute shall not be specified in a declaration of |
| 2762 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2763 | // object declared with the register storage-class specifier. |
| 2764 | int DiagKind = -1; |
| 2765 | if (isa<ParmVarDecl>(D)) { |
| 2766 | DiagKind = 0; |
| 2767 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2768 | if (VD->getStorageClass() == SC_Register) |
| 2769 | DiagKind = 1; |
| 2770 | if (VD->isExceptionVariable()) |
| 2771 | DiagKind = 2; |
| 2772 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2773 | if (FD->isBitField()) |
| 2774 | DiagKind = 3; |
| 2775 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2776 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2777 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2778 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2779 | return; |
| 2780 | } |
| 2781 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2782 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2783 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2784 | return; |
| 2785 | } |
| 2786 | } |
| 2787 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2788 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2789 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2790 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2791 | AA->setPackExpansion(IsPackExpansion); |
| 2792 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2793 | return; |
| 2794 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2795 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2796 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2797 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2798 | ExprResult ICE |
| 2799 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2800 | diag::err_aligned_attribute_argument_not_int, |
| 2801 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2802 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2803 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2804 | |
| 2805 | // C++11 [dcl.align]p2: |
| 2806 | // -- if the constant expression evaluates to zero, the alignment |
| 2807 | // specifier shall have no effect |
| 2808 | // C11 6.7.5p6: |
| 2809 | // An alignment specification of zero has no effect. |
| 2810 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2811 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2812 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2813 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2814 | return; |
| 2815 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2816 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame^] | 2817 | // Alignment calculations can wrap around if it's greater than 2**28. |
| 2818 | unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456; |
| 2819 | if (Alignment.getZExtValue() > MaxValidAlignment) { |
| 2820 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 2821 | << E->getSourceRange(); |
| 2822 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2823 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2824 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2825 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 2826 | ICE.take(), SpellingListIndex); |
| 2827 | AA->setPackExpansion(IsPackExpansion); |
| 2828 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2831 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2832 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2833 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2834 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2835 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2836 | SpellingListIndex); |
| 2837 | AA->setPackExpansion(IsPackExpansion); |
| 2838 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2839 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2840 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2841 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2842 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2843 | |
| 2844 | QualType Ty; |
| 2845 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2846 | Ty = VD->getType(); |
| 2847 | else |
| 2848 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 2849 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2850 | return; |
| 2851 | |
| 2852 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2853 | // The combined effect of all alignment attributes in a declaration shall |
| 2854 | // not specify an alignment that is less strict than the alignment that |
| 2855 | // would otherwise be required for the entity being declared. |
| 2856 | AlignedAttr *AlignasAttr = 0; |
| 2857 | unsigned Align = 0; |
| 2858 | for (specific_attr_iterator<AlignedAttr> |
| 2859 | I = D->specific_attr_begin<AlignedAttr>(), |
| 2860 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 2861 | if (I->isAlignmentDependent()) |
| 2862 | return; |
| 2863 | if (I->isAlignas()) |
| 2864 | AlignasAttr = *I; |
| 2865 | Align = std::max(Align, I->getAlignment(Context)); |
| 2866 | } |
| 2867 | |
| 2868 | if (AlignasAttr && Align) { |
| 2869 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 2870 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 2871 | if (NaturalAlign > RequestedAlign) |
| 2872 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 2873 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 2874 | } |
| 2875 | } |
| 2876 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2877 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2878 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2879 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 2880 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 2881 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2882 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 2883 | // have to wait until the record is defined to catch any mismatches. |
| 2884 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 2885 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2886 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2887 | // The unspecified model never matches what a definition could need. |
| 2888 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 2889 | return false; |
| 2890 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 2891 | if (BestCase) { |
| 2892 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 2893 | return false; |
| 2894 | } else { |
| 2895 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 2896 | return false; |
| 2897 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 2898 | |
| 2899 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 2900 | << 0 /*definition*/; |
| 2901 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 2902 | << RD->getNameAsString(); |
| 2903 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2906 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2907 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2908 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2909 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2910 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2911 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2912 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2913 | // This attribute isn't documented, but glibc uses it. It changes |
| 2914 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2915 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 2916 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 2917 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2918 | return; |
| 2919 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2920 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2921 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 2922 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2923 | |
| 2924 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2925 | if (Str.startswith("__") && Str.endswith("__")) |
| 2926 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2927 | |
| 2928 | unsigned DestWidth = 0; |
| 2929 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2930 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2931 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2932 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2933 | switch (Str[0]) { |
| 2934 | case 'Q': DestWidth = 8; break; |
| 2935 | case 'H': DestWidth = 16; break; |
| 2936 | case 'S': DestWidth = 32; break; |
| 2937 | case 'D': DestWidth = 64; break; |
| 2938 | case 'X': DestWidth = 96; break; |
| 2939 | case 'T': DestWidth = 128; break; |
| 2940 | } |
| 2941 | if (Str[1] == 'F') { |
| 2942 | IntegerMode = false; |
| 2943 | } else if (Str[1] == 'C') { |
| 2944 | IntegerMode = false; |
| 2945 | ComplexMode = true; |
| 2946 | } else if (Str[1] != 'I') { |
| 2947 | DestWidth = 0; |
| 2948 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2949 | break; |
| 2950 | case 4: |
| 2951 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2952 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2953 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2954 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2955 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2956 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2957 | break; |
| 2958 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2959 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2960 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2961 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2962 | case 11: |
| 2963 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 2964 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2965 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2969 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2970 | OldTy = TD->getUnderlyingType(); |
| 2971 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2972 | OldTy = VD->getType(); |
| 2973 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2974 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 2975 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2976 | return; |
| 2977 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2978 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2979 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2980 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2981 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2982 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2983 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2984 | } else if (ComplexMode) { |
| 2985 | if (!OldTy->isComplexType()) |
| 2986 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2987 | } else { |
| 2988 | if (!OldTy->isFloatingType()) |
| 2989 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2990 | } |
| 2991 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2992 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2993 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2994 | // FIXME: Make sure floating-point mappings are accurate |
| 2995 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2996 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 2997 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2998 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | QualType NewTy; |
| 3002 | |
| 3003 | if (IntegerMode) |
| 3004 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3005 | OldTy->isSignedIntegerType()); |
| 3006 | else |
| 3007 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3008 | |
| 3009 | if (NewTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3010 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3011 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3014 | if (ComplexMode) { |
| 3015 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3019 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3020 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3021 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3022 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3023 | |
| 3024 | D->addAttr(::new (S.Context) |
| 3025 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3026 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3027 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3028 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3029 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3030 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3031 | if (!VD->hasGlobalStorage()) |
| 3032 | S.Diag(Attr.getLoc(), |
| 3033 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3034 | << Attr.getName(); |
| 3035 | } else if (!isFunctionOrMethod(D)) { |
| 3036 | S.Diag(Attr.getLoc(), |
| 3037 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3038 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3039 | return; |
| 3040 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3041 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3042 | D->addAttr(::new (S.Context) |
| 3043 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3044 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3047 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3048 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3049 | if (!FD->getReturnType()->isVoidType()) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3050 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| 3051 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
| 3052 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3053 | << FD->getType() |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 3054 | << FixItHint::CreateReplacement(FTL.getReturnLoc().getSourceRange(), |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3055 | "void"); |
| 3056 | } else { |
| 3057 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3058 | << FD->getType(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3059 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3060 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3061 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3062 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3063 | D->addAttr(::new (S.Context) |
| 3064 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3065 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3068 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3069 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3070 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3071 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3072 | return; |
| 3073 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3074 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3075 | D->addAttr(::new (S.Context) |
| 3076 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3077 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3080 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3081 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3082 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3083 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3084 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3085 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3086 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3087 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3088 | return; |
| 3089 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3090 | if (!isa<ObjCMethodDecl>(D)) { |
| 3091 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3092 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3093 | return; |
| 3094 | } |
| 3095 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3096 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3097 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3098 | D->addAttr(::new (S.Context) |
| 3099 | FastCallAttr(Attr.getRange(), S.Context, |
| 3100 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3101 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3102 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3103 | D->addAttr(::new (S.Context) |
| 3104 | StdCallAttr(Attr.getRange(), S.Context, |
| 3105 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3106 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3107 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3108 | D->addAttr(::new (S.Context) |
| 3109 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3110 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3111 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3112 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3113 | D->addAttr(::new (S.Context) |
| 3114 | CDeclAttr(Attr.getRange(), S.Context, |
| 3115 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3116 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3117 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3118 | D->addAttr(::new (S.Context) |
| 3119 | PascalAttr(Attr.getRange(), S.Context, |
| 3120 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3121 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3122 | case AttributeList::AT_MSABI: |
| 3123 | D->addAttr(::new (S.Context) |
| 3124 | MSABIAttr(Attr.getRange(), S.Context, |
| 3125 | Attr.getAttributeSpellingListIndex())); |
| 3126 | return; |
| 3127 | case AttributeList::AT_SysVABI: |
| 3128 | D->addAttr(::new (S.Context) |
| 3129 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3130 | Attr.getAttributeSpellingListIndex())); |
| 3131 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3132 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3133 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3134 | switch (CC) { |
| 3135 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3136 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3137 | break; |
| 3138 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3139 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3140 | break; |
| 3141 | default: |
| 3142 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3145 | D->addAttr(::new (S.Context) |
| 3146 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3147 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3148 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3149 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3150 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3151 | D->addAttr(::new (S.Context) |
| 3152 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3153 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3154 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3155 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3156 | D->addAttr(::new (S.Context) |
| 3157 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3158 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3159 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3160 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3161 | default: |
| 3162 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3166 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3167 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3168 | if (attr.isInvalid()) |
| 3169 | return true; |
| 3170 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3171 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3172 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3173 | attr.setInvalid(); |
| 3174 | return true; |
| 3175 | } |
| 3176 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3177 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3178 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3179 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3180 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3181 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3182 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3183 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3184 | case AttributeList::AT_MSABI: |
| 3185 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3186 | CC_X86_64Win64; |
| 3187 | break; |
| 3188 | case AttributeList::AT_SysVABI: |
| 3189 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3190 | CC_C; |
| 3191 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3192 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3193 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3194 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3195 | attr.setInvalid(); |
| 3196 | return true; |
| 3197 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3198 | if (StrRef == "aapcs") { |
| 3199 | CC = CC_AAPCS; |
| 3200 | break; |
| 3201 | } else if (StrRef == "aapcs-vfp") { |
| 3202 | CC = CC_AAPCS_VFP; |
| 3203 | break; |
| 3204 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3205 | |
| 3206 | attr.setInvalid(); |
| 3207 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3208 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3209 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3210 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3211 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3212 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3215 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3216 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3217 | if (A == TargetInfo::CCCR_Warning) { |
| 3218 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3219 | |
| 3220 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3221 | if (FD) |
| 3222 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3223 | TargetInfo::CCMT_NonMember; |
| 3224 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3227 | return false; |
| 3228 | } |
| 3229 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3230 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3231 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3232 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3233 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3234 | return true; |
| 3235 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3236 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3237 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3238 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3239 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3240 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3241 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3242 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3243 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3244 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3245 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3248 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3249 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3250 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3251 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3252 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3255 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3256 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3257 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3258 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3259 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3260 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3263 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3266 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3267 | const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3268 | // check the attribute arguments. |
| 3269 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| 3270 | // FIXME: 0 is not okay. |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 3271 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3272 | << Attr.getName() << 2; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3273 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3274 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3275 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3276 | uint32_t MaxThreads, MinBlocks = 0; |
| 3277 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3278 | return; |
| 3279 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3280 | Attr.getArgAsExpr(1), |
| 3281 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3282 | return; |
| 3283 | |
| 3284 | D->addAttr(::new (S.Context) |
| 3285 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3286 | MaxThreads, MinBlocks, |
| 3287 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3290 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3291 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3292 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3293 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3294 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3295 | return; |
| 3296 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3297 | |
| 3298 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3299 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3300 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3301 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3302 | |
| 3303 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3304 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3305 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3306 | return; |
| 3307 | } |
| 3308 | |
| 3309 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3310 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3311 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3312 | return; |
| 3313 | |
| 3314 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3315 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3316 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3317 | return; |
| 3318 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3319 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3320 | if (IsPointer) { |
| 3321 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3322 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3323 | if (!BufferTy->isPointerType()) { |
| 3324 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3325 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3329 | D->addAttr(::new (S.Context) |
| 3330 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3331 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3332 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3336 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3337 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3338 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3339 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3342 | |
| 3343 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3344 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3345 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3346 | if (!isa<VarDecl>(D)) { |
| 3347 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3348 | << Attr.getName() << ExpectedVariable; |
| 3349 | return; |
| 3350 | } |
| 3351 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3352 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3353 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3354 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3355 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3356 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3357 | D->addAttr(::new (S.Context) |
| 3358 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3359 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3360 | Attr.getLayoutCompatible(), |
| 3361 | Attr.getMustBeNull(), |
| 3362 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3365 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3366 | // Checker-specific attribute handlers. |
| 3367 | //===----------------------------------------------------------------------===// |
| 3368 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3369 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3370 | return type->isDependentType() || |
| 3371 | type->isObjCObjectPointerType() || |
| 3372 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3373 | } |
| 3374 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3375 | return type->isDependentType() || |
| 3376 | type->isPointerType() || |
| 3377 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3380 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3381 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3382 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3383 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3384 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3385 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3386 | cf = false; |
| 3387 | } else { |
| 3388 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3389 | cf = true; |
| 3390 | } |
| 3391 | |
| 3392 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3393 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3394 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3395 | return; |
| 3396 | } |
| 3397 | |
| 3398 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3399 | param->addAttr(::new (S.Context) |
| 3400 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3401 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3402 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3403 | param->addAttr(::new (S.Context) |
| 3404 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3405 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3408 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3409 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3410 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3411 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3412 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3413 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3414 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3415 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3416 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3417 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3418 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3419 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3420 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3421 | returnType = FD->getReturnType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3422 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3423 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3424 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3425 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3426 | return; |
| 3427 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3428 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3429 | bool typeOK; |
| 3430 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3431 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3432 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3433 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3434 | case AttributeList::AT_NSReturnsRetained: |
| 3435 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3436 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3437 | cf = false; |
| 3438 | break; |
| 3439 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3440 | case AttributeList::AT_CFReturnsRetained: |
| 3441 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3442 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3443 | cf = true; |
| 3444 | break; |
| 3445 | } |
| 3446 | |
| 3447 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3448 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3449 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3450 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3451 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3452 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3453 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3454 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3455 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3456 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3457 | D->addAttr(::new (S.Context) |
| 3458 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3459 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3460 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3461 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3462 | D->addAttr(::new (S.Context) |
| 3463 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3464 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3465 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3466 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3467 | D->addAttr(::new (S.Context) |
| 3468 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3469 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3470 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3471 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3472 | D->addAttr(::new (S.Context) |
| 3473 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3474 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3475 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3476 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3477 | D->addAttr(::new (S.Context) |
| 3478 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3479 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3480 | return; |
| 3481 | }; |
| 3482 | } |
| 3483 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3484 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3485 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3486 | const int EP_ObjCMethod = 1; |
| 3487 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3488 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3489 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3490 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3491 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3492 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3493 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3494 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3495 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3496 | if (!resultType->isReferenceType() && |
| 3497 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3498 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3499 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3500 | << attr.getName() |
| 3501 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3502 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3503 | |
| 3504 | // Drop the attribute. |
| 3505 | return; |
| 3506 | } |
| 3507 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3508 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3509 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3510 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3513 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3514 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3515 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3516 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3517 | DeclContext *DC = method->getDeclContext(); |
| 3518 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3519 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3520 | << attr.getName() << 0; |
| 3521 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3522 | return; |
| 3523 | } |
| 3524 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3525 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3526 | << attr.getName() << 1; |
| 3527 | return; |
| 3528 | } |
| 3529 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3530 | method->addAttr(::new (S.Context) |
| 3531 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3532 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3535 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3536 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3537 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3538 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3539 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3540 | D->addAttr(::new (S.Context) |
| 3541 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3542 | Attr.getAttributeSpellingListIndex())); |
| 3543 | } |
| 3544 | |
| 3545 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3546 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3547 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3548 | return; |
| 3549 | |
| 3550 | D->addAttr(::new (S.Context) |
| 3551 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3552 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3555 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3556 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3557 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3558 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3559 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3560 | 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] | 3561 | return; |
| 3562 | } |
| 3563 | |
| 3564 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3565 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3566 | Attr.getAttributeSpellingListIndex())); |
| 3567 | } |
| 3568 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3569 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3570 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3571 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3572 | |
| 3573 | if (!Parm) { |
| 3574 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3575 | return; |
| 3576 | } |
| 3577 | |
| 3578 | D->addAttr(::new (S.Context) |
| 3579 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3580 | Attr.getAttributeSpellingListIndex())); |
| 3581 | } |
| 3582 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3583 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3584 | const AttributeList &Attr) { |
| 3585 | IdentifierInfo *RelatedClass = |
| 3586 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0; |
| 3587 | if (!RelatedClass) { |
| 3588 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3589 | return; |
| 3590 | } |
| 3591 | IdentifierInfo *ClassMethod = |
| 3592 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0; |
| 3593 | IdentifierInfo *InstanceMethod = |
| 3594 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0; |
| 3595 | D->addAttr(::new (S.Context) |
| 3596 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3597 | ClassMethod, InstanceMethod, |
| 3598 | Attr.getAttributeSpellingListIndex())); |
| 3599 | } |
| 3600 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3601 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3602 | const AttributeList &Attr) { |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3603 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3604 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3605 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3606 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3607 | Attr.getAttributeSpellingListIndex())); |
| 3608 | } |
| 3609 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3610 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3611 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3612 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3613 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3614 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3615 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3618 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3619 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3620 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3621 | QualType type = vd->getType(); |
| 3622 | |
| 3623 | if (!type->isDependentType() && |
| 3624 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3625 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3626 | << type; |
| 3627 | return; |
| 3628 | } |
| 3629 | |
| 3630 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3631 | |
| 3632 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3633 | // going to infer. |
| 3634 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3635 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3636 | |
| 3637 | switch (lifetime) { |
| 3638 | case Qualifiers::OCL_None: |
| 3639 | assert(type->isDependentType() && |
| 3640 | "didn't infer lifetime for non-dependent type?"); |
| 3641 | break; |
| 3642 | |
| 3643 | case Qualifiers::OCL_Weak: // meaningful |
| 3644 | case Qualifiers::OCL_Strong: // meaningful |
| 3645 | break; |
| 3646 | |
| 3647 | case Qualifiers::OCL_ExplicitNone: |
| 3648 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3649 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3650 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3651 | break; |
| 3652 | } |
| 3653 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3654 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3655 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3656 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3659 | //===----------------------------------------------------------------------===// |
| 3660 | // Microsoft specific attribute handlers. |
| 3661 | //===----------------------------------------------------------------------===// |
| 3662 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3663 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3664 | if (!S.LangOpts.CPlusPlus) { |
| 3665 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3666 | << Attr.getName() << AttributeLangSupport::C; |
| 3667 | return; |
| 3668 | } |
| 3669 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3670 | if (!isa<CXXRecordDecl>(D)) { |
| 3671 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3672 | << Attr.getName() << ExpectedClass; |
| 3673 | return; |
| 3674 | } |
| 3675 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3676 | StringRef StrRef; |
| 3677 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3678 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3679 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3680 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3681 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3682 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3683 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3684 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3685 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3686 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3687 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3688 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3689 | return; |
| 3690 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3691 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3692 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3693 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3694 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3695 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3696 | return; |
| 3697 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3698 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3699 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3700 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3701 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3702 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3703 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3704 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3705 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3708 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3709 | if (!S.LangOpts.CPlusPlus) { |
| 3710 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3711 | << Attr.getName() << AttributeLangSupport::C; |
| 3712 | return; |
| 3713 | } |
| 3714 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3715 | D, Attr.getRange(), /*BestCase=*/true, |
| 3716 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3717 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
| 3718 | if (IA) |
| 3719 | D->addAttr(IA); |
| 3720 | } |
| 3721 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3722 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 3723 | const AttributeList &Attr) { |
| 3724 | // Check the attribute arguments. |
| 3725 | if (Attr.getNumArgs() > 1) { |
| 3726 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3727 | << Attr.getName() << 1; |
| 3728 | return; |
| 3729 | } |
| 3730 | |
| 3731 | StringRef Str; |
| 3732 | SourceLocation ArgLoc; |
| 3733 | |
| 3734 | if (Attr.getNumArgs() == 0) |
| 3735 | Str = ""; |
| 3736 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 3737 | return; |
| 3738 | |
| 3739 | ARMInterruptAttr::InterruptType Kind; |
| 3740 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 3741 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3742 | << Attr.getName() << Str << ArgLoc; |
| 3743 | return; |
| 3744 | } |
| 3745 | |
| 3746 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3747 | D->addAttr(::new (S.Context) |
| 3748 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 3749 | } |
| 3750 | |
| 3751 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 3752 | const AttributeList &Attr) { |
| 3753 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3754 | return; |
| 3755 | |
| 3756 | if (!Attr.isArgExpr(0)) { |
| 3757 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3758 | << AANT_ArgumentIntegerConstant; |
| 3759 | return; |
| 3760 | } |
| 3761 | |
| 3762 | // FIXME: Check for decl - it should be void ()(void). |
| 3763 | |
| 3764 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 3765 | llvm::APSInt NumParams(32); |
| 3766 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 3767 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3768 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3769 | << NumParamsExpr->getSourceRange(); |
| 3770 | return; |
| 3771 | } |
| 3772 | |
| 3773 | unsigned Num = NumParams.getLimitedValue(255); |
| 3774 | if ((Num & 1) || Num > 30) { |
| 3775 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3776 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 3777 | << NumParamsExpr->getSourceRange(); |
| 3778 | return; |
| 3779 | } |
| 3780 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3781 | D->addAttr(::new (S.Context) |
| 3782 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 3783 | Attr.getAttributeSpellingListIndex())); |
| 3784 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3788 | // Dispatch the interrupt attribute based on the current target. |
| 3789 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 3790 | handleMSP430InterruptAttr(S, D, Attr); |
| 3791 | else |
| 3792 | handleARMInterruptAttr(S, D, Attr); |
| 3793 | } |
| 3794 | |
| 3795 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 3796 | const AttributeList& Attr) { |
| 3797 | // If we try to apply it to a function pointer, don't warn, but don't |
| 3798 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 3799 | // special about calling a force_align_arg_pointer function. |
| 3800 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3801 | if (VD && VD->getType()->isFunctionPointerType()) |
| 3802 | return; |
| 3803 | // Also don't warn on function pointer typedefs. |
| 3804 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 3805 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 3806 | TD->getUnderlyingType()->isFunctionType())) |
| 3807 | return; |
| 3808 | // Attribute can only be applied to function types. |
| 3809 | if (!isa<FunctionDecl>(D)) { |
| 3810 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3811 | << Attr.getName() << /* function */0; |
| 3812 | return; |
| 3813 | } |
| 3814 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3815 | D->addAttr(::new (S.Context) |
| 3816 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 3817 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 3821 | unsigned AttrSpellingListIndex) { |
| 3822 | if (D->hasAttr<DLLExportAttr>()) { |
| 3823 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport"; |
| 3824 | return NULL; |
| 3825 | } |
| 3826 | |
| 3827 | if (D->hasAttr<DLLImportAttr>()) |
| 3828 | return NULL; |
| 3829 | |
| 3830 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3831 | if (VD->hasDefinition()) { |
| 3832 | // dllimport cannot be applied to definitions. |
| 3833 | Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition) |
| 3834 | << "dllimport"; |
| 3835 | return NULL; |
| 3836 | } |
| 3837 | } |
| 3838 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3839 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3843 | // Attribute can be applied only to functions or variables. |
| 3844 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3845 | if (!FD && !isa<VarDecl>(D)) { |
| 3846 | // Apparently Visual C++ thinks it is okay to not emit a warning |
| 3847 | // in this case, so only emit a warning when -fms-extensions is not |
| 3848 | // specified. |
| 3849 | if (!S.getLangOpts().MicrosoftExt) |
| 3850 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3851 | << Attr.getName() << ExpectedVariableOrFunction; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3852 | return; |
| 3853 | } |
| 3854 | |
| 3855 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 3856 | // Warning is emitted. |
| 3857 | if (FD && FD->isInlineSpecified()) { |
| 3858 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3859 | return; |
| 3860 | } |
| 3861 | |
| 3862 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3863 | DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); |
| 3864 | if (NewAttr) |
| 3865 | D->addAttr(NewAttr); |
| 3866 | } |
| 3867 | |
| 3868 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 3869 | unsigned AttrSpellingListIndex) { |
| 3870 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
| 3871 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport"; |
| 3872 | D->dropAttr<DLLImportAttr>(); |
| 3873 | } |
| 3874 | |
| 3875 | if (D->hasAttr<DLLExportAttr>()) |
| 3876 | return NULL; |
| 3877 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3878 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3882 | // Currently, the dllexport attribute is ignored for inlined functions, unless |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 3883 | // the -fkeep-inline-functions flag has been used. Warning is emitted. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3884 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) { |
| 3885 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 3886 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3887 | return; |
| 3888 | } |
| 3889 | |
| 3890 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3891 | DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); |
| 3892 | if (NewAttr) |
| 3893 | D->addAttr(NewAttr); |
| 3894 | } |
| 3895 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3896 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3897 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3898 | unsigned AttrSpellingListIndex, |
| 3899 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3900 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 3901 | if (IA->getSemanticSpelling() == SemanticSpelling) |
| 3902 | return 0; |
| 3903 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 3904 | << 1 /*previous declaration*/; |
| 3905 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 3906 | D->dropAttr<MSInheritanceAttr>(); |
| 3907 | } |
| 3908 | |
| 3909 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 3910 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3911 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 3912 | SemanticSpelling)) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3913 | return 0; |
| 3914 | } |
| 3915 | } else { |
| 3916 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 3917 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 3918 | << 1 /*partial specialization*/; |
| 3919 | return 0; |
| 3920 | } |
| 3921 | if (RD->getDescribedClassTemplate()) { |
| 3922 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 3923 | << 0 /*primary template*/; |
| 3924 | return 0; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3929 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3932 | /// Handles semantic checking for features that are common to all attributes, |
| 3933 | /// such as checking whether a parameter was properly specified, or the correct |
| 3934 | /// number of arguments were passed, etc. |
| 3935 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 3936 | const AttributeList &Attr) { |
| 3937 | // Several attributes carry different semantics than the parsing requires, so |
| 3938 | // those are opted out of the common handling. |
| 3939 | // |
| 3940 | // We also bail on unknown and ignored attributes because those are handled |
| 3941 | // as part of the target-specific handling logic. |
| 3942 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3943 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3944 | return false; |
| 3945 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3946 | // Check whether the attribute requires specific language extensions to be |
| 3947 | // enabled. |
| 3948 | if (!Attr.diagnoseLangOpts(S)) |
| 3949 | return true; |
| 3950 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3951 | // If there are no optional arguments, then checking for the argument count |
| 3952 | // is trivial. |
| 3953 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 3954 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 3955 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3956 | |
| 3957 | // Check whether the attribute appertains to the given subject. |
| 3958 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 3959 | return true; |
| 3960 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3961 | return false; |
| 3962 | } |
| 3963 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3964 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3965 | // Top Level Sema Entry Points |
| 3966 | //===----------------------------------------------------------------------===// |
| 3967 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3968 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3969 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3970 | /// silently ignore it if a GNU attribute. |
| 3971 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3972 | const AttributeList &Attr, |
| 3973 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3974 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3975 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3976 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3977 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 3978 | // instead. |
| 3979 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 3980 | return; |
| 3981 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3982 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 3983 | // which do not apply to the current target architecture are treated as |
| 3984 | // though they were unknown attributes. |
| 3985 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
| 3986 | !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) { |
| 3987 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 3988 | diag::warn_unhandled_ms_attribute_ignored : |
| 3989 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
| 3990 | return; |
| 3991 | } |
| 3992 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3993 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 3994 | return; |
| 3995 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3996 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3997 | default: |
| 3998 | // Type attributes are handled elsewhere; silently move on. |
| 3999 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 4000 | break; |
| 4001 | case AttributeList::AT_Interrupt: |
| 4002 | handleInterruptAttr(S, D, Attr); break; |
| 4003 | case AttributeList::AT_X86ForceAlignArgPointer: |
| 4004 | handleX86ForceAlignArgPointerAttr(S, D, Attr); break; |
| 4005 | case AttributeList::AT_DLLExport: |
| 4006 | handleDLLExportAttr(S, D, Attr); break; |
| 4007 | case AttributeList::AT_DLLImport: |
| 4008 | handleDLLImportAttr(S, D, Attr); break; |
| 4009 | case AttributeList::AT_Mips16: |
| 4010 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); break; |
| 4011 | case AttributeList::AT_NoMips16: |
| 4012 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 4013 | case AttributeList::AT_IBAction: |
| 4014 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4015 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4016 | case AttributeList::AT_IBOutletCollection: |
| 4017 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4018 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4019 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4020 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4021 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4022 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4023 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4024 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4025 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4026 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4027 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4028 | handleDependencyAttr(S, scope, D, Attr); |
| 4029 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4030 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4031 | case AttributeList::AT_CUDAConstant: |
| 4032 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4033 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4034 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 4035 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4036 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4037 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4038 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4039 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 4040 | case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4041 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4042 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4043 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4044 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4045 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4046 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4047 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4048 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4049 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 4050 | case AttributeList::AT_CUDADevice: |
| 4051 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4052 | case AttributeList::AT_CUDAHost: |
| 4053 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4054 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4055 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4056 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4057 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4058 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4059 | case AttributeList::AT_MayAlias: |
| 4060 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4061 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4062 | case AttributeList::AT_NoCommon: |
| 4063 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 4064 | case AttributeList::AT_NonNull: |
| 4065 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 4066 | handleNonNullAttrParameter(S, PVD, Attr); |
| 4067 | else |
| 4068 | handleNonNullAttr(S, D, Attr); |
| 4069 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 4070 | case AttributeList::AT_ReturnsNonNull: |
| 4071 | handleReturnsNonNullAttr(S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4072 | case AttributeList::AT_Overloadable: |
| 4073 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 4074 | case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4075 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4076 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4077 | case AttributeList::AT_Naked: |
| 4078 | handleSimpleAttribute<NakedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4079 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4080 | case AttributeList::AT_NoThrow: |
| 4081 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4082 | case AttributeList::AT_CUDAShared: |
| 4083 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4084 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4085 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4086 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4087 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4088 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4089 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4090 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4091 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4092 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4093 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4094 | case AttributeList::AT_ObjCRequiresSuper: |
| 4095 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4096 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4097 | case AttributeList::AT_ObjCBridge: |
| 4098 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4099 | |
| 4100 | case AttributeList::AT_ObjCBridgeMutable: |
| 4101 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4102 | |
| 4103 | case AttributeList::AT_ObjCBridgeRelated: |
| 4104 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4105 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4106 | case AttributeList::AT_ObjCDesignatedInitializer: |
| 4107 | handleObjCDesignatedInitializer(S, D, Attr); break; |
| 4108 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4109 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4110 | handleCFAuditedTransferAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4111 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4112 | handleCFUnknownTransferAttr(S, D, Attr); break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4113 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4114 | case AttributeList::AT_CFConsumed: |
| 4115 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4116 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4117 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4118 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4119 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4120 | case AttributeList::AT_NSReturnsNotRetained: |
| 4121 | case AttributeList::AT_CFReturnsNotRetained: |
| 4122 | case AttributeList::AT_NSReturnsRetained: |
| 4123 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4124 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4125 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4126 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4127 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4128 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4129 | case AttributeList::AT_VecTypeHint: |
| 4130 | handleVecTypeHint(S, D, Attr); break; |
| 4131 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4132 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4133 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4134 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4135 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4136 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4137 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4138 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4139 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4140 | case AttributeList::AT_ArcWeakrefUnavailable: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4141 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4142 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4143 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4144 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4145 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 4146 | break; |
| 4147 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4148 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 4149 | case AttributeList::AT_Unused: |
| 4150 | handleSimpleAttribute<UnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4151 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4152 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4153 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4154 | case AttributeList::AT_Visibility: |
| 4155 | handleVisibilityAttr(S, D, Attr, false); |
| 4156 | break; |
| 4157 | case AttributeList::AT_TypeVisibility: |
| 4158 | handleVisibilityAttr(S, D, Attr, true); |
| 4159 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4160 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 6a42b5a | 2013-11-27 16:59:17 +0000 | [diff] [blame] | 4161 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4162 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4163 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4164 | case AttributeList::AT_Weak: |
| 4165 | handleSimpleAttribute<WeakAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4166 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4167 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4168 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4169 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4170 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4171 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4172 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4173 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4174 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4175 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4176 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4177 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4178 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4179 | case AttributeList::AT_Const: |
| 4180 | handleSimpleAttribute<ConstAttr>(S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4181 | case AttributeList::AT_Pure: |
| 4182 | handleSimpleAttribute<PureAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4183 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4184 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4185 | case AttributeList::AT_NoInline: |
| 4186 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4187 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4188 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4189 | case AttributeList::AT_StdCall: |
| 4190 | case AttributeList::AT_CDecl: |
| 4191 | case AttributeList::AT_FastCall: |
| 4192 | case AttributeList::AT_ThisCall: |
| 4193 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4194 | case AttributeList::AT_MSABI: |
| 4195 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4196 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4197 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4198 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4199 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4200 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4201 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4202 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4203 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 2689133 | 2014-01-14 17:41:53 +0000 | [diff] [blame] | 4204 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4205 | |
| 4206 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4207 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4208 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4209 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4210 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4211 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4212 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4213 | case AttributeList::AT_MSInheritance: |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4214 | handleMSInheritanceAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4215 | case AttributeList::AT_ForceInline: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4216 | handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4217 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4218 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4219 | |
| 4220 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4221 | case AttributeList::AT_AssertExclusiveLock: |
| 4222 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4223 | break; |
| 4224 | case AttributeList::AT_AssertSharedLock: |
| 4225 | handleAssertSharedLockAttr(S, D, Attr); |
| 4226 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4227 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 4228 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4229 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4230 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4231 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4232 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4233 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4234 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4235 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4236 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4237 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4238 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4239 | break; |
| 4240 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4241 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4242 | break; |
| 4243 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4244 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4245 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4246 | case AttributeList::AT_Lockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4247 | handleSimpleAttribute<LockableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4248 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4249 | handleGuardedByAttr(S, D, Attr); |
| 4250 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4251 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4252 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4253 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4254 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4255 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4256 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4257 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4258 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4259 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4260 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4261 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4262 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4263 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4264 | handleLockReturnedAttr(S, D, Attr); |
| 4265 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4266 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4267 | handleLocksExcludedAttr(S, D, Attr); |
| 4268 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4269 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4270 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4271 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4272 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4273 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4274 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4275 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4276 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4277 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4278 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4279 | handleUnlockFunAttr(S, D, Attr); |
| 4280 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4281 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4282 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4283 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4284 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4285 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4286 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4287 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4288 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4289 | case AttributeList::AT_Consumable: |
| 4290 | handleConsumableAttr(S, D, Attr); |
| 4291 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4292 | case AttributeList::AT_ConsumableAutoCast: |
| 4293 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break; |
| 4294 | break; |
| 4295 | case AttributeList::AT_ConsumableSetOnRead: |
| 4296 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break; |
| 4297 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4298 | case AttributeList::AT_CallableWhen: |
| 4299 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4300 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4301 | case AttributeList::AT_ParamTypestate: |
| 4302 | handleParamTypestateAttr(S, D, Attr); |
| 4303 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4304 | case AttributeList::AT_ReturnTypestate: |
| 4305 | handleReturnTypestateAttr(S, D, Attr); |
| 4306 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4307 | case AttributeList::AT_SetTypestate: |
| 4308 | handleSetTypestateAttr(S, D, Attr); |
| 4309 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4310 | case AttributeList::AT_TestTypestate: |
| 4311 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4312 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4313 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4314 | // Type safety attributes. |
| 4315 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4316 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4317 | break; |
| 4318 | case AttributeList::AT_TypeTagForDatatype: |
| 4319 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4320 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4321 | } |
| 4322 | } |
| 4323 | |
| 4324 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4325 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4326 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4327 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4328 | bool IncludeCXX11Attributes) { |
| 4329 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4330 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4331 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4332 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4333 | // GCC accepts |
| 4334 | // static int a9 __attribute__((weakref)); |
| 4335 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4336 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 4337 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 4338 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4339 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4340 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4341 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4342 | |
| 4343 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4344 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4345 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 4346 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4347 | D->setInvalidDecl(); |
| 4348 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4349 | if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
| 4350 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4351 | D->setInvalidDecl(); |
| 4352 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4353 | if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
| 4354 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4355 | D->setInvalidDecl(); |
| 4356 | } |
| 4357 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4360 | // Annotation attributes are the only attributes allowed after an access |
| 4361 | // specifier. |
| 4362 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4363 | const AttributeList *AttrList) { |
| 4364 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4365 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4366 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4367 | } else { |
| 4368 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4369 | return true; |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | return false; |
| 4374 | } |
| 4375 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4376 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4377 | /// contains any decl attributes that we should warn about. |
| 4378 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4379 | for ( ; A; A = A->getNext()) { |
| 4380 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4381 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4382 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4383 | |
| 4384 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4385 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4386 | << A->getName() << A->getRange(); |
| 4387 | } else { |
| 4388 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4389 | << A->getName() << A->getRange(); |
| 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4395 | /// used to build a declaration, complain about any decl attributes |
| 4396 | /// which might be lying around on it. |
| 4397 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4398 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4399 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4400 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4401 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4402 | } |
| 4403 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4404 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4405 | /// \#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] | 4406 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4407 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4408 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4409 | NamedDecl *NewD = 0; |
| 4410 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4411 | FunctionDecl *NewFD; |
| 4412 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4413 | // FIXME: Mangling? |
| 4414 | // FIXME: Is the qualifier info correct? |
| 4415 | // FIXME: Is the DeclContext correct? |
| 4416 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4417 | Loc, Loc, DeclarationName(II), |
| 4418 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4419 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4420 | FD->hasPrototype(), |
| 4421 | false/*isConstexprSpecified*/); |
| 4422 | NewD = NewFD; |
| 4423 | |
| 4424 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4425 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4426 | |
| 4427 | // Fake up parameter variables; they are declared as if this were |
| 4428 | // a typedef. |
| 4429 | QualType FDTy = FD->getType(); |
| 4430 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4431 | SmallVector<ParmVarDecl*, 16> Params; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4432 | for (FunctionProtoType::param_type_iterator AI = FT->param_type_begin(), |
| 4433 | AE = FT->param_type_end(); |
| 4434 | AI != AE; ++AI) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4435 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4436 | Param->setScopeInfo(0, Params.size()); |
| 4437 | Params.push_back(Param); |
| 4438 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4439 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4440 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4441 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4442 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4443 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4444 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4445 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4446 | if (VD->getQualifier()) { |
| 4447 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4448 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4449 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4450 | } |
| 4451 | return NewD; |
| 4452 | } |
| 4453 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4454 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4455 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4456 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4457 | if (W.getUsed()) return; // only do this once |
| 4458 | W.setUsed(true); |
| 4459 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4460 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4461 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4462 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 4463 | W.getLocation())); |
| 4464 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4465 | WeakTopLevelDecl.push_back(NewD); |
| 4466 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4467 | // to insert Decl at TU scope, sorry. |
| 4468 | DeclContext *SavedContext = CurContext; |
| 4469 | CurContext = Context.getTranslationUnitDecl(); |
| 4470 | PushOnScopeChains(NewD, S); |
| 4471 | CurContext = SavedContext; |
| 4472 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4473 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4474 | } |
| 4475 | } |
| 4476 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4477 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4478 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4479 | // have to do this. |
| 4480 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4481 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4482 | NamedDecl *ND = NULL; |
| 4483 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4484 | if (VD->isExternC()) |
| 4485 | ND = VD; |
| 4486 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4487 | if (FD->isExternC()) |
| 4488 | ND = FD; |
| 4489 | if (ND) { |
| 4490 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4491 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4492 | = WeakUndeclaredIdentifiers.find(Id); |
| 4493 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4494 | WeakInfo W = I->second; |
| 4495 | DeclApplyPragmaWeak(S, ND, W); |
| 4496 | WeakUndeclaredIdentifiers[Id] = W; |
| 4497 | } |
| 4498 | } |
| 4499 | } |
| 4500 | } |
| 4501 | } |
| 4502 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4503 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4504 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4505 | /// 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] | 4506 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4507 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4508 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4509 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4510 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4511 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4512 | // position to the decl itself. This handles cases like: |
| 4513 | // int *__attr__(x)** D; |
| 4514 | // when X is a decl attribute. |
| 4515 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4516 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4517 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4518 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4519 | // Finally, apply any attributes on the decl itself. |
| 4520 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4521 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4522 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4523 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4524 | /// Is the given declaration allowed to use a forbidden type? |
| 4525 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4526 | // Private ivars are always okay. Unfortunately, people don't |
| 4527 | // always properly make their ivars private, even in system headers. |
| 4528 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4529 | // Function declarations in sys headers will be marked unavailable. |
| 4530 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4531 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4532 | return false; |
| 4533 | |
| 4534 | // Require it to be declared in a system header. |
| 4535 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4536 | } |
| 4537 | |
| 4538 | /// Handle a delayed forbidden-type diagnostic. |
| 4539 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4540 | Decl *decl) { |
| 4541 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4542 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, |
| 4543 | "this system declaration uses an unsupported type", |
| 4544 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4545 | return; |
| 4546 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4547 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4548 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4549 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4550 | // kind of forbidden type messages on unavailable functions. |
| 4551 | if (FD->hasAttr<UnavailableAttr>() && |
| 4552 | diag.getForbiddenTypeDiagnostic() == |
| 4553 | diag::err_arc_array_param_no_ownership) { |
| 4554 | diag.Triggered = true; |
| 4555 | return; |
| 4556 | } |
| 4557 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4558 | |
| 4559 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4560 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4561 | diag.Triggered = true; |
| 4562 | } |
| 4563 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4564 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4565 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4566 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4567 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4568 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4569 | // When delaying diagnostics to run in the context of a parsed |
| 4570 | // declaration, we only want to actually emit anything if parsing |
| 4571 | // succeeds. |
| 4572 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4573 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4574 | // We emit all the active diagnostics in this pool or any of its |
| 4575 | // parents. In general, we'll get one pool for the decl spec |
| 4576 | // and a child pool for each declarator; in a decl group like: |
| 4577 | // deprecated_typedef foo, *bar, baz(); |
| 4578 | // only the declarator pops will be passed decls. This is correct; |
| 4579 | // we really do need to consider delayed diagnostics from the decl spec |
| 4580 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4581 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4582 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4583 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4584 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4585 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4586 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4587 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4588 | continue; |
| 4589 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4590 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4591 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4592 | case DelayedDiagnostic::Unavailable: |
| 4593 | // Don't bother giving deprecation/unavailable diagnostics if |
| 4594 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4595 | if (!decl->isInvalidDecl()) |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4596 | HandleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4597 | break; |
| 4598 | |
| 4599 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4600 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4601 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4602 | |
| 4603 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4604 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4605 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4606 | } |
| 4607 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4608 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4611 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4612 | /// been delayed in the current context instead of in the given pool. |
| 4613 | /// Essentially, this just moves them to the current pool. |
| 4614 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4615 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4616 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4617 | curPool->steal(pool); |
| 4618 | } |
| 4619 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4620 | static bool isDeclDeprecated(Decl *D) { |
| 4621 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4622 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4623 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4624 | // A category implicitly has the availability of the interface. |
| 4625 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4626 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4627 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4628 | return false; |
| 4629 | } |
| 4630 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4631 | static bool isDeclUnavailable(Decl *D) { |
| 4632 | do { |
| 4633 | if (D->isUnavailable()) |
| 4634 | return true; |
| 4635 | // A category implicitly has the availability of the interface. |
| 4636 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4637 | return CatD->getClassInterface()->isUnavailable(); |
| 4638 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4639 | return false; |
| 4640 | } |
| 4641 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4642 | static void |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4643 | DoEmitAvailabilityWarning(Sema &S, |
| 4644 | DelayedDiagnostic::DDKind K, |
| 4645 | Decl *Ctx, |
| 4646 | const NamedDecl *D, |
| 4647 | StringRef Message, |
| 4648 | SourceLocation Loc, |
| 4649 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4650 | const ObjCPropertyDecl *ObjCProperty) { |
| 4651 | |
| 4652 | // Diagnostics for deprecated or unavailable. |
| 4653 | unsigned diag, diag_message, diag_fwdclass_message; |
| 4654 | |
| 4655 | // Matches 'diag::note_property_attribute' options. |
| 4656 | unsigned property_note_select; |
| 4657 | |
| 4658 | // Matches diag::note_availability_specified_here. |
| 4659 | unsigned available_here_select_kind; |
| 4660 | |
| 4661 | // Don't warn if our current context is deprecated or unavailable. |
| 4662 | switch (K) { |
| 4663 | case DelayedDiagnostic::Deprecation: |
| 4664 | if (isDeclDeprecated(Ctx)) |
| 4665 | return; |
| 4666 | diag = diag::warn_deprecated; |
| 4667 | diag_message = diag::warn_deprecated_message; |
| 4668 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 4669 | property_note_select = /* deprecated */ 0; |
| 4670 | available_here_select_kind = /* deprecated */ 2; |
| 4671 | break; |
| 4672 | |
| 4673 | case DelayedDiagnostic::Unavailable: |
| 4674 | if (isDeclUnavailable(Ctx)) |
| 4675 | return; |
| 4676 | diag = diag::err_unavailable; |
| 4677 | diag_message = diag::err_unavailable_message; |
| 4678 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 4679 | property_note_select = /* unavailable */ 1; |
| 4680 | available_here_select_kind = /* unavailable */ 0; |
| 4681 | break; |
| 4682 | |
| 4683 | default: |
| 4684 | llvm_unreachable("Neither a deprecation or unavailable kind"); |
| 4685 | } |
| 4686 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4687 | DeclarationName Name = D->getDeclName(); |
| 4688 | if (!Message.empty()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4689 | S.Diag(Loc, diag_message) << Name << Message; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4690 | if (ObjCProperty) |
| 4691 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4692 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4693 | } else if (!UnknownObjCClass) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4694 | S.Diag(Loc, diag) << Name; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4695 | if (ObjCProperty) |
| 4696 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4697 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4698 | } else { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4699 | S.Diag(Loc, diag_fwdclass_message) << Name; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4700 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4701 | } |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4702 | |
| 4703 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 4704 | << D << available_here_select_kind; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4707 | void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD, |
| 4708 | Decl *Ctx) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4709 | DD.Triggered = true; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4710 | DoEmitAvailabilityWarning(*this, |
| 4711 | (DelayedDiagnostic::DDKind) DD.Kind, |
| 4712 | Ctx, |
| 4713 | DD.getDeprecationDecl(), |
| 4714 | DD.getDeprecationMessage(), |
| 4715 | DD.Loc, |
| 4716 | DD.getUnknownObjCClass(), |
| 4717 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4720 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 4721 | NamedDecl *D, StringRef Message, |
| 4722 | SourceLocation Loc, |
| 4723 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4724 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4725 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4726 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4727 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D, |
| 4728 | UnknownObjCClass, |
| 4729 | ObjCProperty, |
| 4730 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4731 | return; |
| 4732 | } |
| 4733 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4734 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
| 4735 | DelayedDiagnostic::DDKind K; |
| 4736 | switch (AD) { |
| 4737 | case AD_Deprecation: |
| 4738 | K = DelayedDiagnostic::Deprecation; |
| 4739 | break; |
| 4740 | case AD_Unavailable: |
| 4741 | K = DelayedDiagnostic::Unavailable; |
| 4742 | break; |
| 4743 | } |
| 4744 | |
| 4745 | DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc, |
| 4746 | UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4747 | } |