blob: ff2594e114848927100bf2c5a8f158a253dd5f79 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- 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 McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000035namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000036 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000037 C,
38 Cpp,
39 ObjC
40 };
41}
42
Chris Lattner58418ff2008-06-29 00:16:31 +000043//===----------------------------------------------------------------------===//
44// Helper functions
45//===----------------------------------------------------------------------===//
46
Ted Kremenek527042b2009-08-14 20:49:40 +000047/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000048/// type (function or function-typed variable) or an Objective-C
49/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000050static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000051 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000052}
53
John McCall3882ace2011-01-05 12:14:39 +000054/// Return true if the given decl has a declarator that should have
55/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000056static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000057 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
59 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000060}
61
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000062/// hasFunctionProto - Return true if the given decl has a argument
63/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000064/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000065static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000066 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000067 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000068 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000069}
70
Alp Toker601b22c2014-01-21 23:35:24 +000071/// getFunctionOrMethodNumParams - Return number of function or method
72/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000073/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000074static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000075 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000076 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000078 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000080}
81
Alp Toker601b22c2014-01-21 23:35:24 +000082static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000083 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000084 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000086 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000087
Alp Toker03376dc2014-07-07 09:02:20 +000088 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000089}
90
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000091static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
92 if (const auto *FD = dyn_cast<FunctionDecl>(D))
93 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +000094 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000095 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +000096 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000097 return BD->getParamDecl(Idx)->getSourceRange();
98 return SourceRange();
99}
100
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000102 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000103 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000104 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000105}
106
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000107static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
108 if (const auto *FD = dyn_cast<FunctionDecl>(D))
109 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000110 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000111 return MD->getReturnTypeSourceRange();
112 return SourceRange();
113}
114
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000116 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000118 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000119 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000120 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
121 return BD->isVariadic();
122
123 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000124}
125
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126static bool isInstanceMethod(const Decl *D) {
127 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000128 return MethodDecl->isInstance();
129 return false;
130}
131
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000132static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000133 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000134 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000135 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000136
John McCall96fa4842010-05-17 21:00:27 +0000137 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
138 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000139 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000140
John McCall96fa4842010-05-17 21:00:27 +0000141 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000142
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143 // FIXME: Should we walk the chain of classes?
144 return ClsName == &Ctx.Idents.get("NSString") ||
145 ClsName == &Ctx.Idents.get("NSMutableString");
146}
147
Daniel Dunbar980c6692008-09-26 03:32:58 +0000148static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000149 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000150 if (!PT)
151 return false;
152
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000153 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000154 if (!RT)
155 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000156
Daniel Dunbar980c6692008-09-26 03:32:58 +0000157 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000158 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000159 return false;
160
161 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
162}
163
Richard Smithb87c4652013-10-31 21:23:20 +0000164static unsigned getNumAttributeArgs(const AttributeList &Attr) {
165 // FIXME: Include the type in the argument list.
166 return Attr.getNumArgs() + Attr.hasParsedType();
167}
168
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000169template <typename Compare>
170static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
171 unsigned Num, unsigned Diag,
172 Compare Comp) {
173 if (Comp(getNumAttributeArgs(Attr), Num)) {
174 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000175 return false;
176 }
177
178 return true;
179}
180
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000181/// \brief Check if the attribute has exactly as many args as Num. May
182/// output an error.
183static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
184 unsigned Num) {
185 return checkAttributeNumArgsImpl(S, Attr, Num,
186 diag::err_attribute_wrong_number_arguments,
187 std::not_equal_to<unsigned>());
188}
189
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000190/// \brief Check if the attribute has at least as many args as Num. May
191/// output an error.
192static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000193 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000194 return checkAttributeNumArgsImpl(S, Attr, Num,
195 diag::err_attribute_too_few_arguments,
196 std::less<unsigned>());
197}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000198
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000199/// \brief Check if the attribute has at most as many args as Num. May
200/// output an error.
201static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned Num) {
203 return checkAttributeNumArgsImpl(S, Attr, Num,
204 diag::err_attribute_too_many_arguments,
205 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000206}
207
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000208/// \brief If Expr is a valid integer constant, get the value of the integer
209/// expression and return success or failure. May output an error.
210static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
211 const Expr *Expr, uint32_t &Val,
212 unsigned Idx = UINT_MAX) {
213 llvm::APSInt I(32);
214 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
215 !Expr->isIntegerConstantExpr(I, S.Context)) {
216 if (Idx != UINT_MAX)
217 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
218 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
219 << Expr->getSourceRange();
220 else
221 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
222 << Attr.getName() << AANT_ArgumentIntegerConstant
223 << Expr->getSourceRange();
224 return false;
225 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000226
227 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000228 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
229 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000230 return false;
231 }
232
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000233 Val = (uint32_t)I.getZExtValue();
234 return true;
235}
236
Aaron Ballmanfb763042013-12-02 18:05:46 +0000237/// \brief Diagnose mutually exclusive attributes when present on a given
238/// declaration. Returns true if diagnosed.
239template <typename AttrTy>
240static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000241 const AttributeList &Attr) {
242 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000243 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000244 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000245 return true;
246 }
247 return false;
248}
249
Alp Toker601b22c2014-01-21 23:35:24 +0000250/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000251/// instance method D. May output an error.
252///
253/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000254static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
255 const AttributeList &Attr,
256 unsigned AttrArgNum,
257 const Expr *IdxExpr,
258 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000259 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000260
261 // In C++ the implicit 'this' function parameter also counts.
262 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000263 bool HP = hasFunctionProto(D);
264 bool HasImplicitThisParam = isInstanceMethod(D);
265 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000266 unsigned NumParams =
267 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000268
269 llvm::APSInt IdxInt;
270 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
271 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000272 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
273 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
274 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000275 return false;
276 }
277
278 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000279 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000280 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
281 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000282 return false;
283 }
284 Idx--; // Convert to zero-based.
285 if (HasImplicitThisParam) {
286 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000287 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000288 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000289 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000290 return false;
291 }
292 --Idx;
293 }
294
295 return true;
296}
297
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000298/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
299/// If not emit an error and return false. If the argument is an identifier it
300/// will emit an error with a fixit hint and treat it as if it was a string
301/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000302bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
303 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000304 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000305 // Look for identifiers. If we have one emit a hint to fix it to a literal.
306 if (Attr.isArgIdent(ArgNum)) {
307 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000308 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000309 << Attr.getName() << AANT_ArgumentString
310 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000311 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000312 Str = Loc->Ident->getName();
313 if (ArgLocation)
314 *ArgLocation = Loc->Loc;
315 return true;
316 }
317
318 // Now check for an actual string literal.
319 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
320 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
321 if (ArgLocation)
322 *ArgLocation = ArgExpr->getLocStart();
323
324 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000325 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000326 << Attr.getName() << AANT_ArgumentString;
327 return false;
328 }
329
330 Str = Literal->getString();
331 return true;
332}
333
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000334/// \brief Applies the given attribute to the Decl without performing any
335/// additional semantic checking.
336template <typename AttrType>
337static void handleSimpleAttribute(Sema &S, Decl *D,
338 const AttributeList &Attr) {
339 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
340 Attr.getAttributeSpellingListIndex()));
341}
342
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000343/// \brief Check if the passed-in expression is of type int or bool.
344static bool isIntOrBool(Expr *Exp) {
345 QualType QT = Exp->getType();
346 return QT->isBooleanType() || QT->isIntegerType();
347}
348
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000349
350// Check to see if the type is a smart pointer of some kind. We assume
351// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000352static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
353 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
354 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000355 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000356 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000357
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000358 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
359 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000360 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000361 return false;
362
363 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000364}
365
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000366/// \brief Check if passed in Decl is a pointer type.
367/// Note that this function may produce an error message.
368/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000369static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
370 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000371 const ValueDecl *vd = cast<ValueDecl>(D);
372 QualType QT = vd->getType();
373 if (QT->isAnyPointerType())
374 return true;
375
376 if (const RecordType *RT = QT->getAs<RecordType>()) {
377 // If it's an incomplete type, it could be a smart pointer; skip it.
378 // (We don't want to force template instantiation if we can avoid it,
379 // since that would alter the order in which templates are instantiated.)
380 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000381 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000382
Aaron Ballman553e6812013-12-26 14:54:11 +0000383 if (threadSafetyCheckIsSmartPointer(S, RT))
384 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000385 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000386
387 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000388 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000389 return false;
390}
391
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000392/// \brief Checks that the passed in QualType either is of RecordType or points
393/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000394static const RecordType *getRecordType(QualType QT) {
395 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000396 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000397
398 // Now check if we point to record type.
399 if (const PointerType *PT = QT->getAs<PointerType>())
400 return PT->getPointeeType()->getAs<RecordType>();
401
Craig Topperc3ec1492014-05-26 06:22:03 +0000402 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000403}
404
Aaron Ballman76050722014-04-04 15:13:57 +0000405static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000406 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000407
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000408 if (!RT)
409 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000410
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000411 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000412 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000413 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000414
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000415 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000416 // FIXME -- Check the type that the smart pointer points to.
417 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000418 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000419
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000420 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000421 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000422 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000423 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000424
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000425 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000426 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
427 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000428 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
429 void *) {
430 return BS->getType()->getAs<RecordType>()
431 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000432 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000433 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000434 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000435 return false;
436}
437
Aaron Ballman76050722014-04-04 15:13:57 +0000438static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000439 const auto *TD = Ty->getAs<TypedefType>();
440 if (!TD)
441 return false;
442
443 TypedefNameDecl *TN = TD->getDecl();
444 if (!TN)
445 return false;
446
447 return TN->hasAttr<CapabilityAttr>();
448}
449
Aaron Ballman76050722014-04-04 15:13:57 +0000450static bool typeHasCapability(Sema &S, QualType Ty) {
451 if (checkTypedefTypeForCapability(Ty))
452 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000453
Aaron Ballman76050722014-04-04 15:13:57 +0000454 if (checkRecordTypeForCapability(S, Ty))
455 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000456
Aaron Ballman76050722014-04-04 15:13:57 +0000457 return false;
458}
459
460static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
461 // Capability expressions are simple expressions involving the boolean logic
462 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
463 // a DeclRefExpr is found, its type should be checked to determine whether it
464 // is a capability or not.
465
466 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
467 return typeHasCapability(S, E->getType());
468 else if (const auto *E = dyn_cast<CastExpr>(Ex))
469 return isCapabilityExpr(S, E->getSubExpr());
470 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
471 return isCapabilityExpr(S, E->getSubExpr());
472 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
473 if (E->getOpcode() == UO_LNot)
474 return isCapabilityExpr(S, E->getSubExpr());
475 return false;
476 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
477 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
478 return isCapabilityExpr(S, E->getLHS()) &&
479 isCapabilityExpr(S, E->getRHS());
480 return false;
481 }
482
483 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000484}
485
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000486/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
487/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000488/// \param Sidx The attribute argument index to start checking with.
489/// \param ParamIdxOk Whether an argument can be indexing into a function
490/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000491static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
492 const AttributeList &Attr,
493 SmallVectorImpl<Expr *> &Args,
494 int Sidx = 0,
495 bool ParamIdxOk = false) {
496 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000497 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000498
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000499 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000500 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000501 Args.push_back(ArgExp);
502 continue;
503 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000504
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000505 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000506 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000507 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000508 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000509 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000510 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000511 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000512 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000513
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000514 // We allow constant strings to be used as a placeholder for expressions
515 // that are not valid C++ syntax, but warn that they are ignored.
516 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
517 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000518 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000519 continue;
520 }
521
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000522 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000523
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000524 // A pointer to member expression of the form &MyClass::mu is treated
525 // specially -- we need to look at the type of the member.
526 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
527 if (UOp->getOpcode() == UO_AddrOf)
528 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
529 if (DRE->getDecl()->isCXXInstanceMember())
530 ArgTy = DRE->getDecl()->getType();
531
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000532 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000533 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000534
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000535 // Now check if we index into a record type function param.
536 if(!RT && ParamIdxOk) {
537 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000538 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
539 if(FD && IL) {
540 unsigned int NumParams = FD->getNumParams();
541 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000542 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
543 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
544 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
546 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000547 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000548 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000549 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550 }
551 }
552
Aaron Ballman76050722014-04-04 15:13:57 +0000553 // If the type does not have a capability, see if the components of the
554 // expression have capabilities. This allows for writing C code where the
555 // capability may be on the type, and the expression is a capability
556 // boolean logic expression. Eg) requires_capability(A || B && !C)
557 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
558 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
559 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000560
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000561 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000562 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000563}
564
Chris Lattner58418ff2008-06-29 00:16:31 +0000565//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000566// Attribute Implementations
567//===----------------------------------------------------------------------===//
568
Michael Hana9171bc2012-08-03 17:40:43 +0000569static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000570 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000571 if (!threadSafetyCheckIsPointer(S, D, Attr))
572 return;
573
Michael Han99315932013-01-24 16:46:58 +0000574 D->addAttr(::new (S.Context)
575 PtGuardedVarAttr(Attr.getRange(), S.Context,
576 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000577}
578
Michael Hana9171bc2012-08-03 17:40:43 +0000579static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
580 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000581 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000582 SmallVector<Expr*, 1> Args;
583 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000584 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000585 unsigned Size = Args.size();
586 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000587 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000588
Michael Han3be3b442012-07-23 18:48:41 +0000589 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000590
Michael Han3be3b442012-07-23 18:48:41 +0000591 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000592}
593
Michael Han3be3b442012-07-23 18:48:41 +0000594static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000595 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000596 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
597 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000598
Aaron Ballman36a53502014-01-16 13:03:14 +0000599 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
600 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000601}
602
Michael Hana9171bc2012-08-03 17:40:43 +0000603static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000604 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000605 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000606 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
607 return;
608
609 if (!threadSafetyCheckIsPointer(S, D, Attr))
610 return;
611
612 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000613 S.Context, Arg,
614 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000615}
616
Michael Hana9171bc2012-08-03 17:40:43 +0000617static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
618 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000619 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000621 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000622
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000623 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000624 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000625 if (!QT->isDependentType()) {
626 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000627 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000628 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000629 << Attr.getName();
630 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000631 }
632 }
633
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000634 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000635 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000636 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000637 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000638
Michael Han3be3b442012-07-23 18:48:41 +0000639 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000640}
641
Michael Hana9171bc2012-08-03 17:40:43 +0000642static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000643 const AttributeList &Attr) {
644 SmallVector<Expr*, 1> Args;
645 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
646 return;
647
648 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000649 D->addAttr(::new (S.Context)
650 AcquiredAfterAttr(Attr.getRange(), S.Context,
651 StartArg, Args.size(),
652 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000653}
654
Michael Hana9171bc2012-08-03 17:40:43 +0000655static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000656 const AttributeList &Attr) {
657 SmallVector<Expr*, 1> Args;
658 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
659 return;
660
661 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000662 D->addAttr(::new (S.Context)
663 AcquiredBeforeAttr(Attr.getRange(), S.Context,
664 StartArg, Args.size(),
665 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000666}
667
Michael Hana9171bc2012-08-03 17:40:43 +0000668static bool checkLockFunAttrCommon(Sema &S, Decl *D,
669 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000670 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000671 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000672 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000673 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000674
Michael Han3be3b442012-07-23 18:48:41 +0000675 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000676}
677
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000678static void handleAssertSharedLockAttr(Sema &S, Decl *D,
679 const AttributeList &Attr) {
680 SmallVector<Expr*, 1> Args;
681 if (!checkLockFunAttrCommon(S, D, Attr, Args))
682 return;
683
684 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000685 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000686 D->addAttr(::new (S.Context)
687 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
688 Attr.getAttributeSpellingListIndex()));
689}
690
691static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
692 const AttributeList &Attr) {
693 SmallVector<Expr*, 1> Args;
694 if (!checkLockFunAttrCommon(S, D, Attr, Args))
695 return;
696
697 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000698 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000699 D->addAttr(::new (S.Context)
700 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
701 StartArg, Size,
702 Attr.getAttributeSpellingListIndex()));
703}
704
705
Michael Hana9171bc2012-08-03 17:40:43 +0000706static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
707 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000708 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000709 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000710 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000711
Aaron Ballman00e99962013-08-31 01:11:41 +0000712 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000713 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000714 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000715 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000716 }
717
718 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000719 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000720
Michael Han3be3b442012-07-23 18:48:41 +0000721 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000722}
723
Michael Hana9171bc2012-08-03 17:40:43 +0000724static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 2> Args;
727 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
728 return;
729
Michael Han99315932013-01-24 16:46:58 +0000730 D->addAttr(::new (S.Context)
731 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000732 Attr.getArgAsExpr(0),
733 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000734 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000735}
736
Michael Hana9171bc2012-08-03 17:40:43 +0000737static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000738 const AttributeList &Attr) {
739 SmallVector<Expr*, 2> Args;
740 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
741 return;
742
Michael Han99315932013-01-24 16:46:58 +0000743 D->addAttr(::new (S.Context)
744 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000745 Attr.getArgAsExpr(0),
746 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000747 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000748}
749
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000750static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000751 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000752 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000753 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000754 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000755 unsigned Size = Args.size();
756 if (Size == 0)
757 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000758
Michael Han99315932013-01-24 16:46:58 +0000759 D->addAttr(::new (S.Context)
760 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
761 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000762}
763
764static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000765 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000766 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000767 return;
768
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000769 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000770 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000771 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000772 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000773 if (Size == 0)
774 return;
775 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000776
Michael Han99315932013-01-24 16:46:58 +0000777 D->addAttr(::new (S.Context)
778 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
779 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000780}
781
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000782static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
783 Expr *Cond = Attr.getArgAsExpr(0);
784 if (!Cond->isTypeDependent()) {
785 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
786 if (Converted.isInvalid())
787 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000788 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000789 }
790
791 StringRef Msg;
792 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
793 return;
794
795 SmallVector<PartialDiagnosticAt, 8> Diags;
796 if (!Cond->isValueDependent() &&
797 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
798 Diags)) {
799 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
800 for (int I = 0, N = Diags.size(); I != N; ++I)
801 S.Diag(Diags[I].first, Diags[I].second);
802 return;
803 }
804
805 D->addAttr(::new (S.Context)
806 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
807 Attr.getAttributeSpellingListIndex()));
808}
809
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000810static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000811 ConsumableAttr::ConsumedState DefaultState;
812
813 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000814 IdentifierLoc *IL = Attr.getArgAsIdent(0);
815 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
816 DefaultState)) {
817 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
818 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000819 return;
820 }
David Blaikie16f76d22013-09-06 01:28:43 +0000821 } else {
822 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
823 << Attr.getName() << AANT_ArgumentIdentifier;
824 return;
825 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000826
827 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000828 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000829 Attr.getAttributeSpellingListIndex()));
830}
831
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000832
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000833static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
834 const AttributeList &Attr) {
835 ASTContext &CurrContext = S.getASTContext();
836 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
837
838 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
839 if (!RD->hasAttr<ConsumableAttr>()) {
840 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
841 RD->getNameAsString();
842
843 return false;
844 }
845 }
846
847 return true;
848}
849
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000850
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000851static void handleCallableWhenAttr(Sema &S, Decl *D,
852 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000853 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
854 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000855
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000856 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
857 return;
858
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000859 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
860 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
861 CallableWhenAttr::ConsumedState CallableState;
862
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000863 StringRef StateString;
864 SourceLocation Loc;
865 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
866 return;
867
868 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000869 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000870 S.Diag(Loc, diag::warn_attribute_type_not_supported)
871 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000872 return;
873 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000874
875 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000876 }
877
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000878 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000879 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
880 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000881}
882
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000883
DeLesley Hutchins69391772013-10-17 23:23:53 +0000884static void handleParamTypestateAttr(Sema &S, Decl *D,
885 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000886 ParamTypestateAttr::ConsumedState ParamState;
887
888 if (Attr.isArgIdent(0)) {
889 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
890 StringRef StateString = Ident->Ident->getName();
891
892 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
893 ParamState)) {
894 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
895 << Attr.getName() << StateString;
896 return;
897 }
898 } else {
899 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
900 Attr.getName() << AANT_ArgumentIdentifier;
901 return;
902 }
903
904 // FIXME: This check is currently being done in the analysis. It can be
905 // enabled here only after the parser propagates attributes at
906 // template specialization definition, not declaration.
907 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
908 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
909 //
910 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
911 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
912 // ReturnType.getAsString();
913 // return;
914 //}
915
916 D->addAttr(::new (S.Context)
917 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
918 Attr.getAttributeSpellingListIndex()));
919}
920
921
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000922static void handleReturnTypestateAttr(Sema &S, Decl *D,
923 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000924 ReturnTypestateAttr::ConsumedState ReturnState;
925
926 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000927 IdentifierLoc *IL = Attr.getArgAsIdent(0);
928 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
929 ReturnState)) {
930 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
931 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000932 return;
933 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000934 } else {
935 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
936 Attr.getName() << AANT_ArgumentIdentifier;
937 return;
938 }
939
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000940 // FIXME: This check is currently being done in the analysis. It can be
941 // enabled here only after the parser propagates attributes at
942 // template specialization definition, not declaration.
943 //QualType ReturnType;
944 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000945 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
946 // ReturnType = Param->getType();
947 //
948 //} else if (const CXXConstructorDecl *Constructor =
949 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000950 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
951 //
952 //} else {
953 //
954 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
955 //}
956 //
957 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
958 //
959 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
960 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
961 // ReturnType.getAsString();
962 // return;
963 //}
964
965 D->addAttr(::new (S.Context)
966 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
967 Attr.getAttributeSpellingListIndex()));
968}
969
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000970
971static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000972 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
973 return;
974
975 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000976 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000977 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
978 StringRef Param = Ident->Ident->getName();
979 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
980 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
981 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000982 return;
983 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000984 } else {
985 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
986 Attr.getName() << AANT_ArgumentIdentifier;
987 return;
988 }
989
990 D->addAttr(::new (S.Context)
991 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
992 Attr.getAttributeSpellingListIndex()));
993}
994
Chris Wailes9385f9f2013-10-29 20:28:41 +0000995static void handleTestTypestateAttr(Sema &S, Decl *D,
996 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000997 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
998 return;
999
Chris Wailes9385f9f2013-10-29 20:28:41 +00001000 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001001 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001002 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1003 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001004 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001005 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1006 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001007 return;
1008 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001009 } else {
1010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1011 Attr.getName() << AANT_ArgumentIdentifier;
1012 return;
1013 }
1014
1015 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001016 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 Attr.getAttributeSpellingListIndex()));
1018}
1019
Chandler Carruthedc2c642011-07-02 00:01:44 +00001020static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1021 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001022 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001023 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001024}
1025
Chandler Carruthedc2c642011-07-02 00:01:44 +00001026static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001027 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001028 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1029 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001030 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001031 // If the alignment is less than or equal to 8 bits, the packed attribute
1032 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001033 if (!FD->getType()->isDependentType() &&
1034 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001035 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001036 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001037 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001038 else
Michael Han99315932013-01-24 16:46:58 +00001039 FD->addAttr(::new (S.Context)
1040 PackedAttr(Attr.getRange(), S.Context,
1041 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001042 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001043 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001044}
1045
Ted Kremenek7fd17232011-09-29 07:02:25 +00001046static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1047 // The IBOutlet/IBOutletCollection attributes only apply to instance
1048 // variables or properties of Objective-C classes. The outlet must also
1049 // have an object reference type.
1050 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1051 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001052 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001053 << Attr.getName() << VD->getType() << 0;
1054 return false;
1055 }
1056 }
1057 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1058 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001059 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001060 << Attr.getName() << PD->getType() << 1;
1061 return false;
1062 }
1063 }
1064 else {
1065 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1066 return false;
1067 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001068
Ted Kremenek7fd17232011-09-29 07:02:25 +00001069 return true;
1070}
1071
Chandler Carruthedc2c642011-07-02 00:01:44 +00001072static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001073 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001074 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001075
Michael Han99315932013-01-24 16:46:58 +00001076 D->addAttr(::new (S.Context)
1077 IBOutletAttr(Attr.getRange(), S.Context,
1078 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001079}
1080
Chandler Carruthedc2c642011-07-02 00:01:44 +00001081static void handleIBOutletCollection(Sema &S, Decl *D,
1082 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001083
1084 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001085 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001086 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1087 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001088 return;
1089 }
1090
Ted Kremenek7fd17232011-09-29 07:02:25 +00001091 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001092 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001093
Richard Smithb1f9a282013-10-31 01:56:18 +00001094 ParsedType PT;
1095
1096 if (Attr.hasParsedType())
1097 PT = Attr.getTypeArg();
1098 else {
1099 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1100 S.getScopeForContext(D->getDeclContext()->getParent()));
1101 if (!PT) {
1102 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1103 return;
1104 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001105 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001106
Craig Topperc3ec1492014-05-26 06:22:03 +00001107 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001108 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1109 if (!QTLoc)
1110 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001111
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001112 // Diagnose use of non-object type in iboutletcollection attribute.
1113 // FIXME. Gnu attribute extension ignores use of builtin types in
1114 // attributes. So, __attribute__((iboutletcollection(char))) will be
1115 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001116 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001117 S.Diag(Attr.getLoc(),
1118 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1119 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001120 return;
1121 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001122
Michael Han99315932013-01-24 16:46:58 +00001123 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001124 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001125 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001126}
1127
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001128static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001129 if (const RecordType *UT = T->getAsUnionType())
1130 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1131 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001132 for (const auto *I : UD->fields()) {
1133 QualType QT = I->getType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001134 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1135 T = QT;
1136 return;
1137 }
1138 }
1139 }
1140}
1141
Ted Kremenek9aedc152014-01-17 06:24:56 +00001142static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001143 SourceRange AttrParmRange,
1144 SourceRange NonNullTypeRange,
1145 bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001146 T = T.getNonReferenceType();
1147 possibleTransparentUnionPointerType(T);
1148
1149 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001150 S.Diag(Attr.getLoc(), isReturnValue
1151 ? diag::warn_attribute_return_pointers_only
1152 : diag::warn_attribute_pointers_only)
1153 << Attr.getName() << AttrParmRange << NonNullTypeRange;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001154 return false;
1155 }
1156 return true;
1157}
1158
Chandler Carruthedc2c642011-07-02 00:01:44 +00001159static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001160 SmallVector<unsigned, 8> NonNullArgs;
1161 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001162 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001163 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001164 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001165 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001166
1167 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001168 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001169 Ex->getSourceRange(),
1170 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001171 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001172
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001173 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001174 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001175
1176 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1177 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001178 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001179 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1180 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001181 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001182 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001183 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001184 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001185
Ted Kremenek22813f42010-10-21 18:49:36 +00001186 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001187 if (NonNullArgs.empty()) {
1188 // Warn the trivial case only if attribute is not coming from a
1189 // macro instantiation.
1190 if (Attr.getLoc().isFileID())
1191 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001192 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001193 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001194 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001195
Nick Lewyckye1121512013-01-24 01:12:16 +00001196 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001197 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001198 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001199 D->addAttr(::new (S.Context)
1200 NonNullAttr(Attr.getRange(), S.Context, start, size,
1201 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001202}
1203
Jordan Rosec9399072014-02-11 17:27:59 +00001204static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1205 const AttributeList &Attr) {
1206 if (Attr.getNumArgs() > 0) {
1207 if (D->getFunctionType()) {
1208 handleNonNullAttr(S, D, Attr);
1209 } else {
1210 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1211 << D->getSourceRange();
1212 }
1213 return;
1214 }
1215
1216 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001217 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1218 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001219 return;
1220
1221 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001222 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001223 Attr.getAttributeSpellingListIndex()));
1224}
1225
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001226static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1227 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001228 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001229 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1230 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001231 /* isReturnValue */ true))
1232 return;
1233
1234 D->addAttr(::new (S.Context)
1235 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1236 Attr.getAttributeSpellingListIndex()));
1237}
1238
Chandler Carruthedc2c642011-07-02 00:01:44 +00001239static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001240 // This attribute must be applied to a function declaration. The first
1241 // argument to the attribute must be an identifier, the name of the resource,
1242 // for example: malloc. The following arguments must be argument indexes, the
1243 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001244 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001245 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001246 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001247
Aaron Ballman00e99962013-08-31 01:11:41 +00001248 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001249 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001250 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251 return;
1252 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001253
Richard Smith852e9ce2013-11-27 01:46:48 +00001254 // Figure out our Kind.
1255 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001256 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001257 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001258
Richard Smith852e9ce2013-11-27 01:46:48 +00001259 // Check arguments.
1260 switch (K) {
1261 case OwnershipAttr::Takes:
1262 case OwnershipAttr::Holds:
1263 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001264 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1265 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001266 return;
1267 }
1268 break;
1269 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001270 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001271 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1272 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001273 return;
1274 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001275 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001276 }
1277
Richard Smith852e9ce2013-11-27 01:46:48 +00001278 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001279
1280 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001281 StringRef ModuleName = Module->getName();
1282 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1283 ModuleName.size() > 4) {
1284 ModuleName = ModuleName.drop_front(2).drop_back(2);
1285 Module = &S.PP.getIdentifierTable().get(ModuleName);
1286 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001287
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001288 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001289 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1290 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001291 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001292 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001293 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001294
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001295 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001296 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001297 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001298 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001299 case OwnershipAttr::Takes:
1300 case OwnershipAttr::Holds:
1301 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1302 Err = 0;
1303 break;
1304 case OwnershipAttr::Returns:
1305 if (!T->isIntegerType())
1306 Err = 1;
1307 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001308 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001309 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001310 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001311 << Ex->getSourceRange();
1312 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001313 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001314
1315 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001316 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001317 // Cannot have two ownership attributes of different kinds for the same
1318 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001319 if (I->getOwnKind() != K && I->args_end() !=
1320 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001321 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001322 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001323 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001324 } else if (K == OwnershipAttr::Returns &&
1325 I->getOwnKind() == OwnershipAttr::Returns) {
1326 // A returns attribute conflicts with any other returns attribute using
1327 // a different index. Note, diagnostic reporting is 1-based, but stored
1328 // argument indexes are 0-based.
1329 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1330 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1331 << *(I->args_begin()) + 1;
1332 if (I->args_size())
1333 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1334 << (unsigned)Idx + 1 << Ex->getSourceRange();
1335 return;
1336 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001337 }
1338 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001339 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001340 }
1341
1342 unsigned* start = OwnershipArgs.data();
1343 unsigned size = OwnershipArgs.size();
1344 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001345
Michael Han99315932013-01-24 16:46:58 +00001346 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001347 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001348 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001349}
1350
Chandler Carruthedc2c642011-07-02 00:01:44 +00001351static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001352 // Check the attribute arguments.
1353 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001354 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1355 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001356 return;
1357 }
1358
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001359 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001360
Rafael Espindolac18086a2010-02-23 22:00:30 +00001361 // gcc rejects
1362 // class c {
1363 // static int a __attribute__((weakref ("v2")));
1364 // static int b() __attribute__((weakref ("f3")));
1365 // };
1366 // and ignores the attributes of
1367 // void f(void) {
1368 // static int a __attribute__((weakref ("v2")));
1369 // }
1370 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001371 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001372 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001373 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1374 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001375 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001376 }
1377
1378 // The GCC manual says
1379 //
1380 // At present, a declaration to which `weakref' is attached can only
1381 // be `static'.
1382 //
1383 // It also says
1384 //
1385 // Without a TARGET,
1386 // given as an argument to `weakref' or to `alias', `weakref' is
1387 // equivalent to `weak'.
1388 //
1389 // gcc 4.4.1 will accept
1390 // int a7 __attribute__((weakref));
1391 // as
1392 // int a7 __attribute__((weak));
1393 // This looks like a bug in gcc. We reject that for now. We should revisit
1394 // it if this behaviour is actually used.
1395
Rafael Espindolac18086a2010-02-23 22:00:30 +00001396 // GCC rejects
1397 // static ((alias ("y"), weakref)).
1398 // Should we? How to check that weakref is before or after alias?
1399
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001400 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1401 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1402 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001403 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001404 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001405 // GCC will accept anything as the argument of weakref. Should we
1406 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001407 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1408 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001409
Michael Han99315932013-01-24 16:46:58 +00001410 D->addAttr(::new (S.Context)
1411 WeakRefAttr(Attr.getRange(), S.Context,
1412 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001413}
1414
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001415static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1416 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001417 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001418 return;
1419
Douglas Gregore8bbc122011-09-02 00:18:52 +00001420 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001421 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1422 return;
1423 }
1424
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001425 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001426
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001427 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001428 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001429}
1430
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001431static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001432 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001433 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001434
Michael Han99315932013-01-24 16:46:58 +00001435 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1436 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001437}
1438
1439static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001440 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001441 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001442
Michael Han99315932013-01-24 16:46:58 +00001443 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1444 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001445}
1446
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001447static void handleTLSModelAttr(Sema &S, Decl *D,
1448 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001449 StringRef Model;
1450 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001451 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001452 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001453 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001454
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001455 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001456 if (Model != "global-dynamic" && Model != "local-dynamic"
1457 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001458 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001459 return;
1460 }
1461
Michael Han99315932013-01-24 16:46:58 +00001462 D->addAttr(::new (S.Context)
1463 TLSModelAttr(Attr.getRange(), S.Context, Model,
1464 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001465}
1466
Chandler Carruthedc2c642011-07-02 00:01:44 +00001467static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001468 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001469 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001470 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001471 D->addAttr(::new (S.Context)
1472 MallocAttr(Attr.getRange(), S.Context,
1473 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001474 return;
1475 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001476 }
1477
Ted Kremenek08479ae2009-08-15 00:51:46 +00001478 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001479}
1480
Chandler Carruthedc2c642011-07-02 00:01:44 +00001481static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001482 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1484 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001485 return;
1486 }
1487
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001488 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1489 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001490}
1491
Chandler Carruthedc2c642011-07-02 00:01:44 +00001492static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001493 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001494
1495 if (S.CheckNoReturnAttr(attr)) return;
1496
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001497 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001498 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001499 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001500 return;
1501 }
1502
Michael Han99315932013-01-24 16:46:58 +00001503 D->addAttr(::new (S.Context)
1504 NoReturnAttr(attr.getRange(), S.Context,
1505 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001506}
1507
1508bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001509 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001510 attr.setInvalid();
1511 return true;
1512 }
1513
1514 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001515}
1516
Chandler Carruthedc2c642011-07-02 00:01:44 +00001517static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1518 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001519
1520 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1521 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001522 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1523 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001524 if (!VD || (!VD->getType()->isBlockPointerType() &&
1525 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001526 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001527 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001528 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001529 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001530 return;
1531 }
1532 }
1533
Michael Han99315932013-01-24 16:46:58 +00001534 D->addAttr(::new (S.Context)
1535 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1536 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001537}
1538
John Thompsoncdb847ba2010-08-09 21:53:52 +00001539// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001540static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001541/*
1542 Returning a Vector Class in Registers
1543
Eric Christopherbc638a82010-12-01 22:13:54 +00001544 According to the PPU ABI specifications, a class with a single member of
1545 vector type is returned in memory when used as the return value of a function.
1546 This results in inefficient code when implementing vector classes. To return
1547 the value in a single vector register, add the vecreturn attribute to the
1548 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001549
1550 Example:
1551
1552 struct Vector
1553 {
1554 __vector float xyzw;
1555 } __attribute__((vecreturn));
1556
1557 Vector Add(Vector lhs, Vector rhs)
1558 {
1559 Vector result;
1560 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1561 return result; // This will be returned in a register
1562 }
1563*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001564 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1565 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001566 return;
1567 }
1568
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001569 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001570 int count = 0;
1571
1572 if (!isa<CXXRecordDecl>(record)) {
1573 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1574 return;
1575 }
1576
1577 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1578 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1579 return;
1580 }
1581
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001582 for (const auto *I : record->fields()) {
1583 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001584 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1585 return;
1586 }
1587 count++;
1588 }
1589
Michael Han99315932013-01-24 16:46:58 +00001590 D->addAttr(::new (S.Context)
1591 VecReturnAttr(Attr.getRange(), S.Context,
1592 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001593}
1594
Richard Smithe233fbf2013-01-28 22:42:45 +00001595static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1596 const AttributeList &Attr) {
1597 if (isa<ParmVarDecl>(D)) {
1598 // [[carries_dependency]] can only be applied to a parameter if it is a
1599 // parameter of a function declaration or lambda.
1600 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1601 S.Diag(Attr.getLoc(),
1602 diag::err_carries_dependency_param_not_function_decl);
1603 return;
1604 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001605 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001606
1607 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1608 Attr.getRange(), S.Context,
1609 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001610}
1611
Chandler Carruthedc2c642011-07-02 00:01:44 +00001612static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001613 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001614 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001615 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001616 return;
1617 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001618 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001620 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001621 return;
1622 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001623
Michael Han99315932013-01-24 16:46:58 +00001624 D->addAttr(::new (S.Context)
1625 UsedAttr(Attr.getRange(), S.Context,
1626 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001627}
1628
Chandler Carruthedc2c642011-07-02 00:01:44 +00001629static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001630 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001631 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001632 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1633 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001634
Michael Han99315932013-01-24 16:46:58 +00001635 D->addAttr(::new (S.Context)
1636 ConstructorAttr(Attr.getRange(), S.Context, priority,
1637 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001638}
1639
Chandler Carruthedc2c642011-07-02 00:01:44 +00001640static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001641 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001642 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001643 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1644 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001645
Michael Han99315932013-01-24 16:46:58 +00001646 D->addAttr(::new (S.Context)
1647 DestructorAttr(Attr.getRange(), S.Context, priority,
1648 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001649}
1650
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001651template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001652static void handleAttrWithMessage(Sema &S, Decl *D,
1653 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001654 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001655 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001656 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001657 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001658
Michael Han99315932013-01-24 16:46:58 +00001659 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1660 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001661}
1662
Ted Kremenek438f8db2014-02-22 01:06:05 +00001663static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001664 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001665 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001666 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1667 << Attr.getName() << Attr.getRange();
1668 return;
1669 }
1670
Ted Kremenek28eace62013-11-23 01:01:34 +00001671 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001672 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1673 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001674}
1675
Jordy Rose740b0c22012-05-08 03:27:22 +00001676static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1677 IdentifierInfo *Platform,
1678 VersionTuple Introduced,
1679 VersionTuple Deprecated,
1680 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001681 StringRef PlatformName
1682 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1683 if (PlatformName.empty())
1684 PlatformName = Platform->getName();
1685
1686 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1687 // of these steps are needed).
1688 if (!Introduced.empty() && !Deprecated.empty() &&
1689 !(Introduced <= Deprecated)) {
1690 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1691 << 1 << PlatformName << Deprecated.getAsString()
1692 << 0 << Introduced.getAsString();
1693 return true;
1694 }
1695
1696 if (!Introduced.empty() && !Obsoleted.empty() &&
1697 !(Introduced <= Obsoleted)) {
1698 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1699 << 2 << PlatformName << Obsoleted.getAsString()
1700 << 0 << Introduced.getAsString();
1701 return true;
1702 }
1703
1704 if (!Deprecated.empty() && !Obsoleted.empty() &&
1705 !(Deprecated <= Obsoleted)) {
1706 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1707 << 2 << PlatformName << Obsoleted.getAsString()
1708 << 1 << Deprecated.getAsString();
1709 return true;
1710 }
1711
1712 return false;
1713}
1714
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001715/// \brief Check whether the two versions match.
1716///
1717/// If either version tuple is empty, then they are assumed to match. If
1718/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1719static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1720 bool BeforeIsOkay) {
1721 if (X.empty() || Y.empty())
1722 return true;
1723
1724 if (X == Y)
1725 return true;
1726
1727 if (BeforeIsOkay && X < Y)
1728 return true;
1729
1730 return false;
1731}
1732
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001733AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001734 IdentifierInfo *Platform,
1735 VersionTuple Introduced,
1736 VersionTuple Deprecated,
1737 VersionTuple Obsoleted,
1738 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001739 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001740 bool Override,
1741 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001742 VersionTuple MergedIntroduced = Introduced;
1743 VersionTuple MergedDeprecated = Deprecated;
1744 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001745 bool FoundAny = false;
1746
Rafael Espindolac67f2232012-05-10 02:50:16 +00001747 if (D->hasAttrs()) {
1748 AttrVec &Attrs = D->getAttrs();
1749 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1750 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1751 if (!OldAA) {
1752 ++i;
1753 continue;
1754 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001755
Rafael Espindolac67f2232012-05-10 02:50:16 +00001756 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1757 if (OldPlatform != Platform) {
1758 ++i;
1759 continue;
1760 }
1761
1762 FoundAny = true;
1763 VersionTuple OldIntroduced = OldAA->getIntroduced();
1764 VersionTuple OldDeprecated = OldAA->getDeprecated();
1765 VersionTuple OldObsoleted = OldAA->getObsoleted();
1766 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001767
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001768 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1769 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1770 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1771 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001772 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001773 if (Override) {
1774 int Which = -1;
1775 VersionTuple FirstVersion;
1776 VersionTuple SecondVersion;
1777 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1778 Which = 0;
1779 FirstVersion = OldIntroduced;
1780 SecondVersion = Introduced;
1781 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1782 Which = 1;
1783 FirstVersion = Deprecated;
1784 SecondVersion = OldDeprecated;
1785 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1786 Which = 2;
1787 FirstVersion = Obsoleted;
1788 SecondVersion = OldObsoleted;
1789 }
1790
1791 if (Which == -1) {
1792 Diag(OldAA->getLocation(),
1793 diag::warn_mismatched_availability_override_unavail)
1794 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1795 } else {
1796 Diag(OldAA->getLocation(),
1797 diag::warn_mismatched_availability_override)
1798 << Which
1799 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1800 << FirstVersion.getAsString() << SecondVersion.getAsString();
1801 }
1802 Diag(Range.getBegin(), diag::note_overridden_method);
1803 } else {
1804 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1805 Diag(Range.getBegin(), diag::note_previous_attribute);
1806 }
1807
Rafael Espindolac67f2232012-05-10 02:50:16 +00001808 Attrs.erase(Attrs.begin() + i);
1809 --e;
1810 continue;
1811 }
1812
1813 VersionTuple MergedIntroduced2 = MergedIntroduced;
1814 VersionTuple MergedDeprecated2 = MergedDeprecated;
1815 VersionTuple MergedObsoleted2 = MergedObsoleted;
1816
1817 if (MergedIntroduced2.empty())
1818 MergedIntroduced2 = OldIntroduced;
1819 if (MergedDeprecated2.empty())
1820 MergedDeprecated2 = OldDeprecated;
1821 if (MergedObsoleted2.empty())
1822 MergedObsoleted2 = OldObsoleted;
1823
1824 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1825 MergedIntroduced2, MergedDeprecated2,
1826 MergedObsoleted2)) {
1827 Attrs.erase(Attrs.begin() + i);
1828 --e;
1829 continue;
1830 }
1831
1832 MergedIntroduced = MergedIntroduced2;
1833 MergedDeprecated = MergedDeprecated2;
1834 MergedObsoleted = MergedObsoleted2;
1835 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001836 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001837 }
1838
1839 if (FoundAny &&
1840 MergedIntroduced == Introduced &&
1841 MergedDeprecated == Deprecated &&
1842 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001843 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001844
Ted Kremenekb5445722013-04-06 00:34:27 +00001845 // Only create a new attribute if !Override, but we want to do
1846 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001847 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001848 MergedDeprecated, MergedObsoleted) &&
1849 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001850 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1851 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001852 Obsoleted, IsUnavailable, Message,
1853 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001854 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001855 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001856}
1857
Chandler Carruthedc2c642011-07-02 00:01:44 +00001858static void handleAvailabilityAttr(Sema &S, Decl *D,
1859 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001860 if (!checkAttributeNumArgs(S, Attr, 1))
1861 return;
1862 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001863 unsigned Index = Attr.getAttributeSpellingListIndex();
1864
Aaron Ballman00e99962013-08-31 01:11:41 +00001865 IdentifierInfo *II = Platform->Ident;
1866 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1867 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1868 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001869
Rafael Espindolac231fab2013-01-08 21:30:32 +00001870 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1871 if (!ND) {
1872 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1873 return;
1874 }
1875
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001876 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1877 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1878 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001879 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001880 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001881 if (const StringLiteral *SE =
1882 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001883 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001884
Aaron Ballman00e99962013-08-31 01:11:41 +00001885 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001886 Introduced.Version,
1887 Deprecated.Version,
1888 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001889 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001890 /*Override=*/false,
1891 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001892 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001893 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001894}
1895
John McCalld041a9b2013-02-20 01:54:26 +00001896template <class T>
1897static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1898 typename T::VisibilityType value,
1899 unsigned attrSpellingListIndex) {
1900 T *existingAttr = D->getAttr<T>();
1901 if (existingAttr) {
1902 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1903 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001904 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001905 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1906 S.Diag(range.getBegin(), diag::note_previous_attribute);
1907 D->dropAttr<T>();
1908 }
1909 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1910}
1911
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001912VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001913 VisibilityAttr::VisibilityType Vis,
1914 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001915 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1916 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001917}
1918
John McCalld041a9b2013-02-20 01:54:26 +00001919TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1920 TypeVisibilityAttr::VisibilityType Vis,
1921 unsigned AttrSpellingListIndex) {
1922 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1923 AttrSpellingListIndex);
1924}
1925
1926static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1927 bool isTypeVisibility) {
1928 // Visibility attributes don't mean anything on a typedef.
1929 if (isa<TypedefNameDecl>(D)) {
1930 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1931 << Attr.getName();
1932 return;
1933 }
1934
1935 // 'type_visibility' can only go on a type or namespace.
1936 if (isTypeVisibility &&
1937 !(isa<TagDecl>(D) ||
1938 isa<ObjCInterfaceDecl>(D) ||
1939 isa<NamespaceDecl>(D))) {
1940 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1941 << Attr.getName() << ExpectedTypeOrNamespace;
1942 return;
1943 }
1944
Benjamin Kramer70370212013-09-09 15:08:57 +00001945 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001946 StringRef TypeStr;
1947 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001948 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001949 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001950
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001951 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001952 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001953 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001954 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001955 return;
1956 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001957
1958 // Complain about attempts to use protected visibility on targets
1959 // (like Darwin) that don't support it.
1960 if (type == VisibilityAttr::Protected &&
1961 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1962 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1963 type = VisibilityAttr::Default;
1964 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001965
Michael Han99315932013-01-24 16:46:58 +00001966 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001967 clang::Attr *newAttr;
1968 if (isTypeVisibility) {
1969 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1970 (TypeVisibilityAttr::VisibilityType) type,
1971 Index);
1972 } else {
1973 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1974 }
1975 if (newAttr)
1976 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001977}
1978
Chandler Carruthedc2c642011-07-02 00:01:44 +00001979static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1980 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001981 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001982 if (!Attr.isArgIdent(0)) {
1983 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1984 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001985 return;
1986 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001987
Aaron Ballman682ee422013-09-11 19:47:58 +00001988 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1989 ObjCMethodFamilyAttr::FamilyKind F;
1990 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1991 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1992 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001993 return;
1994 }
1995
Alp Toker314cc812014-01-25 16:55:45 +00001996 if (F == ObjCMethodFamilyAttr::OMF_init &&
1997 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001998 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001999 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002000 // Ignore the attribute.
2001 return;
2002 }
2003
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002004 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002005 S.Context, F,
2006 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002007}
2008
Chandler Carruthedc2c642011-07-02 00:01:44 +00002009static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002010 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002011 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002012 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002013 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2014 return;
2015 }
2016 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002017 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2018 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002019 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002020 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2021 return;
2022 }
2023 }
2024 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002025 // It is okay to include this attribute on properties, e.g.:
2026 //
2027 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2028 //
2029 // In this case it follows tradition and suppresses an error in the above
2030 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002031 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002032 }
Michael Han99315932013-01-24 16:46:58 +00002033 D->addAttr(::new (S.Context)
2034 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2035 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002036}
2037
Chandler Carruthedc2c642011-07-02 00:01:44 +00002038static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002039 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002040 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002041 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002042 return;
2043 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002044
Aaron Ballman00e99962013-08-31 01:11:41 +00002045 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002046 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002047 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2048 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2049 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002050 return;
2051 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002052
Michael Han99315932013-01-24 16:46:58 +00002053 D->addAttr(::new (S.Context)
2054 BlocksAttr(Attr.getRange(), S.Context, type,
2055 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002056}
2057
Chandler Carruthedc2c642011-07-02 00:01:44 +00002058static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002059 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002060 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002061 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002062 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002063 if (E->isTypeDependent() || E->isValueDependent() ||
2064 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002065 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002066 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002067 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002068 return;
2069 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002070
John McCallb46f2872011-09-09 07:56:05 +00002071 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002072 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2073 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002074 return;
2075 }
John McCallb46f2872011-09-09 07:56:05 +00002076
2077 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002078 }
2079
Aaron Ballman18a78382013-11-21 00:28:23 +00002080 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002081 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002082 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002083 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002084 if (E->isTypeDependent() || E->isValueDependent() ||
2085 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002087 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002088 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002089 return;
2090 }
2091 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002092
John McCallb46f2872011-09-09 07:56:05 +00002093 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002094 // FIXME: This error message could be improved, it would be nice
2095 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002096 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2097 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002098 return;
2099 }
2100 }
2101
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002102 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002103 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002104 if (isa<FunctionNoProtoType>(FT)) {
2105 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2106 return;
2107 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002108
Chris Lattner9363e312009-03-17 23:03:47 +00002109 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002110 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002111 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002112 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002113 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002114 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002115 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002116 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002117 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002118 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2119 if (!BD->isVariadic()) {
2120 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2121 return;
2122 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002123 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002124 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002125 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002126 const FunctionType *FT = Ty->isFunctionPointerType()
2127 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002128 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002129 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002130 int m = Ty->isFunctionPointerType() ? 0 : 1;
2131 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002132 return;
2133 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002134 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002135 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002136 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002137 return;
2138 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002139 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002140 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002141 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002142 return;
2143 }
Michael Han99315932013-01-24 16:46:58 +00002144 D->addAttr(::new (S.Context)
2145 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2146 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002147}
2148
Chandler Carruthedc2c642011-07-02 00:01:44 +00002149static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002150 if (D->getFunctionType() &&
2151 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002152 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2153 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002154 return;
2155 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002156 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002157 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002158 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2159 << Attr.getName() << 1;
2160 return;
2161 }
2162
Michael Han99315932013-01-24 16:46:58 +00002163 D->addAttr(::new (S.Context)
2164 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2165 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002166}
2167
Chandler Carruthedc2c642011-07-02 00:01:44 +00002168static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002169 // weak_import only applies to variable & function declarations.
2170 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002171 if (!D->canBeWeakImported(isDef)) {
2172 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002173 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2174 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002175 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002176 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002177 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002178 // Nothing to warn about here.
2179 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002180 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002181 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002182
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002183 return;
2184 }
2185
Michael Han99315932013-01-24 16:46:58 +00002186 D->addAttr(::new (S.Context)
2187 WeakImportAttr(Attr.getRange(), S.Context,
2188 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002189}
2190
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002191// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002192template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002193static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002194 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002195 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002196 for (unsigned i = 0; i < 3; ++i) {
2197 const Expr *E = Attr.getArgAsExpr(i);
2198 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002199 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002200 if (WGSize[i] == 0) {
2201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2202 << Attr.getName() << E->getSourceRange();
2203 return;
2204 }
2205 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002206
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002207 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2208 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2209 Existing->getYDim() == WGSize[1] &&
2210 Existing->getZDim() == WGSize[2]))
2211 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002212
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002213 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2214 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002215 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002216}
2217
Joey Goulyaba589c2013-03-08 09:42:32 +00002218static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002219 if (!Attr.hasParsedType()) {
2220 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2221 << Attr.getName() << 1;
2222 return;
2223 }
2224
Craig Topperc3ec1492014-05-26 06:22:03 +00002225 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002226 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2227 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002228
2229 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2230 (ParmType->isBooleanType() ||
2231 !ParmType->isIntegralType(S.getASTContext()))) {
2232 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2233 << ParmType;
2234 return;
2235 }
2236
Aaron Ballmana9e05402013-12-02 22:16:55 +00002237 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002238 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002239 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2240 return;
2241 }
2242 }
2243
2244 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002245 ParmTSI,
2246 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002247}
2248
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002249SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002250 StringRef Name,
2251 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002252 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2253 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002254 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002255 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2256 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002257 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002258 }
Michael Han99315932013-01-24 16:46:58 +00002259 return ::new (Context) SectionAttr(Range, Context, Name,
2260 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002261}
2262
Chandler Carruthedc2c642011-07-02 00:01:44 +00002263static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002264 // Make sure that there is a string literal as the sections's single
2265 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002266 StringRef Str;
2267 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002268 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002269 return;
Mike Stump11289f42009-09-09 15:08:12 +00002270
Chris Lattner30ba6742009-08-10 19:03:04 +00002271 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002272 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002273 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002274 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002275 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002276 return;
2277 }
Mike Stump11289f42009-09-09 15:08:12 +00002278
Michael Han99315932013-01-24 16:46:58 +00002279 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002280 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002281 if (NewAttr)
2282 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002283}
2284
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002285
Chandler Carruthedc2c642011-07-02 00:01:44 +00002286static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002287 VarDecl *VD = cast<VarDecl>(D);
2288 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002289 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002290 return;
2291 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002292
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002293 Expr *E = Attr.getArgAsExpr(0);
2294 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002295 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002296 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002297
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002298 // gcc only allows for simple identifiers. Since we support more than gcc, we
2299 // will warn the user.
2300 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2301 if (DRE->hasQualifier())
2302 S.Diag(Loc, diag::warn_cleanup_ext);
2303 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2304 NI = DRE->getNameInfo();
2305 if (!FD) {
2306 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2307 << NI.getName();
2308 return;
2309 }
2310 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2311 if (ULE->hasExplicitTemplateArgs())
2312 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002313 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2314 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002315 if (!FD) {
2316 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2317 << NI.getName();
2318 if (ULE->getType() == S.Context.OverloadTy)
2319 S.NoteAllOverloadCandidates(ULE);
2320 return;
2321 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002322 } else {
2323 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002324 return;
2325 }
2326
Anders Carlssond277d792009-01-31 01:16:18 +00002327 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002328 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2329 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002330 return;
2331 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002332
Anders Carlsson723f55d2009-02-07 23:16:50 +00002333 // We're currently more strict than GCC about what function types we accept.
2334 // If this ever proves to be a problem it should be easy to fix.
2335 QualType Ty = S.Context.getPointerType(VD->getType());
2336 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002337 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2338 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002339 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2340 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002341 return;
2342 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002343
Michael Han99315932013-01-24 16:46:58 +00002344 D->addAttr(::new (S.Context)
2345 CleanupAttr(Attr.getRange(), S.Context, FD,
2346 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002347}
2348
Mike Stumpd3bb5572009-07-24 19:02:52 +00002349/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002350/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002351static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002352 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002353 uint64_t Idx;
2354 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002355 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002356
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002357 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002358 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002359
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002360 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2361 if (not_nsstring_type &&
2362 !isCFStringType(Ty, S.Context) &&
2363 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002364 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002365 // FIXME: Should highlight the actual expression that has the wrong type.
2366 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002367 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002368 << IdxExpr->getSourceRange();
2369 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002370 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002371 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002372 if (!isNSStringType(Ty, S.Context) &&
2373 !isCFStringType(Ty, S.Context) &&
2374 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002375 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002376 // FIXME: Should highlight the actual expression that has the wrong type.
2377 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002378 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002379 << IdxExpr->getSourceRange();
2380 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002381 }
2382
Alp Toker601b22c2014-01-21 23:35:24 +00002383 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002384 // because that has corrected for the implicit this parameter, and is zero-
2385 // based. The attribute expects what the user wrote explicitly.
2386 llvm::APSInt Val;
2387 IdxExpr->EvaluateAsInt(Val, S.Context);
2388
Michael Han99315932013-01-24 16:46:58 +00002389 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002390 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002391 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002392}
2393
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002394enum FormatAttrKind {
2395 CFStringFormat,
2396 NSStringFormat,
2397 StrftimeFormat,
2398 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002399 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002400 InvalidFormat
2401};
2402
2403/// getFormatAttrKind - Map from format attribute names to supported format
2404/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002405static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002406 return llvm::StringSwitch<FormatAttrKind>(Format)
2407 // Check for formats that get handled specially.
2408 .Case("NSString", NSStringFormat)
2409 .Case("CFString", CFStringFormat)
2410 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002411
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002412 // Otherwise, check for supported formats.
2413 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2414 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2415 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002416
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002417 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2418 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002419}
2420
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002421/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002422/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002423static void handleInitPriorityAttr(Sema &S, Decl *D,
2424 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002425 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002426 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2427 return;
2428 }
2429
Aaron Ballman4a611152013-11-27 16:34:09 +00002430 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002431 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2432 Attr.setInvalid();
2433 return;
2434 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002435 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002436 if (S.Context.getAsArrayType(T))
2437 T = S.Context.getBaseElementType(T);
2438 if (!T->getAs<RecordType>()) {
2439 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2440 Attr.setInvalid();
2441 return;
2442 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002443
2444 Expr *E = Attr.getArgAsExpr(0);
2445 uint32_t prioritynum;
2446 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002447 Attr.setInvalid();
2448 return;
2449 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002450
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002451 if (prioritynum < 101 || prioritynum > 65535) {
2452 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002453 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002454 Attr.setInvalid();
2455 return;
2456 }
Michael Han99315932013-01-24 16:46:58 +00002457 D->addAttr(::new (S.Context)
2458 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2459 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002460}
2461
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002462FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2463 IdentifierInfo *Format, int FormatIdx,
2464 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002465 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002466 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002467 for (auto *F : D->specific_attrs<FormatAttr>()) {
2468 if (F->getType() == Format &&
2469 F->getFormatIdx() == FormatIdx &&
2470 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002471 // If we don't have a valid location for this attribute, adopt the
2472 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002473 if (F->getLocation().isInvalid())
2474 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002475 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002476 }
2477 }
2478
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002479 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2480 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002481}
2482
Mike Stumpd3bb5572009-07-24 19:02:52 +00002483/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002484/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002485static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002486 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002487 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002488 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002489 return;
2490 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002491
Chandler Carruth743682b2010-11-16 08:35:43 +00002492 // In C++ the implicit 'this' function parameter also counts, and they are
2493 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002494 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002495 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002496
Aaron Ballman00e99962013-08-31 01:11:41 +00002497 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2498 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002499
2500 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002501 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002502 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002503 // If we've modified the string name, we need a new identifier for it.
2504 II = &S.Context.Idents.get(Format);
2505 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002506
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002507 // Check for supported formats.
2508 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002509
2510 if (Kind == IgnoredFormat)
2511 return;
2512
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002513 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002514 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002515 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002516 return;
2517 }
2518
2519 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002520 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002521 uint32_t Idx;
2522 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002523 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002524
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002525 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002526 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002527 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002528 return;
2529 }
2530
2531 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002532 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002533
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002534 if (HasImplicitThisParam) {
2535 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002536 S.Diag(Attr.getLoc(),
2537 diag::err_format_attribute_implicit_this_format_string)
2538 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002539 return;
2540 }
2541 ArgIdx--;
2542 }
Mike Stump11289f42009-09-09 15:08:12 +00002543
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002544 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002545 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002546
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002547 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002548 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002549 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2550 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002551 return;
2552 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002553 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002554 // FIXME: do we need to check if the type is NSString*? What are the
2555 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002556 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002557 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002558 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2559 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002560 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002561 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002562 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002563 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002564 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002565 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2566 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002567 return;
2568 }
2569
2570 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002571 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002572 uint32_t FirstArg;
2573 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002574 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002575
2576 // check if the function is variadic if the 3rd argument non-zero
2577 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002578 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002579 ++NumArgs; // +1 for ...
2580 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002581 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002582 return;
2583 }
2584 }
2585
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002586 // strftime requires FirstArg to be 0 because it doesn't read from any
2587 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002588 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002589 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002590 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2591 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592 return;
2593 }
2594 // if 0 it disables parameter checking (to use with e.g. va_list)
2595 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002597 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002598 return;
2599 }
2600
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002601 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002602 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002603 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002604 if (NewAttr)
2605 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606}
2607
Chandler Carruthedc2c642011-07-02 00:01:44 +00002608static void handleTransparentUnionAttr(Sema &S, Decl *D,
2609 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002610 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002611 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002612 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002613 if (TD && TD->getUnderlyingType()->isUnionType())
2614 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2615 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002616 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002617
2618 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002620 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621 return;
2622 }
2623
John McCallf937c022011-10-07 06:10:15 +00002624 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002625 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002626 diag::warn_transparent_union_attribute_not_definition);
2627 return;
2628 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002629
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002630 RecordDecl::field_iterator Field = RD->field_begin(),
2631 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002632 if (Field == FieldEnd) {
2633 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2634 return;
2635 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002636
David Blaikie40ed2972012-06-06 20:45:41 +00002637 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002638 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002639 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002640 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002641 diag::warn_transparent_union_attribute_floating)
2642 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002643 return;
2644 }
2645
2646 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2647 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2648 for (; Field != FieldEnd; ++Field) {
2649 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002650 // FIXME: this isn't fully correct; we also need to test whether the
2651 // members of the union would all have the same calling convention as the
2652 // first member of the union. Checking just the size and alignment isn't
2653 // sufficient (consider structs passed on the stack instead of in registers
2654 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002655 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002656 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002657 // Warn if we drop the attribute.
2658 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002659 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002660 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002661 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002662 diag::warn_transparent_union_attribute_field_size_align)
2663 << isSize << Field->getDeclName() << FieldBits;
2664 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002665 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002666 diag::note_transparent_union_first_field_size_align)
2667 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002668 return;
2669 }
2670 }
2671
Michael Han99315932013-01-24 16:46:58 +00002672 RD->addAttr(::new (S.Context)
2673 TransparentUnionAttr(Attr.getRange(), S.Context,
2674 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675}
2676
Chandler Carruthedc2c642011-07-02 00:01:44 +00002677static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002678 // Make sure that there is a string literal as the annotation's single
2679 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002680 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002681 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002682 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002683
2684 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002685 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2686 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002687 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002688 }
Michael Han99315932013-01-24 16:46:58 +00002689
2690 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002691 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002692 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002693}
2694
Chandler Carruthedc2c642011-07-02 00:01:44 +00002695static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002696 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002697 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002698 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2699 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002700 return;
2701 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002702
Richard Smith848e1f12013-02-01 08:12:08 +00002703 if (Attr.getNumArgs() == 0) {
2704 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002705 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002706 return;
2707 }
2708
Aaron Ballman00e99962013-08-31 01:11:41 +00002709 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002710 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2711 S.Diag(Attr.getEllipsisLoc(),
2712 diag::err_pack_expansion_without_parameter_packs);
2713 return;
2714 }
2715
2716 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2717 return;
2718
2719 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2720 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002721}
2722
2723void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002724 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002725 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2726 SourceLocation AttrLoc = AttrRange.getBegin();
2727
Richard Smith1dba27c2013-01-29 09:02:09 +00002728 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002729 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002730 // C++11 [dcl.align]p1:
2731 // An alignment-specifier may be applied to a variable or to a class
2732 // data member, but it shall not be applied to a bit-field, a function
2733 // parameter, the formal parameter of a catch clause, or a variable
2734 // declared with the register storage class specifier. An
2735 // alignment-specifier may also be applied to the declaration of a class
2736 // or enumeration type.
2737 // C11 6.7.5/2:
2738 // An alignment attribute shall not be specified in a declaration of
2739 // a typedef, or a bit-field, or a function, or a parameter, or an
2740 // object declared with the register storage-class specifier.
2741 int DiagKind = -1;
2742 if (isa<ParmVarDecl>(D)) {
2743 DiagKind = 0;
2744 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2745 if (VD->getStorageClass() == SC_Register)
2746 DiagKind = 1;
2747 if (VD->isExceptionVariable())
2748 DiagKind = 2;
2749 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2750 if (FD->isBitField())
2751 DiagKind = 3;
2752 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002753 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002754 << (TmpAttr.isC11() ? ExpectedVariableOrField
2755 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002756 return;
2757 }
2758 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002759 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002760 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002761 return;
2762 }
2763 }
2764
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002765 if (E->isTypeDependent() || E->isValueDependent()) {
2766 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002767 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2768 AA->setPackExpansion(IsPackExpansion);
2769 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002770 return;
2771 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002772
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002773 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002774 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002775 ExprResult ICE
2776 = VerifyIntegerConstantExpression(E, &Alignment,
2777 diag::err_aligned_attribute_argument_not_int,
2778 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002779 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002780 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002781
2782 // C++11 [dcl.align]p2:
2783 // -- if the constant expression evaluates to zero, the alignment
2784 // specifier shall have no effect
2785 // C11 6.7.5p6:
2786 // An alignment specification of zero has no effect.
2787 if (!(TmpAttr.isAlignas() && !Alignment) &&
2788 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002789 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2790 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002791 return;
2792 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002793
David Majnemerabecae72014-02-12 20:36:10 +00002794 // Alignment calculations can wrap around if it's greater than 2**28.
2795 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2796 if (Alignment.getZExtValue() > MaxValidAlignment) {
2797 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2798 << E->getSourceRange();
2799 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002800 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002801
Richard Smith44c247f2013-02-22 08:32:16 +00002802 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002803 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002804 AA->setPackExpansion(IsPackExpansion);
2805 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002806}
2807
Michael Hanaf02bbe2013-02-01 01:19:17 +00002808void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002809 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002810 // FIXME: Cache the number on the Attr object if non-dependent?
2811 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002812 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2813 SpellingListIndex);
2814 AA->setPackExpansion(IsPackExpansion);
2815 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002816}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002817
Richard Smith848e1f12013-02-01 08:12:08 +00002818void Sema::CheckAlignasUnderalignment(Decl *D) {
2819 assert(D->hasAttrs() && "no attributes on decl");
2820
2821 QualType Ty;
2822 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2823 Ty = VD->getType();
2824 else
2825 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002826 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002827 return;
2828
2829 // C++11 [dcl.align]p5, C11 6.7.5/4:
2830 // The combined effect of all alignment attributes in a declaration shall
2831 // not specify an alignment that is less strict than the alignment that
2832 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00002833 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00002834 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002835 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002836 if (I->isAlignmentDependent())
2837 return;
2838 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002839 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002840 Align = std::max(Align, I->getAlignment(Context));
2841 }
2842
2843 if (AlignasAttr && Align) {
2844 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2845 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2846 if (NaturalAlign > RequestedAlign)
2847 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2848 << Ty << (unsigned)NaturalAlign.getQuantity();
2849 }
2850}
2851
David Majnemer2c4e00a2014-01-29 22:07:36 +00002852bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002853 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002854 MSInheritanceAttr::Spelling SemanticSpelling) {
2855 assert(RD->hasDefinition() && "RD has no definition!");
2856
David Majnemer98c9ee22014-02-07 00:43:07 +00002857 // We may not have seen base specifiers or any virtual methods yet. We will
2858 // have to wait until the record is defined to catch any mismatches.
2859 if (!RD->getDefinition()->isCompleteDefinition())
2860 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002861
David Majnemer98c9ee22014-02-07 00:43:07 +00002862 // The unspecified model never matches what a definition could need.
2863 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2864 return false;
2865
David Majnemer4bb09802014-02-10 19:50:15 +00002866 if (BestCase) {
2867 if (RD->calculateInheritanceModel() == SemanticSpelling)
2868 return false;
2869 } else {
2870 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2871 return false;
2872 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002873
2874 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2875 << 0 /*definition*/;
2876 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2877 << RD->getNameAsString();
2878 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002879}
2880
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002881/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002882/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002883///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002884/// Despite what would be logical, the mode attribute is a decl attribute, not a
2885/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2886/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002887static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002888 // This attribute isn't documented, but glibc uses it. It changes
2889 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002890 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002891 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2892 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002893 return;
2894 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002895
Aaron Ballman00e99962013-08-31 01:11:41 +00002896 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2897 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002898
2899 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002900 if (Str.startswith("__") && Str.endswith("__"))
2901 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002902
2903 unsigned DestWidth = 0;
2904 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002905 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002906 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002907 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002908 switch (Str[0]) {
2909 case 'Q': DestWidth = 8; break;
2910 case 'H': DestWidth = 16; break;
2911 case 'S': DestWidth = 32; break;
2912 case 'D': DestWidth = 64; break;
2913 case 'X': DestWidth = 96; break;
2914 case 'T': DestWidth = 128; break;
2915 }
2916 if (Str[1] == 'F') {
2917 IntegerMode = false;
2918 } else if (Str[1] == 'C') {
2919 IntegerMode = false;
2920 ComplexMode = true;
2921 } else if (Str[1] != 'I') {
2922 DestWidth = 0;
2923 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002924 break;
2925 case 4:
2926 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2927 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002928 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002929 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002930 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002931 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002932 break;
2933 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002934 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002935 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002936 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002937 case 11:
2938 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002939 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002940 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002941 }
2942
2943 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002944 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002945 OldTy = TD->getUnderlyingType();
2946 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2947 OldTy = VD->getType();
2948 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002949 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002950 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002951 return;
2952 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002953
John McCall9dd450b2009-09-21 23:43:11 +00002954 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002955 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2956 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002957 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002958 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2959 } else if (ComplexMode) {
2960 if (!OldTy->isComplexType())
2961 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2962 } else {
2963 if (!OldTy->isFloatingType())
2964 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2965 }
2966
Mike Stump87c57ac2009-05-16 07:39:55 +00002967 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2968 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002969 // FIXME: Make sure floating-point mappings are accurate
2970 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002971 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002972 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002973 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002974 }
2975
2976 QualType NewTy;
2977
2978 if (IntegerMode)
2979 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2980 OldTy->isSignedIntegerType());
2981 else
2982 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2983
2984 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002985 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002986 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002987 }
2988
Eli Friedman4735374e2009-03-03 06:41:03 +00002989 if (ComplexMode) {
2990 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002991 }
2992
2993 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002994 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2995 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2996 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002997 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002998
2999 D->addAttr(::new (S.Context)
3000 ModeAttr(Attr.getRange(), S.Context, Name,
3001 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003002}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003003
Chandler Carruthedc2c642011-07-02 00:01:44 +00003004static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003005 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3006 if (!VD->hasGlobalStorage())
3007 S.Diag(Attr.getLoc(),
3008 diag::warn_attribute_requires_functions_or_static_globals)
3009 << Attr.getName();
3010 } else if (!isFunctionOrMethod(D)) {
3011 S.Diag(Attr.getLoc(),
3012 diag::warn_attribute_requires_functions_or_static_globals)
3013 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003014 return;
3015 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003016
Michael Han99315932013-01-24 16:46:58 +00003017 D->addAttr(::new (S.Context)
3018 NoDebugAttr(Attr.getRange(), S.Context,
3019 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003020}
3021
Paul Robinsonf0674352014-03-31 22:29:15 +00003022static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3023 const AttributeList &Attr) {
3024 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
3025 return;
3026
3027 D->addAttr(::new (S.Context)
3028 AlwaysInlineAttr(Attr.getRange(), S.Context,
3029 Attr.getAttributeSpellingListIndex()));
3030}
3031
3032static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3033 const AttributeList &Attr) {
3034 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
3035 return;
3036
3037 D->addAttr(::new (S.Context)
3038 OptimizeNoneAttr(Attr.getRange(), S.Context,
3039 Attr.getAttributeSpellingListIndex()));
3040}
3041
Chandler Carruthedc2c642011-07-02 00:01:44 +00003042static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003043 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003044 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003045 SourceRange RTRange = FD->getReturnTypeSourceRange();
3046 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003047 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003048 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3049 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003050 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003051 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003052
Aaron Ballman3aff6332013-12-02 19:30:36 +00003053 D->addAttr(::new (S.Context)
3054 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003055 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003056}
3057
Chandler Carruthedc2c642011-07-02 00:01:44 +00003058static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003059 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003060 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003061 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003062 return;
3063 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003064
Michael Han99315932013-01-24 16:46:58 +00003065 D->addAttr(::new (S.Context)
3066 GNUInlineAttr(Attr.getRange(), S.Context,
3067 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003068}
3069
Chandler Carruthedc2c642011-07-02 00:01:44 +00003070static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003071 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003072
Aaron Ballman02df2e02012-12-09 17:45:41 +00003073 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003074 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003075 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3076 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003077 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003078 return;
3079
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003080 if (!isa<ObjCMethodDecl>(D)) {
3081 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3082 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003083 return;
3084 }
3085
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003086 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003087 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003088 D->addAttr(::new (S.Context)
3089 FastCallAttr(Attr.getRange(), S.Context,
3090 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003091 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003092 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003093 D->addAttr(::new (S.Context)
3094 StdCallAttr(Attr.getRange(), S.Context,
3095 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003096 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003097 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003098 D->addAttr(::new (S.Context)
3099 ThisCallAttr(Attr.getRange(), S.Context,
3100 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003101 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003102 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003103 D->addAttr(::new (S.Context)
3104 CDeclAttr(Attr.getRange(), S.Context,
3105 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003106 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003107 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003108 D->addAttr(::new (S.Context)
3109 PascalAttr(Attr.getRange(), S.Context,
3110 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003111 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003112 case AttributeList::AT_MSABI:
3113 D->addAttr(::new (S.Context)
3114 MSABIAttr(Attr.getRange(), S.Context,
3115 Attr.getAttributeSpellingListIndex()));
3116 return;
3117 case AttributeList::AT_SysVABI:
3118 D->addAttr(::new (S.Context)
3119 SysVABIAttr(Attr.getRange(), S.Context,
3120 Attr.getAttributeSpellingListIndex()));
3121 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003122 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003123 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003124 switch (CC) {
3125 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003126 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003127 break;
3128 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003129 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003130 break;
3131 default:
3132 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003133 }
3134
Michael Han99315932013-01-24 16:46:58 +00003135 D->addAttr(::new (S.Context)
3136 PcsAttr(Attr.getRange(), S.Context, PCS,
3137 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003138 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003139 }
Derek Schuffa2020962012-10-16 22:30:41 +00003140 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003141 D->addAttr(::new (S.Context)
3142 PnaclCallAttr(Attr.getRange(), S.Context,
3143 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003144 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003145 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003146 D->addAttr(::new (S.Context)
3147 IntelOclBiccAttr(Attr.getRange(), S.Context,
3148 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003149 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003150
Abramo Bagnara50099372010-04-30 13:10:51 +00003151 default:
3152 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003153 }
3154}
3155
Aaron Ballman02df2e02012-12-09 17:45:41 +00003156bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3157 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003158 if (attr.isInvalid())
3159 return true;
3160
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003161 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003162 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003163 attr.setInvalid();
3164 return true;
3165 }
3166
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003167 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003168 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003169 case AttributeList::AT_CDecl: CC = CC_C; break;
3170 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3171 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3172 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3173 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003174 case AttributeList::AT_MSABI:
3175 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3176 CC_X86_64Win64;
3177 break;
3178 case AttributeList::AT_SysVABI:
3179 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3180 CC_C;
3181 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003182 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003183 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003184 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003185 attr.setInvalid();
3186 return true;
3187 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003188 if (StrRef == "aapcs") {
3189 CC = CC_AAPCS;
3190 break;
3191 } else if (StrRef == "aapcs-vfp") {
3192 CC = CC_AAPCS_VFP;
3193 break;
3194 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003195
3196 attr.setInvalid();
3197 Diag(attr.getLoc(), diag::err_invalid_pcs);
3198 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003199 }
Derek Schuffa2020962012-10-16 22:30:41 +00003200 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003201 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003202 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003203 }
3204
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003205 const TargetInfo &TI = Context.getTargetInfo();
3206 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3207 if (A == TargetInfo::CCCR_Warning) {
3208 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003209
3210 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3211 if (FD)
3212 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3213 TargetInfo::CCMT_NonMember;
3214 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003215 }
3216
John McCall3882ace2011-01-05 12:14:39 +00003217 return false;
3218}
3219
John McCall3882ace2011-01-05 12:14:39 +00003220/// Checks a regparm attribute, returning true if it is ill-formed and
3221/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003222bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3223 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003224 return true;
3225
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003226 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003227 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003228 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003229 }
Eli Friedman7044b762009-03-27 21:06:47 +00003230
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003231 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003232 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003233 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003234 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003235 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003236 }
3237
Douglas Gregore8bbc122011-09-02 00:18:52 +00003238 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003239 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003240 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003241 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003242 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003243 }
3244
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003245 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003246 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003247 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003248 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003249 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003250 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003251 }
3252
John McCall3882ace2011-01-05 12:14:39 +00003253 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003254}
3255
Aaron Ballman66039932013-12-19 00:41:31 +00003256static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3257 const AttributeList &Attr) {
Aaron Ballman66039932013-12-19 00:41:31 +00003258 uint32_t MaxThreads, MinBlocks = 0;
3259 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3260 return;
3261 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3262 Attr.getArgAsExpr(1),
3263 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003264 return;
3265
3266 D->addAttr(::new (S.Context)
3267 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3268 MaxThreads, MinBlocks,
3269 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003270}
3271
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003272static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3273 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003274 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003275 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003276 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003277 return;
3278 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003279
3280 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003281 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003282
Aaron Ballman00e99962013-08-31 01:11:41 +00003283 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003284
3285 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3287 << Attr.getName() << ExpectedFunctionOrMethod;
3288 return;
3289 }
3290
3291 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003292 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3293 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003294 return;
3295
3296 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003297 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3298 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003299 return;
3300
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003301 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003302 if (IsPointer) {
3303 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003304 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003305 if (!BufferTy->isPointerType()) {
3306 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003307 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003308 }
3309 }
3310
Michael Han99315932013-01-24 16:46:58 +00003311 D->addAttr(::new (S.Context)
3312 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3313 ArgumentIdx, TypeTagIdx, IsPointer,
3314 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003315}
3316
3317static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3318 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003319 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003320 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003321 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003322 return;
3323 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003324
3325 if (!checkAttributeNumArgs(S, Attr, 1))
3326 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003327
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003328 if (!isa<VarDecl>(D)) {
3329 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3330 << Attr.getName() << ExpectedVariable;
3331 return;
3332 }
3333
Aaron Ballman00e99962013-08-31 01:11:41 +00003334 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003335 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003336 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3337 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003338
Michael Han99315932013-01-24 16:46:58 +00003339 D->addAttr(::new (S.Context)
3340 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003341 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003342 Attr.getLayoutCompatible(),
3343 Attr.getMustBeNull(),
3344 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003345}
3346
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003347//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003348// Checker-specific attribute handlers.
3349//===----------------------------------------------------------------------===//
3350
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003351static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003352 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003353 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003354}
3355
John McCalled433932011-01-25 03:31:58 +00003356static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003357 return type->isDependentType() ||
3358 type->isObjCObjectPointerType() ||
3359 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003360}
3361static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003362 return type->isDependentType() ||
3363 type->isPointerType() ||
3364 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003365}
3366
Chandler Carruthedc2c642011-07-02 00:01:44 +00003367static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003368 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003369 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003370
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003371 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003372 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3373 cf = false;
3374 } else {
3375 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3376 cf = true;
3377 }
3378
3379 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003380 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003381 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003382 return;
3383 }
3384
3385 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003386 param->addAttr(::new (S.Context)
3387 CFConsumedAttr(Attr.getRange(), S.Context,
3388 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003389 else
Michael Han99315932013-01-24 16:46:58 +00003390 param->addAttr(::new (S.Context)
3391 NSConsumedAttr(Attr.getRange(), S.Context,
3392 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003393}
3394
Chandler Carruthedc2c642011-07-02 00:01:44 +00003395static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3396 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003397
John McCalled433932011-01-25 03:31:58 +00003398 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003399
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003400 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003401 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003402 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003403 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003404 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003405 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3406 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003407 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003408 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003409 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003410 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003411 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003412 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003413 return;
3414 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003415
John McCalled433932011-01-25 03:31:58 +00003416 bool typeOK;
3417 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003418 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003419 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003420 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003421 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003422 cf = false;
3423 break;
3424
3425 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003426 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003427 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3428 cf = false;
3429 break;
3430
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003431 case AttributeList::AT_CFReturnsRetained:
3432 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003433 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3434 cf = true;
3435 break;
3436 }
3437
3438 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003439 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003440 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003441 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003442 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003443
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003444 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003445 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003446 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003447 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003448 D->addAttr(::new (S.Context)
3449 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3450 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003451 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003452 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003453 D->addAttr(::new (S.Context)
3454 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3455 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003456 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003457 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003458 D->addAttr(::new (S.Context)
3459 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3460 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003461 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003462 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003463 D->addAttr(::new (S.Context)
3464 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3465 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003466 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003467 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003468 D->addAttr(::new (S.Context)
3469 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3470 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003471 return;
3472 };
3473}
3474
John McCallcf166702011-07-22 08:53:00 +00003475static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3476 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003477 const int EP_ObjCMethod = 1;
3478 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003479
John McCallcf166702011-07-22 08:53:00 +00003480 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003481 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003482 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003483 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003484 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003485 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003486
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003487 if (!resultType->isReferenceType() &&
3488 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003489 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003490 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003491 << attr.getName()
3492 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003493 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003494
3495 // Drop the attribute.
3496 return;
3497 }
3498
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003499 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003500 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3501 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003502}
3503
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003504static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3505 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003506 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003507
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003508 DeclContext *DC = method->getDeclContext();
3509 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3510 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3511 << attr.getName() << 0;
3512 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3513 return;
3514 }
3515 if (method->getMethodFamily() == OMF_dealloc) {
3516 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3517 << attr.getName() << 1;
3518 return;
3519 }
3520
Michael Han99315932013-01-24 16:46:58 +00003521 method->addAttr(::new (S.Context)
3522 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3523 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003524}
3525
Aaron Ballmanfb763042013-12-02 18:05:46 +00003526static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3527 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003528 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003529 return;
John McCall32f5fe12011-09-30 05:12:12 +00003530
Aaron Ballmanfb763042013-12-02 18:05:46 +00003531 D->addAttr(::new (S.Context)
3532 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3533 Attr.getAttributeSpellingListIndex()));
3534}
3535
3536static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3537 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003538 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003539 return;
3540
3541 D->addAttr(::new (S.Context)
3542 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3543 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003544}
3545
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003546static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3547 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003548 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003549
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003550 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003551 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003552 return;
3553 }
3554
3555 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003556 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003557 Attr.getAttributeSpellingListIndex()));
3558}
3559
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003560static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3561 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003562 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3563
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003564 if (!Parm) {
3565 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3566 return;
3567 }
3568
3569 D->addAttr(::new (S.Context)
3570 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3571 Attr.getAttributeSpellingListIndex()));
3572}
3573
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003574static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3575 const AttributeList &Attr) {
3576 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003577 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003578 if (!RelatedClass) {
3579 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3580 return;
3581 }
3582 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003583 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003584 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003585 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003586 D->addAttr(::new (S.Context)
3587 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3588 ClassMethod, InstanceMethod,
3589 Attr.getAttributeSpellingListIndex()));
3590}
3591
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003592static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3593 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003594 ObjCInterfaceDecl *IFace;
3595 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3596 IFace = CatDecl->getClassInterface();
3597 else
3598 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003599 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003600 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003601 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3602 Attr.getAttributeSpellingListIndex()));
3603}
3604
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003605static void handleObjCRuntimeName(Sema &S, Decl *D,
3606 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003607 StringRef MetaDataName;
3608 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3609 return;
3610 D->addAttr(::new (S.Context)
3611 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3612 MetaDataName,
3613 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003614}
3615
Chandler Carruthedc2c642011-07-02 00:01:44 +00003616static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3617 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003618 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003619
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003620 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003621 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003622}
3623
Chandler Carruthedc2c642011-07-02 00:01:44 +00003624static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3625 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003626 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003627 QualType type = vd->getType();
3628
3629 if (!type->isDependentType() &&
3630 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003631 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003632 << type;
3633 return;
3634 }
3635
3636 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3637
3638 // If we have no lifetime yet, check the lifetime we're presumably
3639 // going to infer.
3640 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3641 lifetime = type->getObjCARCImplicitLifetime();
3642
3643 switch (lifetime) {
3644 case Qualifiers::OCL_None:
3645 assert(type->isDependentType() &&
3646 "didn't infer lifetime for non-dependent type?");
3647 break;
3648
3649 case Qualifiers::OCL_Weak: // meaningful
3650 case Qualifiers::OCL_Strong: // meaningful
3651 break;
3652
3653 case Qualifiers::OCL_ExplicitNone:
3654 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003655 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003656 << (lifetime == Qualifiers::OCL_Autoreleasing);
3657 break;
3658 }
3659
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003660 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003661 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3662 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003663}
3664
Francois Picheta83957a2010-12-19 06:50:37 +00003665//===----------------------------------------------------------------------===//
3666// Microsoft specific attribute handlers.
3667//===----------------------------------------------------------------------===//
3668
Chandler Carruthedc2c642011-07-02 00:01:44 +00003669static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003670 if (!S.LangOpts.CPlusPlus) {
3671 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3672 << Attr.getName() << AttributeLangSupport::C;
3673 return;
3674 }
3675
Aaron Ballman60e705e2013-11-24 20:58:02 +00003676 if (!isa<CXXRecordDecl>(D)) {
3677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3678 << Attr.getName() << ExpectedClass;
3679 return;
3680 }
3681
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003682 StringRef StrRef;
3683 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003684 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003685 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003686
David Majnemer89085342013-08-09 08:56:20 +00003687 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3688 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003689 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3690 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003691
Reid Kleckner140c4a72013-05-17 14:04:52 +00003692 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003693 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003694 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003695 return;
3696 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003697
David Majnemer89085342013-08-09 08:56:20 +00003698 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003699 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003700 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003701 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003702 return;
3703 }
David Majnemer89085342013-08-09 08:56:20 +00003704 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003705 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003706 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003707 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003708 }
Francois Picheta83957a2010-12-19 06:50:37 +00003709
David Majnemer89085342013-08-09 08:56:20 +00003710 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3711 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003712}
3713
David Majnemer2c4e00a2014-01-29 22:07:36 +00003714static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3715 if (!S.LangOpts.CPlusPlus) {
3716 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3717 << Attr.getName() << AttributeLangSupport::C;
3718 return;
3719 }
3720 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003721 D, Attr.getRange(), /*BestCase=*/true,
3722 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003723 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3724 if (IA)
3725 D->addAttr(IA);
3726}
3727
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003728static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3729 const AttributeList &Attr) {
3730 VarDecl *VD = cast<VarDecl>(D);
3731 if (!S.Context.getTargetInfo().isTLSSupported()) {
3732 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3733 return;
3734 }
3735 if (VD->getTSCSpec() != TSCS_unspecified) {
3736 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3737 return;
3738 }
3739 if (VD->hasLocalStorage()) {
3740 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3741 return;
3742 }
3743 VD->addAttr(::new (S.Context) ThreadAttr(
3744 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3745}
3746
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003747static void handleARMInterruptAttr(Sema &S, Decl *D,
3748 const AttributeList &Attr) {
3749 // Check the attribute arguments.
3750 if (Attr.getNumArgs() > 1) {
3751 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3752 << Attr.getName() << 1;
3753 return;
3754 }
3755
3756 StringRef Str;
3757 SourceLocation ArgLoc;
3758
3759 if (Attr.getNumArgs() == 0)
3760 Str = "";
3761 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3762 return;
3763
3764 ARMInterruptAttr::InterruptType Kind;
3765 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3766 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3767 << Attr.getName() << Str << ArgLoc;
3768 return;
3769 }
3770
3771 unsigned Index = Attr.getAttributeSpellingListIndex();
3772 D->addAttr(::new (S.Context)
3773 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3774}
3775
3776static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3777 const AttributeList &Attr) {
3778 if (!checkAttributeNumArgs(S, Attr, 1))
3779 return;
3780
3781 if (!Attr.isArgExpr(0)) {
3782 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3783 << AANT_ArgumentIntegerConstant;
3784 return;
3785 }
3786
3787 // FIXME: Check for decl - it should be void ()(void).
3788
3789 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3790 llvm::APSInt NumParams(32);
3791 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3792 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3793 << Attr.getName() << AANT_ArgumentIntegerConstant
3794 << NumParamsExpr->getSourceRange();
3795 return;
3796 }
3797
3798 unsigned Num = NumParams.getLimitedValue(255);
3799 if ((Num & 1) || Num > 30) {
3800 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3801 << Attr.getName() << (int)NumParams.getSExtValue()
3802 << NumParamsExpr->getSourceRange();
3803 return;
3804 }
3805
Aaron Ballman36a53502014-01-16 13:03:14 +00003806 D->addAttr(::new (S.Context)
3807 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3808 Attr.getAttributeSpellingListIndex()));
3809 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003810}
3811
3812static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3813 // Dispatch the interrupt attribute based on the current target.
3814 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3815 handleMSP430InterruptAttr(S, D, Attr);
3816 else
3817 handleARMInterruptAttr(S, D, Attr);
3818}
3819
3820static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3821 const AttributeList& Attr) {
3822 // If we try to apply it to a function pointer, don't warn, but don't
3823 // do anything, either. It doesn't matter anyway, because there's nothing
3824 // special about calling a force_align_arg_pointer function.
3825 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3826 if (VD && VD->getType()->isFunctionPointerType())
3827 return;
3828 // Also don't warn on function pointer typedefs.
3829 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3830 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3831 TD->getUnderlyingType()->isFunctionType()))
3832 return;
3833 // Attribute can only be applied to function types.
3834 if (!isa<FunctionDecl>(D)) {
3835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3836 << Attr.getName() << /* function */0;
3837 return;
3838 }
3839
Aaron Ballman36a53502014-01-16 13:03:14 +00003840 D->addAttr(::new (S.Context)
3841 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3842 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003843}
3844
3845DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3846 unsigned AttrSpellingListIndex) {
3847 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003848 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00003849 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003850 }
3851
3852 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003853 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003854
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003855 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003856}
3857
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003858DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3859 unsigned AttrSpellingListIndex) {
3860 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003861 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003862 D->dropAttr<DLLImportAttr>();
3863 }
3864
3865 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003866 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003867
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003868 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003869}
3870
Hans Wennborge82f19c2014-06-24 23:57:05 +00003871static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00003872 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
3873 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
3874 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
3875 << A.getName();
3876 return;
3877 }
3878
Hans Wennborge82f19c2014-06-24 23:57:05 +00003879 unsigned Index = A.getAttributeSpellingListIndex();
3880 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
3881 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
3882 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003883 if (NewAttr)
3884 D->addAttr(NewAttr);
3885}
3886
David Majnemer2c4e00a2014-01-29 22:07:36 +00003887MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003888Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003889 unsigned AttrSpellingListIndex,
3890 MSInheritanceAttr::Spelling SemanticSpelling) {
3891 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3892 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00003893 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003894 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3895 << 1 /*previous declaration*/;
3896 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3897 D->dropAttr<MSInheritanceAttr>();
3898 }
3899
3900 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3901 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003902 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3903 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003904 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003905 }
3906 } else {
3907 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3908 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3909 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003910 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003911 }
3912 if (RD->getDescribedClassTemplate()) {
3913 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3914 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003915 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003916 }
3917 }
3918
3919 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003920 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003921}
3922
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003923static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3924 // The capability attributes take a single string parameter for the name of
3925 // the capability they represent. The lockable attribute does not take any
3926 // parameters. However, semantically, both attributes represent the same
3927 // concept, and so they use the same semantic attribute. Eventually, the
3928 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003929 //
Alp Toker958027b2014-07-14 19:42:55 +00003930 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00003931 // literal will be considered a "mutex."
3932 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003933 SourceLocation LiteralLoc;
3934 if (Attr.getKind() == AttributeList::AT_Capability &&
3935 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3936 return;
3937
Aaron Ballman6c810072014-03-05 21:47:13 +00003938 // Currently, there are only two names allowed for a capability: role and
3939 // mutex (case insensitive). Diagnose other capability names.
3940 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3941 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3942
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003943 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3944 Attr.getAttributeSpellingListIndex()));
3945}
3946
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003947static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3948 const AttributeList &Attr) {
3949 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3950 Attr.getArgAsExpr(0),
3951 Attr.getAttributeSpellingListIndex()));
3952}
3953
3954static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3955 const AttributeList &Attr) {
3956 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003957 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003958 return;
3959
3960 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3961 S.Context,
3962 Args.data(), Args.size(),
3963 Attr.getAttributeSpellingListIndex()));
3964}
3965
3966static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3967 const AttributeList &Attr) {
3968 SmallVector<Expr*, 2> Args;
3969 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3970 return;
3971
3972 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3973 S.Context,
3974 Attr.getArgAsExpr(0),
3975 Args.data(),
3976 Args.size(),
3977 Attr.getAttributeSpellingListIndex()));
3978}
3979
3980static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3981 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003982 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003983 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003984 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003985
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003986 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
3987 Attr.getRange(), S.Context, Args.data(), Args.size(),
3988 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003989}
3990
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003991static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3992 const AttributeList &Attr) {
3993 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3994 return;
3995
3996 // check that all arguments are lockable objects
3997 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003998 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003999 if (Args.empty())
4000 return;
4001
4002 RequiresCapabilityAttr *RCA = ::new (S.Context)
4003 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4004 Args.size(), Attr.getAttributeSpellingListIndex());
4005
4006 D->addAttr(RCA);
4007}
4008
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004009/// Handles semantic checking for features that are common to all attributes,
4010/// such as checking whether a parameter was properly specified, or the correct
4011/// number of arguments were passed, etc.
4012static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4013 const AttributeList &Attr) {
4014 // Several attributes carry different semantics than the parsing requires, so
4015 // those are opted out of the common handling.
4016 //
4017 // We also bail on unknown and ignored attributes because those are handled
4018 // as part of the target-specific handling logic.
4019 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004020 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004021 return false;
4022
Aaron Ballman3aff6332013-12-02 19:30:36 +00004023 // Check whether the attribute requires specific language extensions to be
4024 // enabled.
4025 if (!Attr.diagnoseLangOpts(S))
4026 return true;
4027
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004028 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4029 // If there are no optional arguments, then checking for the argument count
4030 // is trivial.
4031 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4032 return true;
4033 } else {
4034 // There are optional arguments, so checking is slightly more involved.
4035 if (Attr.getMinArgs() &&
4036 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4037 return true;
4038 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4039 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4040 return true;
4041 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004042
4043 // Check whether the attribute appertains to the given subject.
4044 if (!Attr.diagnoseAppertainsTo(S, D))
4045 return true;
4046
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004047 return false;
4048}
4049
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004050//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004051// Top Level Sema Entry Points
4052//===----------------------------------------------------------------------===//
4053
Richard Smithf8a75c32013-08-29 00:47:48 +00004054/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4055/// the attribute applies to decls. If the attribute is a type attribute, just
4056/// silently ignore it if a GNU attribute.
4057static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4058 const AttributeList &Attr,
4059 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004060 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004061 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004062
Richard Smithf8a75c32013-08-29 00:47:48 +00004063 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4064 // instead.
4065 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4066 return;
4067
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004068 // Unknown attributes are automatically warned on. Target-specific attributes
4069 // which do not apply to the current target architecture are treated as
4070 // though they were unknown attributes.
4071 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4072 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004073 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4074 ? diag::warn_unhandled_ms_attribute_ignored
4075 : diag::warn_unknown_attribute_ignored)
4076 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004077 return;
4078 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004079
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004080 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4081 return;
4082
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004083 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004084 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004085 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004086 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004087 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004088 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004089 handleInterruptAttr(S, D, Attr);
4090 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004091 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004092 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4093 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004094 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004095 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004096 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004097 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004098 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004099 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4100 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004101 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004102 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4103 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004104 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004105 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4106 break;
4107 case AttributeList::AT_IBOutlet:
4108 handleIBOutlet(S, D, Attr);
4109 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004110 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004111 handleIBOutletCollection(S, D, Attr);
4112 break;
4113 case AttributeList::AT_Alias:
4114 handleAliasAttr(S, D, Attr);
4115 break;
4116 case AttributeList::AT_Aligned:
4117 handleAlignedAttr(S, D, Attr);
4118 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004119 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004120 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004121 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004122 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004123 handleAnalyzerNoReturnAttr(S, D, Attr);
4124 break;
4125 case AttributeList::AT_TLSModel:
4126 handleTLSModelAttr(S, D, Attr);
4127 break;
4128 case AttributeList::AT_Annotate:
4129 handleAnnotateAttr(S, D, Attr);
4130 break;
4131 case AttributeList::AT_Availability:
4132 handleAvailabilityAttr(S, D, Attr);
4133 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004134 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004135 handleDependencyAttr(S, scope, D, Attr);
4136 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004137 case AttributeList::AT_Common:
4138 handleCommonAttr(S, D, Attr);
4139 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004140 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004141 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4142 break;
4143 case AttributeList::AT_Constructor:
4144 handleConstructorAttr(S, D, Attr);
4145 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004146 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004147 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4148 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004149 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004150 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004151 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004152 case AttributeList::AT_Destructor:
4153 handleDestructorAttr(S, D, Attr);
4154 break;
4155 case AttributeList::AT_EnableIf:
4156 handleEnableIfAttr(S, D, Attr);
4157 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004159 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004160 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004161 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004162 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004163 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004164 case AttributeList::AT_OptimizeNone:
4165 handleOptimizeNoneAttr(S, D, Attr);
4166 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004167 case AttributeList::AT_Flatten:
4168 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4169 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004170 case AttributeList::AT_Format:
4171 handleFormatAttr(S, D, Attr);
4172 break;
4173 case AttributeList::AT_FormatArg:
4174 handleFormatArgAttr(S, D, Attr);
4175 break;
4176 case AttributeList::AT_CUDAGlobal:
4177 handleGlobalAttr(S, D, Attr);
4178 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004179 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004180 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4181 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004182 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004183 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4184 break;
4185 case AttributeList::AT_GNUInline:
4186 handleGNUInlineAttr(S, D, Attr);
4187 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004189 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004190 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004191 case AttributeList::AT_Malloc:
4192 handleMallocAttr(S, D, Attr);
4193 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004194 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004195 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4196 break;
4197 case AttributeList::AT_Mode:
4198 handleModeAttr(S, D, Attr);
4199 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004200 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004201 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4202 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004203 case AttributeList::AT_NoSplitStack:
4204 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4205 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004206 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004207 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4208 handleNonNullAttrParameter(S, PVD, Attr);
4209 else
4210 handleNonNullAttr(S, D, Attr);
4211 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004212 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004213 handleReturnsNonNullAttr(S, D, Attr);
4214 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004215 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004216 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4217 break;
4218 case AttributeList::AT_Ownership:
4219 handleOwnershipAttr(S, D, Attr);
4220 break;
4221 case AttributeList::AT_Cold:
4222 handleColdAttr(S, D, Attr);
4223 break;
4224 case AttributeList::AT_Hot:
4225 handleHotAttr(S, D, Attr);
4226 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004227 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004228 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4229 break;
4230 case AttributeList::AT_NoReturn:
4231 handleNoReturnAttr(S, D, Attr);
4232 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004233 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004234 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4235 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004236 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004237 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4238 break;
4239 case AttributeList::AT_VecReturn:
4240 handleVecReturnAttr(S, D, Attr);
4241 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004242
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004243 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004244 handleObjCOwnershipAttr(S, D, Attr);
4245 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004246 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004247 handleObjCPreciseLifetimeAttr(S, D, Attr);
4248 break;
John McCall31168b02011-06-15 23:02:42 +00004249
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004250 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004251 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4252 break;
John McCallcf166702011-07-22 08:53:00 +00004253
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004254 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004255 handleObjCRequiresSuperAttr(S, D, Attr);
4256 break;
4257
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004258 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004259 handleObjCBridgeAttr(S, scope, D, Attr);
4260 break;
4261
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004262 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004263 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4264 break;
4265
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004266 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004267 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4268 break;
John McCallf1e8b342011-09-29 07:17:38 +00004269
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004270 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004271 handleObjCDesignatedInitializer(S, D, Attr);
4272 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004273
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004274 case AttributeList::AT_ObjCRuntimeName:
4275 handleObjCRuntimeName(S, D, Attr);
4276 break;
4277
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004278 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004279 handleCFAuditedTransferAttr(S, D, Attr);
4280 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004281 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004282 handleCFUnknownTransferAttr(S, D, Attr);
4283 break;
John McCall32f5fe12011-09-30 05:12:12 +00004284
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004285 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004286 case AttributeList::AT_NSConsumed:
4287 handleNSConsumedAttr(S, D, Attr);
4288 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004289 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004290 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4291 break;
John McCalled433932011-01-25 03:31:58 +00004292
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004293 case AttributeList::AT_NSReturnsAutoreleased:
4294 case AttributeList::AT_NSReturnsNotRetained:
4295 case AttributeList::AT_CFReturnsNotRetained:
4296 case AttributeList::AT_NSReturnsRetained:
4297 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004298 handleNSReturnsRetainedAttr(S, D, Attr);
4299 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004300 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004301 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4302 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004303 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004304 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4305 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004306 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004307 handleVecTypeHint(S, D, Attr);
4308 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004309
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004310 case AttributeList::AT_InitPriority:
4311 handleInitPriorityAttr(S, D, Attr);
4312 break;
4313
4314 case AttributeList::AT_Packed:
4315 handlePackedAttr(S, D, Attr);
4316 break;
4317 case AttributeList::AT_Section:
4318 handleSectionAttr(S, D, Attr);
4319 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004320 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004321 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004322 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004323 case AttributeList::AT_ArcWeakrefUnavailable:
4324 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4325 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004326 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004327 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4328 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004329 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004330 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004331 break;
4332 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004333 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4334 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004335 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004336 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4337 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004338 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004339 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4340 break;
4341 case AttributeList::AT_Used:
4342 handleUsedAttr(S, D, Attr);
4343 break;
John McCalld041a9b2013-02-20 01:54:26 +00004344 case AttributeList::AT_Visibility:
4345 handleVisibilityAttr(S, D, Attr, false);
4346 break;
4347 case AttributeList::AT_TypeVisibility:
4348 handleVisibilityAttr(S, D, Attr, true);
4349 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004350 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004351 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4352 break;
4353 case AttributeList::AT_WarnUnusedResult:
4354 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004355 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004356 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004357 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4358 break;
4359 case AttributeList::AT_WeakRef:
4360 handleWeakRefAttr(S, D, Attr);
4361 break;
4362 case AttributeList::AT_WeakImport:
4363 handleWeakImportAttr(S, D, Attr);
4364 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004365 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004366 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004367 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004368 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004369 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4370 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004371 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004372 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004373 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004374 case AttributeList::AT_ObjCNSObject:
4375 handleObjCNSObject(S, D, Attr);
4376 break;
4377 case AttributeList::AT_Blocks:
4378 handleBlocksAttr(S, D, Attr);
4379 break;
4380 case AttributeList::AT_Sentinel:
4381 handleSentinelAttr(S, D, Attr);
4382 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004383 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004384 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4385 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004386 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004387 handleSimpleAttribute<PureAttr>(S, D, Attr);
4388 break;
4389 case AttributeList::AT_Cleanup:
4390 handleCleanupAttr(S, D, Attr);
4391 break;
4392 case AttributeList::AT_NoDebug:
4393 handleNoDebugAttr(S, D, Attr);
4394 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004395 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004396 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4397 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004398 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004399 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4400 break;
4401 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4402 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4403 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004404 case AttributeList::AT_StdCall:
4405 case AttributeList::AT_CDecl:
4406 case AttributeList::AT_FastCall:
4407 case AttributeList::AT_ThisCall:
4408 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004409 case AttributeList::AT_MSABI:
4410 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004411 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004412 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004413 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004414 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004415 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004416 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004417 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4418 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004419 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004420 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4421 break;
John McCall8d32c052012-05-22 21:28:12 +00004422
4423 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004424 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004425 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004426 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004427 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004428 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004429 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004430 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004431 handleMSInheritanceAttr(S, D, Attr);
4432 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004433 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004434 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4435 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004436 case AttributeList::AT_Thread:
4437 handleDeclspecThreadAttr(S, D, Attr);
4438 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004439
4440 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004441 case AttributeList::AT_AssertExclusiveLock:
4442 handleAssertExclusiveLockAttr(S, D, Attr);
4443 break;
4444 case AttributeList::AT_AssertSharedLock:
4445 handleAssertSharedLockAttr(S, D, Attr);
4446 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004447 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004448 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4449 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004450 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004451 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004452 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004453 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004454 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4455 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004456 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004457 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004458 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004459 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004460 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004461 break;
4462 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004463 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004464 break;
4465 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004466 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004467 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004468 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004469 handleGuardedByAttr(S, D, Attr);
4470 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004471 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004472 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004473 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004474 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004475 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004476 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004477 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004478 handleLockReturnedAttr(S, D, Attr);
4479 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004480 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004481 handleLocksExcludedAttr(S, D, Attr);
4482 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004483 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004484 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004485 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004486 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004487 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004488 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004489 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004490 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004491 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004492
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004493 // Capability analysis attributes.
4494 case AttributeList::AT_Capability:
4495 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004496 handleCapabilityAttr(S, D, Attr);
4497 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004498 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004499 handleRequiresCapabilityAttr(S, D, Attr);
4500 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004501
4502 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004503 handleAssertCapabilityAttr(S, D, Attr);
4504 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004505 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004506 handleAcquireCapabilityAttr(S, D, Attr);
4507 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004508 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004509 handleReleaseCapabilityAttr(S, D, Attr);
4510 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004511 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004512 handleTryAcquireCapabilityAttr(S, D, Attr);
4513 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004514
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004515 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004516 case AttributeList::AT_Consumable:
4517 handleConsumableAttr(S, D, Attr);
4518 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004519 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004520 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4521 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004522 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004523 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4524 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004525 case AttributeList::AT_CallableWhen:
4526 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004527 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004528 case AttributeList::AT_ParamTypestate:
4529 handleParamTypestateAttr(S, D, Attr);
4530 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004531 case AttributeList::AT_ReturnTypestate:
4532 handleReturnTypestateAttr(S, D, Attr);
4533 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004534 case AttributeList::AT_SetTypestate:
4535 handleSetTypestateAttr(S, D, Attr);
4536 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004537 case AttributeList::AT_TestTypestate:
4538 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004539 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004540
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004541 // Type safety attributes.
4542 case AttributeList::AT_ArgumentWithTypeTag:
4543 handleArgumentWithTypeTagAttr(S, D, Attr);
4544 break;
4545 case AttributeList::AT_TypeTagForDatatype:
4546 handleTypeTagForDatatypeAttr(S, D, Attr);
4547 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004548 }
4549}
4550
4551/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4552/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004553void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004554 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004555 bool IncludeCXX11Attributes) {
4556 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004557 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004558
Joey Gouly2cd9db12013-12-13 16:15:28 +00004559 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004560 // GCC accepts
4561 // static int a9 __attribute__((weakref));
4562 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004563 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004564 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4565 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004566 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004567 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004568 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004569
4570 if (!D->hasAttr<OpenCLKernelAttr>()) {
4571 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004572 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4573 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004574 D->setInvalidDecl();
4575 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004576 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4577 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004578 D->setInvalidDecl();
4579 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004580 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4581 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004582 D->setInvalidDecl();
4583 }
4584 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004585}
4586
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004587// Annotation attributes are the only attributes allowed after an access
4588// specifier.
4589bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4590 const AttributeList *AttrList) {
4591 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004592 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004593 handleAnnotateAttr(*this, ASDecl, *l);
4594 } else {
4595 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4596 return true;
4597 }
4598 }
4599
4600 return false;
4601}
4602
John McCall42856de2011-10-01 05:17:03 +00004603/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4604/// contains any decl attributes that we should warn about.
4605static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4606 for ( ; A; A = A->getNext()) {
4607 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004608 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004609 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4610
4611 if (A->getKind() == AttributeList::UnknownAttribute) {
4612 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4613 << A->getName() << A->getRange();
4614 } else {
4615 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4616 << A->getName() << A->getRange();
4617 }
4618 }
4619}
4620
4621/// checkUnusedDeclAttributes - Given a declarator which is not being
4622/// used to build a declaration, complain about any decl attributes
4623/// which might be lying around on it.
4624void Sema::checkUnusedDeclAttributes(Declarator &D) {
4625 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4626 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4627 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4628 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4629}
4630
Ryan Flynn7d470f32009-07-30 03:15:39 +00004631/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004632/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004633NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4634 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004635 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00004636 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00004637 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004638 FunctionDecl *NewFD;
4639 // FIXME: Missing call to CheckFunctionDeclaration().
4640 // FIXME: Mangling?
4641 // FIXME: Is the qualifier info correct?
4642 // FIXME: Is the DeclContext correct?
4643 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4644 Loc, Loc, DeclarationName(II),
4645 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004646 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004647 FD->hasPrototype(),
4648 false/*isConstexprSpecified*/);
4649 NewD = NewFD;
4650
4651 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004652 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004653
4654 // Fake up parameter variables; they are declared as if this were
4655 // a typedef.
4656 QualType FDTy = FD->getType();
4657 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4658 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004659 for (const auto &AI : FT->param_types()) {
4660 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004661 Param->setScopeInfo(0, Params.size());
4662 Params.push_back(Param);
4663 }
David Blaikie9c70e042011-09-21 18:16:56 +00004664 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004665 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004666 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4667 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004668 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004669 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004670 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004671 if (VD->getQualifier()) {
4672 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004673 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004674 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004675 }
4676 return NewD;
4677}
4678
James Dennett634962f2012-06-14 21:40:34 +00004679/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004680/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004681void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004682 if (W.getUsed()) return; // only do this once
4683 W.setUsed(true);
4684 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4685 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004686 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004687 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4688 W.getLocation()));
4689 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004690 WeakTopLevelDecl.push_back(NewD);
4691 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4692 // to insert Decl at TU scope, sorry.
4693 DeclContext *SavedContext = CurContext;
4694 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004695 NewD->setDeclContext(CurContext);
4696 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004697 PushOnScopeChains(NewD, S);
4698 CurContext = SavedContext;
4699 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004700 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004701 }
4702}
4703
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004704void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4705 // It's valid to "forward-declare" #pragma weak, in which case we
4706 // have to do this.
4707 LoadExternalWeakUndeclaredIdentifiers();
4708 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004709 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004710 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4711 if (VD->isExternC())
4712 ND = VD;
4713 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4714 if (FD->isExternC())
4715 ND = FD;
4716 if (ND) {
4717 if (IdentifierInfo *Id = ND->getIdentifier()) {
4718 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4719 = WeakUndeclaredIdentifiers.find(Id);
4720 if (I != WeakUndeclaredIdentifiers.end()) {
4721 WeakInfo W = I->second;
4722 DeclApplyPragmaWeak(S, ND, W);
4723 WeakUndeclaredIdentifiers[Id] = W;
4724 }
4725 }
4726 }
4727 }
4728}
4729
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004730/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4731/// it, apply them to D. This is a bit tricky because PD can have attributes
4732/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004733void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004734 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004735 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004736 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004737
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004738 // Walk the declarator structure, applying decl attributes that were in a type
4739 // position to the decl itself. This handles cases like:
4740 // int *__attr__(x)** D;
4741 // when X is a decl attribute.
4742 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4743 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004744 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004745
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004746 // Finally, apply any attributes on the decl itself.
4747 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004748 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004749}
John McCall28a6aea2009-11-04 02:18:39 +00004750
John McCall31168b02011-06-15 23:02:42 +00004751/// Is the given declaration allowed to use a forbidden type?
4752static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4753 // Private ivars are always okay. Unfortunately, people don't
4754 // always properly make their ivars private, even in system headers.
4755 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004756 // Function declarations in sys headers will be marked unavailable.
4757 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4758 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004759 return false;
4760
4761 // Require it to be declared in a system header.
4762 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4763}
4764
4765/// Handle a delayed forbidden-type diagnostic.
4766static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4767 Decl *decl) {
4768 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004769 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4770 "this system declaration uses an unsupported type",
4771 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004772 return;
4773 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004774 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004775 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004776 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004777 // kind of forbidden type messages on unavailable functions.
4778 if (FD->hasAttr<UnavailableAttr>() &&
4779 diag.getForbiddenTypeDiagnostic() ==
4780 diag::err_arc_array_param_no_ownership) {
4781 diag.Triggered = true;
4782 return;
4783 }
4784 }
John McCall31168b02011-06-15 23:02:42 +00004785
4786 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4787 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4788 diag.Triggered = true;
4789}
4790
John McCall2ec85372012-05-07 06:16:41 +00004791void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4792 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004793 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004794 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004795
John McCall2ec85372012-05-07 06:16:41 +00004796 // When delaying diagnostics to run in the context of a parsed
4797 // declaration, we only want to actually emit anything if parsing
4798 // succeeds.
4799 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004800
John McCall2ec85372012-05-07 06:16:41 +00004801 // We emit all the active diagnostics in this pool or any of its
4802 // parents. In general, we'll get one pool for the decl spec
4803 // and a child pool for each declarator; in a decl group like:
4804 // deprecated_typedef foo, *bar, baz();
4805 // only the declarator pops will be passed decls. This is correct;
4806 // we really do need to consider delayed diagnostics from the decl spec
4807 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004808 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004809 do {
John McCall6347b682012-05-07 06:16:58 +00004810 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004811 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4812 // This const_cast is a bit lame. Really, Triggered should be mutable.
4813 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004814 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004815 continue;
4816
John McCallc1465822011-02-14 07:13:47 +00004817 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004818 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004819 case DelayedDiagnostic::Unavailable:
4820 // Don't bother giving deprecation/unavailable diagnostics if
4821 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004822 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004823 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004824 break;
4825
4826 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004827 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004828 break;
John McCall31168b02011-06-15 23:02:42 +00004829
4830 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004831 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004832 break;
John McCall86121512010-01-27 03:50:35 +00004833 }
4834 }
John McCall2ec85372012-05-07 06:16:41 +00004835 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004836}
4837
John McCall6347b682012-05-07 06:16:58 +00004838/// Given a set of delayed diagnostics, re-emit them as if they had
4839/// been delayed in the current context instead of in the given pool.
4840/// Essentially, this just moves them to the current pool.
4841void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4842 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4843 assert(curPool && "re-emitting in undelayed context not supported");
4844 curPool->steal(pool);
4845}
4846
John McCall28a6aea2009-11-04 02:18:39 +00004847static bool isDeclDeprecated(Decl *D) {
4848 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004849 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004850 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004851 // A category implicitly has the availability of the interface.
4852 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4853 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004854 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4855 return false;
4856}
4857
Ted Kremenekb79ee572013-12-18 23:30:06 +00004858static bool isDeclUnavailable(Decl *D) {
4859 do {
4860 if (D->isUnavailable())
4861 return true;
4862 // A category implicitly has the availability of the interface.
4863 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4864 return CatD->getClassInterface()->isUnavailable();
4865 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4866 return false;
4867}
4868
Eli Friedman971bfa12012-08-08 21:52:41 +00004869static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004870DoEmitAvailabilityWarning(Sema &S,
4871 DelayedDiagnostic::DDKind K,
4872 Decl *Ctx,
4873 const NamedDecl *D,
4874 StringRef Message,
4875 SourceLocation Loc,
4876 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004877 const ObjCPropertyDecl *ObjCProperty,
4878 bool ObjCPropertyAccess) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004879
4880 // Diagnostics for deprecated or unavailable.
4881 unsigned diag, diag_message, diag_fwdclass_message;
4882
4883 // Matches 'diag::note_property_attribute' options.
4884 unsigned property_note_select;
4885
4886 // Matches diag::note_availability_specified_here.
4887 unsigned available_here_select_kind;
4888
4889 // Don't warn if our current context is deprecated or unavailable.
4890 switch (K) {
4891 case DelayedDiagnostic::Deprecation:
4892 if (isDeclDeprecated(Ctx))
4893 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004894 diag = !ObjCPropertyAccess ? diag::warn_deprecated
4895 : diag::warn_property_method_deprecated;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004896 diag_message = diag::warn_deprecated_message;
4897 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4898 property_note_select = /* deprecated */ 0;
4899 available_here_select_kind = /* deprecated */ 2;
4900 break;
4901
4902 case DelayedDiagnostic::Unavailable:
4903 if (isDeclUnavailable(Ctx))
4904 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004905 diag = !ObjCPropertyAccess ? diag::err_unavailable
4906 : diag::err_property_method_unavailable;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004907 diag_message = diag::err_unavailable_message;
4908 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4909 property_note_select = /* unavailable */ 1;
4910 available_here_select_kind = /* unavailable */ 0;
4911 break;
4912
4913 default:
4914 llvm_unreachable("Neither a deprecation or unavailable kind");
4915 }
4916
Eli Friedman971bfa12012-08-08 21:52:41 +00004917 DeclarationName Name = D->getDeclName();
4918 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004919 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004920 if (ObjCProperty)
4921 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4922 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004923 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004924 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004925 if (ObjCProperty)
4926 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4927 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004928 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004929 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004930 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4931 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004932
4933 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4934 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004935}
4936
Ted Kremenekb79ee572013-12-18 23:30:06 +00004937void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4938 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004939 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004940 DoEmitAvailabilityWarning(*this,
4941 (DelayedDiagnostic::DDKind) DD.Kind,
4942 Ctx,
4943 DD.getDeprecationDecl(),
4944 DD.getDeprecationMessage(),
4945 DD.Loc,
4946 DD.getUnknownObjCClass(),
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004947 DD.getObjCProperty(), false);
John McCall28a6aea2009-11-04 02:18:39 +00004948}
4949
Ted Kremenekb79ee572013-12-18 23:30:06 +00004950void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4951 NamedDecl *D, StringRef Message,
4952 SourceLocation Loc,
4953 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004954 const ObjCPropertyDecl *ObjCProperty,
4955 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00004956 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004957 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004958 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4959 UnknownObjCClass,
4960 ObjCProperty,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004961 Message,
4962 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00004963 return;
4964 }
4965
Ted Kremenekb79ee572013-12-18 23:30:06 +00004966 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4967 DelayedDiagnostic::DDKind K;
4968 switch (AD) {
4969 case AD_Deprecation:
4970 K = DelayedDiagnostic::Deprecation;
4971 break;
4972 case AD_Unavailable:
4973 K = DelayedDiagnostic::Unavailable;
4974 break;
4975 }
4976
4977 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004978 UnknownObjCClass, ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00004979}