blob: 9cace20ee190d22070d52413fa7a67bad5853fbd [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"
Hal Finkelee90a222014-09-26 05:04:30 +000032#include "llvm/Support/MathExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000034using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000037 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000038 C,
39 Cpp,
40 ObjC
41 };
42}
43
Chris Lattner58418ff2008-06-29 00:16:31 +000044//===----------------------------------------------------------------------===//
45// Helper functions
46//===----------------------------------------------------------------------===//
47
Ted Kremenek527042b2009-08-14 20:49:40 +000048/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000049/// type (function or function-typed variable) or an Objective-C
50/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000051static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000052 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000053}
54
John McCall3882ace2011-01-05 12:14:39 +000055/// Return true if the given decl has a declarator that should have
56/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000058 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
60 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000061}
62
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000063/// hasFunctionProto - Return true if the given decl has a argument
64/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000065/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000066static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000067 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000068 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000069 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000070}
71
Alp Toker601b22c2014-01-21 23:35:24 +000072/// getFunctionOrMethodNumParams - Return number of function or method
73/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000074/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000075static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000076 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000077 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000079 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000080 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000081}
82
Alp Toker601b22c2014-01-21 23:35:24 +000083static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000084 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000085 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000086 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000087 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000088
Alp Toker03376dc2014-07-07 09:02:20 +000089 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000090}
91
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000092static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
93 if (const auto *FD = dyn_cast<FunctionDecl>(D))
94 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +000095 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000096 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +000097 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000098 return BD->getParamDecl(Idx)->getSourceRange();
99 return SourceRange();
100}
101
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000102static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000103 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000104 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000105 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000106}
107
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000108static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
109 if (const auto *FD = dyn_cast<FunctionDecl>(D))
110 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000111 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000112 return MD->getReturnTypeSourceRange();
113 return SourceRange();
114}
115
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000116static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000117 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000119 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000120 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000121 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
122 return BD->isVariadic();
123
124 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000125}
126
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000127static bool isInstanceMethod(const Decl *D) {
128 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000129 return MethodDecl->isInstance();
130 return false;
131}
132
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000133static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000134 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000135 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000136 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
John McCall96fa4842010-05-17 21:00:27 +0000138 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
139 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000140 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000141
John McCall96fa4842010-05-17 21:00:27 +0000142 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000143
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000144 // FIXME: Should we walk the chain of classes?
145 return ClsName == &Ctx.Idents.get("NSString") ||
146 ClsName == &Ctx.Idents.get("NSMutableString");
147}
148
Daniel Dunbar980c6692008-09-26 03:32:58 +0000149static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000150 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000151 if (!PT)
152 return false;
153
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000154 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000155 if (!RT)
156 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000157
Daniel Dunbar980c6692008-09-26 03:32:58 +0000158 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000159 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000160 return false;
161
162 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
163}
164
Richard Smithb87c4652013-10-31 21:23:20 +0000165static unsigned getNumAttributeArgs(const AttributeList &Attr) {
166 // FIXME: Include the type in the argument list.
167 return Attr.getNumArgs() + Attr.hasParsedType();
168}
169
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000170template <typename Compare>
171static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
172 unsigned Num, unsigned Diag,
173 Compare Comp) {
174 if (Comp(getNumAttributeArgs(Attr), Num)) {
175 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000176 return false;
177 }
178
179 return true;
180}
181
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000182/// \brief Check if the attribute has exactly as many args as Num. May
183/// output an error.
184static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
185 unsigned Num) {
186 return checkAttributeNumArgsImpl(S, Attr, Num,
187 diag::err_attribute_wrong_number_arguments,
188 std::not_equal_to<unsigned>());
189}
190
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000191/// \brief Check if the attribute has at least as many args as Num. May
192/// output an error.
193static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000194 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000195 return checkAttributeNumArgsImpl(S, Attr, Num,
196 diag::err_attribute_too_few_arguments,
197 std::less<unsigned>());
198}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000199
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000200/// \brief Check if the attribute has at most as many args as Num. May
201/// output an error.
202static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
203 unsigned Num) {
204 return checkAttributeNumArgsImpl(S, Attr, Num,
205 diag::err_attribute_too_many_arguments,
206 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000207}
208
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000209/// \brief If Expr is a valid integer constant, get the value of the integer
210/// expression and return success or failure. May output an error.
211static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
212 const Expr *Expr, uint32_t &Val,
213 unsigned Idx = UINT_MAX) {
214 llvm::APSInt I(32);
215 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
216 !Expr->isIntegerConstantExpr(I, S.Context)) {
217 if (Idx != UINT_MAX)
218 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
219 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
220 << Expr->getSourceRange();
221 else
222 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
223 << Attr.getName() << AANT_ArgumentIntegerConstant
224 << Expr->getSourceRange();
225 return false;
226 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000227
228 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000229 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
230 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000231 return false;
232 }
233
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000234 Val = (uint32_t)I.getZExtValue();
235 return true;
236}
237
Aaron Ballmanfb763042013-12-02 18:05:46 +0000238/// \brief Diagnose mutually exclusive attributes when present on a given
239/// declaration. Returns true if diagnosed.
240template <typename AttrTy>
241static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000242 const AttributeList &Attr) {
243 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000244 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000245 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000246 return true;
247 }
248 return false;
249}
250
Alp Toker601b22c2014-01-21 23:35:24 +0000251/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000252/// instance method D. May output an error.
253///
254/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000255static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
256 const AttributeList &Attr,
257 unsigned AttrArgNum,
258 const Expr *IdxExpr,
259 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000260 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000261
262 // In C++ the implicit 'this' function parameter also counts.
263 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000264 bool HP = hasFunctionProto(D);
265 bool HasImplicitThisParam = isInstanceMethod(D);
266 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000267 unsigned NumParams =
268 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000269
270 llvm::APSInt IdxInt;
271 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
272 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
274 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
275 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000276 return false;
277 }
278
279 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000280 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
282 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000283 return false;
284 }
285 Idx--; // Convert to zero-based.
286 if (HasImplicitThisParam) {
287 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000288 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000289 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000290 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 return false;
292 }
293 --Idx;
294 }
295
296 return true;
297}
298
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000299/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
300/// If not emit an error and return false. If the argument is an identifier it
301/// will emit an error with a fixit hint and treat it as if it was a string
302/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000303bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
304 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000305 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000306 // Look for identifiers. If we have one emit a hint to fix it to a literal.
307 if (Attr.isArgIdent(ArgNum)) {
308 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000309 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000310 << Attr.getName() << AANT_ArgumentString
311 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000312 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000313 Str = Loc->Ident->getName();
314 if (ArgLocation)
315 *ArgLocation = Loc->Loc;
316 return true;
317 }
318
319 // Now check for an actual string literal.
320 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
321 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
322 if (ArgLocation)
323 *ArgLocation = ArgExpr->getLocStart();
324
325 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000326 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000327 << Attr.getName() << AANT_ArgumentString;
328 return false;
329 }
330
331 Str = Literal->getString();
332 return true;
333}
334
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000335/// \brief Applies the given attribute to the Decl without performing any
336/// additional semantic checking.
337template <typename AttrType>
338static void handleSimpleAttribute(Sema &S, Decl *D,
339 const AttributeList &Attr) {
340 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
341 Attr.getAttributeSpellingListIndex()));
342}
343
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000344/// \brief Check if the passed-in expression is of type int or bool.
345static bool isIntOrBool(Expr *Exp) {
346 QualType QT = Exp->getType();
347 return QT->isBooleanType() || QT->isIntegerType();
348}
349
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000350
351// Check to see if the type is a smart pointer of some kind. We assume
352// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000353static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
354 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
355 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000356 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000357 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000358
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000359 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
360 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000361 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000362 return false;
363
364 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000365}
366
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000367/// \brief Check if passed in Decl is a pointer type.
368/// Note that this function may produce an error message.
369/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000370static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
371 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000372 const ValueDecl *vd = cast<ValueDecl>(D);
373 QualType QT = vd->getType();
374 if (QT->isAnyPointerType())
375 return true;
376
377 if (const RecordType *RT = QT->getAs<RecordType>()) {
378 // If it's an incomplete type, it could be a smart pointer; skip it.
379 // (We don't want to force template instantiation if we can avoid it,
380 // since that would alter the order in which templates are instantiated.)
381 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000382 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000383
Aaron Ballman553e6812013-12-26 14:54:11 +0000384 if (threadSafetyCheckIsSmartPointer(S, RT))
385 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000386 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000387
388 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000389 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000390 return false;
391}
392
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000393/// \brief Checks that the passed in QualType either is of RecordType or points
394/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000395static const RecordType *getRecordType(QualType QT) {
396 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000397 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000398
399 // Now check if we point to record type.
400 if (const PointerType *PT = QT->getAs<PointerType>())
401 return PT->getPointeeType()->getAs<RecordType>();
402
Craig Topperc3ec1492014-05-26 06:22:03 +0000403 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000404}
405
Aaron Ballman76050722014-04-04 15:13:57 +0000406static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000407 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000408
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000409 if (!RT)
410 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000411
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000412 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000413 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000414 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000415
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000416 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000417 // FIXME -- Check the type that the smart pointer points to.
418 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000419 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000420
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000421 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000422 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000423 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000424 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000425
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000426 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000427 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
428 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000429 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
430 void *) {
431 return BS->getType()->getAs<RecordType>()
432 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000433 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000434 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000435 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000436 return false;
437}
438
Aaron Ballman76050722014-04-04 15:13:57 +0000439static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000440 const auto *TD = Ty->getAs<TypedefType>();
441 if (!TD)
442 return false;
443
444 TypedefNameDecl *TN = TD->getDecl();
445 if (!TN)
446 return false;
447
448 return TN->hasAttr<CapabilityAttr>();
449}
450
Aaron Ballman76050722014-04-04 15:13:57 +0000451static bool typeHasCapability(Sema &S, QualType Ty) {
452 if (checkTypedefTypeForCapability(Ty))
453 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000454
Aaron Ballman76050722014-04-04 15:13:57 +0000455 if (checkRecordTypeForCapability(S, Ty))
456 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000457
Aaron Ballman76050722014-04-04 15:13:57 +0000458 return false;
459}
460
461static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
462 // Capability expressions are simple expressions involving the boolean logic
463 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
464 // a DeclRefExpr is found, its type should be checked to determine whether it
465 // is a capability or not.
466
467 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
468 return typeHasCapability(S, E->getType());
469 else if (const auto *E = dyn_cast<CastExpr>(Ex))
470 return isCapabilityExpr(S, E->getSubExpr());
471 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
472 return isCapabilityExpr(S, E->getSubExpr());
473 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
474 if (E->getOpcode() == UO_LNot)
475 return isCapabilityExpr(S, E->getSubExpr());
476 return false;
477 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
478 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
479 return isCapabilityExpr(S, E->getLHS()) &&
480 isCapabilityExpr(S, E->getRHS());
481 return false;
482 }
483
484 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000485}
486
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000487/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
488/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000489/// \param Sidx The attribute argument index to start checking with.
490/// \param ParamIdxOk Whether an argument can be indexing into a function
491/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000492static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
493 const AttributeList &Attr,
494 SmallVectorImpl<Expr *> &Args,
495 int Sidx = 0,
496 bool ParamIdxOk = false) {
497 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000498 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000499
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000500 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000501 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000502 Args.push_back(ArgExp);
503 continue;
504 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000507 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000508 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000509 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000510 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000511 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000512 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000513 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000514
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000515 // We allow constant strings to be used as a placeholder for expressions
516 // that are not valid C++ syntax, but warn that they are ignored.
517 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
518 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000519 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 continue;
521 }
522
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000523 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000525 // A pointer to member expression of the form &MyClass::mu is treated
526 // specially -- we need to look at the type of the member.
527 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
528 if (UOp->getOpcode() == UO_AddrOf)
529 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
530 if (DRE->getDecl()->isCXXInstanceMember())
531 ArgTy = DRE->getDecl()->getType();
532
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000533 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000534 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000535
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000536 // Now check if we index into a record type function param.
537 if(!RT && ParamIdxOk) {
538 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000539 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
540 if(FD && IL) {
541 unsigned int NumParams = FD->getNumParams();
542 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000543 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
544 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
545 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
547 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000550 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000551 }
552 }
553
Aaron Ballman76050722014-04-04 15:13:57 +0000554 // If the type does not have a capability, see if the components of the
555 // expression have capabilities. This allows for writing C code where the
556 // capability may be on the type, and the expression is a capability
557 // boolean logic expression. Eg) requires_capability(A || B && !C)
558 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
559 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
560 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000561
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000562 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000563 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000564}
565
Chris Lattner58418ff2008-06-29 00:16:31 +0000566//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000567// Attribute Implementations
568//===----------------------------------------------------------------------===//
569
Michael Hana9171bc2012-08-03 17:40:43 +0000570static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000571 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000572 if (!threadSafetyCheckIsPointer(S, D, Attr))
573 return;
574
Michael Han99315932013-01-24 16:46:58 +0000575 D->addAttr(::new (S.Context)
576 PtGuardedVarAttr(Attr.getRange(), S.Context,
577 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
581 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000582 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000583 SmallVector<Expr*, 1> Args;
584 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000585 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000586 unsigned Size = Args.size();
587 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000588 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000589
Michael Han3be3b442012-07-23 18:48:41 +0000590 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000591
Michael Han3be3b442012-07-23 18:48:41 +0000592 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000593}
594
Michael Han3be3b442012-07-23 18:48:41 +0000595static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000596 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000597 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
598 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000599
Aaron Ballman36a53502014-01-16 13:03:14 +0000600 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
601 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000602}
603
Michael Hana9171bc2012-08-03 17:40:43 +0000604static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000605 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000606 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000607 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
608 return;
609
610 if (!threadSafetyCheckIsPointer(S, D, Attr))
611 return;
612
613 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000614 S.Context, Arg,
615 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000616}
617
Michael Hana9171bc2012-08-03 17:40:43 +0000618static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
619 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000620 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000621 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000622 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000623
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000624 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000625 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000626 if (!QT->isDependentType()) {
627 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000628 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000629 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000630 << Attr.getName();
631 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000632 }
633 }
634
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000635 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000636 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000637 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000638 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000639
Michael Han3be3b442012-07-23 18:48:41 +0000640 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000641}
642
Michael Hana9171bc2012-08-03 17:40:43 +0000643static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000644 const AttributeList &Attr) {
645 SmallVector<Expr*, 1> Args;
646 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
647 return;
648
649 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000650 D->addAttr(::new (S.Context)
651 AcquiredAfterAttr(Attr.getRange(), S.Context,
652 StartArg, Args.size(),
653 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000654}
655
Michael Hana9171bc2012-08-03 17:40:43 +0000656static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000657 const AttributeList &Attr) {
658 SmallVector<Expr*, 1> Args;
659 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
660 return;
661
662 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000663 D->addAttr(::new (S.Context)
664 AcquiredBeforeAttr(Attr.getRange(), S.Context,
665 StartArg, Args.size(),
666 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000667}
668
Michael Hana9171bc2012-08-03 17:40:43 +0000669static bool checkLockFunAttrCommon(Sema &S, Decl *D,
670 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000671 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000672 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000673 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000674 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000675
Michael Han3be3b442012-07-23 18:48:41 +0000676 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000677}
678
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000679static void handleAssertSharedLockAttr(Sema &S, Decl *D,
680 const AttributeList &Attr) {
681 SmallVector<Expr*, 1> Args;
682 if (!checkLockFunAttrCommon(S, D, Attr, Args))
683 return;
684
685 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000686 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000687 D->addAttr(::new (S.Context)
688 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
689 Attr.getAttributeSpellingListIndex()));
690}
691
692static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
693 const AttributeList &Attr) {
694 SmallVector<Expr*, 1> Args;
695 if (!checkLockFunAttrCommon(S, D, Attr, Args))
696 return;
697
698 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000699 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000700 D->addAttr(::new (S.Context)
701 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
702 StartArg, Size,
703 Attr.getAttributeSpellingListIndex()));
704}
705
706
Michael Hana9171bc2012-08-03 17:40:43 +0000707static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
708 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000709 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000710 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000711 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000712
Aaron Ballman00e99962013-08-31 01:11:41 +0000713 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000714 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000715 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000716 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000717 }
718
719 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000720 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000721
Michael Han3be3b442012-07-23 18:48:41 +0000722 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000723}
724
Michael Hana9171bc2012-08-03 17:40:43 +0000725static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000726 const AttributeList &Attr) {
727 SmallVector<Expr*, 2> Args;
728 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
729 return;
730
Michael Han99315932013-01-24 16:46:58 +0000731 D->addAttr(::new (S.Context)
732 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000733 Attr.getArgAsExpr(0),
734 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000735 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000736}
737
Michael Hana9171bc2012-08-03 17:40:43 +0000738static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000739 const AttributeList &Attr) {
740 SmallVector<Expr*, 2> Args;
741 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
742 return;
743
Michael Han99315932013-01-24 16:46:58 +0000744 D->addAttr(::new (S.Context)
745 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000746 Attr.getArgAsExpr(0),
747 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000748 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000749}
750
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000751static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000752 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000753 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000754 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000755 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000756 unsigned Size = Args.size();
757 if (Size == 0)
758 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000759
Michael Han99315932013-01-24 16:46:58 +0000760 D->addAttr(::new (S.Context)
761 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
762 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000763}
764
765static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000766 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000767 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000768 return;
769
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000770 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000771 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000772 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000773 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000774 if (Size == 0)
775 return;
776 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000777
Michael Han99315932013-01-24 16:46:58 +0000778 D->addAttr(::new (S.Context)
779 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
780 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000781}
782
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000783static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
784 Expr *Cond = Attr.getArgAsExpr(0);
785 if (!Cond->isTypeDependent()) {
786 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
787 if (Converted.isInvalid())
788 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000789 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000790 }
791
792 StringRef Msg;
793 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
794 return;
795
796 SmallVector<PartialDiagnosticAt, 8> Diags;
797 if (!Cond->isValueDependent() &&
798 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
799 Diags)) {
800 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
801 for (int I = 0, N = Diags.size(); I != N; ++I)
802 S.Diag(Diags[I].first, Diags[I].second);
803 return;
804 }
805
806 D->addAttr(::new (S.Context)
807 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
808 Attr.getAttributeSpellingListIndex()));
809}
810
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000811static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000812 ConsumableAttr::ConsumedState DefaultState;
813
814 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000815 IdentifierLoc *IL = Attr.getArgAsIdent(0);
816 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
817 DefaultState)) {
818 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
819 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000820 return;
821 }
David Blaikie16f76d22013-09-06 01:28:43 +0000822 } else {
823 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
824 << Attr.getName() << AANT_ArgumentIdentifier;
825 return;
826 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000827
828 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000829 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000830 Attr.getAttributeSpellingListIndex()));
831}
832
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000833
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000834static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
835 const AttributeList &Attr) {
836 ASTContext &CurrContext = S.getASTContext();
837 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
838
839 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
840 if (!RD->hasAttr<ConsumableAttr>()) {
841 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
842 RD->getNameAsString();
843
844 return false;
845 }
846 }
847
848 return true;
849}
850
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000851
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000852static void handleCallableWhenAttr(Sema &S, Decl *D,
853 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000854 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
855 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000856
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000857 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
858 return;
859
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000860 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
861 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
862 CallableWhenAttr::ConsumedState CallableState;
863
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000864 StringRef StateString;
865 SourceLocation Loc;
866 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
867 return;
868
869 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000870 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000871 S.Diag(Loc, diag::warn_attribute_type_not_supported)
872 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000873 return;
874 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000875
876 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000877 }
878
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000879 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000880 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
881 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000882}
883
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000884
DeLesley Hutchins69391772013-10-17 23:23:53 +0000885static void handleParamTypestateAttr(Sema &S, Decl *D,
886 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000887 ParamTypestateAttr::ConsumedState ParamState;
888
889 if (Attr.isArgIdent(0)) {
890 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
891 StringRef StateString = Ident->Ident->getName();
892
893 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
894 ParamState)) {
895 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
896 << Attr.getName() << StateString;
897 return;
898 }
899 } else {
900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
901 Attr.getName() << AANT_ArgumentIdentifier;
902 return;
903 }
904
905 // FIXME: This check is currently being done in the analysis. It can be
906 // enabled here only after the parser propagates attributes at
907 // template specialization definition, not declaration.
908 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
909 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
910 //
911 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
912 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
913 // ReturnType.getAsString();
914 // return;
915 //}
916
917 D->addAttr(::new (S.Context)
918 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
919 Attr.getAttributeSpellingListIndex()));
920}
921
922
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000923static void handleReturnTypestateAttr(Sema &S, Decl *D,
924 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000925 ReturnTypestateAttr::ConsumedState ReturnState;
926
927 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000928 IdentifierLoc *IL = Attr.getArgAsIdent(0);
929 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
930 ReturnState)) {
931 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
932 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000933 return;
934 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000935 } else {
936 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
937 Attr.getName() << AANT_ArgumentIdentifier;
938 return;
939 }
940
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000941 // FIXME: This check is currently being done in the analysis. It can be
942 // enabled here only after the parser propagates attributes at
943 // template specialization definition, not declaration.
944 //QualType ReturnType;
945 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000946 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
947 // ReturnType = Param->getType();
948 //
949 //} else if (const CXXConstructorDecl *Constructor =
950 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000951 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
952 //
953 //} else {
954 //
955 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
956 //}
957 //
958 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
959 //
960 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
961 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
962 // ReturnType.getAsString();
963 // return;
964 //}
965
966 D->addAttr(::new (S.Context)
967 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
968 Attr.getAttributeSpellingListIndex()));
969}
970
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000971
972static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000973 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
974 return;
975
976 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000977 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000978 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
979 StringRef Param = Ident->Ident->getName();
980 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
981 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
982 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000983 return;
984 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000985 } else {
986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
987 Attr.getName() << AANT_ArgumentIdentifier;
988 return;
989 }
990
991 D->addAttr(::new (S.Context)
992 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
993 Attr.getAttributeSpellingListIndex()));
994}
995
Chris Wailes9385f9f2013-10-29 20:28:41 +0000996static void handleTestTypestateAttr(Sema &S, Decl *D,
997 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000998 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
999 return;
1000
Chris Wailes9385f9f2013-10-29 20:28:41 +00001001 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001002 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001003 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1004 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001005 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001006 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1007 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001008 return;
1009 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001010 } else {
1011 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1012 Attr.getName() << AANT_ArgumentIdentifier;
1013 return;
1014 }
1015
1016 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001017 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001018 Attr.getAttributeSpellingListIndex()));
1019}
1020
Chandler Carruthedc2c642011-07-02 00:01:44 +00001021static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1022 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001023 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001024 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001025}
1026
Chandler Carruthedc2c642011-07-02 00:01:44 +00001027static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001028 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001029 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1030 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001031 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001032 // If the alignment is less than or equal to 8 bits, the packed attribute
1033 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001034 if (!FD->getType()->isDependentType() &&
1035 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001036 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001037 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001038 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001039 else
Michael Han99315932013-01-24 16:46:58 +00001040 FD->addAttr(::new (S.Context)
1041 PackedAttr(Attr.getRange(), S.Context,
1042 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001043 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001044 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001045}
1046
Ted Kremenek7fd17232011-09-29 07:02:25 +00001047static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1048 // The IBOutlet/IBOutletCollection attributes only apply to instance
1049 // variables or properties of Objective-C classes. The outlet must also
1050 // have an object reference type.
1051 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1052 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001053 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001054 << Attr.getName() << VD->getType() << 0;
1055 return false;
1056 }
1057 }
1058 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1059 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001060 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001061 << Attr.getName() << PD->getType() << 1;
1062 return false;
1063 }
1064 }
1065 else {
1066 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1067 return false;
1068 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001069
Ted Kremenek7fd17232011-09-29 07:02:25 +00001070 return true;
1071}
1072
Chandler Carruthedc2c642011-07-02 00:01:44 +00001073static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001074 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001075 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001076
Michael Han99315932013-01-24 16:46:58 +00001077 D->addAttr(::new (S.Context)
1078 IBOutletAttr(Attr.getRange(), S.Context,
1079 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001080}
1081
Chandler Carruthedc2c642011-07-02 00:01:44 +00001082static void handleIBOutletCollection(Sema &S, Decl *D,
1083 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001084
1085 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001086 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001087 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1088 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001089 return;
1090 }
1091
Ted Kremenek7fd17232011-09-29 07:02:25 +00001092 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001093 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001094
Richard Smithb1f9a282013-10-31 01:56:18 +00001095 ParsedType PT;
1096
1097 if (Attr.hasParsedType())
1098 PT = Attr.getTypeArg();
1099 else {
1100 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1101 S.getScopeForContext(D->getDeclContext()->getParent()));
1102 if (!PT) {
1103 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1104 return;
1105 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001106 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001107
Craig Topperc3ec1492014-05-26 06:22:03 +00001108 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001109 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1110 if (!QTLoc)
1111 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001112
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001113 // Diagnose use of non-object type in iboutletcollection attribute.
1114 // FIXME. Gnu attribute extension ignores use of builtin types in
1115 // attributes. So, __attribute__((iboutletcollection(char))) will be
1116 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001117 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001118 S.Diag(Attr.getLoc(),
1119 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1120 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001121 return;
1122 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001123
Michael Han99315932013-01-24 16:46:58 +00001124 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001125 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001126 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001127}
1128
Hal Finkelee90a222014-09-26 05:04:30 +00001129bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1130 if (RefOkay) {
1131 if (T->isReferenceType())
1132 return true;
1133 } else {
1134 T = T.getNonReferenceType();
1135 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001136
Hal Finkelee90a222014-09-26 05:04:30 +00001137 // The nonnull attribute, and other similar attributes, can be applied to a
1138 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001139 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001140 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1141 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001142 for (const auto *I : UD->fields()) {
1143 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001144 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1145 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001146 }
1147 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001148 }
1149
1150 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001151}
1152
Ted Kremenek9aedc152014-01-17 06:24:56 +00001153static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001154 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001155 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001156 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001157 if (!S.isValidPointerAttrType(T)) {
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001158 S.Diag(Attr.getLoc(), isReturnValue
1159 ? diag::warn_attribute_return_pointers_only
1160 : diag::warn_attribute_pointers_only)
Hal Finkelee90a222014-09-26 05:04:30 +00001161 << Attr.getName() << AttrParmRange << TypeRange;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001162 return false;
1163 }
1164 return true;
1165}
1166
Chandler Carruthedc2c642011-07-02 00:01:44 +00001167static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001168 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001169 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1170 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001171 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001172 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001173 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001174
1175 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001176 if (Idx < getFunctionOrMethodNumParams(D) &&
1177 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001178 Ex->getSourceRange(),
1179 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001180 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001181
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001182 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001183 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001184
1185 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001186 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1187 // check if the attribute came from a macro expansion or a template
1188 // instantiation.
1189 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1190 S.ActiveTemplateInstantiations.empty()) {
1191 bool AnyPointers = isFunctionOrMethodVariadic(D);
1192 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1193 I != E && !AnyPointers; ++I) {
1194 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001195 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001196 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001197 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001198
Richard Smith588bd9b2014-08-27 04:59:42 +00001199 if (!AnyPointers)
1200 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001201 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001202
Richard Smith588bd9b2014-08-27 04:59:42 +00001203 unsigned *Start = NonNullArgs.data();
1204 unsigned Size = NonNullArgs.size();
1205 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001206 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001207 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001208 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001209}
1210
Jordan Rosec9399072014-02-11 17:27:59 +00001211static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1212 const AttributeList &Attr) {
1213 if (Attr.getNumArgs() > 0) {
1214 if (D->getFunctionType()) {
1215 handleNonNullAttr(S, D, Attr);
1216 } else {
1217 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1218 << D->getSourceRange();
1219 }
1220 return;
1221 }
1222
1223 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001224 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1225 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001226 return;
1227
1228 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001229 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001230 Attr.getAttributeSpellingListIndex()));
1231}
1232
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001233static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1234 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001235 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001236 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1237 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001238 /* isReturnValue */ true))
1239 return;
1240
1241 D->addAttr(::new (S.Context)
1242 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1243 Attr.getAttributeSpellingListIndex()));
1244}
1245
Hal Finkelee90a222014-09-26 05:04:30 +00001246static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1247 const AttributeList &Attr) {
1248 Expr *E = Attr.getArgAsExpr(0),
1249 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1250 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1251 Attr.getAttributeSpellingListIndex());
1252}
1253
1254void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1255 Expr *OE, unsigned SpellingListIndex) {
1256 QualType ResultType = getFunctionOrMethodResultType(D);
1257 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1258
1259 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1260 SourceLocation AttrLoc = AttrRange.getBegin();
1261
1262 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1263 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1264 << &TmpAttr << AttrRange << SR;
1265 return;
1266 }
1267
1268 if (!E->isValueDependent()) {
1269 llvm::APSInt I(64);
1270 if (!E->isIntegerConstantExpr(I, Context)) {
1271 if (OE)
1272 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1273 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1274 << E->getSourceRange();
1275 else
1276 Diag(AttrLoc, diag::err_attribute_argument_type)
1277 << &TmpAttr << AANT_ArgumentIntegerConstant
1278 << E->getSourceRange();
1279 return;
1280 }
1281
1282 if (!I.isPowerOf2()) {
1283 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1284 << E->getSourceRange();
1285 return;
1286 }
1287 }
1288
1289 if (OE) {
1290 if (!OE->isValueDependent()) {
1291 llvm::APSInt I(64);
1292 if (!OE->isIntegerConstantExpr(I, Context)) {
1293 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1294 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1295 << OE->getSourceRange();
1296 return;
1297 }
1298 }
1299 }
1300
1301 D->addAttr(::new (Context)
1302 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1303}
1304
Chandler Carruthedc2c642011-07-02 00:01:44 +00001305static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001306 // This attribute must be applied to a function declaration. The first
1307 // argument to the attribute must be an identifier, the name of the resource,
1308 // for example: malloc. The following arguments must be argument indexes, the
1309 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001310 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001311 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001312 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001313
Aaron Ballman00e99962013-08-31 01:11:41 +00001314 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001315 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001316 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001317 return;
1318 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001319
Richard Smith852e9ce2013-11-27 01:46:48 +00001320 // Figure out our Kind.
1321 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001322 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001323 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001324
Richard Smith852e9ce2013-11-27 01:46:48 +00001325 // Check arguments.
1326 switch (K) {
1327 case OwnershipAttr::Takes:
1328 case OwnershipAttr::Holds:
1329 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001330 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1331 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001332 return;
1333 }
1334 break;
1335 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001336 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001337 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1338 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001339 return;
1340 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001341 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001342 }
1343
Richard Smith852e9ce2013-11-27 01:46:48 +00001344 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001345
1346 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001347 StringRef ModuleName = Module->getName();
1348 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1349 ModuleName.size() > 4) {
1350 ModuleName = ModuleName.drop_front(2).drop_back(2);
1351 Module = &S.PP.getIdentifierTable().get(ModuleName);
1352 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001353
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001354 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001355 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1356 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001357 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001358 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001359 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001360
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001361 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001362 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001363 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001364 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001365 case OwnershipAttr::Takes:
1366 case OwnershipAttr::Holds:
1367 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1368 Err = 0;
1369 break;
1370 case OwnershipAttr::Returns:
1371 if (!T->isIntegerType())
1372 Err = 1;
1373 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001374 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001375 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001376 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001377 << Ex->getSourceRange();
1378 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001379 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001380
1381 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001382 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001383 // Cannot have two ownership attributes of different kinds for the same
1384 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001385 if (I->getOwnKind() != K && I->args_end() !=
1386 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001387 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001388 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001389 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001390 } else if (K == OwnershipAttr::Returns &&
1391 I->getOwnKind() == OwnershipAttr::Returns) {
1392 // A returns attribute conflicts with any other returns attribute using
1393 // a different index. Note, diagnostic reporting is 1-based, but stored
1394 // argument indexes are 0-based.
1395 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1396 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1397 << *(I->args_begin()) + 1;
1398 if (I->args_size())
1399 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1400 << (unsigned)Idx + 1 << Ex->getSourceRange();
1401 return;
1402 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001403 }
1404 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001405 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001406 }
1407
1408 unsigned* start = OwnershipArgs.data();
1409 unsigned size = OwnershipArgs.size();
1410 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001411
Michael Han99315932013-01-24 16:46:58 +00001412 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001413 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001414 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001415}
1416
Chandler Carruthedc2c642011-07-02 00:01:44 +00001417static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001418 // Check the attribute arguments.
1419 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1421 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001422 return;
1423 }
1424
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001425 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001426
Rafael Espindolac18086a2010-02-23 22:00:30 +00001427 // gcc rejects
1428 // class c {
1429 // static int a __attribute__((weakref ("v2")));
1430 // static int b() __attribute__((weakref ("f3")));
1431 // };
1432 // and ignores the attributes of
1433 // void f(void) {
1434 // static int a __attribute__((weakref ("v2")));
1435 // }
1436 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001437 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001438 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001439 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1440 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001441 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001442 }
1443
1444 // The GCC manual says
1445 //
1446 // At present, a declaration to which `weakref' is attached can only
1447 // be `static'.
1448 //
1449 // It also says
1450 //
1451 // Without a TARGET,
1452 // given as an argument to `weakref' or to `alias', `weakref' is
1453 // equivalent to `weak'.
1454 //
1455 // gcc 4.4.1 will accept
1456 // int a7 __attribute__((weakref));
1457 // as
1458 // int a7 __attribute__((weak));
1459 // This looks like a bug in gcc. We reject that for now. We should revisit
1460 // it if this behaviour is actually used.
1461
Rafael Espindolac18086a2010-02-23 22:00:30 +00001462 // GCC rejects
1463 // static ((alias ("y"), weakref)).
1464 // Should we? How to check that weakref is before or after alias?
1465
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001466 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1467 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1468 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001469 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001470 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001471 // GCC will accept anything as the argument of weakref. Should we
1472 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001473 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1474 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001475
Michael Han99315932013-01-24 16:46:58 +00001476 D->addAttr(::new (S.Context)
1477 WeakRefAttr(Attr.getRange(), S.Context,
1478 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001479}
1480
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001481static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1482 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001483 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001484 return;
1485
Douglas Gregore8bbc122011-09-02 00:18:52 +00001486 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001487 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1488 return;
1489 }
1490
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001491 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001492
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001493 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001494 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001495}
1496
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001497static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001498 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001499 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001500
Michael Han99315932013-01-24 16:46:58 +00001501 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1502 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001503}
1504
1505static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001506 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001507 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001508
Michael Han99315932013-01-24 16:46:58 +00001509 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1510 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001511}
1512
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001513static void handleTLSModelAttr(Sema &S, Decl *D,
1514 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001515 StringRef Model;
1516 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001517 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001518 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001519 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001520
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001521 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001522 if (Model != "global-dynamic" && Model != "local-dynamic"
1523 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001524 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001525 return;
1526 }
1527
Michael Han99315932013-01-24 16:46:58 +00001528 D->addAttr(::new (S.Context)
1529 TLSModelAttr(Attr.getRange(), S.Context, Model,
1530 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001531}
1532
Chandler Carruthedc2c642011-07-02 00:01:44 +00001533static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001534 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001535 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001536 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001537 D->addAttr(::new (S.Context)
1538 MallocAttr(Attr.getRange(), S.Context,
1539 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001540 return;
1541 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001542 }
1543
Ted Kremenek08479ae2009-08-15 00:51:46 +00001544 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001545}
1546
Chandler Carruthedc2c642011-07-02 00:01:44 +00001547static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001548 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001549 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1550 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001551 return;
1552 }
1553
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001554 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1555 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001556}
1557
Chandler Carruthedc2c642011-07-02 00:01:44 +00001558static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001559 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001560
1561 if (S.CheckNoReturnAttr(attr)) return;
1562
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001563 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001564 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001565 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001566 return;
1567 }
1568
Michael Han99315932013-01-24 16:46:58 +00001569 D->addAttr(::new (S.Context)
1570 NoReturnAttr(attr.getRange(), S.Context,
1571 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001572}
1573
1574bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001575 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001576 attr.setInvalid();
1577 return true;
1578 }
1579
1580 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001581}
1582
Chandler Carruthedc2c642011-07-02 00:01:44 +00001583static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1584 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001585
1586 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1587 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001588 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1589 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001590 if (!VD || (!VD->getType()->isBlockPointerType() &&
1591 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001592 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001593 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001594 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001595 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001596 return;
1597 }
1598 }
1599
Michael Han99315932013-01-24 16:46:58 +00001600 D->addAttr(::new (S.Context)
1601 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1602 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001603}
1604
John Thompsoncdb847ba2010-08-09 21:53:52 +00001605// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001606static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001607/*
1608 Returning a Vector Class in Registers
1609
Eric Christopherbc638a82010-12-01 22:13:54 +00001610 According to the PPU ABI specifications, a class with a single member of
1611 vector type is returned in memory when used as the return value of a function.
1612 This results in inefficient code when implementing vector classes. To return
1613 the value in a single vector register, add the vecreturn attribute to the
1614 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001615
1616 Example:
1617
1618 struct Vector
1619 {
1620 __vector float xyzw;
1621 } __attribute__((vecreturn));
1622
1623 Vector Add(Vector lhs, Vector rhs)
1624 {
1625 Vector result;
1626 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1627 return result; // This will be returned in a register
1628 }
1629*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001630 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1631 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001632 return;
1633 }
1634
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001635 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001636 int count = 0;
1637
1638 if (!isa<CXXRecordDecl>(record)) {
1639 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1640 return;
1641 }
1642
1643 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1644 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1645 return;
1646 }
1647
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001648 for (const auto *I : record->fields()) {
1649 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001650 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1651 return;
1652 }
1653 count++;
1654 }
1655
Michael Han99315932013-01-24 16:46:58 +00001656 D->addAttr(::new (S.Context)
1657 VecReturnAttr(Attr.getRange(), S.Context,
1658 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001659}
1660
Richard Smithe233fbf2013-01-28 22:42:45 +00001661static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1662 const AttributeList &Attr) {
1663 if (isa<ParmVarDecl>(D)) {
1664 // [[carries_dependency]] can only be applied to a parameter if it is a
1665 // parameter of a function declaration or lambda.
1666 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1667 S.Diag(Attr.getLoc(),
1668 diag::err_carries_dependency_param_not_function_decl);
1669 return;
1670 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001671 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001672
1673 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1674 Attr.getRange(), S.Context,
1675 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001676}
1677
Chandler Carruthedc2c642011-07-02 00:01:44 +00001678static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001679 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001680 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001681 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001682 return;
1683 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001684 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001685 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001686 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001687 return;
1688 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001689
Michael Han99315932013-01-24 16:46:58 +00001690 D->addAttr(::new (S.Context)
1691 UsedAttr(Attr.getRange(), S.Context,
1692 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001693}
1694
Chandler Carruthedc2c642011-07-02 00:01:44 +00001695static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001696 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001697 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001698 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1699 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001700
Michael Han99315932013-01-24 16:46:58 +00001701 D->addAttr(::new (S.Context)
1702 ConstructorAttr(Attr.getRange(), S.Context, priority,
1703 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001704}
1705
Chandler Carruthedc2c642011-07-02 00:01:44 +00001706static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001707 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001708 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001709 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1710 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001711
Michael Han99315932013-01-24 16:46:58 +00001712 D->addAttr(::new (S.Context)
1713 DestructorAttr(Attr.getRange(), S.Context, priority,
1714 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001715}
1716
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001717template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001718static void handleAttrWithMessage(Sema &S, Decl *D,
1719 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001720 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001721 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001722 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001723 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001724
Michael Han99315932013-01-24 16:46:58 +00001725 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1726 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001727}
1728
Ted Kremenek438f8db2014-02-22 01:06:05 +00001729static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001730 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001731 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001732 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1733 << Attr.getName() << Attr.getRange();
1734 return;
1735 }
1736
Ted Kremenek28eace62013-11-23 01:01:34 +00001737 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001738 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1739 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001740}
1741
Jordy Rose740b0c22012-05-08 03:27:22 +00001742static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1743 IdentifierInfo *Platform,
1744 VersionTuple Introduced,
1745 VersionTuple Deprecated,
1746 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001747 StringRef PlatformName
1748 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1749 if (PlatformName.empty())
1750 PlatformName = Platform->getName();
1751
1752 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1753 // of these steps are needed).
1754 if (!Introduced.empty() && !Deprecated.empty() &&
1755 !(Introduced <= Deprecated)) {
1756 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1757 << 1 << PlatformName << Deprecated.getAsString()
1758 << 0 << Introduced.getAsString();
1759 return true;
1760 }
1761
1762 if (!Introduced.empty() && !Obsoleted.empty() &&
1763 !(Introduced <= Obsoleted)) {
1764 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1765 << 2 << PlatformName << Obsoleted.getAsString()
1766 << 0 << Introduced.getAsString();
1767 return true;
1768 }
1769
1770 if (!Deprecated.empty() && !Obsoleted.empty() &&
1771 !(Deprecated <= Obsoleted)) {
1772 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1773 << 2 << PlatformName << Obsoleted.getAsString()
1774 << 1 << Deprecated.getAsString();
1775 return true;
1776 }
1777
1778 return false;
1779}
1780
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001781/// \brief Check whether the two versions match.
1782///
1783/// If either version tuple is empty, then they are assumed to match. If
1784/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1785static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1786 bool BeforeIsOkay) {
1787 if (X.empty() || Y.empty())
1788 return true;
1789
1790 if (X == Y)
1791 return true;
1792
1793 if (BeforeIsOkay && X < Y)
1794 return true;
1795
1796 return false;
1797}
1798
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001799AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001800 IdentifierInfo *Platform,
1801 VersionTuple Introduced,
1802 VersionTuple Deprecated,
1803 VersionTuple Obsoleted,
1804 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001805 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001806 bool Override,
1807 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001808 VersionTuple MergedIntroduced = Introduced;
1809 VersionTuple MergedDeprecated = Deprecated;
1810 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001811 bool FoundAny = false;
1812
Rafael Espindolac67f2232012-05-10 02:50:16 +00001813 if (D->hasAttrs()) {
1814 AttrVec &Attrs = D->getAttrs();
1815 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1816 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1817 if (!OldAA) {
1818 ++i;
1819 continue;
1820 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001821
Rafael Espindolac67f2232012-05-10 02:50:16 +00001822 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1823 if (OldPlatform != Platform) {
1824 ++i;
1825 continue;
1826 }
1827
1828 FoundAny = true;
1829 VersionTuple OldIntroduced = OldAA->getIntroduced();
1830 VersionTuple OldDeprecated = OldAA->getDeprecated();
1831 VersionTuple OldObsoleted = OldAA->getObsoleted();
1832 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001833
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001834 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1835 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1836 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1837 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001838 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001839 if (Override) {
1840 int Which = -1;
1841 VersionTuple FirstVersion;
1842 VersionTuple SecondVersion;
1843 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1844 Which = 0;
1845 FirstVersion = OldIntroduced;
1846 SecondVersion = Introduced;
1847 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1848 Which = 1;
1849 FirstVersion = Deprecated;
1850 SecondVersion = OldDeprecated;
1851 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1852 Which = 2;
1853 FirstVersion = Obsoleted;
1854 SecondVersion = OldObsoleted;
1855 }
1856
1857 if (Which == -1) {
1858 Diag(OldAA->getLocation(),
1859 diag::warn_mismatched_availability_override_unavail)
1860 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1861 } else {
1862 Diag(OldAA->getLocation(),
1863 diag::warn_mismatched_availability_override)
1864 << Which
1865 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1866 << FirstVersion.getAsString() << SecondVersion.getAsString();
1867 }
1868 Diag(Range.getBegin(), diag::note_overridden_method);
1869 } else {
1870 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1871 Diag(Range.getBegin(), diag::note_previous_attribute);
1872 }
1873
Rafael Espindolac67f2232012-05-10 02:50:16 +00001874 Attrs.erase(Attrs.begin() + i);
1875 --e;
1876 continue;
1877 }
1878
1879 VersionTuple MergedIntroduced2 = MergedIntroduced;
1880 VersionTuple MergedDeprecated2 = MergedDeprecated;
1881 VersionTuple MergedObsoleted2 = MergedObsoleted;
1882
1883 if (MergedIntroduced2.empty())
1884 MergedIntroduced2 = OldIntroduced;
1885 if (MergedDeprecated2.empty())
1886 MergedDeprecated2 = OldDeprecated;
1887 if (MergedObsoleted2.empty())
1888 MergedObsoleted2 = OldObsoleted;
1889
1890 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1891 MergedIntroduced2, MergedDeprecated2,
1892 MergedObsoleted2)) {
1893 Attrs.erase(Attrs.begin() + i);
1894 --e;
1895 continue;
1896 }
1897
1898 MergedIntroduced = MergedIntroduced2;
1899 MergedDeprecated = MergedDeprecated2;
1900 MergedObsoleted = MergedObsoleted2;
1901 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001902 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001903 }
1904
1905 if (FoundAny &&
1906 MergedIntroduced == Introduced &&
1907 MergedDeprecated == Deprecated &&
1908 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001909 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001910
Ted Kremenekb5445722013-04-06 00:34:27 +00001911 // Only create a new attribute if !Override, but we want to do
1912 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001913 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001914 MergedDeprecated, MergedObsoleted) &&
1915 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001916 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1917 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001918 Obsoleted, IsUnavailable, Message,
1919 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001920 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001921 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001922}
1923
Chandler Carruthedc2c642011-07-02 00:01:44 +00001924static void handleAvailabilityAttr(Sema &S, Decl *D,
1925 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001926 if (!checkAttributeNumArgs(S, Attr, 1))
1927 return;
1928 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001929 unsigned Index = Attr.getAttributeSpellingListIndex();
1930
Aaron Ballman00e99962013-08-31 01:11:41 +00001931 IdentifierInfo *II = Platform->Ident;
1932 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1933 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1934 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001935
Rafael Espindolac231fab2013-01-08 21:30:32 +00001936 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1937 if (!ND) {
1938 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1939 return;
1940 }
1941
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001942 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1943 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1944 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001945 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001946 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001947 if (const StringLiteral *SE =
1948 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001949 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001950
Aaron Ballman00e99962013-08-31 01:11:41 +00001951 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001952 Introduced.Version,
1953 Deprecated.Version,
1954 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001955 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001956 /*Override=*/false,
1957 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001958 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001959 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001960}
1961
John McCalld041a9b2013-02-20 01:54:26 +00001962template <class T>
1963static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1964 typename T::VisibilityType value,
1965 unsigned attrSpellingListIndex) {
1966 T *existingAttr = D->getAttr<T>();
1967 if (existingAttr) {
1968 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1969 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001970 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001971 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1972 S.Diag(range.getBegin(), diag::note_previous_attribute);
1973 D->dropAttr<T>();
1974 }
1975 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1976}
1977
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001978VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001979 VisibilityAttr::VisibilityType Vis,
1980 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001981 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1982 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001983}
1984
John McCalld041a9b2013-02-20 01:54:26 +00001985TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1986 TypeVisibilityAttr::VisibilityType Vis,
1987 unsigned AttrSpellingListIndex) {
1988 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1989 AttrSpellingListIndex);
1990}
1991
1992static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1993 bool isTypeVisibility) {
1994 // Visibility attributes don't mean anything on a typedef.
1995 if (isa<TypedefNameDecl>(D)) {
1996 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1997 << Attr.getName();
1998 return;
1999 }
2000
2001 // 'type_visibility' can only go on a type or namespace.
2002 if (isTypeVisibility &&
2003 !(isa<TagDecl>(D) ||
2004 isa<ObjCInterfaceDecl>(D) ||
2005 isa<NamespaceDecl>(D))) {
2006 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2007 << Attr.getName() << ExpectedTypeOrNamespace;
2008 return;
2009 }
2010
Benjamin Kramer70370212013-09-09 15:08:57 +00002011 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002012 StringRef TypeStr;
2013 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002014 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002015 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002016
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002017 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002018 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002019 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002020 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002021 return;
2022 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002023
2024 // Complain about attempts to use protected visibility on targets
2025 // (like Darwin) that don't support it.
2026 if (type == VisibilityAttr::Protected &&
2027 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2028 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2029 type = VisibilityAttr::Default;
2030 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002031
Michael Han99315932013-01-24 16:46:58 +00002032 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002033 clang::Attr *newAttr;
2034 if (isTypeVisibility) {
2035 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2036 (TypeVisibilityAttr::VisibilityType) type,
2037 Index);
2038 } else {
2039 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2040 }
2041 if (newAttr)
2042 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002043}
2044
Chandler Carruthedc2c642011-07-02 00:01:44 +00002045static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2046 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002047 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002048 if (!Attr.isArgIdent(0)) {
2049 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2050 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002051 return;
2052 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002053
Aaron Ballman682ee422013-09-11 19:47:58 +00002054 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2055 ObjCMethodFamilyAttr::FamilyKind F;
2056 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2057 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2058 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002059 return;
2060 }
2061
Alp Toker314cc812014-01-25 16:55:45 +00002062 if (F == ObjCMethodFamilyAttr::OMF_init &&
2063 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002064 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002065 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002066 // Ignore the attribute.
2067 return;
2068 }
2069
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002070 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002071 S.Context, F,
2072 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002073}
2074
Chandler Carruthedc2c642011-07-02 00:01:44 +00002075static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002076 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002077 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002078 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002079 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2080 return;
2081 }
2082 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002083 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2084 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002085 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002086 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2087 return;
2088 }
2089 }
2090 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002091 // It is okay to include this attribute on properties, e.g.:
2092 //
2093 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2094 //
2095 // In this case it follows tradition and suppresses an error in the above
2096 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002097 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002098 }
Michael Han99315932013-01-24 16:46:58 +00002099 D->addAttr(::new (S.Context)
2100 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2101 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002102}
2103
Chandler Carruthedc2c642011-07-02 00:01:44 +00002104static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002105 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002107 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002108 return;
2109 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002110
Aaron Ballman00e99962013-08-31 01:11:41 +00002111 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002112 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002113 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2114 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2115 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002116 return;
2117 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002118
Michael Han99315932013-01-24 16:46:58 +00002119 D->addAttr(::new (S.Context)
2120 BlocksAttr(Attr.getRange(), S.Context, type,
2121 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002122}
2123
Chandler Carruthedc2c642011-07-02 00:01:44 +00002124static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002125 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002126 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002127 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002128 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002129 if (E->isTypeDependent() || E->isValueDependent() ||
2130 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002131 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002132 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002133 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002134 return;
2135 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002136
John McCallb46f2872011-09-09 07:56:05 +00002137 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002138 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2139 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002140 return;
2141 }
John McCallb46f2872011-09-09 07:56:05 +00002142
2143 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002144 }
2145
Aaron Ballman18a78382013-11-21 00:28:23 +00002146 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002147 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002148 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002149 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002150 if (E->isTypeDependent() || E->isValueDependent() ||
2151 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002153 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002154 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002155 return;
2156 }
2157 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002158
John McCallb46f2872011-09-09 07:56:05 +00002159 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002160 // FIXME: This error message could be improved, it would be nice
2161 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002162 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2163 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002164 return;
2165 }
2166 }
2167
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002168 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002169 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002170 if (isa<FunctionNoProtoType>(FT)) {
2171 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2172 return;
2173 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002174
Chris Lattner9363e312009-03-17 23:03:47 +00002175 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002176 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002177 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002178 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002179 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002180 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002181 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002182 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002183 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002184 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2185 if (!BD->isVariadic()) {
2186 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2187 return;
2188 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002189 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002190 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002191 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002192 const FunctionType *FT = Ty->isFunctionPointerType()
2193 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002194 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002195 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002196 int m = Ty->isFunctionPointerType() ? 0 : 1;
2197 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002198 return;
2199 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002200 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002202 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002203 return;
2204 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002205 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002206 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002207 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002208 return;
2209 }
Michael Han99315932013-01-24 16:46:58 +00002210 D->addAttr(::new (S.Context)
2211 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2212 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002213}
2214
Chandler Carruthedc2c642011-07-02 00:01:44 +00002215static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002216 if (D->getFunctionType() &&
2217 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002218 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2219 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002220 return;
2221 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002222 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002223 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002224 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2225 << Attr.getName() << 1;
2226 return;
2227 }
2228
Michael Han99315932013-01-24 16:46:58 +00002229 D->addAttr(::new (S.Context)
2230 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2231 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002232}
2233
Chandler Carruthedc2c642011-07-02 00:01:44 +00002234static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002235 // weak_import only applies to variable & function declarations.
2236 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002237 if (!D->canBeWeakImported(isDef)) {
2238 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002239 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2240 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002241 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002242 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002243 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002244 // Nothing to warn about here.
2245 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002246 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002247 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002248
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002249 return;
2250 }
2251
Michael Han99315932013-01-24 16:46:58 +00002252 D->addAttr(::new (S.Context)
2253 WeakImportAttr(Attr.getRange(), S.Context,
2254 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002255}
2256
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002257// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002258template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002259static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002260 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002261 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002262 for (unsigned i = 0; i < 3; ++i) {
2263 const Expr *E = Attr.getArgAsExpr(i);
2264 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002265 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002266 if (WGSize[i] == 0) {
2267 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2268 << Attr.getName() << E->getSourceRange();
2269 return;
2270 }
2271 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002272
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002273 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2274 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2275 Existing->getYDim() == WGSize[1] &&
2276 Existing->getZDim() == WGSize[2]))
2277 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002278
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002279 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2280 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002281 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002282}
2283
Joey Goulyaba589c2013-03-08 09:42:32 +00002284static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002285 if (!Attr.hasParsedType()) {
2286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2287 << Attr.getName() << 1;
2288 return;
2289 }
2290
Craig Topperc3ec1492014-05-26 06:22:03 +00002291 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002292 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2293 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002294
2295 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2296 (ParmType->isBooleanType() ||
2297 !ParmType->isIntegralType(S.getASTContext()))) {
2298 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2299 << ParmType;
2300 return;
2301 }
2302
Aaron Ballmana9e05402013-12-02 22:16:55 +00002303 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002304 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002305 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2306 return;
2307 }
2308 }
2309
2310 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002311 ParmTSI,
2312 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002313}
2314
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002315SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002316 StringRef Name,
2317 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002318 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2319 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002320 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002321 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2322 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002323 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002324 }
Michael Han99315932013-01-24 16:46:58 +00002325 return ::new (Context) SectionAttr(Range, Context, Name,
2326 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002327}
2328
Chandler Carruthedc2c642011-07-02 00:01:44 +00002329static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002330 // Make sure that there is a string literal as the sections's single
2331 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002332 StringRef Str;
2333 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002334 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002335 return;
Mike Stump11289f42009-09-09 15:08:12 +00002336
Chris Lattner30ba6742009-08-10 19:03:04 +00002337 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002338 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002339 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002340 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002341 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002342 return;
2343 }
Mike Stump11289f42009-09-09 15:08:12 +00002344
Michael Han99315932013-01-24 16:46:58 +00002345 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002346 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002347 if (NewAttr)
2348 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002349}
2350
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002351
Chandler Carruthedc2c642011-07-02 00:01:44 +00002352static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002353 VarDecl *VD = cast<VarDecl>(D);
2354 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002355 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002356 return;
2357 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002358
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002359 Expr *E = Attr.getArgAsExpr(0);
2360 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002361 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002362 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002363
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002364 // gcc only allows for simple identifiers. Since we support more than gcc, we
2365 // will warn the user.
2366 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2367 if (DRE->hasQualifier())
2368 S.Diag(Loc, diag::warn_cleanup_ext);
2369 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2370 NI = DRE->getNameInfo();
2371 if (!FD) {
2372 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2373 << NI.getName();
2374 return;
2375 }
2376 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2377 if (ULE->hasExplicitTemplateArgs())
2378 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002379 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2380 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002381 if (!FD) {
2382 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2383 << NI.getName();
2384 if (ULE->getType() == S.Context.OverloadTy)
2385 S.NoteAllOverloadCandidates(ULE);
2386 return;
2387 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002388 } else {
2389 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002390 return;
2391 }
2392
Anders Carlssond277d792009-01-31 01:16:18 +00002393 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002394 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2395 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002396 return;
2397 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002398
Anders Carlsson723f55d2009-02-07 23:16:50 +00002399 // We're currently more strict than GCC about what function types we accept.
2400 // If this ever proves to be a problem it should be easy to fix.
2401 QualType Ty = S.Context.getPointerType(VD->getType());
2402 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002403 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2404 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002405 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2406 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002407 return;
2408 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002409
Michael Han99315932013-01-24 16:46:58 +00002410 D->addAttr(::new (S.Context)
2411 CleanupAttr(Attr.getRange(), S.Context, FD,
2412 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002413}
2414
Mike Stumpd3bb5572009-07-24 19:02:52 +00002415/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002416/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002417static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002418 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002419 uint64_t Idx;
2420 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002421 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002422
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002423 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002424 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002425
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002426 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2427 if (not_nsstring_type &&
2428 !isCFStringType(Ty, S.Context) &&
2429 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002430 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002431 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002432 << (not_nsstring_type ? "a string type" : "an NSString")
2433 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002434 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002435 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002436 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002437 if (!isNSStringType(Ty, S.Context) &&
2438 !isCFStringType(Ty, S.Context) &&
2439 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002440 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002441 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002442 << (not_nsstring_type ? "string type" : "NSString")
2443 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002444 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002445 }
2446
Alp Toker601b22c2014-01-21 23:35:24 +00002447 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002448 // because that has corrected for the implicit this parameter, and is zero-
2449 // based. The attribute expects what the user wrote explicitly.
2450 llvm::APSInt Val;
2451 IdxExpr->EvaluateAsInt(Val, S.Context);
2452
Michael Han99315932013-01-24 16:46:58 +00002453 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002454 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002455 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002456}
2457
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002458enum FormatAttrKind {
2459 CFStringFormat,
2460 NSStringFormat,
2461 StrftimeFormat,
2462 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002463 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002464 InvalidFormat
2465};
2466
2467/// getFormatAttrKind - Map from format attribute names to supported format
2468/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002469static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002470 return llvm::StringSwitch<FormatAttrKind>(Format)
2471 // Check for formats that get handled specially.
2472 .Case("NSString", NSStringFormat)
2473 .Case("CFString", CFStringFormat)
2474 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002475
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002476 // Otherwise, check for supported formats.
2477 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2478 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2479 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002480
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002481 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2482 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002483}
2484
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002485/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002486/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002487static void handleInitPriorityAttr(Sema &S, Decl *D,
2488 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002489 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002490 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2491 return;
2492 }
2493
Aaron Ballman4a611152013-11-27 16:34:09 +00002494 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002495 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2496 Attr.setInvalid();
2497 return;
2498 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002499 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002500 if (S.Context.getAsArrayType(T))
2501 T = S.Context.getBaseElementType(T);
2502 if (!T->getAs<RecordType>()) {
2503 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2504 Attr.setInvalid();
2505 return;
2506 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002507
2508 Expr *E = Attr.getArgAsExpr(0);
2509 uint32_t prioritynum;
2510 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002511 Attr.setInvalid();
2512 return;
2513 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002514
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002515 if (prioritynum < 101 || prioritynum > 65535) {
2516 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002517 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002518 Attr.setInvalid();
2519 return;
2520 }
Michael Han99315932013-01-24 16:46:58 +00002521 D->addAttr(::new (S.Context)
2522 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2523 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002524}
2525
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002526FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2527 IdentifierInfo *Format, int FormatIdx,
2528 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002529 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002530 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002531 for (auto *F : D->specific_attrs<FormatAttr>()) {
2532 if (F->getType() == Format &&
2533 F->getFormatIdx() == FormatIdx &&
2534 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002535 // If we don't have a valid location for this attribute, adopt the
2536 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002537 if (F->getLocation().isInvalid())
2538 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002539 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002540 }
2541 }
2542
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002543 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2544 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002545}
2546
Mike Stumpd3bb5572009-07-24 19:02:52 +00002547/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002548/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002549static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002550 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002552 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002553 return;
2554 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002555
Chandler Carruth743682b2010-11-16 08:35:43 +00002556 // In C++ the implicit 'this' function parameter also counts, and they are
2557 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002558 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002559 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002560
Aaron Ballman00e99962013-08-31 01:11:41 +00002561 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2562 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002563
2564 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002565 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002566 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002567 // If we've modified the string name, we need a new identifier for it.
2568 II = &S.Context.Idents.get(Format);
2569 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002570
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002571 // Check for supported formats.
2572 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002573
2574 if (Kind == IgnoredFormat)
2575 return;
2576
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002577 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002578 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002579 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002580 return;
2581 }
2582
2583 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002584 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002585 uint32_t Idx;
2586 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002588
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002589 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002591 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592 return;
2593 }
2594
2595 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002596 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002597
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002598 if (HasImplicitThisParam) {
2599 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002600 S.Diag(Attr.getLoc(),
2601 diag::err_format_attribute_implicit_this_format_string)
2602 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002603 return;
2604 }
2605 ArgIdx--;
2606 }
Mike Stump11289f42009-09-09 15:08:12 +00002607
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002609 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002611 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002612 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002613 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002614 << "a CFString" << IdxExpr->getSourceRange()
2615 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002616 return;
2617 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002618 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002619 // FIXME: do we need to check if the type is NSString*? What are the
2620 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002621 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002622 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002623 << "an NSString" << IdxExpr->getSourceRange()
2624 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002625 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002626 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002627 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002628 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002629 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002630 << "a string type" << IdxExpr->getSourceRange()
2631 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002632 return;
2633 }
2634
2635 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002636 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002637 uint32_t FirstArg;
2638 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002639 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002640
2641 // check if the function is variadic if the 3rd argument non-zero
2642 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002643 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644 ++NumArgs; // +1 for ...
2645 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002646 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002647 return;
2648 }
2649 }
2650
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002651 // strftime requires FirstArg to be 0 because it doesn't read from any
2652 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002653 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002654 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002655 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2656 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657 return;
2658 }
2659 // if 0 it disables parameter checking (to use with e.g. va_list)
2660 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002662 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002663 return;
2664 }
2665
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002666 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002667 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002668 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002669 if (NewAttr)
2670 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002671}
2672
Chandler Carruthedc2c642011-07-02 00:01:44 +00002673static void handleTransparentUnionAttr(Sema &S, Decl *D,
2674 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002675 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002676 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002677 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002678 if (TD && TD->getUnderlyingType()->isUnionType())
2679 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2680 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002681 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002682
2683 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002684 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002685 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002686 return;
2687 }
2688
John McCallf937c022011-10-07 06:10:15 +00002689 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002690 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002691 diag::warn_transparent_union_attribute_not_definition);
2692 return;
2693 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002694
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002695 RecordDecl::field_iterator Field = RD->field_begin(),
2696 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002697 if (Field == FieldEnd) {
2698 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2699 return;
2700 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002701
David Blaikie40ed2972012-06-06 20:45:41 +00002702 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002703 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002704 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002705 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002706 diag::warn_transparent_union_attribute_floating)
2707 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002708 return;
2709 }
2710
2711 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2712 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2713 for (; Field != FieldEnd; ++Field) {
2714 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002715 // FIXME: this isn't fully correct; we also need to test whether the
2716 // members of the union would all have the same calling convention as the
2717 // first member of the union. Checking just the size and alignment isn't
2718 // sufficient (consider structs passed on the stack instead of in registers
2719 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002720 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002721 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002722 // Warn if we drop the attribute.
2723 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002724 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002725 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002726 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002727 diag::warn_transparent_union_attribute_field_size_align)
2728 << isSize << Field->getDeclName() << FieldBits;
2729 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002730 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002731 diag::note_transparent_union_first_field_size_align)
2732 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002733 return;
2734 }
2735 }
2736
Michael Han99315932013-01-24 16:46:58 +00002737 RD->addAttr(::new (S.Context)
2738 TransparentUnionAttr(Attr.getRange(), S.Context,
2739 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740}
2741
Chandler Carruthedc2c642011-07-02 00:01:44 +00002742static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002743 // Make sure that there is a string literal as the annotation's single
2744 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002745 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002746 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002748
2749 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002750 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2751 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002752 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002753 }
Michael Han99315932013-01-24 16:46:58 +00002754
2755 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002756 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002757 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002758}
2759
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002760static void handleAlignValueAttr(Sema &S, Decl *D,
2761 const AttributeList &Attr) {
2762 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2763 Attr.getAttributeSpellingListIndex());
2764}
2765
2766void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2767 unsigned SpellingListIndex) {
2768 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2769 SourceLocation AttrLoc = AttrRange.getBegin();
2770
2771 QualType T;
2772 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2773 T = TD->getUnderlyingType();
2774 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2775 T = VD->getType();
2776 else
2777 llvm_unreachable("Unknown decl type for align_value");
2778
2779 if (!T->isDependentType() && !T->isAnyPointerType() &&
2780 !T->isReferenceType() && !T->isMemberPointerType()) {
2781 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2782 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2783 return;
2784 }
2785
2786 if (!E->isValueDependent()) {
2787 llvm::APSInt Alignment(32);
2788 ExprResult ICE
2789 = VerifyIntegerConstantExpression(E, &Alignment,
2790 diag::err_align_value_attribute_argument_not_int,
2791 /*AllowFold*/ false);
2792 if (ICE.isInvalid())
2793 return;
2794
2795 if (!Alignment.isPowerOf2()) {
2796 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2797 << E->getSourceRange();
2798 return;
2799 }
2800
2801 D->addAttr(::new (Context)
2802 AlignValueAttr(AttrRange, Context, ICE.get(),
2803 SpellingListIndex));
2804 return;
2805 }
2806
2807 // Save dependent expressions in the AST to be instantiated.
2808 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
2809 return;
2810}
2811
Chandler Carruthedc2c642011-07-02 00:01:44 +00002812static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002813 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002814 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2816 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002817 return;
2818 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002819
Richard Smith848e1f12013-02-01 08:12:08 +00002820 if (Attr.getNumArgs() == 0) {
2821 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002822 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002823 return;
2824 }
2825
Aaron Ballman00e99962013-08-31 01:11:41 +00002826 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002827 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2828 S.Diag(Attr.getEllipsisLoc(),
2829 diag::err_pack_expansion_without_parameter_packs);
2830 return;
2831 }
2832
2833 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2834 return;
2835
2836 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2837 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002838}
2839
2840void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002841 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002842 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2843 SourceLocation AttrLoc = AttrRange.getBegin();
2844
Richard Smith1dba27c2013-01-29 09:02:09 +00002845 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002846 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002847 // C++11 [dcl.align]p1:
2848 // An alignment-specifier may be applied to a variable or to a class
2849 // data member, but it shall not be applied to a bit-field, a function
2850 // parameter, the formal parameter of a catch clause, or a variable
2851 // declared with the register storage class specifier. An
2852 // alignment-specifier may also be applied to the declaration of a class
2853 // or enumeration type.
2854 // C11 6.7.5/2:
2855 // An alignment attribute shall not be specified in a declaration of
2856 // a typedef, or a bit-field, or a function, or a parameter, or an
2857 // object declared with the register storage-class specifier.
2858 int DiagKind = -1;
2859 if (isa<ParmVarDecl>(D)) {
2860 DiagKind = 0;
2861 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2862 if (VD->getStorageClass() == SC_Register)
2863 DiagKind = 1;
2864 if (VD->isExceptionVariable())
2865 DiagKind = 2;
2866 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2867 if (FD->isBitField())
2868 DiagKind = 3;
2869 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002870 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002871 << (TmpAttr.isC11() ? ExpectedVariableOrField
2872 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002873 return;
2874 }
2875 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002876 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002877 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002878 return;
2879 }
2880 }
2881
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002882 if (E->isTypeDependent() || E->isValueDependent()) {
2883 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002884 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2885 AA->setPackExpansion(IsPackExpansion);
2886 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002887 return;
2888 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002889
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002890 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002891 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002892 ExprResult ICE
2893 = VerifyIntegerConstantExpression(E, &Alignment,
2894 diag::err_aligned_attribute_argument_not_int,
2895 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002896 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002897 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002898
2899 // C++11 [dcl.align]p2:
2900 // -- if the constant expression evaluates to zero, the alignment
2901 // specifier shall have no effect
2902 // C11 6.7.5p6:
2903 // An alignment specification of zero has no effect.
2904 if (!(TmpAttr.isAlignas() && !Alignment) &&
2905 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Hal Finkelbcc06082014-09-07 22:58:14 +00002906 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002907 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002908 return;
2909 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002910
David Majnemerabecae72014-02-12 20:36:10 +00002911 // Alignment calculations can wrap around if it's greater than 2**28.
2912 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2913 if (Alignment.getZExtValue() > MaxValidAlignment) {
2914 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2915 << E->getSourceRange();
2916 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002917 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002918
Richard Smith44c247f2013-02-22 08:32:16 +00002919 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002920 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002921 AA->setPackExpansion(IsPackExpansion);
2922 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002923}
2924
Michael Hanaf02bbe2013-02-01 01:19:17 +00002925void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002926 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002927 // FIXME: Cache the number on the Attr object if non-dependent?
2928 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002929 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2930 SpellingListIndex);
2931 AA->setPackExpansion(IsPackExpansion);
2932 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002933}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002934
Richard Smith848e1f12013-02-01 08:12:08 +00002935void Sema::CheckAlignasUnderalignment(Decl *D) {
2936 assert(D->hasAttrs() && "no attributes on decl");
2937
2938 QualType Ty;
2939 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2940 Ty = VD->getType();
2941 else
2942 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002943 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002944 return;
2945
2946 // C++11 [dcl.align]p5, C11 6.7.5/4:
2947 // The combined effect of all alignment attributes in a declaration shall
2948 // not specify an alignment that is less strict than the alignment that
2949 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00002950 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00002951 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002952 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002953 if (I->isAlignmentDependent())
2954 return;
2955 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002956 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002957 Align = std::max(Align, I->getAlignment(Context));
2958 }
2959
2960 if (AlignasAttr && Align) {
2961 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2962 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2963 if (NaturalAlign > RequestedAlign)
2964 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2965 << Ty << (unsigned)NaturalAlign.getQuantity();
2966 }
2967}
2968
David Majnemer2c4e00a2014-01-29 22:07:36 +00002969bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002970 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002971 MSInheritanceAttr::Spelling SemanticSpelling) {
2972 assert(RD->hasDefinition() && "RD has no definition!");
2973
David Majnemer98c9ee22014-02-07 00:43:07 +00002974 // We may not have seen base specifiers or any virtual methods yet. We will
2975 // have to wait until the record is defined to catch any mismatches.
2976 if (!RD->getDefinition()->isCompleteDefinition())
2977 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002978
David Majnemer98c9ee22014-02-07 00:43:07 +00002979 // The unspecified model never matches what a definition could need.
2980 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2981 return false;
2982
David Majnemer4bb09802014-02-10 19:50:15 +00002983 if (BestCase) {
2984 if (RD->calculateInheritanceModel() == SemanticSpelling)
2985 return false;
2986 } else {
2987 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2988 return false;
2989 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002990
2991 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2992 << 0 /*definition*/;
2993 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2994 << RD->getNameAsString();
2995 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002996}
2997
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002998/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002999/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003000///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003001/// Despite what would be logical, the mode attribute is a decl attribute, not a
3002/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3003/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003004static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003005 // This attribute isn't documented, but glibc uses it. It changes
3006 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003007 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003008 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3009 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003010 return;
3011 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003012
Aaron Ballman00e99962013-08-31 01:11:41 +00003013 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3014 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003015
3016 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003017 if (Str.startswith("__") && Str.endswith("__"))
3018 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003019
3020 unsigned DestWidth = 0;
3021 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003022 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003023 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003024 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003025 switch (Str[0]) {
3026 case 'Q': DestWidth = 8; break;
3027 case 'H': DestWidth = 16; break;
3028 case 'S': DestWidth = 32; break;
3029 case 'D': DestWidth = 64; break;
3030 case 'X': DestWidth = 96; break;
3031 case 'T': DestWidth = 128; break;
3032 }
3033 if (Str[1] == 'F') {
3034 IntegerMode = false;
3035 } else if (Str[1] == 'C') {
3036 IntegerMode = false;
3037 ComplexMode = true;
3038 } else if (Str[1] != 'I') {
3039 DestWidth = 0;
3040 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003041 break;
3042 case 4:
3043 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3044 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003045 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003046 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003047 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003048 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003049 break;
3050 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003051 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003052 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003053 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003054 case 11:
3055 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003056 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003057 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003058 }
3059
3060 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003061 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003062 OldTy = TD->getUnderlyingType();
3063 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3064 OldTy = VD->getType();
3065 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003066 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003067 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003068 return;
3069 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003070
John McCall9dd450b2009-09-21 23:43:11 +00003071 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003072 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3073 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003074 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003075 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3076 } else if (ComplexMode) {
3077 if (!OldTy->isComplexType())
3078 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3079 } else {
3080 if (!OldTy->isFloatingType())
3081 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3082 }
3083
Mike Stump87c57ac2009-05-16 07:39:55 +00003084 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3085 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003086 // FIXME: Make sure floating-point mappings are accurate
3087 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003088 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003089 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003090 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003091 }
3092
3093 QualType NewTy;
3094
3095 if (IntegerMode)
3096 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3097 OldTy->isSignedIntegerType());
3098 else
3099 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3100
3101 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003102 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003103 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003104 }
3105
Eli Friedman4735374e2009-03-03 06:41:03 +00003106 if (ComplexMode) {
3107 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003108 }
3109
3110 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003111 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3112 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3113 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003114 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003115
3116 D->addAttr(::new (S.Context)
3117 ModeAttr(Attr.getRange(), S.Context, Name,
3118 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003119}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003120
Chandler Carruthedc2c642011-07-02 00:01:44 +00003121static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003122 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3123 if (!VD->hasGlobalStorage())
3124 S.Diag(Attr.getLoc(),
3125 diag::warn_attribute_requires_functions_or_static_globals)
3126 << Attr.getName();
3127 } else if (!isFunctionOrMethod(D)) {
3128 S.Diag(Attr.getLoc(),
3129 diag::warn_attribute_requires_functions_or_static_globals)
3130 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003131 return;
3132 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003133
Michael Han99315932013-01-24 16:46:58 +00003134 D->addAttr(::new (S.Context)
3135 NoDebugAttr(Attr.getRange(), S.Context,
3136 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003137}
3138
Paul Robinsonf0674352014-03-31 22:29:15 +00003139static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3140 const AttributeList &Attr) {
3141 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
3142 return;
3143
3144 D->addAttr(::new (S.Context)
3145 AlwaysInlineAttr(Attr.getRange(), S.Context,
3146 Attr.getAttributeSpellingListIndex()));
3147}
3148
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003149static void handleMinSizeAttr(Sema &S, Decl *D,
3150 const AttributeList &Attr) {
3151 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
3152 return;
3153
3154 D->addAttr(::new (S.Context)
3155 MinSizeAttr(Attr.getRange(), S.Context,
3156 Attr.getAttributeSpellingListIndex()));
3157}
3158
Paul Robinsonf0674352014-03-31 22:29:15 +00003159static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3160 const AttributeList &Attr) {
3161 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
3162 return;
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003163 if (checkAttrMutualExclusion<MinSizeAttr>(S, D, Attr))
3164 return;
Paul Robinsonf0674352014-03-31 22:29:15 +00003165
3166 D->addAttr(::new (S.Context)
3167 OptimizeNoneAttr(Attr.getRange(), S.Context,
3168 Attr.getAttributeSpellingListIndex()));
3169}
3170
Chandler Carruthedc2c642011-07-02 00:01:44 +00003171static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003172 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003173 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003174 SourceRange RTRange = FD->getReturnTypeSourceRange();
3175 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003176 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003177 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3178 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003179 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003180 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003181
Aaron Ballman3aff6332013-12-02 19:30:36 +00003182 D->addAttr(::new (S.Context)
3183 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003184 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003185}
3186
Chandler Carruthedc2c642011-07-02 00:01:44 +00003187static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003188 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003189 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003190 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003191 return;
3192 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003193
Michael Han99315932013-01-24 16:46:58 +00003194 D->addAttr(::new (S.Context)
3195 GNUInlineAttr(Attr.getRange(), S.Context,
3196 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003197}
3198
Chandler Carruthedc2c642011-07-02 00:01:44 +00003199static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003200 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003201
Aaron Ballman02df2e02012-12-09 17:45:41 +00003202 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003203 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003204 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3205 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003206 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003207 return;
3208
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003209 if (!isa<ObjCMethodDecl>(D)) {
3210 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3211 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003212 return;
3213 }
3214
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003215 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003216 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003217 D->addAttr(::new (S.Context)
3218 FastCallAttr(Attr.getRange(), S.Context,
3219 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003220 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003221 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003222 D->addAttr(::new (S.Context)
3223 StdCallAttr(Attr.getRange(), S.Context,
3224 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003225 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003226 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003227 D->addAttr(::new (S.Context)
3228 ThisCallAttr(Attr.getRange(), S.Context,
3229 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003230 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003231 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003232 D->addAttr(::new (S.Context)
3233 CDeclAttr(Attr.getRange(), S.Context,
3234 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003235 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003236 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003237 D->addAttr(::new (S.Context)
3238 PascalAttr(Attr.getRange(), S.Context,
3239 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003240 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003241 case AttributeList::AT_VectorCall:
3242 D->addAttr(::new (S.Context)
3243 VectorCallAttr(Attr.getRange(), S.Context,
3244 Attr.getAttributeSpellingListIndex()));
3245 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003246 case AttributeList::AT_MSABI:
3247 D->addAttr(::new (S.Context)
3248 MSABIAttr(Attr.getRange(), S.Context,
3249 Attr.getAttributeSpellingListIndex()));
3250 return;
3251 case AttributeList::AT_SysVABI:
3252 D->addAttr(::new (S.Context)
3253 SysVABIAttr(Attr.getRange(), S.Context,
3254 Attr.getAttributeSpellingListIndex()));
3255 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003256 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003257 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003258 switch (CC) {
3259 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003260 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003261 break;
3262 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003263 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003264 break;
3265 default:
3266 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003267 }
3268
Michael Han99315932013-01-24 16:46:58 +00003269 D->addAttr(::new (S.Context)
3270 PcsAttr(Attr.getRange(), S.Context, PCS,
3271 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003272 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003273 }
Derek Schuffa2020962012-10-16 22:30:41 +00003274 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003275 D->addAttr(::new (S.Context)
3276 PnaclCallAttr(Attr.getRange(), S.Context,
3277 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003278 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003279 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003280 D->addAttr(::new (S.Context)
3281 IntelOclBiccAttr(Attr.getRange(), S.Context,
3282 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003283 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003284
Abramo Bagnara50099372010-04-30 13:10:51 +00003285 default:
3286 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003287 }
3288}
3289
Aaron Ballman02df2e02012-12-09 17:45:41 +00003290bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3291 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003292 if (attr.isInvalid())
3293 return true;
3294
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003295 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003296 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003297 attr.setInvalid();
3298 return true;
3299 }
3300
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003301 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003302 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003303 case AttributeList::AT_CDecl: CC = CC_C; break;
3304 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3305 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3306 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3307 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003308 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003309 case AttributeList::AT_MSABI:
3310 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3311 CC_X86_64Win64;
3312 break;
3313 case AttributeList::AT_SysVABI:
3314 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3315 CC_C;
3316 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003317 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003318 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003319 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003320 attr.setInvalid();
3321 return true;
3322 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003323 if (StrRef == "aapcs") {
3324 CC = CC_AAPCS;
3325 break;
3326 } else if (StrRef == "aapcs-vfp") {
3327 CC = CC_AAPCS_VFP;
3328 break;
3329 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003330
3331 attr.setInvalid();
3332 Diag(attr.getLoc(), diag::err_invalid_pcs);
3333 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003334 }
Derek Schuffa2020962012-10-16 22:30:41 +00003335 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003336 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003337 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003338 }
3339
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003340 const TargetInfo &TI = Context.getTargetInfo();
3341 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3342 if (A == TargetInfo::CCCR_Warning) {
3343 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003344
3345 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3346 if (FD)
3347 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3348 TargetInfo::CCMT_NonMember;
3349 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003350 }
3351
John McCall3882ace2011-01-05 12:14:39 +00003352 return false;
3353}
3354
John McCall3882ace2011-01-05 12:14:39 +00003355/// Checks a regparm attribute, returning true if it is ill-formed and
3356/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003357bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3358 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003359 return true;
3360
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003361 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003362 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003363 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003364 }
Eli Friedman7044b762009-03-27 21:06:47 +00003365
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003366 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003367 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003368 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003369 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003370 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003371 }
3372
Douglas Gregore8bbc122011-09-02 00:18:52 +00003373 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003374 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003375 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003376 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003377 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003378 }
3379
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003380 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003381 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003382 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003383 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003384 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003385 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003386 }
3387
John McCall3882ace2011-01-05 12:14:39 +00003388 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003389}
3390
Aaron Ballman66039932013-12-19 00:41:31 +00003391static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3392 const AttributeList &Attr) {
Aaron Ballman66039932013-12-19 00:41:31 +00003393 uint32_t MaxThreads, MinBlocks = 0;
3394 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3395 return;
3396 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3397 Attr.getArgAsExpr(1),
3398 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003399 return;
3400
3401 D->addAttr(::new (S.Context)
3402 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3403 MaxThreads, MinBlocks,
3404 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003405}
3406
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003407static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3408 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003409 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003410 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003411 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003412 return;
3413 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003414
3415 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003416 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003417
Aaron Ballman00e99962013-08-31 01:11:41 +00003418 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003419
3420 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3421 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3422 << Attr.getName() << ExpectedFunctionOrMethod;
3423 return;
3424 }
3425
3426 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003427 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3428 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003429 return;
3430
3431 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003432 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3433 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003434 return;
3435
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003436 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003437 if (IsPointer) {
3438 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003439 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003440 if (!BufferTy->isPointerType()) {
3441 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003442 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003443 }
3444 }
3445
Michael Han99315932013-01-24 16:46:58 +00003446 D->addAttr(::new (S.Context)
3447 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3448 ArgumentIdx, TypeTagIdx, IsPointer,
3449 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003450}
3451
3452static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3453 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003454 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003455 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003456 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003457 return;
3458 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003459
3460 if (!checkAttributeNumArgs(S, Attr, 1))
3461 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003462
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003463 if (!isa<VarDecl>(D)) {
3464 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3465 << Attr.getName() << ExpectedVariable;
3466 return;
3467 }
3468
Aaron Ballman00e99962013-08-31 01:11:41 +00003469 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003470 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003471 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3472 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003473
Michael Han99315932013-01-24 16:46:58 +00003474 D->addAttr(::new (S.Context)
3475 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003476 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003477 Attr.getLayoutCompatible(),
3478 Attr.getMustBeNull(),
3479 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003480}
3481
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003482//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003483// Checker-specific attribute handlers.
3484//===----------------------------------------------------------------------===//
3485
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003486static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003487 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003488 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003489}
3490
John McCalled433932011-01-25 03:31:58 +00003491static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003492 return type->isDependentType() ||
3493 type->isObjCObjectPointerType() ||
3494 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003495}
3496static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003497 return type->isDependentType() ||
3498 type->isPointerType() ||
3499 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003500}
3501
Chandler Carruthedc2c642011-07-02 00:01:44 +00003502static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003503 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003504 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003505
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003506 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003507 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3508 cf = false;
3509 } else {
3510 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3511 cf = true;
3512 }
3513
3514 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003515 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003516 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003517 return;
3518 }
3519
3520 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003521 param->addAttr(::new (S.Context)
3522 CFConsumedAttr(Attr.getRange(), S.Context,
3523 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003524 else
Michael Han99315932013-01-24 16:46:58 +00003525 param->addAttr(::new (S.Context)
3526 NSConsumedAttr(Attr.getRange(), S.Context,
3527 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003528}
3529
Chandler Carruthedc2c642011-07-02 00:01:44 +00003530static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3531 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003532
John McCalled433932011-01-25 03:31:58 +00003533 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003534
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003535 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003536 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003537 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003538 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003539 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003540 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3541 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003542 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003543 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003544 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003545 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003546 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003547 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003548 return;
3549 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003550
John McCalled433932011-01-25 03:31:58 +00003551 bool typeOK;
3552 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003553 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003554 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003555 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003556 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003557 cf = false;
3558 break;
3559
3560 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003561 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003562 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3563 cf = false;
3564 break;
3565
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003566 case AttributeList::AT_CFReturnsRetained:
3567 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003568 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3569 cf = true;
3570 break;
3571 }
3572
3573 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003574 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003575 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003576 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003577 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003578
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003579 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003580 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003581 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003582 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003583 D->addAttr(::new (S.Context)
3584 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3585 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003586 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003587 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003588 D->addAttr(::new (S.Context)
3589 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3590 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003591 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003592 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003593 D->addAttr(::new (S.Context)
3594 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3595 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003596 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003597 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003598 D->addAttr(::new (S.Context)
3599 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3600 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003601 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003602 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003603 D->addAttr(::new (S.Context)
3604 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3605 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003606 return;
3607 };
3608}
3609
John McCallcf166702011-07-22 08:53:00 +00003610static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3611 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003612 const int EP_ObjCMethod = 1;
3613 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003614
John McCallcf166702011-07-22 08:53:00 +00003615 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003616 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003617 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003618 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003619 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003620 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003621
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003622 if (!resultType->isReferenceType() &&
3623 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003624 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003625 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003626 << attr.getName()
3627 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003628 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003629
3630 // Drop the attribute.
3631 return;
3632 }
3633
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003634 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003635 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3636 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003637}
3638
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003639static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3640 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003641 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003642
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003643 DeclContext *DC = method->getDeclContext();
3644 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3645 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3646 << attr.getName() << 0;
3647 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3648 return;
3649 }
3650 if (method->getMethodFamily() == OMF_dealloc) {
3651 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3652 << attr.getName() << 1;
3653 return;
3654 }
3655
Michael Han99315932013-01-24 16:46:58 +00003656 method->addAttr(::new (S.Context)
3657 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3658 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003659}
3660
Aaron Ballmanfb763042013-12-02 18:05:46 +00003661static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3662 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003663 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003664 return;
John McCall32f5fe12011-09-30 05:12:12 +00003665
Aaron Ballmanfb763042013-12-02 18:05:46 +00003666 D->addAttr(::new (S.Context)
3667 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3668 Attr.getAttributeSpellingListIndex()));
3669}
3670
3671static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3672 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003673 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003674 return;
3675
3676 D->addAttr(::new (S.Context)
3677 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3678 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003679}
3680
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003681static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3682 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003683 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003684
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003685 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003686 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003687 return;
3688 }
3689
3690 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003691 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003692 Attr.getAttributeSpellingListIndex()));
3693}
3694
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003695static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3696 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003697 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3698
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003699 if (!Parm) {
3700 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3701 return;
3702 }
3703
3704 D->addAttr(::new (S.Context)
3705 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3706 Attr.getAttributeSpellingListIndex()));
3707}
3708
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003709static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3710 const AttributeList &Attr) {
3711 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003712 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003713 if (!RelatedClass) {
3714 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3715 return;
3716 }
3717 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003718 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003719 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003720 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003721 D->addAttr(::new (S.Context)
3722 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3723 ClassMethod, InstanceMethod,
3724 Attr.getAttributeSpellingListIndex()));
3725}
3726
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003727static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3728 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003729 ObjCInterfaceDecl *IFace;
3730 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3731 IFace = CatDecl->getClassInterface();
3732 else
3733 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003734 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003735 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003736 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3737 Attr.getAttributeSpellingListIndex()));
3738}
3739
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003740static void handleObjCRuntimeName(Sema &S, Decl *D,
3741 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003742 StringRef MetaDataName;
3743 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3744 return;
3745 D->addAttr(::new (S.Context)
3746 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3747 MetaDataName,
3748 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003749}
3750
Chandler Carruthedc2c642011-07-02 00:01:44 +00003751static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3752 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003753 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003754
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003755 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003756 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003757}
3758
Chandler Carruthedc2c642011-07-02 00:01:44 +00003759static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3760 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003761 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003762 QualType type = vd->getType();
3763
3764 if (!type->isDependentType() &&
3765 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003766 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003767 << type;
3768 return;
3769 }
3770
3771 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3772
3773 // If we have no lifetime yet, check the lifetime we're presumably
3774 // going to infer.
3775 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3776 lifetime = type->getObjCARCImplicitLifetime();
3777
3778 switch (lifetime) {
3779 case Qualifiers::OCL_None:
3780 assert(type->isDependentType() &&
3781 "didn't infer lifetime for non-dependent type?");
3782 break;
3783
3784 case Qualifiers::OCL_Weak: // meaningful
3785 case Qualifiers::OCL_Strong: // meaningful
3786 break;
3787
3788 case Qualifiers::OCL_ExplicitNone:
3789 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003790 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003791 << (lifetime == Qualifiers::OCL_Autoreleasing);
3792 break;
3793 }
3794
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003795 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003796 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3797 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003798}
3799
Francois Picheta83957a2010-12-19 06:50:37 +00003800//===----------------------------------------------------------------------===//
3801// Microsoft specific attribute handlers.
3802//===----------------------------------------------------------------------===//
3803
Chandler Carruthedc2c642011-07-02 00:01:44 +00003804static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003805 if (!S.LangOpts.CPlusPlus) {
3806 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3807 << Attr.getName() << AttributeLangSupport::C;
3808 return;
3809 }
3810
Aaron Ballman60e705e2013-11-24 20:58:02 +00003811 if (!isa<CXXRecordDecl>(D)) {
3812 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3813 << Attr.getName() << ExpectedClass;
3814 return;
3815 }
3816
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003817 StringRef StrRef;
3818 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003819 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003820 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003821
David Majnemer89085342013-08-09 08:56:20 +00003822 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3823 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003824 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3825 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003826
Reid Kleckner140c4a72013-05-17 14:04:52 +00003827 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003828 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003829 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003830 return;
3831 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003832
David Majnemer89085342013-08-09 08:56:20 +00003833 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003834 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003835 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003836 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003837 return;
3838 }
David Majnemer89085342013-08-09 08:56:20 +00003839 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003840 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003841 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003842 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003843 }
Francois Picheta83957a2010-12-19 06:50:37 +00003844
David Majnemer89085342013-08-09 08:56:20 +00003845 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3846 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003847}
3848
David Majnemer2c4e00a2014-01-29 22:07:36 +00003849static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3850 if (!S.LangOpts.CPlusPlus) {
3851 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3852 << Attr.getName() << AttributeLangSupport::C;
3853 return;
3854 }
3855 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003856 D, Attr.getRange(), /*BestCase=*/true,
3857 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003858 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3859 if (IA)
3860 D->addAttr(IA);
3861}
3862
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003863static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3864 const AttributeList &Attr) {
3865 VarDecl *VD = cast<VarDecl>(D);
3866 if (!S.Context.getTargetInfo().isTLSSupported()) {
3867 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3868 return;
3869 }
3870 if (VD->getTSCSpec() != TSCS_unspecified) {
3871 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3872 return;
3873 }
3874 if (VD->hasLocalStorage()) {
3875 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3876 return;
3877 }
3878 VD->addAttr(::new (S.Context) ThreadAttr(
3879 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3880}
3881
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003882static void handleARMInterruptAttr(Sema &S, Decl *D,
3883 const AttributeList &Attr) {
3884 // Check the attribute arguments.
3885 if (Attr.getNumArgs() > 1) {
3886 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3887 << Attr.getName() << 1;
3888 return;
3889 }
3890
3891 StringRef Str;
3892 SourceLocation ArgLoc;
3893
3894 if (Attr.getNumArgs() == 0)
3895 Str = "";
3896 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3897 return;
3898
3899 ARMInterruptAttr::InterruptType Kind;
3900 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3901 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3902 << Attr.getName() << Str << ArgLoc;
3903 return;
3904 }
3905
3906 unsigned Index = Attr.getAttributeSpellingListIndex();
3907 D->addAttr(::new (S.Context)
3908 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3909}
3910
3911static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3912 const AttributeList &Attr) {
3913 if (!checkAttributeNumArgs(S, Attr, 1))
3914 return;
3915
3916 if (!Attr.isArgExpr(0)) {
3917 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3918 << AANT_ArgumentIntegerConstant;
3919 return;
3920 }
3921
3922 // FIXME: Check for decl - it should be void ()(void).
3923
3924 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3925 llvm::APSInt NumParams(32);
3926 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3927 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3928 << Attr.getName() << AANT_ArgumentIntegerConstant
3929 << NumParamsExpr->getSourceRange();
3930 return;
3931 }
3932
3933 unsigned Num = NumParams.getLimitedValue(255);
3934 if ((Num & 1) || Num > 30) {
3935 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3936 << Attr.getName() << (int)NumParams.getSExtValue()
3937 << NumParamsExpr->getSourceRange();
3938 return;
3939 }
3940
Aaron Ballman36a53502014-01-16 13:03:14 +00003941 D->addAttr(::new (S.Context)
3942 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3943 Attr.getAttributeSpellingListIndex()));
3944 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003945}
3946
3947static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3948 // Dispatch the interrupt attribute based on the current target.
3949 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3950 handleMSP430InterruptAttr(S, D, Attr);
3951 else
3952 handleARMInterruptAttr(S, D, Attr);
3953}
3954
Matt Arsenault43fae6c2014-12-04 20:38:18 +00003955static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
3956 const AttributeList &Attr) {
3957 uint32_t NumRegs;
3958 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3959 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
3960 return;
3961
3962 D->addAttr(::new (S.Context)
3963 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
3964 NumRegs,
3965 Attr.getAttributeSpellingListIndex()));
3966}
3967
3968static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
3969 const AttributeList &Attr) {
3970 uint32_t NumRegs;
3971 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3972 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
3973 return;
3974
3975 D->addAttr(::new (S.Context)
3976 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
3977 NumRegs,
3978 Attr.getAttributeSpellingListIndex()));
3979}
3980
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003981static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3982 const AttributeList& Attr) {
3983 // If we try to apply it to a function pointer, don't warn, but don't
3984 // do anything, either. It doesn't matter anyway, because there's nothing
3985 // special about calling a force_align_arg_pointer function.
3986 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3987 if (VD && VD->getType()->isFunctionPointerType())
3988 return;
3989 // Also don't warn on function pointer typedefs.
3990 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3991 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3992 TD->getUnderlyingType()->isFunctionType()))
3993 return;
3994 // Attribute can only be applied to function types.
3995 if (!isa<FunctionDecl>(D)) {
3996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3997 << Attr.getName() << /* function */0;
3998 return;
3999 }
4000
Aaron Ballman36a53502014-01-16 13:03:14 +00004001 D->addAttr(::new (S.Context)
4002 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4003 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004004}
4005
4006DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4007 unsigned AttrSpellingListIndex) {
4008 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004009 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004010 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004011 }
4012
4013 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004014 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004015
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004016 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004017}
4018
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004019DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4020 unsigned AttrSpellingListIndex) {
4021 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004022 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004023 D->dropAttr<DLLImportAttr>();
4024 }
4025
4026 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004027 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004028
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004029 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004030}
4031
Hans Wennborge82f19c2014-06-24 23:57:05 +00004032static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004033 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4034 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4035 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4036 << A.getName();
4037 return;
4038 }
4039
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004040 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4041 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4042 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4043 // MinGW doesn't allow dllimport on inline functions.
4044 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4045 << A.getName();
4046 return;
4047 }
4048 }
4049
Hans Wennborge82f19c2014-06-24 23:57:05 +00004050 unsigned Index = A.getAttributeSpellingListIndex();
4051 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4052 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4053 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004054 if (NewAttr)
4055 D->addAttr(NewAttr);
4056}
4057
David Majnemer2c4e00a2014-01-29 22:07:36 +00004058MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004059Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004060 unsigned AttrSpellingListIndex,
4061 MSInheritanceAttr::Spelling SemanticSpelling) {
4062 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4063 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004064 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004065 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4066 << 1 /*previous declaration*/;
4067 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4068 D->dropAttr<MSInheritanceAttr>();
4069 }
4070
4071 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4072 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004073 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4074 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004075 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004076 }
4077 } else {
4078 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4079 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4080 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004081 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004082 }
4083 if (RD->getDescribedClassTemplate()) {
4084 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4085 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004086 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004087 }
4088 }
4089
4090 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004091 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004092}
4093
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004094static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4095 // The capability attributes take a single string parameter for the name of
4096 // the capability they represent. The lockable attribute does not take any
4097 // parameters. However, semantically, both attributes represent the same
4098 // concept, and so they use the same semantic attribute. Eventually, the
4099 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004100 //
Alp Toker958027b2014-07-14 19:42:55 +00004101 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004102 // literal will be considered a "mutex."
4103 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004104 SourceLocation LiteralLoc;
4105 if (Attr.getKind() == AttributeList::AT_Capability &&
4106 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4107 return;
4108
Aaron Ballman6c810072014-03-05 21:47:13 +00004109 // Currently, there are only two names allowed for a capability: role and
4110 // mutex (case insensitive). Diagnose other capability names.
4111 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4112 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4113
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004114 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4115 Attr.getAttributeSpellingListIndex()));
4116}
4117
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004118static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4119 const AttributeList &Attr) {
4120 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4121 Attr.getArgAsExpr(0),
4122 Attr.getAttributeSpellingListIndex()));
4123}
4124
4125static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4126 const AttributeList &Attr) {
4127 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004128 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004129 return;
4130
4131 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4132 S.Context,
4133 Args.data(), Args.size(),
4134 Attr.getAttributeSpellingListIndex()));
4135}
4136
4137static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4138 const AttributeList &Attr) {
4139 SmallVector<Expr*, 2> Args;
4140 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4141 return;
4142
4143 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4144 S.Context,
4145 Attr.getArgAsExpr(0),
4146 Args.data(),
4147 Args.size(),
4148 Attr.getAttributeSpellingListIndex()));
4149}
4150
4151static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4152 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004153 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004154 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004155 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004156
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004157 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4158 Attr.getRange(), S.Context, Args.data(), Args.size(),
4159 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004160}
4161
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004162static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4163 const AttributeList &Attr) {
4164 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4165 return;
4166
4167 // check that all arguments are lockable objects
4168 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004169 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004170 if (Args.empty())
4171 return;
4172
4173 RequiresCapabilityAttr *RCA = ::new (S.Context)
4174 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4175 Args.size(), Attr.getAttributeSpellingListIndex());
4176
4177 D->addAttr(RCA);
4178}
4179
Aaron Ballman43f40102014-11-14 22:34:56 +00004180static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4181 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4182 if (NSD->isAnonymousNamespace()) {
4183 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4184 // Do not want to attach the attribute to the namespace because that will
4185 // cause confusing diagnostic reports for uses of declarations within the
4186 // namespace.
4187 return;
4188 }
4189 }
4190 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4191}
4192
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004193/// Handles semantic checking for features that are common to all attributes,
4194/// such as checking whether a parameter was properly specified, or the correct
4195/// number of arguments were passed, etc.
4196static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4197 const AttributeList &Attr) {
4198 // Several attributes carry different semantics than the parsing requires, so
4199 // those are opted out of the common handling.
4200 //
4201 // We also bail on unknown and ignored attributes because those are handled
4202 // as part of the target-specific handling logic.
4203 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004204 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004205 return false;
4206
Aaron Ballman3aff6332013-12-02 19:30:36 +00004207 // Check whether the attribute requires specific language extensions to be
4208 // enabled.
4209 if (!Attr.diagnoseLangOpts(S))
4210 return true;
4211
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004212 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4213 // If there are no optional arguments, then checking for the argument count
4214 // is trivial.
4215 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4216 return true;
4217 } else {
4218 // There are optional arguments, so checking is slightly more involved.
4219 if (Attr.getMinArgs() &&
4220 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4221 return true;
4222 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4223 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4224 return true;
4225 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004226
4227 // Check whether the attribute appertains to the given subject.
4228 if (!Attr.diagnoseAppertainsTo(S, D))
4229 return true;
4230
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004231 return false;
4232}
4233
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004234//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004235// Top Level Sema Entry Points
4236//===----------------------------------------------------------------------===//
4237
Richard Smithf8a75c32013-08-29 00:47:48 +00004238/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4239/// the attribute applies to decls. If the attribute is a type attribute, just
4240/// silently ignore it if a GNU attribute.
4241static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4242 const AttributeList &Attr,
4243 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004244 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004245 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004246
Richard Smithf8a75c32013-08-29 00:47:48 +00004247 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4248 // instead.
4249 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4250 return;
4251
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004252 // Unknown attributes are automatically warned on. Target-specific attributes
4253 // which do not apply to the current target architecture are treated as
4254 // though they were unknown attributes.
4255 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4256 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004257 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4258 ? diag::warn_unhandled_ms_attribute_ignored
4259 : diag::warn_unknown_attribute_ignored)
4260 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004261 return;
4262 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004263
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004264 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4265 return;
4266
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004267 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004268 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004269 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004270 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004271 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004272 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004273 handleInterruptAttr(S, D, Attr);
4274 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004275 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004276 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4277 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004278 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004279 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004280 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004281 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004282 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004283 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4284 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004285 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004286 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4287 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004288 case AttributeList::AT_AMDGPUNumVGPR:
4289 handleAMDGPUNumVGPRAttr(S, D, Attr);
4290 break;
4291 case AttributeList::AT_AMDGPUNumSGPR:
4292 handleAMDGPUNumSGPRAttr(S, D, Attr);
4293 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004294 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004295 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4296 break;
4297 case AttributeList::AT_IBOutlet:
4298 handleIBOutlet(S, D, Attr);
4299 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004300 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004301 handleIBOutletCollection(S, D, Attr);
4302 break;
4303 case AttributeList::AT_Alias:
4304 handleAliasAttr(S, D, Attr);
4305 break;
4306 case AttributeList::AT_Aligned:
4307 handleAlignedAttr(S, D, Attr);
4308 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004309 case AttributeList::AT_AlignValue:
4310 handleAlignValueAttr(S, D, Attr);
4311 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004312 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004313 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004314 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004315 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004316 handleAnalyzerNoReturnAttr(S, D, Attr);
4317 break;
4318 case AttributeList::AT_TLSModel:
4319 handleTLSModelAttr(S, D, Attr);
4320 break;
4321 case AttributeList::AT_Annotate:
4322 handleAnnotateAttr(S, D, Attr);
4323 break;
4324 case AttributeList::AT_Availability:
4325 handleAvailabilityAttr(S, D, Attr);
4326 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004327 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004328 handleDependencyAttr(S, scope, D, Attr);
4329 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004330 case AttributeList::AT_Common:
4331 handleCommonAttr(S, D, Attr);
4332 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004333 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004334 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4335 break;
4336 case AttributeList::AT_Constructor:
4337 handleConstructorAttr(S, D, Attr);
4338 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004339 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004340 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4341 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004342 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004343 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004344 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004345 case AttributeList::AT_Destructor:
4346 handleDestructorAttr(S, D, Attr);
4347 break;
4348 case AttributeList::AT_EnableIf:
4349 handleEnableIfAttr(S, D, Attr);
4350 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004351 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004352 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004353 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004354 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004355 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004356 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004357 case AttributeList::AT_OptimizeNone:
4358 handleOptimizeNoneAttr(S, D, Attr);
4359 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004360 case AttributeList::AT_FlagEnum:
4361 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4362 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004363 case AttributeList::AT_Flatten:
4364 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4365 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004366 case AttributeList::AT_Format:
4367 handleFormatAttr(S, D, Attr);
4368 break;
4369 case AttributeList::AT_FormatArg:
4370 handleFormatArgAttr(S, D, Attr);
4371 break;
4372 case AttributeList::AT_CUDAGlobal:
4373 handleGlobalAttr(S, D, Attr);
4374 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004375 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004376 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4377 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004378 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004379 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4380 break;
4381 case AttributeList::AT_GNUInline:
4382 handleGNUInlineAttr(S, D, Attr);
4383 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004384 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004385 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004386 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004387 case AttributeList::AT_Malloc:
4388 handleMallocAttr(S, D, Attr);
4389 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004390 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004391 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4392 break;
4393 case AttributeList::AT_Mode:
4394 handleModeAttr(S, D, Attr);
4395 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004396 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004397 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4398 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004399 case AttributeList::AT_NoSplitStack:
4400 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4401 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004402 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004403 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4404 handleNonNullAttrParameter(S, PVD, Attr);
4405 else
4406 handleNonNullAttr(S, D, Attr);
4407 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004408 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004409 handleReturnsNonNullAttr(S, D, Attr);
4410 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004411 case AttributeList::AT_AssumeAligned:
4412 handleAssumeAlignedAttr(S, D, Attr);
4413 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004414 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004415 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4416 break;
4417 case AttributeList::AT_Ownership:
4418 handleOwnershipAttr(S, D, Attr);
4419 break;
4420 case AttributeList::AT_Cold:
4421 handleColdAttr(S, D, Attr);
4422 break;
4423 case AttributeList::AT_Hot:
4424 handleHotAttr(S, D, Attr);
4425 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004426 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004427 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4428 break;
4429 case AttributeList::AT_NoReturn:
4430 handleNoReturnAttr(S, D, Attr);
4431 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004432 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004433 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4434 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004435 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004436 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4437 break;
4438 case AttributeList::AT_VecReturn:
4439 handleVecReturnAttr(S, D, Attr);
4440 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004441
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004442 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004443 handleObjCOwnershipAttr(S, D, Attr);
4444 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004445 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004446 handleObjCPreciseLifetimeAttr(S, D, Attr);
4447 break;
John McCall31168b02011-06-15 23:02:42 +00004448
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004449 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004450 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4451 break;
John McCallcf166702011-07-22 08:53:00 +00004452
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004453 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004454 handleObjCRequiresSuperAttr(S, D, Attr);
4455 break;
4456
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004457 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004458 handleObjCBridgeAttr(S, scope, D, Attr);
4459 break;
4460
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004461 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004462 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4463 break;
4464
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004465 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004466 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4467 break;
John McCallf1e8b342011-09-29 07:17:38 +00004468
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004469 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004470 handleObjCDesignatedInitializer(S, D, Attr);
4471 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004472
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004473 case AttributeList::AT_ObjCRuntimeName:
4474 handleObjCRuntimeName(S, D, Attr);
4475 break;
4476
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004477 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004478 handleCFAuditedTransferAttr(S, D, Attr);
4479 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004480 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004481 handleCFUnknownTransferAttr(S, D, Attr);
4482 break;
John McCall32f5fe12011-09-30 05:12:12 +00004483
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004484 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004485 case AttributeList::AT_NSConsumed:
4486 handleNSConsumedAttr(S, D, Attr);
4487 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004488 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004489 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4490 break;
John McCalled433932011-01-25 03:31:58 +00004491
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004492 case AttributeList::AT_NSReturnsAutoreleased:
4493 case AttributeList::AT_NSReturnsNotRetained:
4494 case AttributeList::AT_CFReturnsNotRetained:
4495 case AttributeList::AT_NSReturnsRetained:
4496 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004497 handleNSReturnsRetainedAttr(S, D, Attr);
4498 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004499 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004500 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4501 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004502 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004503 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4504 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004505 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004506 handleVecTypeHint(S, D, Attr);
4507 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004508
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004509 case AttributeList::AT_InitPriority:
4510 handleInitPriorityAttr(S, D, Attr);
4511 break;
4512
4513 case AttributeList::AT_Packed:
4514 handlePackedAttr(S, D, Attr);
4515 break;
4516 case AttributeList::AT_Section:
4517 handleSectionAttr(S, D, Attr);
4518 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004519 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004520 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004521 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004522 case AttributeList::AT_ArcWeakrefUnavailable:
4523 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4524 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004525 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004526 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4527 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004528 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004529 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004530 break;
4531 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004532 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4533 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004534 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004535 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4536 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004537 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004538 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4539 break;
4540 case AttributeList::AT_Used:
4541 handleUsedAttr(S, D, Attr);
4542 break;
John McCalld041a9b2013-02-20 01:54:26 +00004543 case AttributeList::AT_Visibility:
4544 handleVisibilityAttr(S, D, Attr, false);
4545 break;
4546 case AttributeList::AT_TypeVisibility:
4547 handleVisibilityAttr(S, D, Attr, true);
4548 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004549 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004550 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4551 break;
4552 case AttributeList::AT_WarnUnusedResult:
4553 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004554 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004555 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004556 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4557 break;
4558 case AttributeList::AT_WeakRef:
4559 handleWeakRefAttr(S, D, Attr);
4560 break;
4561 case AttributeList::AT_WeakImport:
4562 handleWeakImportAttr(S, D, Attr);
4563 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004564 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004565 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004566 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004567 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004568 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4569 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004570 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004571 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004572 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004573 case AttributeList::AT_ObjCNSObject:
4574 handleObjCNSObject(S, D, Attr);
4575 break;
4576 case AttributeList::AT_Blocks:
4577 handleBlocksAttr(S, D, Attr);
4578 break;
4579 case AttributeList::AT_Sentinel:
4580 handleSentinelAttr(S, D, Attr);
4581 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004582 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004583 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4584 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004585 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004586 handleSimpleAttribute<PureAttr>(S, D, Attr);
4587 break;
4588 case AttributeList::AT_Cleanup:
4589 handleCleanupAttr(S, D, Attr);
4590 break;
4591 case AttributeList::AT_NoDebug:
4592 handleNoDebugAttr(S, D, Attr);
4593 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004594 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004595 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4596 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004597 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004598 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4599 break;
4600 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4601 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4602 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004603 case AttributeList::AT_StdCall:
4604 case AttributeList::AT_CDecl:
4605 case AttributeList::AT_FastCall:
4606 case AttributeList::AT_ThisCall:
4607 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004608 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00004609 case AttributeList::AT_MSABI:
4610 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004611 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004612 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004613 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004614 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004615 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004616 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004617 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4618 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004619 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004620 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4621 break;
John McCall8d32c052012-05-22 21:28:12 +00004622
4623 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004624 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004625 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004626 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004627 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004628 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004629 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004630 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004631 handleMSInheritanceAttr(S, D, Attr);
4632 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004633 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004634 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4635 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004636 case AttributeList::AT_Thread:
4637 handleDeclspecThreadAttr(S, D, Attr);
4638 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004639
4640 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004641 case AttributeList::AT_AssertExclusiveLock:
4642 handleAssertExclusiveLockAttr(S, D, Attr);
4643 break;
4644 case AttributeList::AT_AssertSharedLock:
4645 handleAssertSharedLockAttr(S, D, Attr);
4646 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004647 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004648 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4649 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004650 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004651 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004652 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004653 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004654 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4655 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004656 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004657 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004658 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004659 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004660 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004661 break;
4662 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004663 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004664 break;
4665 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004666 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004667 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004668 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004669 handleGuardedByAttr(S, D, Attr);
4670 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004671 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004672 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004673 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004674 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004675 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004676 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004677 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004678 handleLockReturnedAttr(S, D, Attr);
4679 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004680 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004681 handleLocksExcludedAttr(S, D, Attr);
4682 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004683 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004684 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004685 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004686 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004687 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004688 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004689 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004690 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004691 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004692
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004693 // Capability analysis attributes.
4694 case AttributeList::AT_Capability:
4695 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004696 handleCapabilityAttr(S, D, Attr);
4697 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004698 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004699 handleRequiresCapabilityAttr(S, D, Attr);
4700 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004701
4702 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004703 handleAssertCapabilityAttr(S, D, Attr);
4704 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004705 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004706 handleAcquireCapabilityAttr(S, D, Attr);
4707 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004708 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004709 handleReleaseCapabilityAttr(S, D, Attr);
4710 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004711 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004712 handleTryAcquireCapabilityAttr(S, D, Attr);
4713 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004714
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004715 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004716 case AttributeList::AT_Consumable:
4717 handleConsumableAttr(S, D, Attr);
4718 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004719 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004720 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4721 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004722 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004723 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4724 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004725 case AttributeList::AT_CallableWhen:
4726 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004727 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004728 case AttributeList::AT_ParamTypestate:
4729 handleParamTypestateAttr(S, D, Attr);
4730 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004731 case AttributeList::AT_ReturnTypestate:
4732 handleReturnTypestateAttr(S, D, Attr);
4733 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004734 case AttributeList::AT_SetTypestate:
4735 handleSetTypestateAttr(S, D, Attr);
4736 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004737 case AttributeList::AT_TestTypestate:
4738 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004739 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004740
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004741 // Type safety attributes.
4742 case AttributeList::AT_ArgumentWithTypeTag:
4743 handleArgumentWithTypeTagAttr(S, D, Attr);
4744 break;
4745 case AttributeList::AT_TypeTagForDatatype:
4746 handleTypeTagForDatatypeAttr(S, D, Attr);
4747 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004748 }
4749}
4750
4751/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4752/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004753void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004754 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004755 bool IncludeCXX11Attributes) {
4756 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004757 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004758
Joey Gouly2cd9db12013-12-13 16:15:28 +00004759 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004760 // GCC accepts
4761 // static int a9 __attribute__((weakref));
4762 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004763 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004764 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4765 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004766 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004767 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004768 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004769
Aaron Ballmanbe243a72014-12-04 22:45:31 +00004770 // FIXME: We should be able to handle this in TableGen as well. It would be
4771 // good to have a way to specify "these attributes must appear as a group",
4772 // for these. Additionally, it would be good to have a way to specify "these
4773 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00004774 if (!D->hasAttr<OpenCLKernelAttr>()) {
4775 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004776 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00004777 // FIXME: This emits a different error message than
4778 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004779 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004780 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00004781 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00004782 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004783 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00004784 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00004785 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004786 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00004787 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
4788 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
4789 << A << ExpectedKernelFunction;
4790 D->setInvalidDecl();
4791 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
4792 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
4793 << A << ExpectedKernelFunction;
4794 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00004795 }
4796 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004797}
4798
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004799// Annotation attributes are the only attributes allowed after an access
4800// specifier.
4801bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4802 const AttributeList *AttrList) {
4803 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004804 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004805 handleAnnotateAttr(*this, ASDecl, *l);
4806 } else {
4807 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4808 return true;
4809 }
4810 }
4811
4812 return false;
4813}
4814
John McCall42856de2011-10-01 05:17:03 +00004815/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4816/// contains any decl attributes that we should warn about.
4817static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4818 for ( ; A; A = A->getNext()) {
4819 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004820 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004821 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4822
4823 if (A->getKind() == AttributeList::UnknownAttribute) {
4824 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4825 << A->getName() << A->getRange();
4826 } else {
4827 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4828 << A->getName() << A->getRange();
4829 }
4830 }
4831}
4832
4833/// checkUnusedDeclAttributes - Given a declarator which is not being
4834/// used to build a declaration, complain about any decl attributes
4835/// which might be lying around on it.
4836void Sema::checkUnusedDeclAttributes(Declarator &D) {
4837 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4838 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4839 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4840 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4841}
4842
Ryan Flynn7d470f32009-07-30 03:15:39 +00004843/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004844/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004845NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4846 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004847 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00004848 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00004849 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004850 FunctionDecl *NewFD;
4851 // FIXME: Missing call to CheckFunctionDeclaration().
4852 // FIXME: Mangling?
4853 // FIXME: Is the qualifier info correct?
4854 // FIXME: Is the DeclContext correct?
4855 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4856 Loc, Loc, DeclarationName(II),
4857 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004858 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004859 FD->hasPrototype(),
4860 false/*isConstexprSpecified*/);
4861 NewD = NewFD;
4862
4863 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004864 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004865
4866 // Fake up parameter variables; they are declared as if this were
4867 // a typedef.
4868 QualType FDTy = FD->getType();
4869 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4870 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004871 for (const auto &AI : FT->param_types()) {
4872 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004873 Param->setScopeInfo(0, Params.size());
4874 Params.push_back(Param);
4875 }
David Blaikie9c70e042011-09-21 18:16:56 +00004876 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004877 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004878 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4879 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004880 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004881 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004882 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004883 if (VD->getQualifier()) {
4884 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004885 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004886 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004887 }
4888 return NewD;
4889}
4890
James Dennett634962f2012-06-14 21:40:34 +00004891/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004892/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004893void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004894 if (W.getUsed()) return; // only do this once
4895 W.setUsed(true);
4896 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4897 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004898 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004899 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4900 W.getLocation()));
4901 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004902 WeakTopLevelDecl.push_back(NewD);
4903 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4904 // to insert Decl at TU scope, sorry.
4905 DeclContext *SavedContext = CurContext;
4906 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004907 NewD->setDeclContext(CurContext);
4908 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004909 PushOnScopeChains(NewD, S);
4910 CurContext = SavedContext;
4911 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004912 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004913 }
4914}
4915
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004916void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4917 // It's valid to "forward-declare" #pragma weak, in which case we
4918 // have to do this.
4919 LoadExternalWeakUndeclaredIdentifiers();
4920 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004921 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004922 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4923 if (VD->isExternC())
4924 ND = VD;
4925 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4926 if (FD->isExternC())
4927 ND = FD;
4928 if (ND) {
4929 if (IdentifierInfo *Id = ND->getIdentifier()) {
4930 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4931 = WeakUndeclaredIdentifiers.find(Id);
4932 if (I != WeakUndeclaredIdentifiers.end()) {
4933 WeakInfo W = I->second;
4934 DeclApplyPragmaWeak(S, ND, W);
4935 WeakUndeclaredIdentifiers[Id] = W;
4936 }
4937 }
4938 }
4939 }
4940}
4941
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004942/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4943/// it, apply them to D. This is a bit tricky because PD can have attributes
4944/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004945void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004946 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004947 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004948 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004949
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004950 // Walk the declarator structure, applying decl attributes that were in a type
4951 // position to the decl itself. This handles cases like:
4952 // int *__attr__(x)** D;
4953 // when X is a decl attribute.
4954 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4955 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004956 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004957
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004958 // Finally, apply any attributes on the decl itself.
4959 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004960 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004961}
John McCall28a6aea2009-11-04 02:18:39 +00004962
John McCall31168b02011-06-15 23:02:42 +00004963/// Is the given declaration allowed to use a forbidden type?
4964static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4965 // Private ivars are always okay. Unfortunately, people don't
4966 // always properly make their ivars private, even in system headers.
4967 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004968 // Function declarations in sys headers will be marked unavailable.
4969 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4970 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004971 return false;
4972
4973 // Require it to be declared in a system header.
4974 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4975}
4976
4977/// Handle a delayed forbidden-type diagnostic.
4978static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4979 Decl *decl) {
4980 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004981 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4982 "this system declaration uses an unsupported type",
4983 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004984 return;
4985 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004986 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004987 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004988 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004989 // kind of forbidden type messages on unavailable functions.
4990 if (FD->hasAttr<UnavailableAttr>() &&
4991 diag.getForbiddenTypeDiagnostic() ==
4992 diag::err_arc_array_param_no_ownership) {
4993 diag.Triggered = true;
4994 return;
4995 }
4996 }
John McCall31168b02011-06-15 23:02:42 +00004997
4998 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4999 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5000 diag.Triggered = true;
5001}
5002
Aaron Ballmanfb237522014-10-15 15:37:51 +00005003
5004static bool isDeclDeprecated(Decl *D) {
5005 do {
5006 if (D->isDeprecated())
5007 return true;
5008 // A category implicitly has the availability of the interface.
5009 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5010 return CatD->getClassInterface()->isDeprecated();
5011 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5012 return false;
5013}
5014
5015static bool isDeclUnavailable(Decl *D) {
5016 do {
5017 if (D->isUnavailable())
5018 return true;
5019 // A category implicitly has the availability of the interface.
5020 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5021 return CatD->getClassInterface()->isUnavailable();
5022 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5023 return false;
5024}
5025
5026static void DoEmitAvailabilityWarning(Sema &S, DelayedDiagnostic::DDKind K,
5027 Decl *Ctx, const NamedDecl *D,
5028 StringRef Message, SourceLocation Loc,
5029 const ObjCInterfaceDecl *UnknownObjCClass,
5030 const ObjCPropertyDecl *ObjCProperty,
5031 bool ObjCPropertyAccess) {
5032 // Diagnostics for deprecated or unavailable.
5033 unsigned diag, diag_message, diag_fwdclass_message;
5034
5035 // Matches 'diag::note_property_attribute' options.
5036 unsigned property_note_select;
5037
5038 // Matches diag::note_availability_specified_here.
5039 unsigned available_here_select_kind;
5040
5041 // Don't warn if our current context is deprecated or unavailable.
5042 switch (K) {
5043 case DelayedDiagnostic::Deprecation:
5044 if (isDeclDeprecated(Ctx))
5045 return;
5046 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5047 : diag::warn_property_method_deprecated;
5048 diag_message = diag::warn_deprecated_message;
5049 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5050 property_note_select = /* deprecated */ 0;
5051 available_here_select_kind = /* deprecated */ 2;
5052 break;
5053
5054 case DelayedDiagnostic::Unavailable:
5055 if (isDeclUnavailable(Ctx))
5056 return;
5057 diag = !ObjCPropertyAccess ? diag::err_unavailable
5058 : diag::err_property_method_unavailable;
5059 diag_message = diag::err_unavailable_message;
5060 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5061 property_note_select = /* unavailable */ 1;
5062 available_here_select_kind = /* unavailable */ 0;
5063 break;
5064
5065 default:
5066 llvm_unreachable("Neither a deprecation or unavailable kind");
5067 }
5068
Aaron Ballmanfb237522014-10-15 15:37:51 +00005069 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005070 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005071 if (ObjCProperty)
5072 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5073 << ObjCProperty->getDeclName() << property_note_select;
5074 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005075 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005076 if (ObjCProperty)
5077 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5078 << ObjCProperty->getDeclName() << property_note_select;
5079 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005080 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005081 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5082 }
5083
5084 S.Diag(D->getLocation(), diag::note_availability_specified_here)
5085 << D << available_here_select_kind;
5086}
5087
5088static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5089 Decl *Ctx) {
5090 DD.Triggered = true;
5091 DoEmitAvailabilityWarning(S, (DelayedDiagnostic::DDKind)DD.Kind, Ctx,
5092 DD.getDeprecationDecl(), DD.getDeprecationMessage(),
5093 DD.Loc, DD.getUnknownObjCClass(),
5094 DD.getObjCProperty(), false);
5095}
5096
John McCall2ec85372012-05-07 06:16:41 +00005097void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5098 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005099 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005100 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005101
John McCall2ec85372012-05-07 06:16:41 +00005102 // When delaying diagnostics to run in the context of a parsed
5103 // declaration, we only want to actually emit anything if parsing
5104 // succeeds.
5105 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005106
John McCall2ec85372012-05-07 06:16:41 +00005107 // We emit all the active diagnostics in this pool or any of its
5108 // parents. In general, we'll get one pool for the decl spec
5109 // and a child pool for each declarator; in a decl group like:
5110 // deprecated_typedef foo, *bar, baz();
5111 // only the declarator pops will be passed decls. This is correct;
5112 // we really do need to consider delayed diagnostics from the decl spec
5113 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005114 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005115 do {
John McCall6347b682012-05-07 06:16:58 +00005116 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005117 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5118 // This const_cast is a bit lame. Really, Triggered should be mutable.
5119 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005120 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005121 continue;
5122
John McCallc1465822011-02-14 07:13:47 +00005123 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005124 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005125 case DelayedDiagnostic::Unavailable:
5126 // Don't bother giving deprecation/unavailable diagnostics if
5127 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005128 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005129 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005130 break;
5131
5132 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005133 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005134 break;
John McCall31168b02011-06-15 23:02:42 +00005135
5136 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005137 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005138 break;
John McCall86121512010-01-27 03:50:35 +00005139 }
5140 }
John McCall2ec85372012-05-07 06:16:41 +00005141 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005142}
5143
John McCall6347b682012-05-07 06:16:58 +00005144/// Given a set of delayed diagnostics, re-emit them as if they had
5145/// been delayed in the current context instead of in the given pool.
5146/// Essentially, this just moves them to the current pool.
5147void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5148 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5149 assert(curPool && "re-emitting in undelayed context not supported");
5150 curPool->steal(pool);
5151}
5152
Ted Kremenekb79ee572013-12-18 23:30:06 +00005153void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5154 NamedDecl *D, StringRef Message,
5155 SourceLocation Loc,
5156 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005157 const ObjCPropertyDecl *ObjCProperty,
5158 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005159 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005160 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00005161 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
5162 UnknownObjCClass,
5163 ObjCProperty,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005164 Message,
5165 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005166 return;
5167 }
5168
Ted Kremenekb79ee572013-12-18 23:30:06 +00005169 Decl *Ctx = cast<Decl>(getCurLexicalContext());
5170 DelayedDiagnostic::DDKind K;
5171 switch (AD) {
5172 case AD_Deprecation:
5173 K = DelayedDiagnostic::Deprecation;
5174 break;
5175 case AD_Unavailable:
5176 K = DelayedDiagnostic::Unavailable;
5177 break;
5178 }
5179
5180 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005181 UnknownObjCClass, ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005182}