blob: b2169962a5d500f7bfe9152bdfe5e79d97612b1d [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"
David Majnemer929025d2016-01-26 19:30:26 +000015#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000022#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000023#include "clang/AST/Mangle.h"
Alex Denisovfde64952015-06-26 05:28:36 +000024#include "clang/AST/ASTMutationListener.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000025#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000026#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000028#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000029#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000030#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000031#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000032#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000033#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000034#include "llvm/Support/MathExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000036using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000037
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000038namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000039 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000040 C,
41 Cpp,
42 ObjC
43 };
44}
45
Chris Lattner58418ff2008-06-29 00:16:31 +000046//===----------------------------------------------------------------------===//
47// Helper functions
48//===----------------------------------------------------------------------===//
49
Ted Kremenek527042b2009-08-14 20:49:40 +000050/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000051/// type (function or function-typed variable) or an Objective-C
52/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000053static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000054 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000055}
David Majnemer06864812015-04-07 06:01:53 +000056/// \brief Return true if the given decl has function type (function or
57/// function-typed variable) or an Objective-C method or a block.
58static bool isFunctionOrMethodOrBlock(const Decl *D) {
59 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
60}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000061
John McCall3882ace2011-01-05 12:14:39 +000062/// Return true if the given decl has a declarator that should have
63/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000065 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000066 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
67 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000068}
69
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000070/// hasFunctionProto - Return true if the given decl has a argument
71/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000072/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000073static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000074 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000075 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000076 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000077}
78
Alp Toker601b22c2014-01-21 23:35:24 +000079/// getFunctionOrMethodNumParams - Return number of function or method
80/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000081/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000082static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000083 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000084 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000086 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088}
89
Alp Toker601b22c2014-01-21 23:35:24 +000090static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000091 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000092 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000094 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000095
Alp Toker03376dc2014-07-07 09:02:20 +000096 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000097}
98
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000099static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
100 if (const auto *FD = dyn_cast<FunctionDecl>(D))
101 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000102 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000103 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000104 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000105 return BD->getParamDecl(Idx)->getSourceRange();
106 return SourceRange();
107}
108
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000109static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000110 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000111 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000112 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000113}
114
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000115static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
116 if (const auto *FD = dyn_cast<FunctionDecl>(D))
117 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000118 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000119 return MD->getReturnTypeSourceRange();
120 return SourceRange();
121}
122
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000124 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000125 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000127 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
129 return BD->isVariadic();
130
131 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000132}
133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static bool isInstanceMethod(const Decl *D) {
135 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000136 return MethodDecl->isInstance();
137 return false;
138}
139
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000140static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000141 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000142 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000144
John McCall96fa4842010-05-17 21:00:27 +0000145 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
146 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000147 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000148
John McCall96fa4842010-05-17 21:00:27 +0000149 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000150
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000151 // FIXME: Should we walk the chain of classes?
152 return ClsName == &Ctx.Idents.get("NSString") ||
153 ClsName == &Ctx.Idents.get("NSMutableString");
154}
155
Daniel Dunbar980c6692008-09-26 03:32:58 +0000156static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000157 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000158 if (!PT)
159 return false;
160
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000161 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000162 if (!RT)
163 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000164
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000166 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000167 return false;
168
169 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
170}
171
Richard Smithb87c4652013-10-31 21:23:20 +0000172static unsigned getNumAttributeArgs(const AttributeList &Attr) {
173 // FIXME: Include the type in the argument list.
174 return Attr.getNumArgs() + Attr.hasParsedType();
175}
176
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000177template <typename Compare>
178static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
179 unsigned Num, unsigned Diag,
180 Compare Comp) {
181 if (Comp(getNumAttributeArgs(Attr), Num)) {
182 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000183 return false;
184 }
185
186 return true;
187}
188
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000189/// \brief Check if the attribute has exactly as many args as Num. May
190/// output an error.
191static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
192 unsigned Num) {
193 return checkAttributeNumArgsImpl(S, Attr, Num,
194 diag::err_attribute_wrong_number_arguments,
195 std::not_equal_to<unsigned>());
196}
197
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000198/// \brief Check if the attribute has at least as many args as Num. May
199/// output an error.
200static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000201 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000202 return checkAttributeNumArgsImpl(S, Attr, Num,
203 diag::err_attribute_too_few_arguments,
204 std::less<unsigned>());
205}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000206
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000207/// \brief Check if the attribute has at most as many args as Num. May
208/// output an error.
209static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
210 unsigned Num) {
211 return checkAttributeNumArgsImpl(S, Attr, Num,
212 diag::err_attribute_too_many_arguments,
213 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214}
215
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000216/// \brief If Expr is a valid integer constant, get the value of the integer
217/// expression and return success or failure. May output an error.
218static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
219 const Expr *Expr, uint32_t &Val,
220 unsigned Idx = UINT_MAX) {
221 llvm::APSInt I(32);
222 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
223 !Expr->isIntegerConstantExpr(I, S.Context)) {
224 if (Idx != UINT_MAX)
225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
226 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
227 << Expr->getSourceRange();
228 else
229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
230 << Attr.getName() << AANT_ArgumentIntegerConstant
231 << Expr->getSourceRange();
232 return false;
233 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000234
235 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000236 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
237 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000238 return false;
239 }
240
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000241 Val = (uint32_t)I.getZExtValue();
242 return true;
243}
244
Aaron Ballmanfb763042013-12-02 18:05:46 +0000245/// \brief Diagnose mutually exclusive attributes when present on a given
246/// declaration. Returns true if diagnosed.
247template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000248static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
249 IdentifierInfo *Ident) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000250 if (AttrTy *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000251 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
252 << A;
253 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000254 return true;
255 }
256 return false;
257}
258
Alp Toker601b22c2014-01-21 23:35:24 +0000259/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000260/// instance method D. May output an error.
261///
262/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000263static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
264 const AttributeList &Attr,
265 unsigned AttrArgNum,
266 const Expr *IdxExpr,
267 uint64_t &Idx) {
David Majnemer06864812015-04-07 06:01:53 +0000268 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000269
270 // In C++ the implicit 'this' function parameter also counts.
271 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000272 bool HP = hasFunctionProto(D);
273 bool HasImplicitThisParam = isInstanceMethod(D);
274 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000275 unsigned NumParams =
276 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000277
278 llvm::APSInt IdxInt;
279 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
280 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
282 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
283 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000284 return false;
285 }
286
287 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000288 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000289 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
290 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 return false;
292 }
293 Idx--; // Convert to zero-based.
294 if (HasImplicitThisParam) {
295 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000296 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000297 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000298 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000299 return false;
300 }
301 --Idx;
302 }
303
304 return true;
305}
306
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000307/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
308/// If not emit an error and return false. If the argument is an identifier it
309/// will emit an error with a fixit hint and treat it as if it was a string
310/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000311bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
312 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000313 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000314 // Look for identifiers. If we have one emit a hint to fix it to a literal.
315 if (Attr.isArgIdent(ArgNum)) {
316 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000317 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000318 << Attr.getName() << AANT_ArgumentString
319 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000320 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000321 Str = Loc->Ident->getName();
322 if (ArgLocation)
323 *ArgLocation = Loc->Loc;
324 return true;
325 }
326
327 // Now check for an actual string literal.
328 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
329 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
330 if (ArgLocation)
331 *ArgLocation = ArgExpr->getLocStart();
332
333 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000334 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000335 << Attr.getName() << AANT_ArgumentString;
336 return false;
337 }
338
339 Str = Literal->getString();
340 return true;
341}
342
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000343/// \brief Applies the given attribute to the Decl without performing any
344/// additional semantic checking.
345template <typename AttrType>
346static void handleSimpleAttribute(Sema &S, Decl *D,
347 const AttributeList &Attr) {
348 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
349 Attr.getAttributeSpellingListIndex()));
350}
351
Justin Lebar3eaaf862016-01-13 01:07:35 +0000352template <typename AttrType>
353static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
354 const AttributeList &Attr) {
355 handleSimpleAttribute<AttrType>(S, D, Attr);
356}
357
358/// \brief Applies the given attribute to the Decl so long as the Decl doesn't
359/// already have one of the given incompatible attributes.
360template <typename AttrType, typename IncompatibleAttrType,
361 typename... IncompatibleAttrTypes>
362static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
363 const AttributeList &Attr) {
364 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(),
365 Attr.getName()))
366 return;
367 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
368 Attr);
369}
370
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000371/// \brief Check if the passed-in expression is of type int or bool.
372static bool isIntOrBool(Expr *Exp) {
373 QualType QT = Exp->getType();
374 return QT->isBooleanType() || QT->isIntegerType();
375}
376
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000377
378// Check to see if the type is a smart pointer of some kind. We assume
379// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000380static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000381 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
382 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000383 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000384 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000385
Richard Smithcf4bdde2015-02-21 02:45:19 +0000386 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
387 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000388 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000389 return false;
390
391 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000392}
393
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000394/// \brief Check if passed in Decl is a pointer type.
395/// Note that this function may produce an error message.
396/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000397static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
398 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000399 const ValueDecl *vd = cast<ValueDecl>(D);
400 QualType QT = vd->getType();
401 if (QT->isAnyPointerType())
402 return true;
403
404 if (const RecordType *RT = QT->getAs<RecordType>()) {
405 // If it's an incomplete type, it could be a smart pointer; skip it.
406 // (We don't want to force template instantiation if we can avoid it,
407 // since that would alter the order in which templates are instantiated.)
408 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000409 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000410
Aaron Ballman553e6812013-12-26 14:54:11 +0000411 if (threadSafetyCheckIsSmartPointer(S, RT))
412 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000413 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000414
415 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000416 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000417 return false;
418}
419
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000420/// \brief Checks that the passed in QualType either is of RecordType or points
421/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000422static const RecordType *getRecordType(QualType QT) {
423 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000424 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000425
426 // Now check if we point to record type.
427 if (const PointerType *PT = QT->getAs<PointerType>())
428 return PT->getPointeeType()->getAs<RecordType>();
429
Craig Topperc3ec1492014-05-26 06:22:03 +0000430 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000431}
432
Aaron Ballman76050722014-04-04 15:13:57 +0000433static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000435
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000436 if (!RT)
437 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000438
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000439 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000440 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000441 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000442
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000443 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000444 // FIXME -- Check the type that the smart pointer points to.
445 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000446 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000447
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000448 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000449 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000450 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000451 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000453 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000454 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
455 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000456 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
457 const auto *Type = BS->getType()->getAs<RecordType>();
458 return Type->getDecl()->hasAttr<CapabilityAttr>();
459 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000460 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000461 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000462 return false;
463}
464
Aaron Ballman76050722014-04-04 15:13:57 +0000465static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000466 const auto *TD = Ty->getAs<TypedefType>();
467 if (!TD)
468 return false;
469
470 TypedefNameDecl *TN = TD->getDecl();
471 if (!TN)
472 return false;
473
474 return TN->hasAttr<CapabilityAttr>();
475}
476
Aaron Ballman76050722014-04-04 15:13:57 +0000477static bool typeHasCapability(Sema &S, QualType Ty) {
478 if (checkTypedefTypeForCapability(Ty))
479 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000480
Aaron Ballman76050722014-04-04 15:13:57 +0000481 if (checkRecordTypeForCapability(S, Ty))
482 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000483
Aaron Ballman76050722014-04-04 15:13:57 +0000484 return false;
485}
486
487static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
488 // Capability expressions are simple expressions involving the boolean logic
489 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
490 // a DeclRefExpr is found, its type should be checked to determine whether it
491 // is a capability or not.
492
493 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
494 return typeHasCapability(S, E->getType());
495 else if (const auto *E = dyn_cast<CastExpr>(Ex))
496 return isCapabilityExpr(S, E->getSubExpr());
497 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
498 return isCapabilityExpr(S, E->getSubExpr());
499 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
500 if (E->getOpcode() == UO_LNot)
501 return isCapabilityExpr(S, E->getSubExpr());
502 return false;
503 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
504 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
505 return isCapabilityExpr(S, E->getLHS()) &&
506 isCapabilityExpr(S, E->getRHS());
507 return false;
508 }
509
510 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000511}
512
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000513/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
514/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515/// \param Sidx The attribute argument index to start checking with.
516/// \param ParamIdxOk Whether an argument can be indexing into a function
517/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000518static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
519 const AttributeList &Attr,
520 SmallVectorImpl<Expr *> &Args,
521 int Sidx = 0,
522 bool ParamIdxOk = false) {
523 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000524 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000525
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000526 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000527 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000528 Args.push_back(ArgExp);
529 continue;
530 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000531
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000532 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000533 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000534 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000535 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000536 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000537 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000538 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000539 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000540
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000541 // We allow constant strings to be used as a placeholder for expressions
542 // that are not valid C++ syntax, but warn that they are ignored.
543 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
544 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000545 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000546 continue;
547 }
548
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000549 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000551 // A pointer to member expression of the form &MyClass::mu is treated
552 // specially -- we need to look at the type of the member.
553 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
554 if (UOp->getOpcode() == UO_AddrOf)
555 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
556 if (DRE->getDecl()->isCXXInstanceMember())
557 ArgTy = DRE->getDecl()->getType();
558
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000559 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000560 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000561
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000562 // Now check if we index into a record type function param.
563 if(!RT && ParamIdxOk) {
564 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000565 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
566 if(FD && IL) {
567 unsigned int NumParams = FD->getNumParams();
568 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000569 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
570 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
571 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000572 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
573 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000574 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000575 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000576 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000577 }
578 }
579
Aaron Ballman76050722014-04-04 15:13:57 +0000580 // If the type does not have a capability, see if the components of the
581 // expression have capabilities. This allows for writing C code where the
582 // capability may be on the type, and the expression is a capability
583 // boolean logic expression. Eg) requires_capability(A || B && !C)
584 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
585 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
586 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000587
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000588 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000590}
591
Chris Lattner58418ff2008-06-29 00:16:31 +0000592//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000593// Attribute Implementations
594//===----------------------------------------------------------------------===//
595
Michael Hana9171bc2012-08-03 17:40:43 +0000596static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000597 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000598 if (!threadSafetyCheckIsPointer(S, D, Attr))
599 return;
600
Michael Han99315932013-01-24 16:46:58 +0000601 D->addAttr(::new (S.Context)
602 PtGuardedVarAttr(Attr.getRange(), S.Context,
603 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000604}
605
Michael Hana9171bc2012-08-03 17:40:43 +0000606static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
607 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000608 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000609 SmallVector<Expr*, 1> Args;
610 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000611 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000612 unsigned Size = Args.size();
613 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000614 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000615
Michael Han3be3b442012-07-23 18:48:41 +0000616 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000617
Michael Han3be3b442012-07-23 18:48:41 +0000618 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000619}
620
Michael Han3be3b442012-07-23 18:48:41 +0000621static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000622 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000623 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
624 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000625
Aaron Ballman36a53502014-01-16 13:03:14 +0000626 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
627 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000628}
629
Michael Hana9171bc2012-08-03 17:40:43 +0000630static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000631 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000632 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000633 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
634 return;
635
636 if (!threadSafetyCheckIsPointer(S, D, Attr))
637 return;
638
639 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000640 S.Context, Arg,
641 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000642}
643
Michael Hana9171bc2012-08-03 17:40:43 +0000644static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
645 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000646 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000647 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000648 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000649
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000650 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000651 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000652 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
653 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
654 << Attr.getName();
655 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000656 }
657
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000658 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000659 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000660 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000661 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000662
Michael Han3be3b442012-07-23 18:48:41 +0000663 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000664}
665
Michael Hana9171bc2012-08-03 17:40:43 +0000666static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000667 const AttributeList &Attr) {
668 SmallVector<Expr*, 1> Args;
669 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
670 return;
671
672 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000673 D->addAttr(::new (S.Context)
674 AcquiredAfterAttr(Attr.getRange(), S.Context,
675 StartArg, Args.size(),
676 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000677}
678
Michael Hana9171bc2012-08-03 17:40:43 +0000679static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000680 const AttributeList &Attr) {
681 SmallVector<Expr*, 1> Args;
682 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
683 return;
684
685 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000686 D->addAttr(::new (S.Context)
687 AcquiredBeforeAttr(Attr.getRange(), S.Context,
688 StartArg, Args.size(),
689 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000690}
691
Michael Hana9171bc2012-08-03 17:40:43 +0000692static bool checkLockFunAttrCommon(Sema &S, Decl *D,
693 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000694 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000695 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000696 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000697 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000698
Michael Han3be3b442012-07-23 18:48:41 +0000699 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000700}
701
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000702static void handleAssertSharedLockAttr(Sema &S, Decl *D,
703 const AttributeList &Attr) {
704 SmallVector<Expr*, 1> Args;
705 if (!checkLockFunAttrCommon(S, D, Attr, Args))
706 return;
707
708 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000709 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000710 D->addAttr(::new (S.Context)
711 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
712 Attr.getAttributeSpellingListIndex()));
713}
714
715static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
716 const AttributeList &Attr) {
717 SmallVector<Expr*, 1> Args;
718 if (!checkLockFunAttrCommon(S, D, Attr, Args))
719 return;
720
721 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000722 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000723 D->addAttr(::new (S.Context)
724 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
725 StartArg, Size,
726 Attr.getAttributeSpellingListIndex()));
727}
728
729
Michael Hana9171bc2012-08-03 17:40:43 +0000730static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
731 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000732 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000733 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000734 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000735
Aaron Ballman00e99962013-08-31 01:11:41 +0000736 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000738 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000739 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000740 }
741
742 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000743 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000744
Michael Han3be3b442012-07-23 18:48:41 +0000745 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000746}
747
Michael Hana9171bc2012-08-03 17:40:43 +0000748static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000749 const AttributeList &Attr) {
750 SmallVector<Expr*, 2> Args;
751 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
752 return;
753
Michael Han99315932013-01-24 16:46:58 +0000754 D->addAttr(::new (S.Context)
755 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000756 Attr.getArgAsExpr(0),
757 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000758 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000759}
760
Michael Hana9171bc2012-08-03 17:40:43 +0000761static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000762 const AttributeList &Attr) {
763 SmallVector<Expr*, 2> Args;
764 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
765 return;
766
Nico Weber462fd1e2015-01-07 23:50:05 +0000767 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
768 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
769 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000770}
771
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000772static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000773 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000774 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000775 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000776 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000777 unsigned Size = Args.size();
778 if (Size == 0)
779 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000780
Michael Han99315932013-01-24 16:46:58 +0000781 D->addAttr(::new (S.Context)
782 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
783 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784}
785
786static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000788 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000789 return;
790
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000791 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000792 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000793 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000794 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000795 if (Size == 0)
796 return;
797 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000798
Michael Han99315932013-01-24 16:46:58 +0000799 D->addAttr(::new (S.Context)
800 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
801 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000802}
803
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000804static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
805 Expr *Cond = Attr.getArgAsExpr(0);
806 if (!Cond->isTypeDependent()) {
807 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
808 if (Converted.isInvalid())
809 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000810 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000811 }
812
813 StringRef Msg;
814 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
815 return;
816
817 SmallVector<PartialDiagnosticAt, 8> Diags;
818 if (!Cond->isValueDependent() &&
819 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
820 Diags)) {
821 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
822 for (int I = 0, N = Diags.size(); I != N; ++I)
823 S.Diag(Diags[I].first, Diags[I].second);
824 return;
825 }
826
827 D->addAttr(::new (S.Context)
828 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
829 Attr.getAttributeSpellingListIndex()));
830}
831
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000832static void handlePassObjectSizeAttr(Sema &S, Decl *D,
833 const AttributeList &Attr) {
834 if (D->hasAttr<PassObjectSizeAttr>()) {
835 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
836 << Attr.getName();
837 return;
838 }
839
840 Expr *E = Attr.getArgAsExpr(0);
841 uint32_t Type;
842 if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1))
843 return;
844
845 // pass_object_size's argument is passed in as the second argument of
846 // __builtin_object_size. So, it has the same constraints as that second
847 // argument; namely, it must be in the range [0, 3].
848 if (Type > 3) {
849 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
850 << Attr.getName() << 0 << 3 << E->getSourceRange();
851 return;
852 }
853
854 // pass_object_size is only supported on constant pointer parameters; as a
855 // kindness to users, we allow the parameter to be non-const for declarations.
856 // At this point, we have no clue if `D` belongs to a function declaration or
857 // definition, so we defer the constness check until later.
858 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
859 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
860 << Attr.getName() << 1;
861 return;
862 }
863
864 D->addAttr(::new (S.Context)
865 PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type,
866 Attr.getAttributeSpellingListIndex()));
867}
868
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000869static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000870 ConsumableAttr::ConsumedState DefaultState;
871
872 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000873 IdentifierLoc *IL = Attr.getArgAsIdent(0);
874 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
875 DefaultState)) {
876 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
877 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000878 return;
879 }
David Blaikie16f76d22013-09-06 01:28:43 +0000880 } else {
881 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
882 << Attr.getName() << AANT_ArgumentIdentifier;
883 return;
884 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000885
886 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000887 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000888 Attr.getAttributeSpellingListIndex()));
889}
890
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000891
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000892static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
893 const AttributeList &Attr) {
894 ASTContext &CurrContext = S.getASTContext();
895 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
896
897 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
898 if (!RD->hasAttr<ConsumableAttr>()) {
899 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
900 RD->getNameAsString();
901
902 return false;
903 }
904 }
905
906 return true;
907}
908
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000909
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000910static void handleCallableWhenAttr(Sema &S, Decl *D,
911 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000912 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
913 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000914
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000915 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
916 return;
917
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000918 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
919 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
920 CallableWhenAttr::ConsumedState CallableState;
921
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000922 StringRef StateString;
923 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000924 if (Attr.isArgIdent(ArgIndex)) {
925 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
926 StateString = Ident->Ident->getName();
927 Loc = Ident->Loc;
928 } else {
929 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
930 return;
931 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000932
933 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000934 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000935 S.Diag(Loc, diag::warn_attribute_type_not_supported)
936 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000937 return;
938 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000939
940 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000941 }
942
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000943 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000944 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
945 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000946}
947
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000948
DeLesley Hutchins69391772013-10-17 23:23:53 +0000949static void handleParamTypestateAttr(Sema &S, Decl *D,
950 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000951 ParamTypestateAttr::ConsumedState ParamState;
952
953 if (Attr.isArgIdent(0)) {
954 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
955 StringRef StateString = Ident->Ident->getName();
956
957 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
958 ParamState)) {
959 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
960 << Attr.getName() << StateString;
961 return;
962 }
963 } else {
964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
965 Attr.getName() << AANT_ArgumentIdentifier;
966 return;
967 }
968
969 // FIXME: This check is currently being done in the analysis. It can be
970 // enabled here only after the parser propagates attributes at
971 // template specialization definition, not declaration.
972 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
973 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
974 //
975 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
976 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
977 // ReturnType.getAsString();
978 // return;
979 //}
980
981 D->addAttr(::new (S.Context)
982 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
983 Attr.getAttributeSpellingListIndex()));
984}
985
986
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000987static void handleReturnTypestateAttr(Sema &S, Decl *D,
988 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000989 ReturnTypestateAttr::ConsumedState ReturnState;
990
991 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000992 IdentifierLoc *IL = Attr.getArgAsIdent(0);
993 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
994 ReturnState)) {
995 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
996 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000997 return;
998 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000999 } else {
1000 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1001 Attr.getName() << AANT_ArgumentIdentifier;
1002 return;
1003 }
1004
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001005 // FIXME: This check is currently being done in the analysis. It can be
1006 // enabled here only after the parser propagates attributes at
1007 // template specialization definition, not declaration.
1008 //QualType ReturnType;
1009 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001010 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1011 // ReturnType = Param->getType();
1012 //
1013 //} else if (const CXXConstructorDecl *Constructor =
1014 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001015 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1016 //
1017 //} else {
1018 //
1019 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1020 //}
1021 //
1022 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1023 //
1024 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1025 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1026 // ReturnType.getAsString();
1027 // return;
1028 //}
1029
1030 D->addAttr(::new (S.Context)
1031 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1032 Attr.getAttributeSpellingListIndex()));
1033}
1034
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001035
1036static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001037 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1038 return;
1039
1040 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001041 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001042 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1043 StringRef Param = Ident->Ident->getName();
1044 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1045 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1046 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001047 return;
1048 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001049 } else {
1050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1051 Attr.getName() << AANT_ArgumentIdentifier;
1052 return;
1053 }
1054
1055 D->addAttr(::new (S.Context)
1056 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1057 Attr.getAttributeSpellingListIndex()));
1058}
1059
Chris Wailes9385f9f2013-10-29 20:28:41 +00001060static void handleTestTypestateAttr(Sema &S, Decl *D,
1061 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001062 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1063 return;
1064
Chris Wailes9385f9f2013-10-29 20:28:41 +00001065 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001066 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001067 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1068 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001069 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001070 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1071 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001072 return;
1073 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001074 } else {
1075 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1076 Attr.getName() << AANT_ArgumentIdentifier;
1077 return;
1078 }
1079
1080 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001081 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001082 Attr.getAttributeSpellingListIndex()));
1083}
1084
Chandler Carruthedc2c642011-07-02 00:01:44 +00001085static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1086 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001087 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001088 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001089}
1090
Chandler Carruthedc2c642011-07-02 00:01:44 +00001091static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001092 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001093 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1094 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001095 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001096 // Report warning about changed offset in the newer compiler versions.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001097 if (!FD->getType()->isDependentType() &&
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001098 !FD->getType()->isIncompleteType() && FD->isBitField() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001099 S.Context.getTypeAlign(FD->getType()) <= 8)
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001100 S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield);
1101
1102 FD->addAttr(::new (S.Context) PackedAttr(
1103 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001104 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001105 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001106}
1107
Ted Kremenek7fd17232011-09-29 07:02:25 +00001108static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1109 // The IBOutlet/IBOutletCollection attributes only apply to instance
1110 // variables or properties of Objective-C classes. The outlet must also
1111 // have an object reference type.
1112 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1113 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001114 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001115 << Attr.getName() << VD->getType() << 0;
1116 return false;
1117 }
1118 }
1119 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1120 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001121 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001122 << Attr.getName() << PD->getType() << 1;
1123 return false;
1124 }
1125 }
1126 else {
1127 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1128 return false;
1129 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001130
Ted Kremenek7fd17232011-09-29 07:02:25 +00001131 return true;
1132}
1133
Chandler Carruthedc2c642011-07-02 00:01:44 +00001134static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001135 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001136 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001137
Michael Han99315932013-01-24 16:46:58 +00001138 D->addAttr(::new (S.Context)
1139 IBOutletAttr(Attr.getRange(), S.Context,
1140 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001141}
1142
Chandler Carruthedc2c642011-07-02 00:01:44 +00001143static void handleIBOutletCollection(Sema &S, Decl *D,
1144 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001145
1146 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001147 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1149 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001150 return;
1151 }
1152
Ted Kremenek7fd17232011-09-29 07:02:25 +00001153 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001154 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001155
Richard Smithb1f9a282013-10-31 01:56:18 +00001156 ParsedType PT;
1157
1158 if (Attr.hasParsedType())
1159 PT = Attr.getTypeArg();
1160 else {
1161 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1162 S.getScopeForContext(D->getDeclContext()->getParent()));
1163 if (!PT) {
1164 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1165 return;
1166 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001167 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001168
Craig Topperc3ec1492014-05-26 06:22:03 +00001169 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001170 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1171 if (!QTLoc)
1172 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001173
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001174 // Diagnose use of non-object type in iboutletcollection attribute.
1175 // FIXME. Gnu attribute extension ignores use of builtin types in
1176 // attributes. So, __attribute__((iboutletcollection(char))) will be
1177 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001178 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001179 S.Diag(Attr.getLoc(),
1180 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1181 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001182 return;
1183 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001184
Michael Han99315932013-01-24 16:46:58 +00001185 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001186 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001187 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001188}
1189
Hal Finkelee90a222014-09-26 05:04:30 +00001190bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1191 if (RefOkay) {
1192 if (T->isReferenceType())
1193 return true;
1194 } else {
1195 T = T.getNonReferenceType();
1196 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001197
Hal Finkelee90a222014-09-26 05:04:30 +00001198 // The nonnull attribute, and other similar attributes, can be applied to a
1199 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001200 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001201 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1202 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001203 for (const auto *I : UD->fields()) {
1204 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001205 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1206 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001207 }
1208 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001209 }
1210
1211 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001212}
1213
Ted Kremenek9aedc152014-01-17 06:24:56 +00001214static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001215 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001216 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001217 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001218 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001219 if (isReturnValue)
1220 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1221 << Attr.getName() << AttrParmRange << TypeRange;
1222 else
1223 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1224 << Attr.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001225 return false;
1226 }
1227 return true;
1228}
1229
Chandler Carruthedc2c642011-07-02 00:01:44 +00001230static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001231 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001232 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1233 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001234 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001235 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001237
1238 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001239 if (Idx < getFunctionOrMethodNumParams(D) &&
1240 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001241 Ex->getSourceRange(),
1242 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001243 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001244
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001245 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001246 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001247
1248 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001249 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1250 // check if the attribute came from a macro expansion or a template
1251 // instantiation.
1252 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1253 S.ActiveTemplateInstantiations.empty()) {
1254 bool AnyPointers = isFunctionOrMethodVariadic(D);
1255 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1256 I != E && !AnyPointers; ++I) {
1257 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001258 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001259 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Richard Smith588bd9b2014-08-27 04:59:42 +00001262 if (!AnyPointers)
1263 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001264 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001265
Richard Smith588bd9b2014-08-27 04:59:42 +00001266 unsigned *Start = NonNullArgs.data();
1267 unsigned Size = NonNullArgs.size();
1268 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001269 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001270 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001271 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001272}
1273
Jordan Rosec9399072014-02-11 17:27:59 +00001274static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1275 const AttributeList &Attr) {
1276 if (Attr.getNumArgs() > 0) {
1277 if (D->getFunctionType()) {
1278 handleNonNullAttr(S, D, Attr);
1279 } else {
1280 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1281 << D->getSourceRange();
1282 }
1283 return;
1284 }
1285
1286 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001287 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1288 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001289 return;
1290
1291 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001292 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001293 Attr.getAttributeSpellingListIndex()));
1294}
1295
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001296static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1297 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001298 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001299 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1300 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001301 /* isReturnValue */ true))
1302 return;
1303
1304 D->addAttr(::new (S.Context)
1305 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1306 Attr.getAttributeSpellingListIndex()));
1307}
1308
Hal Finkelee90a222014-09-26 05:04:30 +00001309static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1310 const AttributeList &Attr) {
1311 Expr *E = Attr.getArgAsExpr(0),
1312 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1313 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1314 Attr.getAttributeSpellingListIndex());
1315}
1316
1317void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1318 Expr *OE, unsigned SpellingListIndex) {
1319 QualType ResultType = getFunctionOrMethodResultType(D);
1320 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1321
1322 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1323 SourceLocation AttrLoc = AttrRange.getBegin();
1324
1325 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1326 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1327 << &TmpAttr << AttrRange << SR;
1328 return;
1329 }
1330
1331 if (!E->isValueDependent()) {
1332 llvm::APSInt I(64);
1333 if (!E->isIntegerConstantExpr(I, Context)) {
1334 if (OE)
1335 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1336 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1337 << E->getSourceRange();
1338 else
1339 Diag(AttrLoc, diag::err_attribute_argument_type)
1340 << &TmpAttr << AANT_ArgumentIntegerConstant
1341 << E->getSourceRange();
1342 return;
1343 }
1344
1345 if (!I.isPowerOf2()) {
1346 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1347 << E->getSourceRange();
1348 return;
1349 }
1350 }
1351
1352 if (OE) {
1353 if (!OE->isValueDependent()) {
1354 llvm::APSInt I(64);
1355 if (!OE->isIntegerConstantExpr(I, Context)) {
1356 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1357 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1358 << OE->getSourceRange();
1359 return;
1360 }
1361 }
1362 }
1363
1364 D->addAttr(::new (Context)
1365 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1366}
1367
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001368/// Normalize the attribute, __foo__ becomes foo.
1369/// Returns true if normalization was applied.
1370static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001371 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1372 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001373 AttrName = AttrName.drop_front(2).drop_back(2);
1374 return true;
1375 }
1376 return false;
1377}
1378
Chandler Carruthedc2c642011-07-02 00:01:44 +00001379static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001380 // This attribute must be applied to a function declaration. The first
1381 // argument to the attribute must be an identifier, the name of the resource,
1382 // for example: malloc. The following arguments must be argument indexes, the
1383 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001385 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001386 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001387
Aaron Ballman00e99962013-08-31 01:11:41 +00001388 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001389 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001390 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001391 return;
1392 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001393
Richard Smith852e9ce2013-11-27 01:46:48 +00001394 // Figure out our Kind.
1395 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001396 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001397 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001398
Richard Smith852e9ce2013-11-27 01:46:48 +00001399 // Check arguments.
1400 switch (K) {
1401 case OwnershipAttr::Takes:
1402 case OwnershipAttr::Holds:
1403 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001404 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1405 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001406 return;
1407 }
1408 break;
1409 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001410 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001411 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1412 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001413 return;
1414 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001415 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001416 }
1417
Richard Smith852e9ce2013-11-27 01:46:48 +00001418 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001419
Richard Smith852e9ce2013-11-27 01:46:48 +00001420 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001421 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001422 Module = &S.PP.getIdentifierTable().get(ModuleName);
1423 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001424
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001425 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001426 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1427 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001429 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001430 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001431
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001432 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001433 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001434 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001435 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001436 case OwnershipAttr::Takes:
1437 case OwnershipAttr::Holds:
1438 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1439 Err = 0;
1440 break;
1441 case OwnershipAttr::Returns:
1442 if (!T->isIntegerType())
1443 Err = 1;
1444 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001445 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001446 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001447 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001448 << Ex->getSourceRange();
1449 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001450 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001451
1452 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001453 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001454 // Cannot have two ownership attributes of different kinds for the same
1455 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001456 if (I->getOwnKind() != K && I->args_end() !=
1457 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001458 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001459 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001460 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001461 } else if (K == OwnershipAttr::Returns &&
1462 I->getOwnKind() == OwnershipAttr::Returns) {
1463 // A returns attribute conflicts with any other returns attribute using
1464 // a different index. Note, diagnostic reporting is 1-based, but stored
1465 // argument indexes are 0-based.
1466 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1467 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1468 << *(I->args_begin()) + 1;
1469 if (I->args_size())
1470 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1471 << (unsigned)Idx + 1 << Ex->getSourceRange();
1472 return;
1473 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001474 }
1475 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001476 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001477 }
1478
1479 unsigned* start = OwnershipArgs.data();
1480 unsigned size = OwnershipArgs.size();
1481 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001482
Michael Han99315932013-01-24 16:46:58 +00001483 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001484 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001485 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001486}
1487
Chandler Carruthedc2c642011-07-02 00:01:44 +00001488static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001489 // Check the attribute arguments.
1490 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1492 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001493 return;
1494 }
1495
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001496 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001497
Rafael Espindolac18086a2010-02-23 22:00:30 +00001498 // gcc rejects
1499 // class c {
1500 // static int a __attribute__((weakref ("v2")));
1501 // static int b() __attribute__((weakref ("f3")));
1502 // };
1503 // and ignores the attributes of
1504 // void f(void) {
1505 // static int a __attribute__((weakref ("v2")));
1506 // }
1507 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001508 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001509 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001510 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1511 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001512 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001513 }
1514
1515 // The GCC manual says
1516 //
1517 // At present, a declaration to which `weakref' is attached can only
1518 // be `static'.
1519 //
1520 // It also says
1521 //
1522 // Without a TARGET,
1523 // given as an argument to `weakref' or to `alias', `weakref' is
1524 // equivalent to `weak'.
1525 //
1526 // gcc 4.4.1 will accept
1527 // int a7 __attribute__((weakref));
1528 // as
1529 // int a7 __attribute__((weak));
1530 // This looks like a bug in gcc. We reject that for now. We should revisit
1531 // it if this behaviour is actually used.
1532
Rafael Espindolac18086a2010-02-23 22:00:30 +00001533 // GCC rejects
1534 // static ((alias ("y"), weakref)).
1535 // Should we? How to check that weakref is before or after alias?
1536
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001537 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1538 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1539 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001540 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001541 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001542 // GCC will accept anything as the argument of weakref. Should we
1543 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001544 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1545 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001546
Michael Han99315932013-01-24 16:46:58 +00001547 D->addAttr(::new (S.Context)
1548 WeakRefAttr(Attr.getRange(), S.Context,
1549 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001550}
1551
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001552static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1553 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001554 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001555 return;
1556
Douglas Gregore8bbc122011-09-02 00:18:52 +00001557 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001558 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1559 return;
1560 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001561 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
1562 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx);
1563 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001564
David Majnemer2dc81462015-01-19 09:00:28 +00001565 // Aliases should be on declarations, not definitions.
1566 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1567 if (FD->isThisDeclarationADefinition()) {
1568 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1569 return;
1570 }
1571 } else {
1572 const auto *VD = cast<VarDecl>(D);
1573 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1574 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1575 return;
1576 }
1577 }
1578
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001579 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001580
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001581 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001582 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001583}
1584
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001585static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001586 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001587 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001588
Michael Han99315932013-01-24 16:46:58 +00001589 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1590 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001591}
1592
1593static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001594 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001595 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001596
Michael Han99315932013-01-24 16:46:58 +00001597 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1598 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001599}
1600
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001601static void handleTLSModelAttr(Sema &S, Decl *D,
1602 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001603 StringRef Model;
1604 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001605 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001606 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001607 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001608
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001609 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001610 if (Model != "global-dynamic" && Model != "local-dynamic"
1611 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001612 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001613 return;
1614 }
1615
Michael Han99315932013-01-24 16:46:58 +00001616 D->addAttr(::new (S.Context)
1617 TLSModelAttr(Attr.getRange(), S.Context, Model,
1618 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001619}
1620
David Majnemer631a90b2015-02-04 07:23:21 +00001621static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1622 QualType ResultType = getFunctionOrMethodResultType(D);
1623 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1624 D->addAttr(::new (S.Context) RestrictAttr(
1625 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1626 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001627 }
1628
David Majnemer631a90b2015-02-04 07:23:21 +00001629 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1630 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001631}
1632
Chandler Carruthedc2c642011-07-02 00:01:44 +00001633static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001634 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001635 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001636 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001637 return;
1638 }
1639
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001640 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1641 Attr.getAttributeSpellingListIndex()))
1642 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001643}
1644
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001645static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1646 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1647 Attr.getName()))
1648 return;
1649
1650 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1651 Attr.getAttributeSpellingListIndex()));
1652}
1653
Chandler Carruthedc2c642011-07-02 00:01:44 +00001654static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001655 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001656
1657 if (S.CheckNoReturnAttr(attr)) return;
1658
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001659 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001660 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001661 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001662 return;
1663 }
1664
Michael Han99315932013-01-24 16:46:58 +00001665 D->addAttr(::new (S.Context)
1666 NoReturnAttr(attr.getRange(), S.Context,
1667 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001668}
1669
1670bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001671 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001672 attr.setInvalid();
1673 return true;
1674 }
1675
1676 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001677}
1678
Chandler Carruthedc2c642011-07-02 00:01:44 +00001679static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1680 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001681
1682 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1683 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001684 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001685 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001686 if (!VD || (!VD->getType()->isBlockPointerType() &&
1687 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001688 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001689 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001690 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001691 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001692 return;
1693 }
1694 }
1695
Michael Han99315932013-01-24 16:46:58 +00001696 D->addAttr(::new (S.Context)
1697 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1698 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001699}
1700
John Thompsoncdb847ba2010-08-09 21:53:52 +00001701// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001702static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001703/*
1704 Returning a Vector Class in Registers
1705
Eric Christopherbc638a82010-12-01 22:13:54 +00001706 According to the PPU ABI specifications, a class with a single member of
1707 vector type is returned in memory when used as the return value of a function.
1708 This results in inefficient code when implementing vector classes. To return
1709 the value in a single vector register, add the vecreturn attribute to the
1710 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001711
1712 Example:
1713
1714 struct Vector
1715 {
1716 __vector float xyzw;
1717 } __attribute__((vecreturn));
1718
1719 Vector Add(Vector lhs, Vector rhs)
1720 {
1721 Vector result;
1722 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1723 return result; // This will be returned in a register
1724 }
1725*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001726 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1727 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001728 return;
1729 }
1730
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001731 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001732 int count = 0;
1733
1734 if (!isa<CXXRecordDecl>(record)) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1736 return;
1737 }
1738
1739 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1740 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1741 return;
1742 }
1743
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001744 for (const auto *I : record->fields()) {
1745 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001746 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1747 return;
1748 }
1749 count++;
1750 }
1751
Michael Han99315932013-01-24 16:46:58 +00001752 D->addAttr(::new (S.Context)
1753 VecReturnAttr(Attr.getRange(), S.Context,
1754 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001755}
1756
Richard Smithe233fbf2013-01-28 22:42:45 +00001757static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1758 const AttributeList &Attr) {
1759 if (isa<ParmVarDecl>(D)) {
1760 // [[carries_dependency]] can only be applied to a parameter if it is a
1761 // parameter of a function declaration or lambda.
1762 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1763 S.Diag(Attr.getLoc(),
1764 diag::err_carries_dependency_param_not_function_decl);
1765 return;
1766 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001767 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001768
1769 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1770 Attr.getRange(), S.Context,
1771 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001772}
1773
Akira Hatanakac8667622015-11-06 23:56:15 +00001774static void handleNotTailCalledAttr(Sema &S, Decl *D,
1775 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001776 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1777 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001778 return;
1779
1780 D->addAttr(::new (S.Context) NotTailCalledAttr(
1781 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1782}
1783
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001784static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1785 const AttributeList &Attr) {
1786 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1787 Attr.getName()))
1788 return;
1789
1790 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1791 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1792}
1793
Chandler Carruthedc2c642011-07-02 00:01:44 +00001794static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001795 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001796 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001797 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001798 return;
1799 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001800 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001802 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001803 return;
1804 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001805
Michael Han99315932013-01-24 16:46:58 +00001806 D->addAttr(::new (S.Context)
1807 UsedAttr(Attr.getRange(), S.Context,
1808 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001809}
1810
Chandler Carruthedc2c642011-07-02 00:01:44 +00001811static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001812 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001813 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001814 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1815 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001816
Michael Han99315932013-01-24 16:46:58 +00001817 D->addAttr(::new (S.Context)
1818 ConstructorAttr(Attr.getRange(), S.Context, priority,
1819 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001820}
1821
Chandler Carruthedc2c642011-07-02 00:01:44 +00001822static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001823 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001824 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001825 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1826 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001827
Michael Han99315932013-01-24 16:46:58 +00001828 D->addAttr(::new (S.Context)
1829 DestructorAttr(Attr.getRange(), S.Context, priority,
1830 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001831}
1832
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001833template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001834static void handleAttrWithMessage(Sema &S, Decl *D,
1835 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001836 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001837 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001838 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001839 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001840
Michael Han99315932013-01-24 16:46:58 +00001841 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1842 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001843}
1844
Ted Kremenek438f8db2014-02-22 01:06:05 +00001845static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001846 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001847 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001848 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1849 << Attr.getName() << Attr.getRange();
1850 return;
1851 }
1852
Ted Kremenek28eace62013-11-23 01:01:34 +00001853 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001854 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1855 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001856}
1857
Jordy Rose740b0c22012-05-08 03:27:22 +00001858static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1859 IdentifierInfo *Platform,
1860 VersionTuple Introduced,
1861 VersionTuple Deprecated,
1862 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001863 StringRef PlatformName
1864 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1865 if (PlatformName.empty())
1866 PlatformName = Platform->getName();
1867
1868 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1869 // of these steps are needed).
1870 if (!Introduced.empty() && !Deprecated.empty() &&
1871 !(Introduced <= Deprecated)) {
1872 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1873 << 1 << PlatformName << Deprecated.getAsString()
1874 << 0 << Introduced.getAsString();
1875 return true;
1876 }
1877
1878 if (!Introduced.empty() && !Obsoleted.empty() &&
1879 !(Introduced <= Obsoleted)) {
1880 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1881 << 2 << PlatformName << Obsoleted.getAsString()
1882 << 0 << Introduced.getAsString();
1883 return true;
1884 }
1885
1886 if (!Deprecated.empty() && !Obsoleted.empty() &&
1887 !(Deprecated <= Obsoleted)) {
1888 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1889 << 2 << PlatformName << Obsoleted.getAsString()
1890 << 1 << Deprecated.getAsString();
1891 return true;
1892 }
1893
1894 return false;
1895}
1896
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001897/// \brief Check whether the two versions match.
1898///
1899/// If either version tuple is empty, then they are assumed to match. If
1900/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1901static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1902 bool BeforeIsOkay) {
1903 if (X.empty() || Y.empty())
1904 return true;
1905
1906 if (X == Y)
1907 return true;
1908
1909 if (BeforeIsOkay && X < Y)
1910 return true;
1911
1912 return false;
1913}
1914
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001915AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001916 IdentifierInfo *Platform,
1917 VersionTuple Introduced,
1918 VersionTuple Deprecated,
1919 VersionTuple Obsoleted,
1920 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001921 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001922 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001923 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001924 VersionTuple MergedIntroduced = Introduced;
1925 VersionTuple MergedDeprecated = Deprecated;
1926 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001927 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001928 bool OverrideOrImpl = false;
1929 switch (AMK) {
1930 case AMK_None:
1931 case AMK_Redeclaration:
1932 OverrideOrImpl = false;
1933 break;
1934
1935 case AMK_Override:
1936 case AMK_ProtocolImplementation:
1937 OverrideOrImpl = true;
1938 break;
1939 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001940
Rafael Espindolac67f2232012-05-10 02:50:16 +00001941 if (D->hasAttrs()) {
1942 AttrVec &Attrs = D->getAttrs();
1943 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1944 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1945 if (!OldAA) {
1946 ++i;
1947 continue;
1948 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001949
Rafael Espindolac67f2232012-05-10 02:50:16 +00001950 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1951 if (OldPlatform != Platform) {
1952 ++i;
1953 continue;
1954 }
1955
Tim Northover7a73cc72015-10-30 16:30:49 +00001956 // If there is an existing availability attribute for this platform that
1957 // is explicit and the new one is implicit use the explicit one and
1958 // discard the new implicit attribute.
1959 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1960 return nullptr;
1961 }
1962
1963 // If there is an existing attribute for this platform that is implicit
1964 // and the new attribute is explicit then erase the old one and
1965 // continue processing the attributes.
1966 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1967 Attrs.erase(Attrs.begin() + i);
1968 --e;
1969 continue;
1970 }
1971
Rafael Espindolac67f2232012-05-10 02:50:16 +00001972 FoundAny = true;
1973 VersionTuple OldIntroduced = OldAA->getIntroduced();
1974 VersionTuple OldDeprecated = OldAA->getDeprecated();
1975 VersionTuple OldObsoleted = OldAA->getObsoleted();
1976 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001977
Douglas Gregord2a713e2015-09-30 21:27:42 +00001978 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1979 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1980 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001981 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001982 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1983 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001984 int Which = -1;
1985 VersionTuple FirstVersion;
1986 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001987 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001988 Which = 0;
1989 FirstVersion = OldIntroduced;
1990 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001991 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001992 Which = 1;
1993 FirstVersion = Deprecated;
1994 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001995 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001996 Which = 2;
1997 FirstVersion = Obsoleted;
1998 SecondVersion = OldObsoleted;
1999 }
2000
2001 if (Which == -1) {
2002 Diag(OldAA->getLocation(),
2003 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002004 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2005 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002006 } else {
2007 Diag(OldAA->getLocation(),
2008 diag::warn_mismatched_availability_override)
2009 << Which
2010 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002011 << FirstVersion.getAsString() << SecondVersion.getAsString()
2012 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002013 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002014 if (AMK == AMK_Override)
2015 Diag(Range.getBegin(), diag::note_overridden_method);
2016 else
2017 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002018 } else {
2019 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2020 Diag(Range.getBegin(), diag::note_previous_attribute);
2021 }
2022
Rafael Espindolac67f2232012-05-10 02:50:16 +00002023 Attrs.erase(Attrs.begin() + i);
2024 --e;
2025 continue;
2026 }
2027
2028 VersionTuple MergedIntroduced2 = MergedIntroduced;
2029 VersionTuple MergedDeprecated2 = MergedDeprecated;
2030 VersionTuple MergedObsoleted2 = MergedObsoleted;
2031
2032 if (MergedIntroduced2.empty())
2033 MergedIntroduced2 = OldIntroduced;
2034 if (MergedDeprecated2.empty())
2035 MergedDeprecated2 = OldDeprecated;
2036 if (MergedObsoleted2.empty())
2037 MergedObsoleted2 = OldObsoleted;
2038
2039 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2040 MergedIntroduced2, MergedDeprecated2,
2041 MergedObsoleted2)) {
2042 Attrs.erase(Attrs.begin() + i);
2043 --e;
2044 continue;
2045 }
2046
2047 MergedIntroduced = MergedIntroduced2;
2048 MergedDeprecated = MergedDeprecated2;
2049 MergedObsoleted = MergedObsoleted2;
2050 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002051 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002052 }
2053
2054 if (FoundAny &&
2055 MergedIntroduced == Introduced &&
2056 MergedDeprecated == Deprecated &&
2057 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002058 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002059
Douglas Gregord2a713e2015-09-30 21:27:42 +00002060 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002061 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002062 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002063 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002064 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002065 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2066 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002067 Obsoleted, IsUnavailable, Message,
2068 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002069 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002070 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002071}
2072
Chandler Carruthedc2c642011-07-02 00:01:44 +00002073static void handleAvailabilityAttr(Sema &S, Decl *D,
2074 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002075 if (!checkAttributeNumArgs(S, Attr, 1))
2076 return;
2077 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002078 unsigned Index = Attr.getAttributeSpellingListIndex();
2079
Aaron Ballman00e99962013-08-31 01:11:41 +00002080 IdentifierInfo *II = Platform->Ident;
2081 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2082 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2083 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002084
Rafael Espindolac231fab2013-01-08 21:30:32 +00002085 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2086 if (!ND) {
2087 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2088 return;
2089 }
2090
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002091 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2092 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2093 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002094 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002095 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002096 if (const StringLiteral *SE =
2097 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002098 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002099
Aaron Ballman00e99962013-08-31 01:11:41 +00002100 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002101 Introduced.Version,
2102 Deprecated.Version,
2103 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002104 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002105 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002106 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002107 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002108 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002109
2110 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2111 // matches before the start of the watchOS platform.
2112 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2113 IdentifierInfo *NewII = nullptr;
2114 if (II->getName() == "ios")
2115 NewII = &S.Context.Idents.get("watchos");
2116 else if (II->getName() == "ios_app_extension")
2117 NewII = &S.Context.Idents.get("watchos_app_extension");
2118
2119 if (NewII) {
2120 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2121 if (Version.empty())
2122 return Version;
2123 auto Major = Version.getMajor();
2124 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2125 if (NewMajor >= 2) {
2126 if (Version.getMinor().hasValue()) {
2127 if (Version.getSubminor().hasValue())
2128 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2129 Version.getSubminor().getValue());
2130 else
2131 return VersionTuple(NewMajor, Version.getMinor().getValue());
2132 }
2133 }
2134
2135 return VersionTuple(2, 0);
2136 };
2137
2138 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2139 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2140 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2141
2142 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2143 SourceRange(),
2144 NewII,
2145 NewIntroduced,
2146 NewDeprecated,
2147 NewObsoleted,
2148 IsUnavailable, Str,
2149 Sema::AMK_None,
2150 Index);
2151 if (NewAttr)
2152 D->addAttr(NewAttr);
2153 }
2154 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2155 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2156 // matches before the start of the tvOS platform.
2157 IdentifierInfo *NewII = nullptr;
2158 if (II->getName() == "ios")
2159 NewII = &S.Context.Idents.get("tvos");
2160 else if (II->getName() == "ios_app_extension")
2161 NewII = &S.Context.Idents.get("tvos_app_extension");
2162
2163 if (NewII) {
2164 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2165 SourceRange(),
2166 NewII,
2167 Introduced.Version,
2168 Deprecated.Version,
2169 Obsoleted.Version,
2170 IsUnavailable, Str,
2171 Sema::AMK_None,
2172 Index);
2173 if (NewAttr)
2174 D->addAttr(NewAttr);
2175 }
2176 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002177}
2178
John McCalld041a9b2013-02-20 01:54:26 +00002179template <class T>
2180static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2181 typename T::VisibilityType value,
2182 unsigned attrSpellingListIndex) {
2183 T *existingAttr = D->getAttr<T>();
2184 if (existingAttr) {
2185 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2186 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002187 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002188 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2189 S.Diag(range.getBegin(), diag::note_previous_attribute);
2190 D->dropAttr<T>();
2191 }
2192 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2193}
2194
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002195VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002196 VisibilityAttr::VisibilityType Vis,
2197 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002198 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2199 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002200}
2201
John McCalld041a9b2013-02-20 01:54:26 +00002202TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2203 TypeVisibilityAttr::VisibilityType Vis,
2204 unsigned AttrSpellingListIndex) {
2205 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2206 AttrSpellingListIndex);
2207}
2208
2209static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2210 bool isTypeVisibility) {
2211 // Visibility attributes don't mean anything on a typedef.
2212 if (isa<TypedefNameDecl>(D)) {
2213 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2214 << Attr.getName();
2215 return;
2216 }
2217
2218 // 'type_visibility' can only go on a type or namespace.
2219 if (isTypeVisibility &&
2220 !(isa<TagDecl>(D) ||
2221 isa<ObjCInterfaceDecl>(D) ||
2222 isa<NamespaceDecl>(D))) {
2223 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2224 << Attr.getName() << ExpectedTypeOrNamespace;
2225 return;
2226 }
2227
Benjamin Kramer70370212013-09-09 15:08:57 +00002228 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002229 StringRef TypeStr;
2230 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002231 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002232 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002233
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002234 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002235 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002236 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002237 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002238 return;
2239 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002240
2241 // Complain about attempts to use protected visibility on targets
2242 // (like Darwin) that don't support it.
2243 if (type == VisibilityAttr::Protected &&
2244 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2245 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2246 type = VisibilityAttr::Default;
2247 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002248
Michael Han99315932013-01-24 16:46:58 +00002249 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002250 clang::Attr *newAttr;
2251 if (isTypeVisibility) {
2252 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2253 (TypeVisibilityAttr::VisibilityType) type,
2254 Index);
2255 } else {
2256 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2257 }
2258 if (newAttr)
2259 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002260}
2261
Chandler Carruthedc2c642011-07-02 00:01:44 +00002262static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2263 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002264 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002265 if (!Attr.isArgIdent(0)) {
2266 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2267 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002268 return;
2269 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002270
Aaron Ballman682ee422013-09-11 19:47:58 +00002271 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2272 ObjCMethodFamilyAttr::FamilyKind F;
2273 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2274 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2275 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002276 return;
2277 }
2278
Alp Toker314cc812014-01-25 16:55:45 +00002279 if (F == ObjCMethodFamilyAttr::OMF_init &&
2280 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002281 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002282 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002283 // Ignore the attribute.
2284 return;
2285 }
2286
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002287 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002288 S.Context, F,
2289 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002290}
2291
Chandler Carruthedc2c642011-07-02 00:01:44 +00002292static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002293 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002294 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002295 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002296 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2297 return;
2298 }
2299 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002300 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2301 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002302 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002303 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2304 return;
2305 }
2306 }
2307 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002308 // It is okay to include this attribute on properties, e.g.:
2309 //
2310 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2311 //
2312 // In this case it follows tradition and suppresses an error in the above
2313 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002314 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002315 }
Michael Han99315932013-01-24 16:46:58 +00002316 D->addAttr(::new (S.Context)
2317 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2318 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002319}
2320
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002321static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2322 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2323 QualType T = TD->getUnderlyingType();
2324 if (!T->isObjCObjectPointerType()) {
2325 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2326 return;
2327 }
2328 } else {
2329 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2330 return;
2331 }
2332 D->addAttr(::new (S.Context)
2333 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2334 Attr.getAttributeSpellingListIndex()));
2335}
2336
Chandler Carruthedc2c642011-07-02 00:01:44 +00002337static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002338 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002339 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002340 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002341 return;
2342 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002343
Aaron Ballman00e99962013-08-31 01:11:41 +00002344 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002345 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002346 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2347 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2348 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002349 return;
2350 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002351
Michael Han99315932013-01-24 16:46:58 +00002352 D->addAttr(::new (S.Context)
2353 BlocksAttr(Attr.getRange(), S.Context, type,
2354 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002355}
2356
Chandler Carruthedc2c642011-07-02 00:01:44 +00002357static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002358 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002359 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002360 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002361 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002362 if (E->isTypeDependent() || E->isValueDependent() ||
2363 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002364 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002365 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002366 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002367 return;
2368 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002369
John McCallb46f2872011-09-09 07:56:05 +00002370 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002371 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2372 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002373 return;
2374 }
John McCallb46f2872011-09-09 07:56:05 +00002375
2376 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002377 }
2378
Aaron Ballman18a78382013-11-21 00:28:23 +00002379 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002380 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002381 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002382 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002383 if (E->isTypeDependent() || E->isValueDependent() ||
2384 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002385 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002386 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002387 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002388 return;
2389 }
2390 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002391
John McCallb46f2872011-09-09 07:56:05 +00002392 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002393 // FIXME: This error message could be improved, it would be nice
2394 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002395 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2396 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002397 return;
2398 }
2399 }
2400
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002401 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002402 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002403 if (isa<FunctionNoProtoType>(FT)) {
2404 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2405 return;
2406 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002407
Chris Lattner9363e312009-03-17 23:03:47 +00002408 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002409 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002410 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002411 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002412 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002413 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002414 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002415 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002416 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002417 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2418 if (!BD->isVariadic()) {
2419 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2420 return;
2421 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002422 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002423 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002424 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002425 const FunctionType *FT = Ty->isFunctionPointerType()
2426 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002427 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002428 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002429 int m = Ty->isFunctionPointerType() ? 0 : 1;
2430 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002431 return;
2432 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002433 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002434 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002435 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002436 return;
2437 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002438 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002440 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002441 return;
2442 }
Michael Han99315932013-01-24 16:46:58 +00002443 D->addAttr(::new (S.Context)
2444 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2445 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002446}
2447
Chandler Carruthedc2c642011-07-02 00:01:44 +00002448static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002449 if (D->getFunctionType() &&
2450 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002451 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2452 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002453 return;
2454 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002455 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002456 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002457 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2458 << Attr.getName() << 1;
2459 return;
2460 }
2461
Michael Han99315932013-01-24 16:46:58 +00002462 D->addAttr(::new (S.Context)
2463 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2464 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002465}
2466
Chandler Carruthedc2c642011-07-02 00:01:44 +00002467static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002468 // weak_import only applies to variable & function declarations.
2469 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002470 if (!D->canBeWeakImported(isDef)) {
2471 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002472 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2473 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002474 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002475 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002476 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002477 // Nothing to warn about here.
2478 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002479 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002480 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002481
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002482 return;
2483 }
2484
Michael Han99315932013-01-24 16:46:58 +00002485 D->addAttr(::new (S.Context)
2486 WeakImportAttr(Attr.getRange(), S.Context,
2487 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002488}
2489
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002490// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002491template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002492static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002493 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002494 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002495 for (unsigned i = 0; i < 3; ++i) {
2496 const Expr *E = Attr.getArgAsExpr(i);
2497 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002498 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002499 if (WGSize[i] == 0) {
2500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2501 << Attr.getName() << E->getSourceRange();
2502 return;
2503 }
2504 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002505
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002506 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2507 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2508 Existing->getYDim() == WGSize[1] &&
2509 Existing->getZDim() == WGSize[2]))
2510 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002511
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002512 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2513 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002514 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002515}
2516
Joey Goulyaba589c2013-03-08 09:42:32 +00002517static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002518 if (!Attr.hasParsedType()) {
2519 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2520 << Attr.getName() << 1;
2521 return;
2522 }
2523
Craig Topperc3ec1492014-05-26 06:22:03 +00002524 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002525 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2526 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002527
2528 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2529 (ParmType->isBooleanType() ||
2530 !ParmType->isIntegralType(S.getASTContext()))) {
2531 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2532 << ParmType;
2533 return;
2534 }
2535
Aaron Ballmana9e05402013-12-02 22:16:55 +00002536 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002537 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002538 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2539 return;
2540 }
2541 }
2542
2543 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002544 ParmTSI,
2545 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002546}
2547
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002548SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002549 StringRef Name,
2550 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002551 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2552 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002553 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002554 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2555 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002556 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002557 }
Michael Han99315932013-01-24 16:46:58 +00002558 return ::new (Context) SectionAttr(Range, Context, Name,
2559 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002560}
2561
Reid Kleckner2a133222015-03-04 23:39:17 +00002562bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2563 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2564 if (!Error.empty()) {
2565 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2566 return false;
2567 }
2568 return true;
2569}
2570
Chandler Carruthedc2c642011-07-02 00:01:44 +00002571static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002572 // Make sure that there is a string literal as the sections's single
2573 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002574 StringRef Str;
2575 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002576 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002577 return;
Mike Stump11289f42009-09-09 15:08:12 +00002578
Reid Kleckner2a133222015-03-04 23:39:17 +00002579 if (!S.checkSectionName(LiteralLoc, Str))
2580 return;
2581
Chris Lattner30ba6742009-08-10 19:03:04 +00002582 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002583 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002584 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002585 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002586 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002587 return;
2588 }
Mike Stump11289f42009-09-09 15:08:12 +00002589
Michael Han99315932013-01-24 16:46:58 +00002590 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002591 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002592 if (NewAttr)
2593 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002594}
2595
Eric Christopher789a7ad2015-06-12 01:36:05 +00002596// Check for things we'd like to warn about, no errors or validation for now.
2597// TODO: Validation should use a backend target library that specifies
2598// the allowable subtarget features and cpus. We could use something like a
2599// TargetCodeGenInfo hook here to do validation.
2600void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2601 for (auto Str : {"tune=", "fpmath="})
2602 if (AttrStr.find(Str) != StringRef::npos)
2603 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2604}
2605
Eric Christopher11acf732015-06-12 01:35:52 +00002606static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002607 StringRef Str;
2608 SourceLocation LiteralLoc;
2609 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2610 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002611 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002612 unsigned Index = Attr.getAttributeSpellingListIndex();
2613 TargetAttr *NewAttr =
2614 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2615 D->addAttr(NewAttr);
2616}
2617
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618
Chandler Carruthedc2c642011-07-02 00:01:44 +00002619static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002620 VarDecl *VD = cast<VarDecl>(D);
2621 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002622 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002623 return;
2624 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002625
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002626 Expr *E = Attr.getArgAsExpr(0);
2627 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002628 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002629 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002630
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002631 // gcc only allows for simple identifiers. Since we support more than gcc, we
2632 // will warn the user.
2633 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2634 if (DRE->hasQualifier())
2635 S.Diag(Loc, diag::warn_cleanup_ext);
2636 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2637 NI = DRE->getNameInfo();
2638 if (!FD) {
2639 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2640 << NI.getName();
2641 return;
2642 }
2643 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2644 if (ULE->hasExplicitTemplateArgs())
2645 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002646 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2647 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002648 if (!FD) {
2649 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2650 << NI.getName();
2651 if (ULE->getType() == S.Context.OverloadTy)
2652 S.NoteAllOverloadCandidates(ULE);
2653 return;
2654 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002655 } else {
2656 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002657 return;
2658 }
2659
Anders Carlssond277d792009-01-31 01:16:18 +00002660 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002661 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2662 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002663 return;
2664 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002665
Anders Carlsson723f55d2009-02-07 23:16:50 +00002666 // We're currently more strict than GCC about what function types we accept.
2667 // If this ever proves to be a problem it should be easy to fix.
2668 QualType Ty = S.Context.getPointerType(VD->getType());
2669 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002670 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2671 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002672 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2673 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002674 return;
2675 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002676
Michael Han99315932013-01-24 16:46:58 +00002677 D->addAttr(::new (S.Context)
2678 CleanupAttr(Attr.getRange(), S.Context, FD,
2679 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002680}
2681
Mike Stumpd3bb5572009-07-24 19:02:52 +00002682/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002683/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002684static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002685 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002686 uint64_t Idx;
2687 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002688 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002689
Eric Christopherb64963e2015-08-13 21:34:35 +00002690 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002691 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002692
Eric Christopherb64963e2015-08-13 21:34:35 +00002693 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2694 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002695 !isCFStringType(Ty, S.Context) &&
2696 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002697 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002698 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002699 << "a string type" << IdxExpr->getSourceRange()
2700 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002701 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002702 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002703 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002704 if (!isNSStringType(Ty, S.Context) &&
2705 !isCFStringType(Ty, S.Context) &&
2706 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002707 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002708 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002709 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002710 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002711 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002712 }
2713
Alp Toker601b22c2014-01-21 23:35:24 +00002714 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002715 // because that has corrected for the implicit this parameter, and is zero-
2716 // based. The attribute expects what the user wrote explicitly.
2717 llvm::APSInt Val;
2718 IdxExpr->EvaluateAsInt(Val, S.Context);
2719
Michael Han99315932013-01-24 16:46:58 +00002720 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002721 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002722 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002723}
2724
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002725enum FormatAttrKind {
2726 CFStringFormat,
2727 NSStringFormat,
2728 StrftimeFormat,
2729 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002730 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002731 InvalidFormat
2732};
2733
2734/// getFormatAttrKind - Map from format attribute names to supported format
2735/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002736static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002737 return llvm::StringSwitch<FormatAttrKind>(Format)
2738 // Check for formats that get handled specially.
2739 .Case("NSString", NSStringFormat)
2740 .Case("CFString", CFStringFormat)
2741 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002742
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002743 // Otherwise, check for supported formats.
2744 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2745 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2746 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002747 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002748 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002749
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002750 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2751 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002752}
2753
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002754/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002755/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002756static void handleInitPriorityAttr(Sema &S, Decl *D,
2757 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002758 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002759 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2760 return;
2761 }
2762
Aaron Ballman4a611152013-11-27 16:34:09 +00002763 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002764 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2765 Attr.setInvalid();
2766 return;
2767 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002768 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002769 if (S.Context.getAsArrayType(T))
2770 T = S.Context.getBaseElementType(T);
2771 if (!T->getAs<RecordType>()) {
2772 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2773 Attr.setInvalid();
2774 return;
2775 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002776
2777 Expr *E = Attr.getArgAsExpr(0);
2778 uint32_t prioritynum;
2779 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002780 Attr.setInvalid();
2781 return;
2782 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002783
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002784 if (prioritynum < 101 || prioritynum > 65535) {
2785 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00002786 << E->getSourceRange() << Attr.getName() << 101 << 65535;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002787 Attr.setInvalid();
2788 return;
2789 }
Michael Han99315932013-01-24 16:46:58 +00002790 D->addAttr(::new (S.Context)
2791 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2792 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002793}
2794
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002795FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2796 IdentifierInfo *Format, int FormatIdx,
2797 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002798 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002799 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002800 for (auto *F : D->specific_attrs<FormatAttr>()) {
2801 if (F->getType() == Format &&
2802 F->getFormatIdx() == FormatIdx &&
2803 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002804 // If we don't have a valid location for this attribute, adopt the
2805 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002806 if (F->getLocation().isInvalid())
2807 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002808 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002809 }
2810 }
2811
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002812 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2813 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002814}
2815
Mike Stumpd3bb5572009-07-24 19:02:52 +00002816/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002817/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002818static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002819 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002820 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002821 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002822 return;
2823 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002824
Chandler Carruth743682b2010-11-16 08:35:43 +00002825 // In C++ the implicit 'this' function parameter also counts, and they are
2826 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002827 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002828 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002829
Aaron Ballman00e99962013-08-31 01:11:41 +00002830 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2831 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002832
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002833 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002834 // If we've modified the string name, we need a new identifier for it.
2835 II = &S.Context.Idents.get(Format);
2836 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002837
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002838 // Check for supported formats.
2839 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002840
2841 if (Kind == IgnoredFormat)
2842 return;
2843
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002844 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002845 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002846 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002847 return;
2848 }
2849
2850 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002851 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002852 uint32_t Idx;
2853 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002854 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002856 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002857 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002858 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002859 return;
2860 }
2861
2862 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002863 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002864
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002865 if (HasImplicitThisParam) {
2866 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002867 S.Diag(Attr.getLoc(),
2868 diag::err_format_attribute_implicit_this_format_string)
2869 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002870 return;
2871 }
2872 ArgIdx--;
2873 }
Mike Stump11289f42009-09-09 15:08:12 +00002874
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002875 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002876 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002877
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002878 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002879 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002880 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002881 << "a CFString" << IdxExpr->getSourceRange()
2882 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002883 return;
2884 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002885 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002886 // FIXME: do we need to check if the type is NSString*? What are the
2887 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002888 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002889 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002890 << "an NSString" << IdxExpr->getSourceRange()
2891 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002892 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002893 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002894 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002895 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002896 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002897 << "a string type" << IdxExpr->getSourceRange()
2898 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002899 return;
2900 }
2901
2902 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002903 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002904 uint32_t FirstArg;
2905 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002906 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907
2908 // check if the function is variadic if the 3rd argument non-zero
2909 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002910 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002911 ++NumArgs; // +1 for ...
2912 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002913 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002914 return;
2915 }
2916 }
2917
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002918 // strftime requires FirstArg to be 0 because it doesn't read from any
2919 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002920 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002921 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002922 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2923 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002924 return;
2925 }
2926 // if 0 it disables parameter checking (to use with e.g. va_list)
2927 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002928 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002929 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002930 return;
2931 }
2932
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002933 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002934 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002935 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002936 if (NewAttr)
2937 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002938}
2939
Chandler Carruthedc2c642011-07-02 00:01:44 +00002940static void handleTransparentUnionAttr(Sema &S, Decl *D,
2941 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002942 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002943 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002944 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002945 if (TD && TD->getUnderlyingType()->isUnionType())
2946 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2947 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002948 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002949
2950 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002951 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002952 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002953 return;
2954 }
2955
John McCallf937c022011-10-07 06:10:15 +00002956 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002957 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002958 diag::warn_transparent_union_attribute_not_definition);
2959 return;
2960 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002961
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002962 RecordDecl::field_iterator Field = RD->field_begin(),
2963 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002964 if (Field == FieldEnd) {
2965 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2966 return;
2967 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002968
David Blaikie40ed2972012-06-06 20:45:41 +00002969 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002970 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002971 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002972 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002973 diag::warn_transparent_union_attribute_floating)
2974 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002975 return;
2976 }
2977
2978 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2979 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2980 for (; Field != FieldEnd; ++Field) {
2981 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002982 // FIXME: this isn't fully correct; we also need to test whether the
2983 // members of the union would all have the same calling convention as the
2984 // first member of the union. Checking just the size and alignment isn't
2985 // sufficient (consider structs passed on the stack instead of in registers
2986 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002987 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002988 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002989 // Warn if we drop the attribute.
2990 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002991 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002992 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002993 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002994 diag::warn_transparent_union_attribute_field_size_align)
2995 << isSize << Field->getDeclName() << FieldBits;
2996 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002997 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002998 diag::note_transparent_union_first_field_size_align)
2999 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003000 return;
3001 }
3002 }
3003
Michael Han99315932013-01-24 16:46:58 +00003004 RD->addAttr(::new (S.Context)
3005 TransparentUnionAttr(Attr.getRange(), S.Context,
3006 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003007}
3008
Chandler Carruthedc2c642011-07-02 00:01:44 +00003009static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003010 // Make sure that there is a string literal as the annotation's single
3011 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003012 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003013 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003014 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003015
3016 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003017 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3018 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003019 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003020 }
Michael Han99315932013-01-24 16:46:58 +00003021
3022 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003023 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003024 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003025}
3026
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003027static void handleAlignValueAttr(Sema &S, Decl *D,
3028 const AttributeList &Attr) {
3029 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3030 Attr.getAttributeSpellingListIndex());
3031}
3032
3033void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3034 unsigned SpellingListIndex) {
3035 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3036 SourceLocation AttrLoc = AttrRange.getBegin();
3037
3038 QualType T;
3039 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3040 T = TD->getUnderlyingType();
3041 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3042 T = VD->getType();
3043 else
3044 llvm_unreachable("Unknown decl type for align_value");
3045
3046 if (!T->isDependentType() && !T->isAnyPointerType() &&
3047 !T->isReferenceType() && !T->isMemberPointerType()) {
3048 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3049 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3050 return;
3051 }
3052
3053 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003054 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003055 ExprResult ICE
3056 = VerifyIntegerConstantExpression(E, &Alignment,
3057 diag::err_align_value_attribute_argument_not_int,
3058 /*AllowFold*/ false);
3059 if (ICE.isInvalid())
3060 return;
3061
3062 if (!Alignment.isPowerOf2()) {
3063 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3064 << E->getSourceRange();
3065 return;
3066 }
3067
3068 D->addAttr(::new (Context)
3069 AlignValueAttr(AttrRange, Context, ICE.get(),
3070 SpellingListIndex));
3071 return;
3072 }
3073
3074 // Save dependent expressions in the AST to be instantiated.
3075 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
3076 return;
3077}
3078
Chandler Carruthedc2c642011-07-02 00:01:44 +00003079static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003080 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003081 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3083 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003084 return;
3085 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003086
Richard Smith848e1f12013-02-01 08:12:08 +00003087 if (Attr.getNumArgs() == 0) {
3088 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003089 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003090 return;
3091 }
3092
Aaron Ballman00e99962013-08-31 01:11:41 +00003093 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003094 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3095 S.Diag(Attr.getEllipsisLoc(),
3096 diag::err_pack_expansion_without_parameter_packs);
3097 return;
3098 }
3099
3100 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3101 return;
3102
David Majnemer26a1e0e2015-04-07 02:37:09 +00003103 if (E->isValueDependent()) {
3104 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3105 if (!TND->getUnderlyingType()->isDependentType()) {
3106 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3107 << E->getSourceRange();
3108 return;
3109 }
3110 }
3111 }
3112
Richard Smith44c247f2013-02-22 08:32:16 +00003113 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3114 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003115}
3116
3117void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003118 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003119 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3120 SourceLocation AttrLoc = AttrRange.getBegin();
3121
Richard Smith1dba27c2013-01-29 09:02:09 +00003122 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003123 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003124 // C++11 [dcl.align]p1:
3125 // An alignment-specifier may be applied to a variable or to a class
3126 // data member, but it shall not be applied to a bit-field, a function
3127 // parameter, the formal parameter of a catch clause, or a variable
3128 // declared with the register storage class specifier. An
3129 // alignment-specifier may also be applied to the declaration of a class
3130 // or enumeration type.
3131 // C11 6.7.5/2:
3132 // An alignment attribute shall not be specified in a declaration of
3133 // a typedef, or a bit-field, or a function, or a parameter, or an
3134 // object declared with the register storage-class specifier.
3135 int DiagKind = -1;
3136 if (isa<ParmVarDecl>(D)) {
3137 DiagKind = 0;
3138 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3139 if (VD->getStorageClass() == SC_Register)
3140 DiagKind = 1;
3141 if (VD->isExceptionVariable())
3142 DiagKind = 2;
3143 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3144 if (FD->isBitField())
3145 DiagKind = 3;
3146 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003147 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003148 << (TmpAttr.isC11() ? ExpectedVariableOrField
3149 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003150 return;
3151 }
3152 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003153 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003154 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003155 return;
3156 }
3157 }
3158
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003159 if (E->isTypeDependent() || E->isValueDependent()) {
3160 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003161 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3162 AA->setPackExpansion(IsPackExpansion);
3163 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003164 return;
3165 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003166
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003167 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003168 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003169 ExprResult ICE
3170 = VerifyIntegerConstantExpression(E, &Alignment,
3171 diag::err_aligned_attribute_argument_not_int,
3172 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003173 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003174 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003175
David Majnemer0be6bd02015-07-26 09:02:21 +00003176 uint64_t AlignVal = Alignment.getZExtValue();
3177
Richard Smith848e1f12013-02-01 08:12:08 +00003178 // C++11 [dcl.align]p2:
3179 // -- if the constant expression evaluates to zero, the alignment
3180 // specifier shall have no effect
3181 // C11 6.7.5p6:
3182 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003183 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003184 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003185 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3186 << E->getSourceRange();
3187 return;
3188 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003189 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003190
David Majnemerabecae72014-02-12 20:36:10 +00003191 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003192 unsigned MaxValidAlignment =
3193 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3194 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003195 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003196 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3197 << E->getSourceRange();
3198 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003199 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003200
David Majnemer0be6bd02015-07-26 09:02:21 +00003201 if (Context.getTargetInfo().isTLSSupported()) {
3202 unsigned MaxTLSAlign =
3203 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3204 .getQuantity();
3205 auto *VD = dyn_cast<VarDecl>(D);
3206 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3207 VD->getTLSKind() != VarDecl::TLS_None) {
3208 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3209 << (unsigned)AlignVal << VD << MaxTLSAlign;
3210 return;
3211 }
3212 }
3213
Richard Smith44c247f2013-02-22 08:32:16 +00003214 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003215 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003216 AA->setPackExpansion(IsPackExpansion);
3217 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003218}
3219
Michael Hanaf02bbe2013-02-01 01:19:17 +00003220void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003221 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003222 // FIXME: Cache the number on the Attr object if non-dependent?
3223 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003224 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3225 SpellingListIndex);
3226 AA->setPackExpansion(IsPackExpansion);
3227 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003228}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003229
Richard Smith848e1f12013-02-01 08:12:08 +00003230void Sema::CheckAlignasUnderalignment(Decl *D) {
3231 assert(D->hasAttrs() && "no attributes on decl");
3232
David Majnemer475b25e2015-01-21 10:54:38 +00003233 QualType UnderlyingTy, DiagTy;
3234 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3235 UnderlyingTy = DiagTy = VD->getType();
3236 } else {
3237 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3238 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3239 UnderlyingTy = ED->getIntegerType();
3240 }
3241 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003242 return;
3243
3244 // C++11 [dcl.align]p5, C11 6.7.5/4:
3245 // The combined effect of all alignment attributes in a declaration shall
3246 // not specify an alignment that is less strict than the alignment that
3247 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003248 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003249 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003250 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003251 if (I->isAlignmentDependent())
3252 return;
3253 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003254 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003255 Align = std::max(Align, I->getAlignment(Context));
3256 }
3257
3258 if (AlignasAttr && Align) {
3259 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003260 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003261 if (NaturalAlign > RequestedAlign)
3262 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003263 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003264 }
3265}
3266
David Majnemer2c4e00a2014-01-29 22:07:36 +00003267bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003268 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003269 MSInheritanceAttr::Spelling SemanticSpelling) {
3270 assert(RD->hasDefinition() && "RD has no definition!");
3271
David Majnemer98c9ee22014-02-07 00:43:07 +00003272 // We may not have seen base specifiers or any virtual methods yet. We will
3273 // have to wait until the record is defined to catch any mismatches.
3274 if (!RD->getDefinition()->isCompleteDefinition())
3275 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003276
David Majnemer98c9ee22014-02-07 00:43:07 +00003277 // The unspecified model never matches what a definition could need.
3278 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3279 return false;
3280
David Majnemer4bb09802014-02-10 19:50:15 +00003281 if (BestCase) {
3282 if (RD->calculateInheritanceModel() == SemanticSpelling)
3283 return false;
3284 } else {
3285 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3286 return false;
3287 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003288
3289 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3290 << 0 /*definition*/;
3291 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3292 << RD->getNameAsString();
3293 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003294}
3295
Alexey Bataevf278eb12015-11-19 10:13:11 +00003296/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3297/// attribute.
3298static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3299 bool &IntegerMode, bool &ComplexMode) {
Daniel Dunbarafff4342009-10-18 02:09:24 +00003300 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003301 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003302 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003303 case 'Q':
3304 DestWidth = 8;
3305 break;
3306 case 'H':
3307 DestWidth = 16;
3308 break;
3309 case 'S':
3310 DestWidth = 32;
3311 break;
3312 case 'D':
3313 DestWidth = 64;
3314 break;
3315 case 'X':
3316 DestWidth = 96;
3317 break;
3318 case 'T':
3319 DestWidth = 128;
3320 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003321 }
3322 if (Str[1] == 'F') {
3323 IntegerMode = false;
3324 } else if (Str[1] == 'C') {
3325 IntegerMode = false;
3326 ComplexMode = true;
3327 } else if (Str[1] != 'I') {
3328 DestWidth = 0;
3329 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003330 break;
3331 case 4:
3332 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3333 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003334 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003335 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003336 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003337 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003338 break;
3339 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003340 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003341 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003342 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003343 case 11:
3344 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003345 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003346 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003347 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003348}
3349
3350/// handleModeAttr - This attribute modifies the width of a decl with primitive
3351/// type.
3352///
3353/// Despite what would be logical, the mode attribute is a decl attribute, not a
3354/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3355/// HImode, not an intermediate pointer.
3356static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3357 // This attribute isn't documented, but glibc uses it. It changes
3358 // the width of an int or unsigned int to the specified size.
3359 if (!Attr.isArgIdent(0)) {
3360 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3361 << AANT_ArgumentIdentifier;
3362 return;
3363 }
3364
3365 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3366 StringRef Str = Name->getName();
3367
3368 normalizeName(Str);
3369
3370 unsigned DestWidth = 0;
3371 bool IntegerMode = true;
3372 bool ComplexMode = false;
3373 llvm::APInt VectorSize(64, 0);
3374 if (Str.size() >= 4 && Str[0] == 'V') {
3375 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3376 size_t StrSize = Str.size();
3377 size_t VectorStringLength = 0;
3378 while ((VectorStringLength + 1) < StrSize &&
3379 isdigit(Str[VectorStringLength + 1]))
3380 ++VectorStringLength;
3381 if (VectorStringLength &&
3382 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3383 VectorSize.isPowerOf2()) {
3384 parseModeAttrArg(S, Str.substr(VectorStringLength + 1), DestWidth,
3385 IntegerMode, ComplexMode);
3386 S.Diag(Attr.getLoc(), diag::warn_vector_mode_deprecated);
3387 } else {
3388 VectorSize = 0;
3389 }
3390 }
3391
3392 if (!VectorSize)
3393 parseModeAttrArg(S, Str, DestWidth, IntegerMode, ComplexMode);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003394
3395 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003396 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003397 OldTy = TD->getUnderlyingType();
Alexey Bataev2c485a72016-01-15 04:36:32 +00003398 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003399 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003400
Alexey Bataev326057d2015-06-19 07:46:21 +00003401 // Base type can also be a vector type (see PR17453).
3402 // Distinguish between base type and base element type.
3403 QualType OldElemTy = OldTy;
3404 if (const VectorType *VT = OldTy->getAs<VectorType>())
3405 OldElemTy = VT->getElementType();
3406
3407 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003408 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3409 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003410 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003411 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3412 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003413 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003414 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3415 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003416 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003417 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3418 }
3419
Mike Stump87c57ac2009-05-16 07:39:55 +00003420 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3421 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003422 // FIXME: Make sure floating-point mappings are accurate
3423 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003424 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003425 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003426 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003427 }
3428
Alexey Bataev326057d2015-06-19 07:46:21 +00003429 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003430
3431 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003432 NewElemTy = S.Context.getIntTypeForBitwidth(
3433 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003434 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003435 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003436
Alexey Bataev326057d2015-06-19 07:46:21 +00003437 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003438 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003439 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003440 }
3441
Eli Friedman4735374e2009-03-03 06:41:03 +00003442 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003443 NewElemTy = S.Context.getComplexType(NewElemTy);
3444 }
3445
3446 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003447 if (VectorSize.getBoolValue()) {
3448 NewTy = S.Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3449 VectorType::GenericVector);
3450 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003451 // Complex machine mode does not support base vector types.
3452 if (ComplexMode) {
3453 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3454 return;
3455 }
3456 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3457 OldVT->getNumElements() /
3458 S.Context.getTypeSize(NewElemTy);
3459 NewTy =
3460 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3461 }
3462
3463 if (NewTy.isNull()) {
3464 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3465 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003466 }
3467
3468 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003469 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3470 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3471 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003472 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003473
3474 D->addAttr(::new (S.Context)
3475 ModeAttr(Attr.getRange(), S.Context, Name,
3476 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003477}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003478
Chandler Carruthedc2c642011-07-02 00:01:44 +00003479static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003480 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3481 if (!VD->hasGlobalStorage())
3482 S.Diag(Attr.getLoc(),
3483 diag::warn_attribute_requires_functions_or_static_globals)
3484 << Attr.getName();
3485 } else if (!isFunctionOrMethod(D)) {
3486 S.Diag(Attr.getLoc(),
3487 diag::warn_attribute_requires_functions_or_static_globals)
3488 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003489 return;
3490 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003491
Michael Han99315932013-01-24 16:46:58 +00003492 D->addAttr(::new (S.Context)
3493 NoDebugAttr(Attr.getRange(), S.Context,
3494 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003495}
3496
Paul Robinson30e41fb2014-12-15 18:57:28 +00003497AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003498 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003499 unsigned AttrSpellingListIndex) {
3500 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003501 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003502 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3503 return nullptr;
3504 }
3505
3506 if (D->hasAttr<AlwaysInlineAttr>())
3507 return nullptr;
3508
3509 return ::new (Context) AlwaysInlineAttr(Range, Context,
3510 AttrSpellingListIndex);
3511}
3512
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003513CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3514 IdentifierInfo *Ident,
3515 unsigned AttrSpellingListIndex) {
3516 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3517 return nullptr;
3518
3519 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3520}
3521
3522InternalLinkageAttr *
3523Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3524 IdentifierInfo *Ident,
3525 unsigned AttrSpellingListIndex) {
3526 if (auto VD = dyn_cast<VarDecl>(D)) {
3527 // Attribute applies to Var but not any subclass of it (like ParmVar,
3528 // ImplicitParm or VarTemplateSpecialization).
3529 if (VD->getKind() != Decl::Var) {
3530 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3531 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3532 : ExpectedVariableOrFunction);
3533 return nullptr;
3534 }
3535 // Attribute does not apply to non-static local variables.
3536 if (VD->hasLocalStorage()) {
3537 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3538 return nullptr;
3539 }
3540 }
3541
3542 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3543 return nullptr;
3544
3545 return ::new (Context)
3546 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3547}
3548
Paul Robinson30e41fb2014-12-15 18:57:28 +00003549MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3550 unsigned AttrSpellingListIndex) {
3551 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3552 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3553 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3554 return nullptr;
3555 }
3556
3557 if (D->hasAttr<MinSizeAttr>())
3558 return nullptr;
3559
3560 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3561}
3562
3563OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3564 unsigned AttrSpellingListIndex) {
3565 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3566 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3567 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3568 D->dropAttr<AlwaysInlineAttr>();
3569 }
3570 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3571 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3572 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3573 D->dropAttr<MinSizeAttr>();
3574 }
3575
3576 if (D->hasAttr<OptimizeNoneAttr>())
3577 return nullptr;
3578
3579 return ::new (Context) OptimizeNoneAttr(Range, Context,
3580 AttrSpellingListIndex);
3581}
3582
Paul Robinsonf0674352014-03-31 22:29:15 +00003583static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3584 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003585 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3586 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003587 return;
3588
Paul Robinson080b1f32015-01-13 18:34:56 +00003589 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3590 D, Attr.getRange(), Attr.getName(),
3591 Attr.getAttributeSpellingListIndex()))
3592 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003593}
3594
Paul Robinson080b1f32015-01-13 18:34:56 +00003595static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3596 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3597 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3598 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003599}
3600
Paul Robinsonf0674352014-03-31 22:29:15 +00003601static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3602 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003603 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3604 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3605 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003606}
3607
Chandler Carruthedc2c642011-07-02 00:01:44 +00003608static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00003609 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(),
3610 Attr.getName()) ||
3611 checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(),
3612 Attr.getName())) {
3613 return;
3614 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003615 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003616 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003617 SourceRange RTRange = FD->getReturnTypeSourceRange();
3618 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003619 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003620 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3621 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003622 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003623 }
Justin Lebarc66a1062016-01-20 00:26:57 +00003624 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
3625 if (Method->isInstance()) {
3626 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
3627 << Method;
3628 return;
3629 }
3630 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
3631 }
3632 // Only warn for "inline" when compiling for host, to cut down on noise.
3633 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
3634 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003635
Aaron Ballman3aff6332013-12-02 19:30:36 +00003636 D->addAttr(::new (S.Context)
3637 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003638 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003639}
3640
Chandler Carruthedc2c642011-07-02 00:01:44 +00003641static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003642 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003643 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003644 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003645 return;
3646 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003647
Michael Han99315932013-01-24 16:46:58 +00003648 D->addAttr(::new (S.Context)
3649 GNUInlineAttr(Attr.getRange(), S.Context,
3650 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003651}
3652
Chandler Carruthedc2c642011-07-02 00:01:44 +00003653static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003654 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003655
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003656 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003657 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3658 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003659 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003660 return;
3661
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003662 if (!isa<ObjCMethodDecl>(D)) {
3663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3664 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003665 return;
3666 }
3667
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003668 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003669 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003670 D->addAttr(::new (S.Context)
3671 FastCallAttr(Attr.getRange(), S.Context,
3672 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003673 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003674 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003675 D->addAttr(::new (S.Context)
3676 StdCallAttr(Attr.getRange(), S.Context,
3677 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003678 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003679 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003680 D->addAttr(::new (S.Context)
3681 ThisCallAttr(Attr.getRange(), S.Context,
3682 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003683 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003684 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003685 D->addAttr(::new (S.Context)
3686 CDeclAttr(Attr.getRange(), S.Context,
3687 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003688 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003689 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003690 D->addAttr(::new (S.Context)
3691 PascalAttr(Attr.getRange(), S.Context,
3692 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003693 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003694 case AttributeList::AT_VectorCall:
3695 D->addAttr(::new (S.Context)
3696 VectorCallAttr(Attr.getRange(), S.Context,
3697 Attr.getAttributeSpellingListIndex()));
3698 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003699 case AttributeList::AT_MSABI:
3700 D->addAttr(::new (S.Context)
3701 MSABIAttr(Attr.getRange(), S.Context,
3702 Attr.getAttributeSpellingListIndex()));
3703 return;
3704 case AttributeList::AT_SysVABI:
3705 D->addAttr(::new (S.Context)
3706 SysVABIAttr(Attr.getRange(), S.Context,
3707 Attr.getAttributeSpellingListIndex()));
3708 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003709 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003710 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003711 switch (CC) {
3712 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003713 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003714 break;
3715 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003716 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003717 break;
3718 default:
3719 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003720 }
3721
Michael Han99315932013-01-24 16:46:58 +00003722 D->addAttr(::new (S.Context)
3723 PcsAttr(Attr.getRange(), S.Context, PCS,
3724 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003725 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003726 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003727 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003728 D->addAttr(::new (S.Context)
3729 IntelOclBiccAttr(Attr.getRange(), S.Context,
3730 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003731 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003732
Abramo Bagnara50099372010-04-30 13:10:51 +00003733 default:
3734 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003735 }
3736}
3737
Aaron Ballman02df2e02012-12-09 17:45:41 +00003738bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3739 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003740 if (attr.isInvalid())
3741 return true;
3742
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003743 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003744 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003745 attr.setInvalid();
3746 return true;
3747 }
3748
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003749 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003750 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003751 case AttributeList::AT_CDecl: CC = CC_C; break;
3752 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3753 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3754 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3755 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003756 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003757 case AttributeList::AT_MSABI:
3758 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3759 CC_X86_64Win64;
3760 break;
3761 case AttributeList::AT_SysVABI:
3762 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3763 CC_C;
3764 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003765 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003766 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003767 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003768 attr.setInvalid();
3769 return true;
3770 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003771 if (StrRef == "aapcs") {
3772 CC = CC_AAPCS;
3773 break;
3774 } else if (StrRef == "aapcs-vfp") {
3775 CC = CC_AAPCS_VFP;
3776 break;
3777 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003778
3779 attr.setInvalid();
3780 Diag(attr.getLoc(), diag::err_invalid_pcs);
3781 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003782 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003783 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003784 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003785 }
3786
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003787 const TargetInfo &TI = Context.getTargetInfo();
3788 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003789 if (A != TargetInfo::CCCR_OK) {
3790 if (A == TargetInfo::CCCR_Warning)
3791 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003792
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003793 // This convention is not valid for the target. Use the default function or
3794 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003795 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3796 if (FD)
3797 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3798 TargetInfo::CCMT_NonMember;
3799 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003800 }
3801
John McCall3882ace2011-01-05 12:14:39 +00003802 return false;
3803}
3804
John McCall3882ace2011-01-05 12:14:39 +00003805/// Checks a regparm attribute, returning true if it is ill-formed and
3806/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003807bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3808 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003809 return true;
3810
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003811 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003812 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003813 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003814 }
Eli Friedman7044b762009-03-27 21:06:47 +00003815
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003816 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003817 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003818 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003819 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003820 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003821 }
3822
Douglas Gregore8bbc122011-09-02 00:18:52 +00003823 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003824 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003825 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003826 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003827 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003828 }
3829
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003830 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003831 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003832 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003833 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003834 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003835 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003836 }
3837
John McCall3882ace2011-01-05 12:14:39 +00003838 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003839}
3840
Artem Belevich7093e402015-04-21 22:55:54 +00003841// Checks whether an argument of launch_bounds attribute is acceptable
3842// May output an error.
3843static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3844 const CUDALaunchBoundsAttr &Attr,
3845 const unsigned Idx) {
3846
3847 if (S.DiagnoseUnexpandedParameterPack(E))
3848 return false;
3849
3850 // Accept template arguments for now as they depend on something else.
3851 // We'll get to check them when they eventually get instantiated.
3852 if (E->isValueDependent())
3853 return true;
3854
3855 llvm::APSInt I(64);
3856 if (!E->isIntegerConstantExpr(I, S.Context)) {
3857 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3858 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3859 return false;
3860 }
3861 // Make sure we can fit it in 32 bits.
3862 if (!I.isIntN(32)) {
3863 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3864 << 32 << /* Unsigned */ 1;
3865 return false;
3866 }
3867 if (I < 0)
3868 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3869 << &Attr << Idx << E->getSourceRange();
3870
3871 return true;
3872}
3873
3874void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3875 Expr *MinBlocks, unsigned SpellingListIndex) {
3876 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3877 SpellingListIndex);
3878
3879 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003880 return;
3881
Artem Belevich7093e402015-04-21 22:55:54 +00003882 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3883 return;
3884
3885 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3886 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3887}
3888
3889static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3890 const AttributeList &Attr) {
3891 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3892 !checkAttributeAtMostNumArgs(S, Attr, 2))
3893 return;
3894
3895 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3896 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3897 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003898}
3899
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003900static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3901 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003902 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003903 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003904 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003905 return;
3906 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003907
3908 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003909 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003910
Aaron Ballman00e99962013-08-31 01:11:41 +00003911 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003912
3913 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3914 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3915 << Attr.getName() << ExpectedFunctionOrMethod;
3916 return;
3917 }
3918
3919 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003920 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3921 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003922 return;
3923
3924 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003925 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3926 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003927 return;
3928
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003929 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003930 if (IsPointer) {
3931 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003932 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003933 if (!BufferTy->isPointerType()) {
3934 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00003935 << Attr.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003936 }
3937 }
3938
Michael Han99315932013-01-24 16:46:58 +00003939 D->addAttr(::new (S.Context)
3940 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3941 ArgumentIdx, TypeTagIdx, IsPointer,
3942 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003943}
3944
3945static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3946 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003947 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003948 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003949 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003950 return;
3951 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003952
3953 if (!checkAttributeNumArgs(S, Attr, 1))
3954 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003955
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003956 if (!isa<VarDecl>(D)) {
3957 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3958 << Attr.getName() << ExpectedVariable;
3959 return;
3960 }
3961
Aaron Ballman00e99962013-08-31 01:11:41 +00003962 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003963 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003964 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3965 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003966
Michael Han99315932013-01-24 16:46:58 +00003967 D->addAttr(::new (S.Context)
3968 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003969 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003970 Attr.getLayoutCompatible(),
3971 Attr.getMustBeNull(),
3972 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003973}
3974
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003975//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003976// Checker-specific attribute handlers.
3977//===----------------------------------------------------------------------===//
3978
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003979static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003980 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003981 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003982}
3983
John McCalled433932011-01-25 03:31:58 +00003984static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003985 return type->isDependentType() ||
3986 type->isObjCObjectPointerType() ||
3987 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003988}
3989static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003990 return type->isDependentType() ||
3991 type->isPointerType() ||
3992 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003993}
3994
Chandler Carruthedc2c642011-07-02 00:01:44 +00003995static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003996 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003997 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003998
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004000 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4001 cf = false;
4002 } else {
4003 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4004 cf = true;
4005 }
4006
4007 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004008 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004009 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004010 return;
4011 }
4012
4013 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004014 param->addAttr(::new (S.Context)
4015 CFConsumedAttr(Attr.getRange(), S.Context,
4016 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004017 else
Michael Han99315932013-01-24 16:46:58 +00004018 param->addAttr(::new (S.Context)
4019 NSConsumedAttr(Attr.getRange(), S.Context,
4020 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004021}
4022
Chandler Carruthedc2c642011-07-02 00:01:44 +00004023static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4024 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004025
John McCalled433932011-01-25 03:31:58 +00004026 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004027
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004028 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004029 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004030 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004031 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004032 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004033 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4034 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004035 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004036 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004037 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
4038 returnType = Param->getType()->getPointeeType();
4039 if (returnType.isNull()) {
4040 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4041 << Attr.getName() << /*pointer-to-CF*/2
4042 << Attr.getRange();
4043 return;
4044 }
4045 } else {
4046 AttributeDeclKind ExpectedDeclKind;
4047 switch (Attr.getKind()) {
4048 default: llvm_unreachable("invalid ownership attribute");
4049 case AttributeList::AT_NSReturnsRetained:
4050 case AttributeList::AT_NSReturnsAutoreleased:
4051 case AttributeList::AT_NSReturnsNotRetained:
4052 ExpectedDeclKind = ExpectedFunctionOrMethod;
4053 break;
4054
4055 case AttributeList::AT_CFReturnsRetained:
4056 case AttributeList::AT_CFReturnsNotRetained:
4057 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4058 break;
4059 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004060 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004061 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004062 return;
4063 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004064
John McCalled433932011-01-25 03:31:58 +00004065 bool typeOK;
4066 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004067 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004068 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004069 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004070 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004071 cf = false;
4072 break;
4073
4074 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004075 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004076 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4077 cf = false;
4078 break;
4079
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004080 case AttributeList::AT_CFReturnsRetained:
4081 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004082 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4083 cf = true;
4084 break;
4085 }
4086
4087 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004088 if (isa<ParmVarDecl>(D)) {
4089 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4090 << Attr.getName() << /*pointer-to-CF*/2
4091 << Attr.getRange();
4092 } else {
4093 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4094 enum : unsigned {
4095 Function,
4096 Method,
4097 Property
4098 } SubjectKind = Function;
4099 if (isa<ObjCMethodDecl>(D))
4100 SubjectKind = Method;
4101 else if (isa<ObjCPropertyDecl>(D))
4102 SubjectKind = Property;
4103 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4104 << Attr.getName() << SubjectKind << cf
4105 << Attr.getRange();
4106 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004107 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004108 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004109
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004110 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004111 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004112 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004113 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004114 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4115 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004116 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004117 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004118 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4119 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004120 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004121 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004122 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4123 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004124 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004125 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004126 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4127 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004128 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004129 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004130 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4131 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004132 return;
4133 };
4134}
4135
John McCallcf166702011-07-22 08:53:00 +00004136static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4137 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004138 const int EP_ObjCMethod = 1;
4139 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004140
John McCallcf166702011-07-22 08:53:00 +00004141 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004142 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004143 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004144 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004145 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004146 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004147
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004148 if (!resultType->isReferenceType() &&
4149 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004150 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004151 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004152 << attr.getName()
4153 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004154 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004155
4156 // Drop the attribute.
4157 return;
4158 }
4159
Nico Weber462fd1e2015-01-07 23:50:05 +00004160 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4161 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004162}
4163
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004164static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4165 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004166 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004167
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004168 DeclContext *DC = method->getDeclContext();
4169 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4170 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4171 << attr.getName() << 0;
4172 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4173 return;
4174 }
4175 if (method->getMethodFamily() == OMF_dealloc) {
4176 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4177 << attr.getName() << 1;
4178 return;
4179 }
4180
Michael Han99315932013-01-24 16:46:58 +00004181 method->addAttr(::new (S.Context)
4182 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4183 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004184}
4185
Aaron Ballmanfb763042013-12-02 18:05:46 +00004186static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4187 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004188 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4189 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004190 return;
John McCall32f5fe12011-09-30 05:12:12 +00004191
Aaron Ballmanfb763042013-12-02 18:05:46 +00004192 D->addAttr(::new (S.Context)
4193 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4194 Attr.getAttributeSpellingListIndex()));
4195}
4196
4197static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4198 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004199 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4200 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004201 return;
4202
4203 D->addAttr(::new (S.Context)
4204 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4205 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004206}
4207
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004208static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4209 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004210 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004211
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004212 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004213 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004214 return;
4215 }
John McCall28592582015-02-01 22:34:06 +00004216
4217 // Typedefs only allow objc_bridge(id) and have some additional checking.
4218 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4219 if (!Parm->Ident->isStr("id")) {
4220 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4221 << Attr.getName();
4222 return;
4223 }
4224
4225 // Only allow 'cv void *'.
4226 QualType T = TD->getUnderlyingType();
4227 if (!T->isVoidPointerType()) {
4228 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4229 return;
4230 }
4231 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004232
4233 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004234 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004235 Attr.getAttributeSpellingListIndex()));
4236}
4237
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004238static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4239 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004240 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4241
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004242 if (!Parm) {
4243 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4244 return;
4245 }
4246
4247 D->addAttr(::new (S.Context)
4248 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4249 Attr.getAttributeSpellingListIndex()));
4250}
4251
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004252static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4253 const AttributeList &Attr) {
4254 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004255 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004256 if (!RelatedClass) {
4257 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4258 return;
4259 }
4260 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004261 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004262 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004263 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004264 D->addAttr(::new (S.Context)
4265 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4266 ClassMethod, InstanceMethod,
4267 Attr.getAttributeSpellingListIndex()));
4268}
4269
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004270static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4271 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004272 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004273 if (ObjCCategoryDecl *CatDecl =
4274 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004275 IFace = CatDecl->getClassInterface();
4276 else
4277 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004278
4279 if (!IFace)
4280 return;
4281
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004282 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004283 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004284 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4285 Attr.getAttributeSpellingListIndex()));
4286}
4287
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004288static void handleObjCRuntimeName(Sema &S, Decl *D,
4289 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004290 StringRef MetaDataName;
4291 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4292 return;
4293 D->addAttr(::new (S.Context)
4294 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4295 MetaDataName,
4296 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004297}
4298
Alex Denisovfde64952015-06-26 05:28:36 +00004299// when a user wants to use objc_boxable with a union or struct
4300// but she doesn't have access to the declaration (legacy/third-party code)
4301// then she can 'enable' this feature via trick with a typedef
4302// e.g.:
4303// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4304static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4305 bool notify = false;
4306
4307 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4308 if (RD && RD->getDefinition()) {
4309 RD = RD->getDefinition();
4310 notify = true;
4311 }
4312
4313 if (RD) {
4314 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4315 ObjCBoxableAttr(Attr.getRange(), S.Context,
4316 Attr.getAttributeSpellingListIndex());
4317 RD->addAttr(BoxableAttr);
4318 if (notify) {
4319 // we need to notify ASTReader/ASTWriter about
4320 // modification of existing declaration
4321 if (ASTMutationListener *L = S.getASTMutationListener())
4322 L->AddedAttributeToRecord(BoxableAttr, RD);
4323 }
4324 }
4325}
4326
Chandler Carruthedc2c642011-07-02 00:01:44 +00004327static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4328 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004329 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004330
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004331 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004332 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004333}
4334
Chandler Carruthedc2c642011-07-02 00:01:44 +00004335static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4336 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004337 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004338 QualType type = vd->getType();
4339
4340 if (!type->isDependentType() &&
4341 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004342 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004343 << type;
4344 return;
4345 }
4346
4347 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4348
4349 // If we have no lifetime yet, check the lifetime we're presumably
4350 // going to infer.
4351 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4352 lifetime = type->getObjCARCImplicitLifetime();
4353
4354 switch (lifetime) {
4355 case Qualifiers::OCL_None:
4356 assert(type->isDependentType() &&
4357 "didn't infer lifetime for non-dependent type?");
4358 break;
4359
4360 case Qualifiers::OCL_Weak: // meaningful
4361 case Qualifiers::OCL_Strong: // meaningful
4362 break;
4363
4364 case Qualifiers::OCL_ExplicitNone:
4365 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004366 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004367 << (lifetime == Qualifiers::OCL_Autoreleasing);
4368 break;
4369 }
4370
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004371 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004372 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4373 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004374}
4375
Francois Picheta83957a2010-12-19 06:50:37 +00004376//===----------------------------------------------------------------------===//
4377// Microsoft specific attribute handlers.
4378//===----------------------------------------------------------------------===//
4379
Chandler Carruthedc2c642011-07-02 00:01:44 +00004380static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004381 if (!S.LangOpts.CPlusPlus) {
4382 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4383 << Attr.getName() << AttributeLangSupport::C;
4384 return;
4385 }
4386
Aaron Ballman60e705e2013-11-24 20:58:02 +00004387 if (!isa<CXXRecordDecl>(D)) {
4388 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4389 << Attr.getName() << ExpectedClass;
4390 return;
4391 }
4392
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004393 StringRef StrRef;
4394 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004395 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004396 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004397
David Majnemer89085342013-08-09 08:56:20 +00004398 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4399 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004400 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4401 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004402
Reid Kleckner140c4a72013-05-17 14:04:52 +00004403 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004404 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004405 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004406 return;
4407 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004408
David Majnemer89085342013-08-09 08:56:20 +00004409 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004410 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004411 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004412 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004413 return;
4414 }
David Majnemer89085342013-08-09 08:56:20 +00004415 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004416 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004417 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004418 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004419 }
Francois Picheta83957a2010-12-19 06:50:37 +00004420
David Majnemer89085342013-08-09 08:56:20 +00004421 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4422 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004423}
4424
David Majnemer2c4e00a2014-01-29 22:07:36 +00004425static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4426 if (!S.LangOpts.CPlusPlus) {
4427 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4428 << Attr.getName() << AttributeLangSupport::C;
4429 return;
4430 }
4431 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004432 D, Attr.getRange(), /*BestCase=*/true,
4433 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004434 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00004435 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00004436 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00004437 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
4438 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00004439}
4440
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004441static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4442 const AttributeList &Attr) {
4443 VarDecl *VD = cast<VarDecl>(D);
4444 if (!S.Context.getTargetInfo().isTLSSupported()) {
4445 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4446 return;
4447 }
4448 if (VD->getTSCSpec() != TSCS_unspecified) {
4449 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4450 return;
4451 }
4452 if (VD->hasLocalStorage()) {
4453 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4454 return;
4455 }
4456 VD->addAttr(::new (S.Context) ThreadAttr(
4457 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4458}
4459
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004460static void handleARMInterruptAttr(Sema &S, Decl *D,
4461 const AttributeList &Attr) {
4462 // Check the attribute arguments.
4463 if (Attr.getNumArgs() > 1) {
4464 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4465 << Attr.getName() << 1;
4466 return;
4467 }
4468
4469 StringRef Str;
4470 SourceLocation ArgLoc;
4471
4472 if (Attr.getNumArgs() == 0)
4473 Str = "";
4474 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4475 return;
4476
4477 ARMInterruptAttr::InterruptType Kind;
4478 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4479 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4480 << Attr.getName() << Str << ArgLoc;
4481 return;
4482 }
4483
4484 unsigned Index = Attr.getAttributeSpellingListIndex();
4485 D->addAttr(::new (S.Context)
4486 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4487}
4488
4489static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4490 const AttributeList &Attr) {
4491 if (!checkAttributeNumArgs(S, Attr, 1))
4492 return;
4493
4494 if (!Attr.isArgExpr(0)) {
4495 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4496 << AANT_ArgumentIntegerConstant;
4497 return;
4498 }
4499
4500 // FIXME: Check for decl - it should be void ()(void).
4501
4502 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4503 llvm::APSInt NumParams(32);
4504 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4505 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4506 << Attr.getName() << AANT_ArgumentIntegerConstant
4507 << NumParamsExpr->getSourceRange();
4508 return;
4509 }
4510
4511 unsigned Num = NumParams.getLimitedValue(255);
4512 if ((Num & 1) || Num > 30) {
4513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4514 << Attr.getName() << (int)NumParams.getSExtValue()
4515 << NumParamsExpr->getSourceRange();
4516 return;
4517 }
4518
Aaron Ballman36a53502014-01-16 13:03:14 +00004519 D->addAttr(::new (S.Context)
4520 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4521 Attr.getAttributeSpellingListIndex()));
4522 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004523}
4524
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004525static void handleMipsInterruptAttr(Sema &S, Decl *D,
4526 const AttributeList &Attr) {
4527 // Only one optional argument permitted.
4528 if (Attr.getNumArgs() > 1) {
4529 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4530 << Attr.getName() << 1;
4531 return;
4532 }
4533
4534 StringRef Str;
4535 SourceLocation ArgLoc;
4536
4537 if (Attr.getNumArgs() == 0)
4538 Str = "";
4539 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4540 return;
4541
4542 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
4543 // a) Must be a function.
4544 // b) Must have no parameters.
4545 // c) Must have the 'void' return type.
4546 // d) Cannot have the 'mips16' attribute, as that instruction set
4547 // lacks the 'eret' instruction.
4548 // e) The attribute itself must either have no argument or one of the
4549 // valid interrupt types, see [MipsInterruptDocs].
4550
4551 if (!isFunctionOrMethod(D)) {
4552 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
4553 << "'interrupt'" << ExpectedFunctionOrMethod;
4554 return;
4555 }
4556
4557 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
4558 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4559 << 0;
4560 return;
4561 }
4562
4563 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4564 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4565 << 1;
4566 return;
4567 }
4568
4569 if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(),
4570 Attr.getName()))
4571 return;
4572
4573 MipsInterruptAttr::InterruptType Kind;
4574 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4575 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4576 << Attr.getName() << "'" + std::string(Str) + "'";
4577 return;
4578 }
4579
4580 D->addAttr(::new (S.Context) MipsInterruptAttr(
4581 Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex()));
4582}
4583
Alexey Bataevd51e9932016-01-15 04:06:31 +00004584static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
4585 const AttributeList &Attr) {
4586 // Semantic checks for a function with the 'interrupt' attribute.
4587 // a) Must be a function.
4588 // b) Must have the 'void' return type.
4589 // c) Must take 1 or 2 arguments.
4590 // d) The 1st argument must be a pointer.
4591 // e) The 2nd argument (if any) must be an unsigned integer.
4592 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
4593 CXXMethodDecl::isStaticOverloadedOperator(
4594 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
4595 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4596 << Attr.getName() << ExpectedFunctionWithProtoType;
4597 return;
4598 }
4599 // Interrupt handler must have void return type.
4600 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4601 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
4602 diag::err_anyx86_interrupt_attribute)
4603 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4604 ? 0
4605 : 1)
4606 << 0;
4607 return;
4608 }
4609 // Interrupt handler must have 1 or 2 parameters.
4610 unsigned NumParams = getFunctionOrMethodNumParams(D);
4611 if (NumParams < 1 || NumParams > 2) {
4612 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
4613 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4614 ? 0
4615 : 1)
4616 << 1;
4617 return;
4618 }
4619 // The first argument must be a pointer.
4620 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
4621 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
4622 diag::err_anyx86_interrupt_attribute)
4623 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4624 ? 0
4625 : 1)
4626 << 2;
4627 return;
4628 }
4629 // The second argument, if present, must be an unsigned integer.
4630 unsigned TypeSize =
4631 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
4632 ? 64
4633 : 32;
4634 if (NumParams == 2 &&
4635 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
4636 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
4637 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
4638 diag::err_anyx86_interrupt_attribute)
4639 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4640 ? 0
4641 : 1)
4642 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
4643 return;
4644 }
4645 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
4646 Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex()));
4647 D->addAttr(UsedAttr::CreateImplicit(S.Context));
4648}
4649
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004650static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4651 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00004652 switch (S.Context.getTargetInfo().getTriple().getArch()) {
4653 case llvm::Triple::msp430:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004654 handleMSP430InterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004655 break;
4656 case llvm::Triple::mipsel:
4657 case llvm::Triple::mips:
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004658 handleMipsInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004659 break;
4660 case llvm::Triple::x86:
4661 case llvm::Triple::x86_64:
4662 handleAnyX86InterruptAttr(S, D, Attr);
4663 break;
4664 default:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004665 handleARMInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004666 break;
4667 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004668}
4669
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004670static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4671 const AttributeList &Attr) {
4672 uint32_t NumRegs;
4673 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4674 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4675 return;
4676
4677 D->addAttr(::new (S.Context)
4678 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4679 NumRegs,
4680 Attr.getAttributeSpellingListIndex()));
4681}
4682
4683static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4684 const AttributeList &Attr) {
4685 uint32_t NumRegs;
4686 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4687 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4688 return;
4689
4690 D->addAttr(::new (S.Context)
4691 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4692 NumRegs,
4693 Attr.getAttributeSpellingListIndex()));
4694}
4695
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004696static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4697 const AttributeList& Attr) {
4698 // If we try to apply it to a function pointer, don't warn, but don't
4699 // do anything, either. It doesn't matter anyway, because there's nothing
4700 // special about calling a force_align_arg_pointer function.
4701 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4702 if (VD && VD->getType()->isFunctionPointerType())
4703 return;
4704 // Also don't warn on function pointer typedefs.
4705 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4706 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4707 TD->getUnderlyingType()->isFunctionType()))
4708 return;
4709 // Attribute can only be applied to function types.
4710 if (!isa<FunctionDecl>(D)) {
4711 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4712 << Attr.getName() << /* function */0;
4713 return;
4714 }
4715
Aaron Ballman36a53502014-01-16 13:03:14 +00004716 D->addAttr(::new (S.Context)
4717 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4718 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004719}
4720
4721DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4722 unsigned AttrSpellingListIndex) {
4723 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004724 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004725 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004726 }
4727
4728 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004729 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004730
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004731 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004732}
4733
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004734DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4735 unsigned AttrSpellingListIndex) {
4736 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004737 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004738 D->dropAttr<DLLImportAttr>();
4739 }
4740
4741 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004742 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004743
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004744 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004745}
4746
Hans Wennborge82f19c2014-06-24 23:57:05 +00004747static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004748 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4749 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4750 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4751 << A.getName();
4752 return;
4753 }
4754
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004755 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4756 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4757 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4758 // MinGW doesn't allow dllimport on inline functions.
4759 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4760 << A.getName();
4761 return;
4762 }
4763 }
4764
Hans Wennborg5869ec42015-09-15 21:05:30 +00004765 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4766 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4767 MD->getParent()->isLambda()) {
4768 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4769 return;
4770 }
4771 }
4772
Hans Wennborge82f19c2014-06-24 23:57:05 +00004773 unsigned Index = A.getAttributeSpellingListIndex();
4774 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4775 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4776 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004777 if (NewAttr)
4778 D->addAttr(NewAttr);
4779}
4780
David Majnemer2c4e00a2014-01-29 22:07:36 +00004781MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004782Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004783 unsigned AttrSpellingListIndex,
4784 MSInheritanceAttr::Spelling SemanticSpelling) {
4785 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4786 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004787 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004788 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4789 << 1 /*previous declaration*/;
4790 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4791 D->dropAttr<MSInheritanceAttr>();
4792 }
4793
4794 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4795 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004796 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4797 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004798 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004799 }
4800 } else {
4801 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4802 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4803 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004804 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004805 }
4806 if (RD->getDescribedClassTemplate()) {
4807 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4808 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004809 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004810 }
4811 }
4812
4813 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004814 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004815}
4816
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004817static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4818 // The capability attributes take a single string parameter for the name of
4819 // the capability they represent. The lockable attribute does not take any
4820 // parameters. However, semantically, both attributes represent the same
4821 // concept, and so they use the same semantic attribute. Eventually, the
4822 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004823 //
Alp Toker958027b2014-07-14 19:42:55 +00004824 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004825 // literal will be considered a "mutex."
4826 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004827 SourceLocation LiteralLoc;
4828 if (Attr.getKind() == AttributeList::AT_Capability &&
4829 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4830 return;
4831
Aaron Ballman6c810072014-03-05 21:47:13 +00004832 // Currently, there are only two names allowed for a capability: role and
4833 // mutex (case insensitive). Diagnose other capability names.
4834 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4835 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4836
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004837 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4838 Attr.getAttributeSpellingListIndex()));
4839}
4840
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004841static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4842 const AttributeList &Attr) {
4843 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4844 Attr.getArgAsExpr(0),
4845 Attr.getAttributeSpellingListIndex()));
4846}
4847
4848static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4849 const AttributeList &Attr) {
4850 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004851 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004852 return;
4853
4854 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4855 S.Context,
4856 Args.data(), Args.size(),
4857 Attr.getAttributeSpellingListIndex()));
4858}
4859
4860static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4861 const AttributeList &Attr) {
4862 SmallVector<Expr*, 2> Args;
4863 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4864 return;
4865
4866 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4867 S.Context,
4868 Attr.getArgAsExpr(0),
4869 Args.data(),
4870 Args.size(),
4871 Attr.getAttributeSpellingListIndex()));
4872}
4873
4874static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4875 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004876 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004877 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004878 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004879
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004880 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4881 Attr.getRange(), S.Context, Args.data(), Args.size(),
4882 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004883}
4884
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004885static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4886 const AttributeList &Attr) {
4887 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4888 return;
4889
4890 // check that all arguments are lockable objects
4891 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004892 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004893 if (Args.empty())
4894 return;
4895
4896 RequiresCapabilityAttr *RCA = ::new (S.Context)
4897 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4898 Args.size(), Attr.getAttributeSpellingListIndex());
4899
4900 D->addAttr(RCA);
4901}
4902
Aaron Ballman43f40102014-11-14 22:34:56 +00004903static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4904 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4905 if (NSD->isAnonymousNamespace()) {
4906 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4907 // Do not want to attach the attribute to the namespace because that will
4908 // cause confusing diagnostic reports for uses of declarations within the
4909 // namespace.
4910 return;
4911 }
4912 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004913
4914 if (!S.getLangOpts().CPlusPlus14)
4915 if (Attr.isCXX11Attribute() &&
4916 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004917 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004918
Aaron Ballman43f40102014-11-14 22:34:56 +00004919 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4920}
4921
Peter Collingbourne915df992015-05-15 18:33:32 +00004922static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4923 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4924 return;
4925
4926 std::vector<std::string> Sanitizers;
4927
4928 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4929 StringRef SanitizerName;
4930 SourceLocation LiteralLoc;
4931
4932 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4933 return;
4934
4935 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4936 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4937
4938 Sanitizers.push_back(SanitizerName);
4939 }
4940
4941 D->addAttr(::new (S.Context) NoSanitizeAttr(
4942 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4943 Attr.getAttributeSpellingListIndex()));
4944}
4945
4946static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4947 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004948 StringRef AttrName = Attr.getName()->getName();
4949 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004950 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004951 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004952 .Case("no_address_safety_analysis", "address")
4953 .Case("no_sanitize_address", "address")
4954 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004955 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004956 D->addAttr(::new (S.Context)
4957 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4958 Attr.getAttributeSpellingListIndex()));
4959}
4960
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004961static void handleInternalLinkageAttr(Sema &S, Decl *D,
4962 const AttributeList &Attr) {
4963 if (InternalLinkageAttr *Internal =
4964 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
4965 Attr.getAttributeSpellingListIndex()))
4966 D->addAttr(Internal);
4967}
4968
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004969/// Handles semantic checking for features that are common to all attributes,
4970/// such as checking whether a parameter was properly specified, or the correct
4971/// number of arguments were passed, etc.
4972static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4973 const AttributeList &Attr) {
4974 // Several attributes carry different semantics than the parsing requires, so
4975 // those are opted out of the common handling.
4976 //
4977 // We also bail on unknown and ignored attributes because those are handled
4978 // as part of the target-specific handling logic.
4979 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004980 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004981 return false;
4982
Aaron Ballman3aff6332013-12-02 19:30:36 +00004983 // Check whether the attribute requires specific language extensions to be
4984 // enabled.
4985 if (!Attr.diagnoseLangOpts(S))
4986 return true;
4987
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004988 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4989 // If there are no optional arguments, then checking for the argument count
4990 // is trivial.
4991 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4992 return true;
4993 } else {
4994 // There are optional arguments, so checking is slightly more involved.
4995 if (Attr.getMinArgs() &&
4996 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4997 return true;
4998 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4999 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
5000 return true;
5001 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005002
5003 // Check whether the attribute appertains to the given subject.
5004 if (!Attr.diagnoseAppertainsTo(S, D))
5005 return true;
5006
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005007 return false;
5008}
5009
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005010//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005011// Top Level Sema Entry Points
5012//===----------------------------------------------------------------------===//
5013
Richard Smithf8a75c32013-08-29 00:47:48 +00005014/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5015/// the attribute applies to decls. If the attribute is a type attribute, just
5016/// silently ignore it if a GNU attribute.
5017static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5018 const AttributeList &Attr,
5019 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005020 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005021 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005022
Richard Smithf8a75c32013-08-29 00:47:48 +00005023 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5024 // instead.
5025 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5026 return;
5027
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005028 // Unknown attributes are automatically warned on. Target-specific attributes
5029 // which do not apply to the current target architecture are treated as
5030 // though they were unknown attributes.
5031 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00005032 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005033 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
5034 ? diag::warn_unhandled_ms_attribute_ignored
5035 : diag::warn_unknown_attribute_ignored)
5036 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005037 return;
5038 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005039
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005040 if (handleCommonAttributeFeatures(S, scope, D, Attr))
5041 return;
5042
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005043 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005044 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005045 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005046 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005047 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005048 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005049 handleInterruptAttr(S, D, Attr);
5050 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005051 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005052 handleX86ForceAlignArgPointerAttr(S, D, Attr);
5053 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005054 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005055 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00005056 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005057 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005058 case AttributeList::AT_Mips16:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005059 handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D,
5060 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005061 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005062 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005063 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
5064 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005065 case AttributeList::AT_AMDGPUNumVGPR:
5066 handleAMDGPUNumVGPRAttr(S, D, Attr);
5067 break;
5068 case AttributeList::AT_AMDGPUNumSGPR:
5069 handleAMDGPUNumSGPRAttr(S, D, Attr);
5070 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00005071 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005072 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
5073 break;
5074 case AttributeList::AT_IBOutlet:
5075 handleIBOutlet(S, D, Attr);
5076 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005077 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005078 handleIBOutletCollection(S, D, Attr);
5079 break;
5080 case AttributeList::AT_Alias:
5081 handleAliasAttr(S, D, Attr);
5082 break;
5083 case AttributeList::AT_Aligned:
5084 handleAlignedAttr(S, D, Attr);
5085 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005086 case AttributeList::AT_AlignValue:
5087 handleAlignValueAttr(S, D, Attr);
5088 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005089 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00005090 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005091 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005092 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005093 handleAnalyzerNoReturnAttr(S, D, Attr);
5094 break;
5095 case AttributeList::AT_TLSModel:
5096 handleTLSModelAttr(S, D, Attr);
5097 break;
5098 case AttributeList::AT_Annotate:
5099 handleAnnotateAttr(S, D, Attr);
5100 break;
5101 case AttributeList::AT_Availability:
5102 handleAvailabilityAttr(S, D, Attr);
5103 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005104 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00005105 handleDependencyAttr(S, scope, D, Attr);
5106 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005107 case AttributeList::AT_Common:
5108 handleCommonAttr(S, D, Attr);
5109 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005110 case AttributeList::AT_CUDAConstant:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005111 handleSimpleAttributeWithExclusions<CUDAConstantAttr, CUDASharedAttr>(S, D,
5112 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005113 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005114 case AttributeList::AT_PassObjectSize:
5115 handlePassObjectSizeAttr(S, D, Attr);
5116 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005117 case AttributeList::AT_Constructor:
5118 handleConstructorAttr(S, D, Attr);
5119 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005120 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005121 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
5122 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005123 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00005124 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005125 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005126 case AttributeList::AT_Destructor:
5127 handleDestructorAttr(S, D, Attr);
5128 break;
5129 case AttributeList::AT_EnableIf:
5130 handleEnableIfAttr(S, D, Attr);
5131 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005132 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005133 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005134 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005135 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00005136 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00005137 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005138 case AttributeList::AT_OptimizeNone:
5139 handleOptimizeNoneAttr(S, D, Attr);
5140 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005141 case AttributeList::AT_FlagEnum:
5142 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
5143 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005144 case AttributeList::AT_Flatten:
5145 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
5146 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005147 case AttributeList::AT_Format:
5148 handleFormatAttr(S, D, Attr);
5149 break;
5150 case AttributeList::AT_FormatArg:
5151 handleFormatArgAttr(S, D, Attr);
5152 break;
5153 case AttributeList::AT_CUDAGlobal:
5154 handleGlobalAttr(S, D, Attr);
5155 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005156 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005157 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
5158 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005159 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005160 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005161 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
5162 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005163 break;
5164 case AttributeList::AT_GNUInline:
5165 handleGNUInlineAttr(S, D, Attr);
5166 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005167 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005168 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005169 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005170 case AttributeList::AT_Restrict:
5171 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005172 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005173 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005174 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
5175 break;
5176 case AttributeList::AT_Mode:
5177 handleModeAttr(S, D, Attr);
5178 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00005179 case AttributeList::AT_NoAlias:
5180 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
5181 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005182 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005183 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
5184 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00005185 case AttributeList::AT_NoSplitStack:
5186 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
5187 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00005188 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005189 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
5190 handleNonNullAttrParameter(S, PVD, Attr);
5191 else
5192 handleNonNullAttr(S, D, Attr);
5193 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00005194 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005195 handleReturnsNonNullAttr(S, D, Attr);
5196 break;
Hal Finkelee90a222014-09-26 05:04:30 +00005197 case AttributeList::AT_AssumeAligned:
5198 handleAssumeAlignedAttr(S, D, Attr);
5199 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005200 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005201 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
5202 break;
5203 case AttributeList::AT_Ownership:
5204 handleOwnershipAttr(S, D, Attr);
5205 break;
5206 case AttributeList::AT_Cold:
5207 handleColdAttr(S, D, Attr);
5208 break;
5209 case AttributeList::AT_Hot:
5210 handleHotAttr(S, D, Attr);
5211 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005212 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005213 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005214 break;
5215 case AttributeList::AT_NoReturn:
5216 handleNoReturnAttr(S, D, Attr);
5217 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005218 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005219 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5220 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005221 case AttributeList::AT_CUDAShared:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005222 handleSimpleAttributeWithExclusions<CUDASharedAttr, CUDAConstantAttr>(S, D,
5223 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005224 break;
5225 case AttributeList::AT_VecReturn:
5226 handleVecReturnAttr(S, D, Attr);
5227 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005228
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005229 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005230 handleObjCOwnershipAttr(S, D, Attr);
5231 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005232 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005233 handleObjCPreciseLifetimeAttr(S, D, Attr);
5234 break;
John McCall31168b02011-06-15 23:02:42 +00005235
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005236 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005237 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5238 break;
John McCallcf166702011-07-22 08:53:00 +00005239
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005240 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005241 handleObjCRequiresSuperAttr(S, D, Attr);
5242 break;
5243
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005244 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005245 handleObjCBridgeAttr(S, scope, D, Attr);
5246 break;
5247
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005248 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005249 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5250 break;
5251
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005252 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005253 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5254 break;
John McCallf1e8b342011-09-29 07:17:38 +00005255
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005256 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005257 handleObjCDesignatedInitializer(S, D, Attr);
5258 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005259
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005260 case AttributeList::AT_ObjCRuntimeName:
5261 handleObjCRuntimeName(S, D, Attr);
5262 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005263
5264 case AttributeList::AT_ObjCBoxable:
5265 handleObjCBoxable(S, D, Attr);
5266 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005267
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005268 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005269 handleCFAuditedTransferAttr(S, D, Attr);
5270 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005271 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005272 handleCFUnknownTransferAttr(S, D, Attr);
5273 break;
John McCall32f5fe12011-09-30 05:12:12 +00005274
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005275 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005276 case AttributeList::AT_NSConsumed:
5277 handleNSConsumedAttr(S, D, Attr);
5278 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005279 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005280 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5281 break;
John McCalled433932011-01-25 03:31:58 +00005282
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005283 case AttributeList::AT_NSReturnsAutoreleased:
5284 case AttributeList::AT_NSReturnsNotRetained:
5285 case AttributeList::AT_CFReturnsNotRetained:
5286 case AttributeList::AT_NSReturnsRetained:
5287 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005288 handleNSReturnsRetainedAttr(S, D, Attr);
5289 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005290 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005291 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5292 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005293 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005294 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5295 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005296 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005297 handleVecTypeHint(S, D, Attr);
5298 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005299
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005300 case AttributeList::AT_InitPriority:
5301 handleInitPriorityAttr(S, D, Attr);
5302 break;
5303
5304 case AttributeList::AT_Packed:
5305 handlePackedAttr(S, D, Attr);
5306 break;
5307 case AttributeList::AT_Section:
5308 handleSectionAttr(S, D, Attr);
5309 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005310 case AttributeList::AT_Target:
5311 handleTargetAttr(S, D, Attr);
5312 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005313 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005314 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005315 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005316 case AttributeList::AT_ArcWeakrefUnavailable:
5317 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5318 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005319 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005320 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5321 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005322 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005323 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005324 break;
5325 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005326 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5327 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005328 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005329 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5330 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005331 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005332 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5333 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005334 case AttributeList::AT_NotTailCalled:
5335 handleNotTailCalledAttr(S, D, Attr);
5336 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005337 case AttributeList::AT_DisableTailCalls:
5338 handleDisableTailCallsAttr(S, D, Attr);
5339 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005340 case AttributeList::AT_Used:
5341 handleUsedAttr(S, D, Attr);
5342 break;
John McCalld041a9b2013-02-20 01:54:26 +00005343 case AttributeList::AT_Visibility:
5344 handleVisibilityAttr(S, D, Attr, false);
5345 break;
5346 case AttributeList::AT_TypeVisibility:
5347 handleVisibilityAttr(S, D, Attr, true);
5348 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005349 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005350 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5351 break;
5352 case AttributeList::AT_WarnUnusedResult:
5353 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005354 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005355 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005356 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5357 break;
5358 case AttributeList::AT_WeakRef:
5359 handleWeakRefAttr(S, D, Attr);
5360 break;
5361 case AttributeList::AT_WeakImport:
5362 handleWeakImportAttr(S, D, Attr);
5363 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005364 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005365 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005366 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005367 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005368 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5369 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005370 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005371 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005372 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005373 case AttributeList::AT_ObjCNSObject:
5374 handleObjCNSObject(S, D, Attr);
5375 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005376 case AttributeList::AT_ObjCIndependentClass:
5377 handleObjCIndependentClass(S, D, Attr);
5378 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005379 case AttributeList::AT_Blocks:
5380 handleBlocksAttr(S, D, Attr);
5381 break;
5382 case AttributeList::AT_Sentinel:
5383 handleSentinelAttr(S, D, Attr);
5384 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005385 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005386 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5387 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005388 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005389 handleSimpleAttribute<PureAttr>(S, D, Attr);
5390 break;
5391 case AttributeList::AT_Cleanup:
5392 handleCleanupAttr(S, D, Attr);
5393 break;
5394 case AttributeList::AT_NoDebug:
5395 handleNoDebugAttr(S, D, Attr);
5396 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005397 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005398 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5399 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005400 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005401 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5402 break;
5403 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5404 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5405 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005406 case AttributeList::AT_StdCall:
5407 case AttributeList::AT_CDecl:
5408 case AttributeList::AT_FastCall:
5409 case AttributeList::AT_ThisCall:
5410 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005411 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005412 case AttributeList::AT_MSABI:
5413 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005414 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005415 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005416 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005417 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005418 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005419 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5420 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005421 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005422 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5423 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005424 case AttributeList::AT_InternalLinkage:
5425 handleInternalLinkageAttr(S, D, Attr);
5426 break;
John McCall8d32c052012-05-22 21:28:12 +00005427
5428 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005429 case AttributeList::AT_MSNoVTable:
5430 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005431 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005432 case AttributeList::AT_MSStruct:
5433 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005434 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005435 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005436 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005437 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005438 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005439 handleMSInheritanceAttr(S, D, Attr);
5440 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005441 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005442 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5443 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005444 case AttributeList::AT_Thread:
5445 handleDeclspecThreadAttr(S, D, Attr);
5446 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005447
5448 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005449 case AttributeList::AT_AssertExclusiveLock:
5450 handleAssertExclusiveLockAttr(S, D, Attr);
5451 break;
5452 case AttributeList::AT_AssertSharedLock:
5453 handleAssertSharedLockAttr(S, D, Attr);
5454 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005455 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005456 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5457 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005458 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005459 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005460 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005461 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005462 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5463 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005464 case AttributeList::AT_NoSanitize:
5465 handleNoSanitizeAttr(S, D, Attr);
5466 break;
5467 case AttributeList::AT_NoSanitizeSpecific:
5468 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005469 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005470 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005471 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005472 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005473 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005474 handleGuardedByAttr(S, D, Attr);
5475 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005476 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005477 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005478 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005479 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005480 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005481 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005482 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005483 handleLockReturnedAttr(S, D, Attr);
5484 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005485 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005486 handleLocksExcludedAttr(S, D, Attr);
5487 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005488 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005489 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005490 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005491 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005492 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005493 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005494 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005495 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005496 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005497
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005498 // Capability analysis attributes.
5499 case AttributeList::AT_Capability:
5500 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005501 handleCapabilityAttr(S, D, Attr);
5502 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005503 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005504 handleRequiresCapabilityAttr(S, D, Attr);
5505 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005506
5507 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005508 handleAssertCapabilityAttr(S, D, Attr);
5509 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005510 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005511 handleAcquireCapabilityAttr(S, D, Attr);
5512 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005513 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005514 handleReleaseCapabilityAttr(S, D, Attr);
5515 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005516 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005517 handleTryAcquireCapabilityAttr(S, D, Attr);
5518 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005519
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005520 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005521 case AttributeList::AT_Consumable:
5522 handleConsumableAttr(S, D, Attr);
5523 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005524 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005525 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5526 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005527 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005528 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5529 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005530 case AttributeList::AT_CallableWhen:
5531 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005532 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005533 case AttributeList::AT_ParamTypestate:
5534 handleParamTypestateAttr(S, D, Attr);
5535 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005536 case AttributeList::AT_ReturnTypestate:
5537 handleReturnTypestateAttr(S, D, Attr);
5538 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005539 case AttributeList::AT_SetTypestate:
5540 handleSetTypestateAttr(S, D, Attr);
5541 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005542 case AttributeList::AT_TestTypestate:
5543 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005544 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005545
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005546 // Type safety attributes.
5547 case AttributeList::AT_ArgumentWithTypeTag:
5548 handleArgumentWithTypeTagAttr(S, D, Attr);
5549 break;
5550 case AttributeList::AT_TypeTagForDatatype:
5551 handleTypeTagForDatatypeAttr(S, D, Attr);
5552 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005553 }
5554}
5555
5556/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5557/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005558void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005559 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005560 bool IncludeCXX11Attributes) {
5561 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005562 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005563
Joey Gouly2cd9db12013-12-13 16:15:28 +00005564 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005565 // GCC accepts
5566 // static int a9 __attribute__((weakref));
5567 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005568 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005569 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5570 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005571 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005572 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005573 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005574
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005575 // FIXME: We should be able to handle this in TableGen as well. It would be
5576 // good to have a way to specify "these attributes must appear as a group",
5577 // for these. Additionally, it would be good to have a way to specify "these
5578 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005579 if (!D->hasAttr<OpenCLKernelAttr>()) {
5580 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005581 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005582 // FIXME: This emits a different error message than
5583 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005584 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005585 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005586 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005587 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005588 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005589 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005590 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005591 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005592 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5593 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5594 << A << ExpectedKernelFunction;
5595 D->setInvalidDecl();
5596 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5597 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5598 << A << ExpectedKernelFunction;
5599 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005600 }
5601 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005602}
5603
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005604// Annotation attributes are the only attributes allowed after an access
5605// specifier.
5606bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5607 const AttributeList *AttrList) {
5608 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005609 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005610 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005611 } else {
5612 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5613 return true;
5614 }
5615 }
5616
5617 return false;
5618}
5619
John McCall42856de2011-10-01 05:17:03 +00005620/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5621/// contains any decl attributes that we should warn about.
5622static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5623 for ( ; A; A = A->getNext()) {
5624 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005625 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005626 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5627
5628 if (A->getKind() == AttributeList::UnknownAttribute) {
5629 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5630 << A->getName() << A->getRange();
5631 } else {
5632 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5633 << A->getName() << A->getRange();
5634 }
5635 }
5636}
5637
5638/// checkUnusedDeclAttributes - Given a declarator which is not being
5639/// used to build a declaration, complain about any decl attributes
5640/// which might be lying around on it.
5641void Sema::checkUnusedDeclAttributes(Declarator &D) {
5642 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5643 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5644 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5645 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5646}
5647
Ryan Flynn7d470f32009-07-30 03:15:39 +00005648/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005649/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005650NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5651 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005652 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005653 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005654 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00005655 FunctionDecl *NewFD;
5656 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00005657 // FIXME: Mangling?
5658 // FIXME: Is the qualifier info correct?
5659 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00005660 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5661 Loc, Loc, DeclarationName(II),
5662 FD->getType(), FD->getTypeSourceInfo(),
5663 SC_None, false/*isInlineSpecified*/,
5664 FD->hasPrototype(),
5665 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005666 NewD = NewFD;
5667
5668 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005669 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005670
5671 // Fake up parameter variables; they are declared as if this were
5672 // a typedef.
5673 QualType FDTy = FD->getType();
5674 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5675 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005676 for (const auto &AI : FT->param_types()) {
5677 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005678 Param->setScopeInfo(0, Params.size());
5679 Params.push_back(Param);
5680 }
David Blaikie9c70e042011-09-21 18:16:56 +00005681 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005682 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005683 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5684 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005685 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005686 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005687 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005688 if (VD->getQualifier()) {
5689 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005690 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005691 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005692 }
5693 return NewD;
5694}
5695
James Dennett634962f2012-06-14 21:40:34 +00005696/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005697/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005698void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005699 if (W.getUsed()) return; // only do this once
5700 W.setUsed(true);
5701 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5702 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005703 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005704 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5705 W.getLocation()));
5706 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005707 WeakTopLevelDecl.push_back(NewD);
5708 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5709 // to insert Decl at TU scope, sorry.
5710 DeclContext *SavedContext = CurContext;
5711 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005712 NewD->setDeclContext(CurContext);
5713 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005714 PushOnScopeChains(NewD, S);
5715 CurContext = SavedContext;
5716 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005717 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005718 }
5719}
5720
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005721void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5722 // It's valid to "forward-declare" #pragma weak, in which case we
5723 // have to do this.
5724 LoadExternalWeakUndeclaredIdentifiers();
5725 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005726 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005727 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5728 if (VD->isExternC())
5729 ND = VD;
5730 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5731 if (FD->isExternC())
5732 ND = FD;
5733 if (ND) {
5734 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005735 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005736 if (I != WeakUndeclaredIdentifiers.end()) {
5737 WeakInfo W = I->second;
5738 DeclApplyPragmaWeak(S, ND, W);
5739 WeakUndeclaredIdentifiers[Id] = W;
5740 }
5741 }
5742 }
5743 }
5744}
5745
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005746/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5747/// it, apply them to D. This is a bit tricky because PD can have attributes
5748/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005749void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005750 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005751 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005752 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005753
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005754 // Walk the declarator structure, applying decl attributes that were in a type
5755 // position to the decl itself. This handles cases like:
5756 // int *__attr__(x)** D;
5757 // when X is a decl attribute.
5758 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5759 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005760 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005761
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005762 // Finally, apply any attributes on the decl itself.
5763 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005764 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005765}
John McCall28a6aea2009-11-04 02:18:39 +00005766
John McCall31168b02011-06-15 23:02:42 +00005767/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005768/// If so, it'll still be annotated with an attribute that makes it
5769/// illegal to actually use.
5770static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5771 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005772 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005773 // Private ivars are always okay. Unfortunately, people don't
5774 // always properly make their ivars private, even in system headers.
5775 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005776 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5777 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005778 return false;
5779
John McCallc6af8c62015-10-28 05:03:19 +00005780 // Silently accept unsupported uses of __weak in both user and system
5781 // declarations when it's been disabled, for ease of integration with
5782 // -fno-objc-arc files. We do have to take some care against attempts
5783 // to define such things; for now, we've only done that for ivars
5784 // and properties.
5785 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5786 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5787 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5788 reason = UnavailableAttr::IR_ForbiddenWeak;
5789 return true;
5790 }
John McCallb61e14e2015-10-27 04:54:50 +00005791 }
5792
John McCallc6af8c62015-10-28 05:03:19 +00005793 // Allow all sorts of things in system headers.
5794 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5795 // Currently, all the failures dealt with this way are due to ARC
5796 // restrictions.
5797 reason = UnavailableAttr::IR_ARCForbiddenType;
5798 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005799 }
5800
5801 return false;
John McCall31168b02011-06-15 23:02:42 +00005802}
5803
5804/// Handle a delayed forbidden-type diagnostic.
5805static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5806 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005807 auto reason = UnavailableAttr::IR_None;
5808 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5809 assert(reason && "didn't set reason?");
5810 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5811 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005812 return;
5813 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005814 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005815 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005816 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005817 // kind of forbidden type messages on unavailable functions.
5818 if (FD->hasAttr<UnavailableAttr>() &&
5819 diag.getForbiddenTypeDiagnostic() ==
5820 diag::err_arc_array_param_no_ownership) {
5821 diag.Triggered = true;
5822 return;
5823 }
5824 }
John McCall31168b02011-06-15 23:02:42 +00005825
5826 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5827 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5828 diag.Triggered = true;
5829}
5830
Aaron Ballmanfb237522014-10-15 15:37:51 +00005831
5832static bool isDeclDeprecated(Decl *D) {
5833 do {
5834 if (D->isDeprecated())
5835 return true;
5836 // A category implicitly has the availability of the interface.
5837 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005838 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5839 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005840 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5841 return false;
5842}
5843
5844static bool isDeclUnavailable(Decl *D) {
5845 do {
5846 if (D->isUnavailable())
5847 return true;
5848 // A category implicitly has the availability of the interface.
5849 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005850 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5851 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005852 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5853 return false;
5854}
5855
Nico Weber0055a192015-03-19 19:18:22 +00005856static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005857 Decl *Ctx, const NamedDecl *D,
5858 StringRef Message, SourceLocation Loc,
5859 const ObjCInterfaceDecl *UnknownObjCClass,
5860 const ObjCPropertyDecl *ObjCProperty,
5861 bool ObjCPropertyAccess) {
5862 // Diagnostics for deprecated or unavailable.
5863 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005864 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005865
5866 // Matches 'diag::note_property_attribute' options.
5867 unsigned property_note_select;
5868
5869 // Matches diag::note_availability_specified_here.
5870 unsigned available_here_select_kind;
5871
5872 // Don't warn if our current context is deprecated or unavailable.
5873 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005874 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005875 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005876 return;
5877 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5878 : diag::warn_property_method_deprecated;
5879 diag_message = diag::warn_deprecated_message;
5880 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5881 property_note_select = /* deprecated */ 0;
5882 available_here_select_kind = /* deprecated */ 2;
5883 break;
5884
Nico Weber0055a192015-03-19 19:18:22 +00005885 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005886 if (isDeclUnavailable(Ctx))
5887 return;
5888 diag = !ObjCPropertyAccess ? diag::err_unavailable
5889 : diag::err_property_method_unavailable;
5890 diag_message = diag::err_unavailable_message;
5891 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5892 property_note_select = /* unavailable */ 1;
5893 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005894
John McCallc6af8c62015-10-28 05:03:19 +00005895 if (auto attr = D->getAttr<UnavailableAttr>()) {
5896 if (attr->isImplicit() && attr->getImplicitReason()) {
5897 // Most of these failures are due to extra restrictions in ARC;
5898 // reflect that in the primary diagnostic when applicable.
5899 auto flagARCError = [&] {
5900 if (S.getLangOpts().ObjCAutoRefCount &&
5901 S.getSourceManager().isInSystemHeader(D->getLocation()))
5902 diag = diag::err_unavailable_in_arc;
5903 };
5904
5905 switch (attr->getImplicitReason()) {
5906 case UnavailableAttr::IR_None: break;
5907
5908 case UnavailableAttr::IR_ARCForbiddenType:
5909 flagARCError();
5910 diag_available_here = diag::note_arc_forbidden_type;
5911 break;
5912
5913 case UnavailableAttr::IR_ForbiddenWeak:
5914 if (S.getLangOpts().ObjCWeakRuntime)
5915 diag_available_here = diag::note_arc_weak_disabled;
5916 else
5917 diag_available_here = diag::note_arc_weak_no_runtime;
5918 break;
5919
5920 case UnavailableAttr::IR_ARCForbiddenConversion:
5921 flagARCError();
5922 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5923 break;
5924
5925 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5926 flagARCError();
5927 diag_available_here = diag::note_arc_init_returns_unrelated;
5928 break;
5929
5930 case UnavailableAttr::IR_ARCFieldWithOwnership:
5931 flagARCError();
5932 diag_available_here = diag::note_arc_field_with_ownership;
5933 break;
5934 }
5935 }
John McCallb61e14e2015-10-27 04:54:50 +00005936 }
5937
Aaron Ballmanfb237522014-10-15 15:37:51 +00005938 break;
5939
Nico Weber0055a192015-03-19 19:18:22 +00005940 case Sema::AD_Partial:
5941 diag = diag::warn_partial_availability;
5942 diag_message = diag::warn_partial_message;
5943 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5944 property_note_select = /* partial */ 2;
5945 available_here_select_kind = /* partial */ 3;
5946 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005947 }
5948
Aaron Ballmanfb237522014-10-15 15:37:51 +00005949 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005950 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005951 if (ObjCProperty)
5952 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5953 << ObjCProperty->getDeclName() << property_note_select;
5954 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005955 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005956 if (ObjCProperty)
5957 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5958 << ObjCProperty->getDeclName() << property_note_select;
5959 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005960 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005961 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5962 }
5963
John McCallb61e14e2015-10-27 04:54:50 +00005964 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005965 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005966 if (K == Sema::AD_Partial)
5967 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005968}
5969
5970static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5971 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005972 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5973 DD.Kind == DelayedDiagnostic::Unavailable);
5974 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5975 ? Sema::AD_Deprecation
5976 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005977 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005978 DoEmitAvailabilityWarning(
5979 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5980 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005981}
5982
John McCall2ec85372012-05-07 06:16:41 +00005983void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5984 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005985 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005986 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005987
John McCall2ec85372012-05-07 06:16:41 +00005988 // When delaying diagnostics to run in the context of a parsed
5989 // declaration, we only want to actually emit anything if parsing
5990 // succeeds.
5991 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005992
John McCall2ec85372012-05-07 06:16:41 +00005993 // We emit all the active diagnostics in this pool or any of its
5994 // parents. In general, we'll get one pool for the decl spec
5995 // and a child pool for each declarator; in a decl group like:
5996 // deprecated_typedef foo, *bar, baz();
5997 // only the declarator pops will be passed decls. This is correct;
5998 // we really do need to consider delayed diagnostics from the decl spec
5999 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00006000 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00006001 do {
John McCall6347b682012-05-07 06:16:58 +00006002 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00006003 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
6004 // This const_cast is a bit lame. Really, Triggered should be mutable.
6005 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00006006 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00006007 continue;
6008
John McCallc1465822011-02-14 07:13:47 +00006009 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00006010 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00006011 case DelayedDiagnostic::Unavailable:
6012 // Don't bother giving deprecation/unavailable diagnostics if
6013 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00006014 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00006015 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00006016 break;
6017
6018 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00006019 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00006020 break;
John McCall31168b02011-06-15 23:02:42 +00006021
6022 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00006023 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00006024 break;
John McCall86121512010-01-27 03:50:35 +00006025 }
6026 }
John McCall2ec85372012-05-07 06:16:41 +00006027 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00006028}
6029
John McCall6347b682012-05-07 06:16:58 +00006030/// Given a set of delayed diagnostics, re-emit them as if they had
6031/// been delayed in the current context instead of in the given pool.
6032/// Essentially, this just moves them to the current pool.
6033void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
6034 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
6035 assert(curPool && "re-emitting in undelayed context not supported");
6036 curPool->steal(pool);
6037}
6038
Ted Kremenekb79ee572013-12-18 23:30:06 +00006039void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
6040 NamedDecl *D, StringRef Message,
6041 SourceLocation Loc,
6042 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00006043 const ObjCPropertyDecl *ObjCProperty,
6044 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00006045 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00006046 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00006047 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
6048 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
6049 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00006050 return;
6051 }
6052
Ted Kremenekb79ee572013-12-18 23:30:06 +00006053 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00006054 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
6055 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00006056}