blob: 5c5321ef2d5a7186556092b0fdfbe4e3166ec80e [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000032#include "llvm/Support/MathExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000034using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000036namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000037 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000038 C,
39 Cpp,
40 ObjC
41 };
42}
43
Chris Lattner58418ff2008-06-29 00:16:31 +000044//===----------------------------------------------------------------------===//
45// Helper functions
46//===----------------------------------------------------------------------===//
47
Ted Kremenek527042b2009-08-14 20:49:40 +000048/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000049/// type (function or function-typed variable) or an Objective-C
50/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000051static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000052 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000053}
David Majnemer06864812015-04-07 06:01:53 +000054/// \brief Return true if the given decl has function type (function or
55/// function-typed variable) or an Objective-C method or a block.
56static bool isFunctionOrMethodOrBlock(const Decl *D) {
57 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
58}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000059
John McCall3882ace2011-01-05 12:14:39 +000060/// Return true if the given decl has a declarator that should have
61/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000062static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000063 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
65 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000066}
67
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068/// hasFunctionProto - Return true if the given decl has a argument
69/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000070/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000071static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000072 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000073 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000074 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000075}
76
Alp Toker601b22c2014-01-21 23:35:24 +000077/// getFunctionOrMethodNumParams - Return number of function or method
78/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000079/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000080static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000081 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000082 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000083 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000084 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000086}
87
Alp Toker601b22c2014-01-21 23:35:24 +000088static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000089 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000090 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000092 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000093
Alp Toker03376dc2014-07-07 09:02:20 +000094 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000095}
96
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000097static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
98 if (const auto *FD = dyn_cast<FunctionDecl>(D))
99 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000100 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000101 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000102 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000103 return BD->getParamDecl(Idx)->getSourceRange();
104 return SourceRange();
105}
106
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000108 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000109 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000110 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000111}
112
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000113static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
114 if (const auto *FD = dyn_cast<FunctionDecl>(D))
115 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000116 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000117 return MD->getReturnTypeSourceRange();
118 return SourceRange();
119}
120
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000121static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000122 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000123 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000124 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000125 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000126 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
127 return BD->isVariadic();
128
129 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000130}
131
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000132static bool isInstanceMethod(const Decl *D) {
133 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000134 return MethodDecl->isInstance();
135 return false;
136}
137
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000138static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000139 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000140 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000141 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000142
John McCall96fa4842010-05-17 21:00:27 +0000143 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
144 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000145 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000146
John McCall96fa4842010-05-17 21:00:27 +0000147 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000148
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000149 // FIXME: Should we walk the chain of classes?
150 return ClsName == &Ctx.Idents.get("NSString") ||
151 ClsName == &Ctx.Idents.get("NSMutableString");
152}
153
Daniel Dunbar980c6692008-09-26 03:32:58 +0000154static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000155 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000156 if (!PT)
157 return false;
158
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000159 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000160 if (!RT)
161 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000162
Daniel Dunbar980c6692008-09-26 03:32:58 +0000163 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000164 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165 return false;
166
167 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
168}
169
Richard Smithb87c4652013-10-31 21:23:20 +0000170static unsigned getNumAttributeArgs(const AttributeList &Attr) {
171 // FIXME: Include the type in the argument list.
172 return Attr.getNumArgs() + Attr.hasParsedType();
173}
174
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000175template <typename Compare>
176static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
177 unsigned Num, unsigned Diag,
178 Compare Comp) {
179 if (Comp(getNumAttributeArgs(Attr), Num)) {
180 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000181 return false;
182 }
183
184 return true;
185}
186
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000187/// \brief Check if the attribute has exactly as many args as Num. May
188/// output an error.
189static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
190 unsigned Num) {
191 return checkAttributeNumArgsImpl(S, Attr, Num,
192 diag::err_attribute_wrong_number_arguments,
193 std::not_equal_to<unsigned>());
194}
195
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000196/// \brief Check if the attribute has at least as many args as Num. May
197/// output an error.
198static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000199 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000200 return checkAttributeNumArgsImpl(S, Attr, Num,
201 diag::err_attribute_too_few_arguments,
202 std::less<unsigned>());
203}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000204
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000205/// \brief Check if the attribute has at most as many args as Num. May
206/// output an error.
207static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
208 unsigned Num) {
209 return checkAttributeNumArgsImpl(S, Attr, Num,
210 diag::err_attribute_too_many_arguments,
211 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000212}
213
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000214/// \brief If Expr is a valid integer constant, get the value of the integer
215/// expression and return success or failure. May output an error.
216static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
217 const Expr *Expr, uint32_t &Val,
218 unsigned Idx = UINT_MAX) {
219 llvm::APSInt I(32);
220 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
221 !Expr->isIntegerConstantExpr(I, S.Context)) {
222 if (Idx != UINT_MAX)
223 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
224 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
225 << Expr->getSourceRange();
226 else
227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
228 << Attr.getName() << AANT_ArgumentIntegerConstant
229 << Expr->getSourceRange();
230 return false;
231 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000232
233 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000234 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
235 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000236 return false;
237 }
238
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000239 Val = (uint32_t)I.getZExtValue();
240 return true;
241}
242
Aaron Ballmanfb763042013-12-02 18:05:46 +0000243/// \brief Diagnose mutually exclusive attributes when present on a given
244/// declaration. Returns true if diagnosed.
245template <typename AttrTy>
246static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000247 const AttributeList &Attr) {
248 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000249 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000250 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000251 return true;
252 }
253 return false;
254}
255
Alp Toker601b22c2014-01-21 23:35:24 +0000256/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000257/// instance method D. May output an error.
258///
259/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000260static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
261 const AttributeList &Attr,
262 unsigned AttrArgNum,
263 const Expr *IdxExpr,
264 uint64_t &Idx) {
David Majnemer06864812015-04-07 06:01:53 +0000265 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000266
267 // In C++ the implicit 'this' function parameter also counts.
268 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000269 bool HP = hasFunctionProto(D);
270 bool HasImplicitThisParam = isInstanceMethod(D);
271 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000272 unsigned NumParams =
273 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000274
275 llvm::APSInt IdxInt;
276 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
277 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
279 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
280 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000281 return false;
282 }
283
284 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000285 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
287 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000288 return false;
289 }
290 Idx--; // Convert to zero-based.
291 if (HasImplicitThisParam) {
292 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000293 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000294 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000295 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000296 return false;
297 }
298 --Idx;
299 }
300
301 return true;
302}
303
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000304/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
305/// If not emit an error and return false. If the argument is an identifier it
306/// will emit an error with a fixit hint and treat it as if it was a string
307/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000308bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
309 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000310 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000311 // Look for identifiers. If we have one emit a hint to fix it to a literal.
312 if (Attr.isArgIdent(ArgNum)) {
313 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000314 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000315 << Attr.getName() << AANT_ArgumentString
316 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000317 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000318 Str = Loc->Ident->getName();
319 if (ArgLocation)
320 *ArgLocation = Loc->Loc;
321 return true;
322 }
323
324 // Now check for an actual string literal.
325 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
326 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
327 if (ArgLocation)
328 *ArgLocation = ArgExpr->getLocStart();
329
330 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000331 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000332 << Attr.getName() << AANT_ArgumentString;
333 return false;
334 }
335
336 Str = Literal->getString();
337 return true;
338}
339
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000340/// \brief Applies the given attribute to the Decl without performing any
341/// additional semantic checking.
342template <typename AttrType>
343static void handleSimpleAttribute(Sema &S, Decl *D,
344 const AttributeList &Attr) {
345 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
346 Attr.getAttributeSpellingListIndex()));
347}
348
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000349/// \brief Check if the passed-in expression is of type int or bool.
350static bool isIntOrBool(Expr *Exp) {
351 QualType QT = Exp->getType();
352 return QT->isBooleanType() || QT->isIntegerType();
353}
354
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000355
356// Check to see if the type is a smart pointer of some kind. We assume
357// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000358static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000359 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
360 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000361 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000362 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000363
Richard Smithcf4bdde2015-02-21 02:45:19 +0000364 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
365 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000366 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000367 return false;
368
369 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000370}
371
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000372/// \brief Check if passed in Decl is a pointer type.
373/// Note that this function may produce an error message.
374/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
376 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000377 const ValueDecl *vd = cast<ValueDecl>(D);
378 QualType QT = vd->getType();
379 if (QT->isAnyPointerType())
380 return true;
381
382 if (const RecordType *RT = QT->getAs<RecordType>()) {
383 // If it's an incomplete type, it could be a smart pointer; skip it.
384 // (We don't want to force template instantiation if we can avoid it,
385 // since that would alter the order in which templates are instantiated.)
386 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000387 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000388
Aaron Ballman553e6812013-12-26 14:54:11 +0000389 if (threadSafetyCheckIsSmartPointer(S, RT))
390 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000391 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000392
393 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000394 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000395 return false;
396}
397
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000398/// \brief Checks that the passed in QualType either is of RecordType or points
399/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000400static const RecordType *getRecordType(QualType QT) {
401 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000402 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000403
404 // Now check if we point to record type.
405 if (const PointerType *PT = QT->getAs<PointerType>())
406 return PT->getPointeeType()->getAs<RecordType>();
407
Craig Topperc3ec1492014-05-26 06:22:03 +0000408 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000409}
410
Aaron Ballman76050722014-04-04 15:13:57 +0000411static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000412 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000413
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000414 if (!RT)
415 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000416
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000417 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000418 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000419 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000420
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000421 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000422 // FIXME -- Check the type that the smart pointer points to.
423 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000424 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000425
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000426 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000427 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000428 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000429 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000430
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000431 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000432 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
433 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000434 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
435 void *) {
436 return BS->getType()->getAs<RecordType>()
437 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000438 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000439 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000440 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000441 return false;
442}
443
Aaron Ballman76050722014-04-04 15:13:57 +0000444static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000445 const auto *TD = Ty->getAs<TypedefType>();
446 if (!TD)
447 return false;
448
449 TypedefNameDecl *TN = TD->getDecl();
450 if (!TN)
451 return false;
452
453 return TN->hasAttr<CapabilityAttr>();
454}
455
Aaron Ballman76050722014-04-04 15:13:57 +0000456static bool typeHasCapability(Sema &S, QualType Ty) {
457 if (checkTypedefTypeForCapability(Ty))
458 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000459
Aaron Ballman76050722014-04-04 15:13:57 +0000460 if (checkRecordTypeForCapability(S, Ty))
461 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000462
Aaron Ballman76050722014-04-04 15:13:57 +0000463 return false;
464}
465
466static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
467 // Capability expressions are simple expressions involving the boolean logic
468 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
469 // a DeclRefExpr is found, its type should be checked to determine whether it
470 // is a capability or not.
471
472 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
473 return typeHasCapability(S, E->getType());
474 else if (const auto *E = dyn_cast<CastExpr>(Ex))
475 return isCapabilityExpr(S, E->getSubExpr());
476 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
477 return isCapabilityExpr(S, E->getSubExpr());
478 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
479 if (E->getOpcode() == UO_LNot)
480 return isCapabilityExpr(S, E->getSubExpr());
481 return false;
482 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
483 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
484 return isCapabilityExpr(S, E->getLHS()) &&
485 isCapabilityExpr(S, E->getRHS());
486 return false;
487 }
488
489 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000490}
491
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000492/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
493/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000494/// \param Sidx The attribute argument index to start checking with.
495/// \param ParamIdxOk Whether an argument can be indexing into a function
496/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000497static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
498 const AttributeList &Attr,
499 SmallVectorImpl<Expr *> &Args,
500 int Sidx = 0,
501 bool ParamIdxOk = false) {
502 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000503 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000504
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000505 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000506 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000507 Args.push_back(ArgExp);
508 continue;
509 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000510
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000511 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000512 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000513 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000514 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000515 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000516 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000517 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000518 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000519
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 // We allow constant strings to be used as a placeholder for expressions
521 // that are not valid C++ syntax, but warn that they are ignored.
522 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
523 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000524 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000525 continue;
526 }
527
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000530 // A pointer to member expression of the form &MyClass::mu is treated
531 // specially -- we need to look at the type of the member.
532 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
533 if (UOp->getOpcode() == UO_AddrOf)
534 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
535 if (DRE->getDecl()->isCXXInstanceMember())
536 ArgTy = DRE->getDecl()->getType();
537
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000538 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000539 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000540
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000541 // Now check if we index into a record type function param.
542 if(!RT && ParamIdxOk) {
543 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000544 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
545 if(FD && IL) {
546 unsigned int NumParams = FD->getNumParams();
547 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000548 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
549 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
550 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
552 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000553 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000554 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000555 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000556 }
557 }
558
Aaron Ballman76050722014-04-04 15:13:57 +0000559 // If the type does not have a capability, see if the components of the
560 // expression have capabilities. This allows for writing C code where the
561 // capability may be on the type, and the expression is a capability
562 // boolean logic expression. Eg) requires_capability(A || B && !C)
563 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
564 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
565 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000566
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000567 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000568 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000569}
570
Chris Lattner58418ff2008-06-29 00:16:31 +0000571//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000572// Attribute Implementations
573//===----------------------------------------------------------------------===//
574
Michael Hana9171bc2012-08-03 17:40:43 +0000575static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000576 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000577 if (!threadSafetyCheckIsPointer(S, D, Attr))
578 return;
579
Michael Han99315932013-01-24 16:46:58 +0000580 D->addAttr(::new (S.Context)
581 PtGuardedVarAttr(Attr.getRange(), S.Context,
582 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000583}
584
Michael Hana9171bc2012-08-03 17:40:43 +0000585static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
586 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000587 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000588 SmallVector<Expr*, 1> Args;
589 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000590 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000591 unsigned Size = Args.size();
592 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000593 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000594
Michael Han3be3b442012-07-23 18:48:41 +0000595 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596
Michael Han3be3b442012-07-23 18:48:41 +0000597 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000598}
599
Michael Han3be3b442012-07-23 18:48:41 +0000600static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000601 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000602 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
603 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000604
Aaron Ballman36a53502014-01-16 13:03:14 +0000605 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
606 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000607}
608
Michael Hana9171bc2012-08-03 17:40:43 +0000609static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000610 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000611 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000612 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
613 return;
614
615 if (!threadSafetyCheckIsPointer(S, D, Attr))
616 return;
617
618 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000619 S.Context, Arg,
620 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000621}
622
Michael Hana9171bc2012-08-03 17:40:43 +0000623static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
624 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000625 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000626 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000627 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000628
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000629 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000630 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000631 if (!QT->isDependentType()) {
632 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000633 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000634 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000635 << Attr.getName();
636 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000637 }
638 }
639
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000640 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000641 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000642 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000643 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000644
Michael Han3be3b442012-07-23 18:48:41 +0000645 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000646}
647
Michael Hana9171bc2012-08-03 17:40:43 +0000648static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000649 const AttributeList &Attr) {
650 SmallVector<Expr*, 1> Args;
651 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
652 return;
653
654 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000655 D->addAttr(::new (S.Context)
656 AcquiredAfterAttr(Attr.getRange(), S.Context,
657 StartArg, Args.size(),
658 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000659}
660
Michael Hana9171bc2012-08-03 17:40:43 +0000661static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000662 const AttributeList &Attr) {
663 SmallVector<Expr*, 1> Args;
664 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
665 return;
666
667 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000668 D->addAttr(::new (S.Context)
669 AcquiredBeforeAttr(Attr.getRange(), S.Context,
670 StartArg, Args.size(),
671 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000672}
673
Michael Hana9171bc2012-08-03 17:40:43 +0000674static bool checkLockFunAttrCommon(Sema &S, Decl *D,
675 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000676 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000677 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000678 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000679 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000680
Michael Han3be3b442012-07-23 18:48:41 +0000681 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000682}
683
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000684static void handleAssertSharedLockAttr(Sema &S, Decl *D,
685 const AttributeList &Attr) {
686 SmallVector<Expr*, 1> Args;
687 if (!checkLockFunAttrCommon(S, D, Attr, Args))
688 return;
689
690 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000691 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000692 D->addAttr(::new (S.Context)
693 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
694 Attr.getAttributeSpellingListIndex()));
695}
696
697static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
698 const AttributeList &Attr) {
699 SmallVector<Expr*, 1> Args;
700 if (!checkLockFunAttrCommon(S, D, Attr, Args))
701 return;
702
703 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000704 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000705 D->addAttr(::new (S.Context)
706 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
707 StartArg, Size,
708 Attr.getAttributeSpellingListIndex()));
709}
710
711
Michael Hana9171bc2012-08-03 17:40:43 +0000712static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
713 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000714 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000715 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000716 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000717
Aaron Ballman00e99962013-08-31 01:11:41 +0000718 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000719 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000720 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000721 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000722 }
723
724 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000725 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000726
Michael Han3be3b442012-07-23 18:48:41 +0000727 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000728}
729
Michael Hana9171bc2012-08-03 17:40:43 +0000730static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000731 const AttributeList &Attr) {
732 SmallVector<Expr*, 2> Args;
733 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
734 return;
735
Michael Han99315932013-01-24 16:46:58 +0000736 D->addAttr(::new (S.Context)
737 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000738 Attr.getArgAsExpr(0),
739 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000740 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000741}
742
Michael Hana9171bc2012-08-03 17:40:43 +0000743static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000744 const AttributeList &Attr) {
745 SmallVector<Expr*, 2> Args;
746 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
747 return;
748
Nico Weber462fd1e2015-01-07 23:50:05 +0000749 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
750 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
751 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000752}
753
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000754static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000755 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000756 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000757 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000758 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000759 unsigned Size = Args.size();
760 if (Size == 0)
761 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000762
Michael Han99315932013-01-24 16:46:58 +0000763 D->addAttr(::new (S.Context)
764 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
765 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000766}
767
768static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000769 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000770 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000771 return;
772
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000773 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000774 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000775 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000776 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000777 if (Size == 0)
778 return;
779 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000780
Michael Han99315932013-01-24 16:46:58 +0000781 D->addAttr(::new (S.Context)
782 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
783 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784}
785
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000786static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
787 Expr *Cond = Attr.getArgAsExpr(0);
788 if (!Cond->isTypeDependent()) {
789 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
790 if (Converted.isInvalid())
791 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000792 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000793 }
794
795 StringRef Msg;
796 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
797 return;
798
799 SmallVector<PartialDiagnosticAt, 8> Diags;
800 if (!Cond->isValueDependent() &&
801 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
802 Diags)) {
803 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
804 for (int I = 0, N = Diags.size(); I != N; ++I)
805 S.Diag(Diags[I].first, Diags[I].second);
806 return;
807 }
808
809 D->addAttr(::new (S.Context)
810 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
811 Attr.getAttributeSpellingListIndex()));
812}
813
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000814static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000815 ConsumableAttr::ConsumedState DefaultState;
816
817 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000818 IdentifierLoc *IL = Attr.getArgAsIdent(0);
819 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
820 DefaultState)) {
821 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
822 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000823 return;
824 }
David Blaikie16f76d22013-09-06 01:28:43 +0000825 } else {
826 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
827 << Attr.getName() << AANT_ArgumentIdentifier;
828 return;
829 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000830
831 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000832 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000833 Attr.getAttributeSpellingListIndex()));
834}
835
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000836
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000837static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
838 const AttributeList &Attr) {
839 ASTContext &CurrContext = S.getASTContext();
840 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
841
842 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
843 if (!RD->hasAttr<ConsumableAttr>()) {
844 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
845 RD->getNameAsString();
846
847 return false;
848 }
849 }
850
851 return true;
852}
853
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000854
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000855static void handleCallableWhenAttr(Sema &S, Decl *D,
856 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000857 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
858 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000859
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000860 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
861 return;
862
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000863 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
864 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
865 CallableWhenAttr::ConsumedState CallableState;
866
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000867 StringRef StateString;
868 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000869 if (Attr.isArgIdent(ArgIndex)) {
870 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
871 StateString = Ident->Ident->getName();
872 Loc = Ident->Loc;
873 } else {
874 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
875 return;
876 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000877
878 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000879 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000880 S.Diag(Loc, diag::warn_attribute_type_not_supported)
881 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000882 return;
883 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000884
885 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000886 }
887
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000888 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000889 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
890 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000891}
892
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000893
DeLesley Hutchins69391772013-10-17 23:23:53 +0000894static void handleParamTypestateAttr(Sema &S, Decl *D,
895 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000896 ParamTypestateAttr::ConsumedState ParamState;
897
898 if (Attr.isArgIdent(0)) {
899 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
900 StringRef StateString = Ident->Ident->getName();
901
902 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
903 ParamState)) {
904 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
905 << Attr.getName() << StateString;
906 return;
907 }
908 } else {
909 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
910 Attr.getName() << AANT_ArgumentIdentifier;
911 return;
912 }
913
914 // FIXME: This check is currently being done in the analysis. It can be
915 // enabled here only after the parser propagates attributes at
916 // template specialization definition, not declaration.
917 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
918 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
919 //
920 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
921 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
922 // ReturnType.getAsString();
923 // return;
924 //}
925
926 D->addAttr(::new (S.Context)
927 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
928 Attr.getAttributeSpellingListIndex()));
929}
930
931
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000932static void handleReturnTypestateAttr(Sema &S, Decl *D,
933 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000934 ReturnTypestateAttr::ConsumedState ReturnState;
935
936 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000937 IdentifierLoc *IL = Attr.getArgAsIdent(0);
938 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
939 ReturnState)) {
940 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
941 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000942 return;
943 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000944 } else {
945 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
946 Attr.getName() << AANT_ArgumentIdentifier;
947 return;
948 }
949
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000950 // FIXME: This check is currently being done in the analysis. It can be
951 // enabled here only after the parser propagates attributes at
952 // template specialization definition, not declaration.
953 //QualType ReturnType;
954 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000955 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
956 // ReturnType = Param->getType();
957 //
958 //} else if (const CXXConstructorDecl *Constructor =
959 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000960 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
961 //
962 //} else {
963 //
964 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
965 //}
966 //
967 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
968 //
969 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
970 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
971 // ReturnType.getAsString();
972 // return;
973 //}
974
975 D->addAttr(::new (S.Context)
976 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
977 Attr.getAttributeSpellingListIndex()));
978}
979
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000980
981static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000982 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
983 return;
984
985 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000986 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000987 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
988 StringRef Param = Ident->Ident->getName();
989 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
990 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
991 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000992 return;
993 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000994 } else {
995 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
996 Attr.getName() << AANT_ArgumentIdentifier;
997 return;
998 }
999
1000 D->addAttr(::new (S.Context)
1001 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1002 Attr.getAttributeSpellingListIndex()));
1003}
1004
Chris Wailes9385f9f2013-10-29 20:28:41 +00001005static void handleTestTypestateAttr(Sema &S, Decl *D,
1006 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001007 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1008 return;
1009
Chris Wailes9385f9f2013-10-29 20:28:41 +00001010 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001011 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001012 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1013 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001014 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001015 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1016 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 return;
1018 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001019 } else {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1021 Attr.getName() << AANT_ArgumentIdentifier;
1022 return;
1023 }
1024
1025 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001026 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001027 Attr.getAttributeSpellingListIndex()));
1028}
1029
Chandler Carruthedc2c642011-07-02 00:01:44 +00001030static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1031 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001032 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001033 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001034}
1035
Chandler Carruthedc2c642011-07-02 00:01:44 +00001036static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001037 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001038 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1039 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001040 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001041 // If the alignment is less than or equal to 8 bits, the packed attribute
1042 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001043 if (!FD->getType()->isDependentType() &&
1044 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001045 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001046 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001047 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001048 else
Michael Han99315932013-01-24 16:46:58 +00001049 FD->addAttr(::new (S.Context)
1050 PackedAttr(Attr.getRange(), S.Context,
1051 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001052 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001053 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001054}
1055
Ted Kremenek7fd17232011-09-29 07:02:25 +00001056static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1057 // The IBOutlet/IBOutletCollection attributes only apply to instance
1058 // variables or properties of Objective-C classes. The outlet must also
1059 // have an object reference type.
1060 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1061 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001062 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001063 << Attr.getName() << VD->getType() << 0;
1064 return false;
1065 }
1066 }
1067 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1068 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001069 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001070 << Attr.getName() << PD->getType() << 1;
1071 return false;
1072 }
1073 }
1074 else {
1075 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1076 return false;
1077 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001078
Ted Kremenek7fd17232011-09-29 07:02:25 +00001079 return true;
1080}
1081
Chandler Carruthedc2c642011-07-02 00:01:44 +00001082static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001083 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001084 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001085
Michael Han99315932013-01-24 16:46:58 +00001086 D->addAttr(::new (S.Context)
1087 IBOutletAttr(Attr.getRange(), S.Context,
1088 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001089}
1090
Chandler Carruthedc2c642011-07-02 00:01:44 +00001091static void handleIBOutletCollection(Sema &S, Decl *D,
1092 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001093
1094 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001095 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001096 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1097 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001098 return;
1099 }
1100
Ted Kremenek7fd17232011-09-29 07:02:25 +00001101 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001102 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001103
Richard Smithb1f9a282013-10-31 01:56:18 +00001104 ParsedType PT;
1105
1106 if (Attr.hasParsedType())
1107 PT = Attr.getTypeArg();
1108 else {
1109 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1110 S.getScopeForContext(D->getDeclContext()->getParent()));
1111 if (!PT) {
1112 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1113 return;
1114 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001115 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001116
Craig Topperc3ec1492014-05-26 06:22:03 +00001117 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001118 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1119 if (!QTLoc)
1120 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001121
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001122 // Diagnose use of non-object type in iboutletcollection attribute.
1123 // FIXME. Gnu attribute extension ignores use of builtin types in
1124 // attributes. So, __attribute__((iboutletcollection(char))) will be
1125 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001126 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001127 S.Diag(Attr.getLoc(),
1128 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1129 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001130 return;
1131 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001132
Michael Han99315932013-01-24 16:46:58 +00001133 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001134 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001135 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001136}
1137
Hal Finkelee90a222014-09-26 05:04:30 +00001138bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1139 if (RefOkay) {
1140 if (T->isReferenceType())
1141 return true;
1142 } else {
1143 T = T.getNonReferenceType();
1144 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001145
Hal Finkelee90a222014-09-26 05:04:30 +00001146 // The nonnull attribute, and other similar attributes, can be applied to a
1147 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001148 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001149 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1150 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001151 for (const auto *I : UD->fields()) {
1152 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001153 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1154 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001155 }
1156 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001157 }
1158
1159 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001160}
1161
Ted Kremenek9aedc152014-01-17 06:24:56 +00001162static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001163 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001164 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001165 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001166 if (!S.isValidPointerAttrType(T)) {
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001167 S.Diag(Attr.getLoc(), isReturnValue
1168 ? diag::warn_attribute_return_pointers_only
1169 : diag::warn_attribute_pointers_only)
Hal Finkelee90a222014-09-26 05:04:30 +00001170 << Attr.getName() << AttrParmRange << TypeRange;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001171 return false;
1172 }
1173 return true;
1174}
1175
Chandler Carruthedc2c642011-07-02 00:01:44 +00001176static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001177 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001178 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1179 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001180 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001181 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001182 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001183
1184 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001185 if (Idx < getFunctionOrMethodNumParams(D) &&
1186 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001187 Ex->getSourceRange(),
1188 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001189 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001190
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001191 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001192 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001193
1194 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001195 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1196 // check if the attribute came from a macro expansion or a template
1197 // instantiation.
1198 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1199 S.ActiveTemplateInstantiations.empty()) {
1200 bool AnyPointers = isFunctionOrMethodVariadic(D);
1201 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1202 I != E && !AnyPointers; ++I) {
1203 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001204 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001205 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001206 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001207
Richard Smith588bd9b2014-08-27 04:59:42 +00001208 if (!AnyPointers)
1209 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001210 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001211
Richard Smith588bd9b2014-08-27 04:59:42 +00001212 unsigned *Start = NonNullArgs.data();
1213 unsigned Size = NonNullArgs.size();
1214 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001215 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001216 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001217 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001218}
1219
Jordan Rosec9399072014-02-11 17:27:59 +00001220static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1221 const AttributeList &Attr) {
1222 if (Attr.getNumArgs() > 0) {
1223 if (D->getFunctionType()) {
1224 handleNonNullAttr(S, D, Attr);
1225 } else {
1226 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1227 << D->getSourceRange();
1228 }
1229 return;
1230 }
1231
1232 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001233 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1234 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001235 return;
1236
1237 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001238 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001239 Attr.getAttributeSpellingListIndex()));
1240}
1241
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001242static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1243 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001244 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001245 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1246 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001247 /* isReturnValue */ true))
1248 return;
1249
1250 D->addAttr(::new (S.Context)
1251 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1252 Attr.getAttributeSpellingListIndex()));
1253}
1254
Hal Finkelee90a222014-09-26 05:04:30 +00001255static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1256 const AttributeList &Attr) {
1257 Expr *E = Attr.getArgAsExpr(0),
1258 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1259 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1260 Attr.getAttributeSpellingListIndex());
1261}
1262
1263void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1264 Expr *OE, unsigned SpellingListIndex) {
1265 QualType ResultType = getFunctionOrMethodResultType(D);
1266 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1267
1268 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1269 SourceLocation AttrLoc = AttrRange.getBegin();
1270
1271 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1272 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1273 << &TmpAttr << AttrRange << SR;
1274 return;
1275 }
1276
1277 if (!E->isValueDependent()) {
1278 llvm::APSInt I(64);
1279 if (!E->isIntegerConstantExpr(I, Context)) {
1280 if (OE)
1281 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1282 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1283 << E->getSourceRange();
1284 else
1285 Diag(AttrLoc, diag::err_attribute_argument_type)
1286 << &TmpAttr << AANT_ArgumentIntegerConstant
1287 << E->getSourceRange();
1288 return;
1289 }
1290
1291 if (!I.isPowerOf2()) {
1292 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1293 << E->getSourceRange();
1294 return;
1295 }
1296 }
1297
1298 if (OE) {
1299 if (!OE->isValueDependent()) {
1300 llvm::APSInt I(64);
1301 if (!OE->isIntegerConstantExpr(I, Context)) {
1302 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1303 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1304 << OE->getSourceRange();
1305 return;
1306 }
1307 }
1308 }
1309
1310 D->addAttr(::new (Context)
1311 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1312}
1313
Chandler Carruthedc2c642011-07-02 00:01:44 +00001314static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001315 // This attribute must be applied to a function declaration. The first
1316 // argument to the attribute must be an identifier, the name of the resource,
1317 // for example: malloc. The following arguments must be argument indexes, the
1318 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001319 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001320 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001321 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001322
Aaron Ballman00e99962013-08-31 01:11:41 +00001323 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001324 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001325 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001326 return;
1327 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001328
Richard Smith852e9ce2013-11-27 01:46:48 +00001329 // Figure out our Kind.
1330 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001331 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001332 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001333
Richard Smith852e9ce2013-11-27 01:46:48 +00001334 // Check arguments.
1335 switch (K) {
1336 case OwnershipAttr::Takes:
1337 case OwnershipAttr::Holds:
1338 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001339 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1340 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001341 return;
1342 }
1343 break;
1344 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001345 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001346 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1347 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001348 return;
1349 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001350 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001351 }
1352
Richard Smith852e9ce2013-11-27 01:46:48 +00001353 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001354
1355 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001356 StringRef ModuleName = Module->getName();
1357 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1358 ModuleName.size() > 4) {
1359 ModuleName = ModuleName.drop_front(2).drop_back(2);
1360 Module = &S.PP.getIdentifierTable().get(ModuleName);
1361 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001362
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001363 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001364 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1365 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001366 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001367 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001368 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001369
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001370 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001371 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001372 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001373 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001374 case OwnershipAttr::Takes:
1375 case OwnershipAttr::Holds:
1376 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1377 Err = 0;
1378 break;
1379 case OwnershipAttr::Returns:
1380 if (!T->isIntegerType())
1381 Err = 1;
1382 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001383 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001384 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001385 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001386 << Ex->getSourceRange();
1387 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001388 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001389
1390 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001391 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001392 // Cannot have two ownership attributes of different kinds for the same
1393 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001394 if (I->getOwnKind() != K && I->args_end() !=
1395 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001396 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001397 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001398 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001399 } else if (K == OwnershipAttr::Returns &&
1400 I->getOwnKind() == OwnershipAttr::Returns) {
1401 // A returns attribute conflicts with any other returns attribute using
1402 // a different index. Note, diagnostic reporting is 1-based, but stored
1403 // argument indexes are 0-based.
1404 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1405 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1406 << *(I->args_begin()) + 1;
1407 if (I->args_size())
1408 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1409 << (unsigned)Idx + 1 << Ex->getSourceRange();
1410 return;
1411 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001412 }
1413 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001414 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001415 }
1416
1417 unsigned* start = OwnershipArgs.data();
1418 unsigned size = OwnershipArgs.size();
1419 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001420
Michael Han99315932013-01-24 16:46:58 +00001421 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001422 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001423 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001424}
1425
Chandler Carruthedc2c642011-07-02 00:01:44 +00001426static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001427 // Check the attribute arguments.
1428 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1430 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001431 return;
1432 }
1433
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001434 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001435
Rafael Espindolac18086a2010-02-23 22:00:30 +00001436 // gcc rejects
1437 // class c {
1438 // static int a __attribute__((weakref ("v2")));
1439 // static int b() __attribute__((weakref ("f3")));
1440 // };
1441 // and ignores the attributes of
1442 // void f(void) {
1443 // static int a __attribute__((weakref ("v2")));
1444 // }
1445 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001446 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001447 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001448 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1449 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001450 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001451 }
1452
1453 // The GCC manual says
1454 //
1455 // At present, a declaration to which `weakref' is attached can only
1456 // be `static'.
1457 //
1458 // It also says
1459 //
1460 // Without a TARGET,
1461 // given as an argument to `weakref' or to `alias', `weakref' is
1462 // equivalent to `weak'.
1463 //
1464 // gcc 4.4.1 will accept
1465 // int a7 __attribute__((weakref));
1466 // as
1467 // int a7 __attribute__((weak));
1468 // This looks like a bug in gcc. We reject that for now. We should revisit
1469 // it if this behaviour is actually used.
1470
Rafael Espindolac18086a2010-02-23 22:00:30 +00001471 // GCC rejects
1472 // static ((alias ("y"), weakref)).
1473 // Should we? How to check that weakref is before or after alias?
1474
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001475 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1476 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1477 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001478 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001479 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001480 // GCC will accept anything as the argument of weakref. Should we
1481 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001482 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1483 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001484
Michael Han99315932013-01-24 16:46:58 +00001485 D->addAttr(::new (S.Context)
1486 WeakRefAttr(Attr.getRange(), S.Context,
1487 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001488}
1489
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001490static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1491 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001492 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001493 return;
1494
Douglas Gregore8bbc122011-09-02 00:18:52 +00001495 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001496 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1497 return;
1498 }
1499
David Majnemer2dc81462015-01-19 09:00:28 +00001500 // Aliases should be on declarations, not definitions.
1501 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1502 if (FD->isThisDeclarationADefinition()) {
1503 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1504 return;
1505 }
1506 } else {
1507 const auto *VD = cast<VarDecl>(D);
1508 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1509 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1510 return;
1511 }
1512 }
1513
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001514 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001515
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001516 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001517 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001518}
1519
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001520static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001521 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001522 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001523
Michael Han99315932013-01-24 16:46:58 +00001524 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1525 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001526}
1527
1528static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001529 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001530 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001531
Michael Han99315932013-01-24 16:46:58 +00001532 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1533 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001534}
1535
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001536static void handleTLSModelAttr(Sema &S, Decl *D,
1537 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001538 StringRef Model;
1539 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001540 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001541 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001542 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001543
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001544 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001545 if (Model != "global-dynamic" && Model != "local-dynamic"
1546 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001547 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001548 return;
1549 }
1550
Michael Han99315932013-01-24 16:46:58 +00001551 D->addAttr(::new (S.Context)
1552 TLSModelAttr(Attr.getRange(), S.Context, Model,
1553 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001554}
1555
David Majnemer631a90b2015-02-04 07:23:21 +00001556static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1557 QualType ResultType = getFunctionOrMethodResultType(D);
1558 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1559 D->addAttr(::new (S.Context) RestrictAttr(
1560 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1561 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001562 }
1563
David Majnemer631a90b2015-02-04 07:23:21 +00001564 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1565 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001566}
1567
Chandler Carruthedc2c642011-07-02 00:01:44 +00001568static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001569 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001570 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1571 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001572 return;
1573 }
1574
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001575 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1576 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001577}
1578
Chandler Carruthedc2c642011-07-02 00:01:44 +00001579static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001580 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001581
1582 if (S.CheckNoReturnAttr(attr)) return;
1583
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001584 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001585 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001586 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001587 return;
1588 }
1589
Michael Han99315932013-01-24 16:46:58 +00001590 D->addAttr(::new (S.Context)
1591 NoReturnAttr(attr.getRange(), S.Context,
1592 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001593}
1594
1595bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001596 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001597 attr.setInvalid();
1598 return true;
1599 }
1600
1601 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001602}
1603
Chandler Carruthedc2c642011-07-02 00:01:44 +00001604static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1605 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001606
1607 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1608 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001609 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001610 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001611 if (!VD || (!VD->getType()->isBlockPointerType() &&
1612 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001613 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001614 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001615 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001616 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001617 return;
1618 }
1619 }
1620
Michael Han99315932013-01-24 16:46:58 +00001621 D->addAttr(::new (S.Context)
1622 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1623 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001624}
1625
John Thompsoncdb847ba2010-08-09 21:53:52 +00001626// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001627static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001628/*
1629 Returning a Vector Class in Registers
1630
Eric Christopherbc638a82010-12-01 22:13:54 +00001631 According to the PPU ABI specifications, a class with a single member of
1632 vector type is returned in memory when used as the return value of a function.
1633 This results in inefficient code when implementing vector classes. To return
1634 the value in a single vector register, add the vecreturn attribute to the
1635 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001636
1637 Example:
1638
1639 struct Vector
1640 {
1641 __vector float xyzw;
1642 } __attribute__((vecreturn));
1643
1644 Vector Add(Vector lhs, Vector rhs)
1645 {
1646 Vector result;
1647 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1648 return result; // This will be returned in a register
1649 }
1650*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001651 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1652 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001653 return;
1654 }
1655
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001656 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001657 int count = 0;
1658
1659 if (!isa<CXXRecordDecl>(record)) {
1660 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1661 return;
1662 }
1663
1664 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1665 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1666 return;
1667 }
1668
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001669 for (const auto *I : record->fields()) {
1670 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001671 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1672 return;
1673 }
1674 count++;
1675 }
1676
Michael Han99315932013-01-24 16:46:58 +00001677 D->addAttr(::new (S.Context)
1678 VecReturnAttr(Attr.getRange(), S.Context,
1679 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001680}
1681
Richard Smithe233fbf2013-01-28 22:42:45 +00001682static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1683 const AttributeList &Attr) {
1684 if (isa<ParmVarDecl>(D)) {
1685 // [[carries_dependency]] can only be applied to a parameter if it is a
1686 // parameter of a function declaration or lambda.
1687 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1688 S.Diag(Attr.getLoc(),
1689 diag::err_carries_dependency_param_not_function_decl);
1690 return;
1691 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001692 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001693
1694 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1695 Attr.getRange(), S.Context,
1696 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001697}
1698
Chandler Carruthedc2c642011-07-02 00:01:44 +00001699static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001700 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001701 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001702 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001703 return;
1704 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001705 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001707 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001708 return;
1709 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001710
Michael Han99315932013-01-24 16:46:58 +00001711 D->addAttr(::new (S.Context)
1712 UsedAttr(Attr.getRange(), S.Context,
1713 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001714}
1715
Chandler Carruthedc2c642011-07-02 00:01:44 +00001716static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001717 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001718 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001719 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1720 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001721
Michael Han99315932013-01-24 16:46:58 +00001722 D->addAttr(::new (S.Context)
1723 ConstructorAttr(Attr.getRange(), S.Context, priority,
1724 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001725}
1726
Chandler Carruthedc2c642011-07-02 00:01:44 +00001727static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001728 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001729 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001730 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1731 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001732
Michael Han99315932013-01-24 16:46:58 +00001733 D->addAttr(::new (S.Context)
1734 DestructorAttr(Attr.getRange(), S.Context, priority,
1735 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001736}
1737
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001738template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001739static void handleAttrWithMessage(Sema &S, Decl *D,
1740 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001741 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001742 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001743 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001744 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001745
Michael Han99315932013-01-24 16:46:58 +00001746 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1747 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001748}
1749
Ted Kremenek438f8db2014-02-22 01:06:05 +00001750static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001751 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001752 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001753 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1754 << Attr.getName() << Attr.getRange();
1755 return;
1756 }
1757
Ted Kremenek28eace62013-11-23 01:01:34 +00001758 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001759 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1760 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001761}
1762
Jordy Rose740b0c22012-05-08 03:27:22 +00001763static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1764 IdentifierInfo *Platform,
1765 VersionTuple Introduced,
1766 VersionTuple Deprecated,
1767 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001768 StringRef PlatformName
1769 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1770 if (PlatformName.empty())
1771 PlatformName = Platform->getName();
1772
1773 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1774 // of these steps are needed).
1775 if (!Introduced.empty() && !Deprecated.empty() &&
1776 !(Introduced <= Deprecated)) {
1777 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1778 << 1 << PlatformName << Deprecated.getAsString()
1779 << 0 << Introduced.getAsString();
1780 return true;
1781 }
1782
1783 if (!Introduced.empty() && !Obsoleted.empty() &&
1784 !(Introduced <= Obsoleted)) {
1785 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1786 << 2 << PlatformName << Obsoleted.getAsString()
1787 << 0 << Introduced.getAsString();
1788 return true;
1789 }
1790
1791 if (!Deprecated.empty() && !Obsoleted.empty() &&
1792 !(Deprecated <= Obsoleted)) {
1793 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1794 << 2 << PlatformName << Obsoleted.getAsString()
1795 << 1 << Deprecated.getAsString();
1796 return true;
1797 }
1798
1799 return false;
1800}
1801
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001802/// \brief Check whether the two versions match.
1803///
1804/// If either version tuple is empty, then they are assumed to match. If
1805/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1806static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1807 bool BeforeIsOkay) {
1808 if (X.empty() || Y.empty())
1809 return true;
1810
1811 if (X == Y)
1812 return true;
1813
1814 if (BeforeIsOkay && X < Y)
1815 return true;
1816
1817 return false;
1818}
1819
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001820AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001821 IdentifierInfo *Platform,
1822 VersionTuple Introduced,
1823 VersionTuple Deprecated,
1824 VersionTuple Obsoleted,
1825 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001826 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001827 bool Override,
1828 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001829 VersionTuple MergedIntroduced = Introduced;
1830 VersionTuple MergedDeprecated = Deprecated;
1831 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001832 bool FoundAny = false;
1833
Rafael Espindolac67f2232012-05-10 02:50:16 +00001834 if (D->hasAttrs()) {
1835 AttrVec &Attrs = D->getAttrs();
1836 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1837 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1838 if (!OldAA) {
1839 ++i;
1840 continue;
1841 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001842
Rafael Espindolac67f2232012-05-10 02:50:16 +00001843 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1844 if (OldPlatform != Platform) {
1845 ++i;
1846 continue;
1847 }
1848
1849 FoundAny = true;
1850 VersionTuple OldIntroduced = OldAA->getIntroduced();
1851 VersionTuple OldDeprecated = OldAA->getDeprecated();
1852 VersionTuple OldObsoleted = OldAA->getObsoleted();
1853 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001854
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001855 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1856 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1857 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1858 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001859 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001860 if (Override) {
1861 int Which = -1;
1862 VersionTuple FirstVersion;
1863 VersionTuple SecondVersion;
1864 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1865 Which = 0;
1866 FirstVersion = OldIntroduced;
1867 SecondVersion = Introduced;
1868 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1869 Which = 1;
1870 FirstVersion = Deprecated;
1871 SecondVersion = OldDeprecated;
1872 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1873 Which = 2;
1874 FirstVersion = Obsoleted;
1875 SecondVersion = OldObsoleted;
1876 }
1877
1878 if (Which == -1) {
1879 Diag(OldAA->getLocation(),
1880 diag::warn_mismatched_availability_override_unavail)
1881 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1882 } else {
1883 Diag(OldAA->getLocation(),
1884 diag::warn_mismatched_availability_override)
1885 << Which
1886 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1887 << FirstVersion.getAsString() << SecondVersion.getAsString();
1888 }
1889 Diag(Range.getBegin(), diag::note_overridden_method);
1890 } else {
1891 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1892 Diag(Range.getBegin(), diag::note_previous_attribute);
1893 }
1894
Rafael Espindolac67f2232012-05-10 02:50:16 +00001895 Attrs.erase(Attrs.begin() + i);
1896 --e;
1897 continue;
1898 }
1899
1900 VersionTuple MergedIntroduced2 = MergedIntroduced;
1901 VersionTuple MergedDeprecated2 = MergedDeprecated;
1902 VersionTuple MergedObsoleted2 = MergedObsoleted;
1903
1904 if (MergedIntroduced2.empty())
1905 MergedIntroduced2 = OldIntroduced;
1906 if (MergedDeprecated2.empty())
1907 MergedDeprecated2 = OldDeprecated;
1908 if (MergedObsoleted2.empty())
1909 MergedObsoleted2 = OldObsoleted;
1910
1911 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1912 MergedIntroduced2, MergedDeprecated2,
1913 MergedObsoleted2)) {
1914 Attrs.erase(Attrs.begin() + i);
1915 --e;
1916 continue;
1917 }
1918
1919 MergedIntroduced = MergedIntroduced2;
1920 MergedDeprecated = MergedDeprecated2;
1921 MergedObsoleted = MergedObsoleted2;
1922 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001923 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924 }
1925
1926 if (FoundAny &&
1927 MergedIntroduced == Introduced &&
1928 MergedDeprecated == Deprecated &&
1929 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001930 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001931
Ted Kremenekb5445722013-04-06 00:34:27 +00001932 // Only create a new attribute if !Override, but we want to do
1933 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001934 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001935 MergedDeprecated, MergedObsoleted) &&
1936 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001937 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1938 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001939 Obsoleted, IsUnavailable, Message,
1940 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001941 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001942 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001943}
1944
Chandler Carruthedc2c642011-07-02 00:01:44 +00001945static void handleAvailabilityAttr(Sema &S, Decl *D,
1946 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001947 if (!checkAttributeNumArgs(S, Attr, 1))
1948 return;
1949 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001950 unsigned Index = Attr.getAttributeSpellingListIndex();
1951
Aaron Ballman00e99962013-08-31 01:11:41 +00001952 IdentifierInfo *II = Platform->Ident;
1953 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1954 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1955 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001956
Rafael Espindolac231fab2013-01-08 21:30:32 +00001957 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1958 if (!ND) {
1959 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1960 return;
1961 }
1962
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001963 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1964 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1965 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001966 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001967 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001968 if (const StringLiteral *SE =
1969 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001970 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001971
Aaron Ballman00e99962013-08-31 01:11:41 +00001972 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001973 Introduced.Version,
1974 Deprecated.Version,
1975 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001976 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001977 /*Override=*/false,
1978 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001979 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001980 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001981}
1982
John McCalld041a9b2013-02-20 01:54:26 +00001983template <class T>
1984static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1985 typename T::VisibilityType value,
1986 unsigned attrSpellingListIndex) {
1987 T *existingAttr = D->getAttr<T>();
1988 if (existingAttr) {
1989 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1990 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001991 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001992 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1993 S.Diag(range.getBegin(), diag::note_previous_attribute);
1994 D->dropAttr<T>();
1995 }
1996 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1997}
1998
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001999VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002000 VisibilityAttr::VisibilityType Vis,
2001 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002002 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2003 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002004}
2005
John McCalld041a9b2013-02-20 01:54:26 +00002006TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2007 TypeVisibilityAttr::VisibilityType Vis,
2008 unsigned AttrSpellingListIndex) {
2009 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2010 AttrSpellingListIndex);
2011}
2012
2013static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2014 bool isTypeVisibility) {
2015 // Visibility attributes don't mean anything on a typedef.
2016 if (isa<TypedefNameDecl>(D)) {
2017 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2018 << Attr.getName();
2019 return;
2020 }
2021
2022 // 'type_visibility' can only go on a type or namespace.
2023 if (isTypeVisibility &&
2024 !(isa<TagDecl>(D) ||
2025 isa<ObjCInterfaceDecl>(D) ||
2026 isa<NamespaceDecl>(D))) {
2027 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2028 << Attr.getName() << ExpectedTypeOrNamespace;
2029 return;
2030 }
2031
Benjamin Kramer70370212013-09-09 15:08:57 +00002032 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002033 StringRef TypeStr;
2034 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002035 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002036 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002037
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002038 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002039 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002040 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002041 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002042 return;
2043 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002044
2045 // Complain about attempts to use protected visibility on targets
2046 // (like Darwin) that don't support it.
2047 if (type == VisibilityAttr::Protected &&
2048 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2049 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2050 type = VisibilityAttr::Default;
2051 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002052
Michael Han99315932013-01-24 16:46:58 +00002053 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002054 clang::Attr *newAttr;
2055 if (isTypeVisibility) {
2056 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2057 (TypeVisibilityAttr::VisibilityType) type,
2058 Index);
2059 } else {
2060 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2061 }
2062 if (newAttr)
2063 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002064}
2065
Chandler Carruthedc2c642011-07-02 00:01:44 +00002066static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2067 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002068 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002069 if (!Attr.isArgIdent(0)) {
2070 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2071 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002072 return;
2073 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002074
Aaron Ballman682ee422013-09-11 19:47:58 +00002075 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2076 ObjCMethodFamilyAttr::FamilyKind F;
2077 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2078 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2079 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002080 return;
2081 }
2082
Alp Toker314cc812014-01-25 16:55:45 +00002083 if (F == ObjCMethodFamilyAttr::OMF_init &&
2084 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002085 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002086 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002087 // Ignore the attribute.
2088 return;
2089 }
2090
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002091 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002092 S.Context, F,
2093 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002094}
2095
Chandler Carruthedc2c642011-07-02 00:01:44 +00002096static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002097 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002098 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002099 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002100 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2101 return;
2102 }
2103 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002104 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2105 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002106 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002107 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2108 return;
2109 }
2110 }
2111 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002112 // It is okay to include this attribute on properties, e.g.:
2113 //
2114 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2115 //
2116 // In this case it follows tradition and suppresses an error in the above
2117 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002118 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002119 }
Michael Han99315932013-01-24 16:46:58 +00002120 D->addAttr(::new (S.Context)
2121 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2122 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002123}
2124
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002125static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2126 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2127 QualType T = TD->getUnderlyingType();
2128 if (!T->isObjCObjectPointerType()) {
2129 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2130 return;
2131 }
2132 } else {
2133 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2134 return;
2135 }
2136 D->addAttr(::new (S.Context)
2137 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2138 Attr.getAttributeSpellingListIndex()));
2139}
2140
Chandler Carruthedc2c642011-07-02 00:01:44 +00002141static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002142 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002144 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002145 return;
2146 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002147
Aaron Ballman00e99962013-08-31 01:11:41 +00002148 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002149 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002150 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2151 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2152 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002153 return;
2154 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002155
Michael Han99315932013-01-24 16:46:58 +00002156 D->addAttr(::new (S.Context)
2157 BlocksAttr(Attr.getRange(), S.Context, type,
2158 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002159}
2160
Chandler Carruthedc2c642011-07-02 00:01:44 +00002161static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002162 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002163 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002164 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002165 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002166 if (E->isTypeDependent() || E->isValueDependent() ||
2167 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002169 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002170 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002171 return;
2172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002173
John McCallb46f2872011-09-09 07:56:05 +00002174 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002175 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2176 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002177 return;
2178 }
John McCallb46f2872011-09-09 07:56:05 +00002179
2180 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002181 }
2182
Aaron Ballman18a78382013-11-21 00:28:23 +00002183 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002184 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002185 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002186 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002187 if (E->isTypeDependent() || E->isValueDependent() ||
2188 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002190 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002191 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002192 return;
2193 }
2194 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002195
John McCallb46f2872011-09-09 07:56:05 +00002196 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002197 // FIXME: This error message could be improved, it would be nice
2198 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002199 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2200 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002201 return;
2202 }
2203 }
2204
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002205 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002206 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002207 if (isa<FunctionNoProtoType>(FT)) {
2208 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2209 return;
2210 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002211
Chris Lattner9363e312009-03-17 23:03:47 +00002212 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002213 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002214 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002215 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002216 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002217 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002218 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002219 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002220 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002221 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2222 if (!BD->isVariadic()) {
2223 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2224 return;
2225 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002226 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002227 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002228 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002229 const FunctionType *FT = Ty->isFunctionPointerType()
2230 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002231 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002232 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002233 int m = Ty->isFunctionPointerType() ? 0 : 1;
2234 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002235 return;
2236 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002237 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002238 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002239 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002240 return;
2241 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002242 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002244 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002245 return;
2246 }
Michael Han99315932013-01-24 16:46:58 +00002247 D->addAttr(::new (S.Context)
2248 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2249 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002250}
2251
Chandler Carruthedc2c642011-07-02 00:01:44 +00002252static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002253 if (D->getFunctionType() &&
2254 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002255 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2256 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002257 return;
2258 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002259 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002260 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002261 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2262 << Attr.getName() << 1;
2263 return;
2264 }
2265
Michael Han99315932013-01-24 16:46:58 +00002266 D->addAttr(::new (S.Context)
2267 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2268 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002269}
2270
Chandler Carruthedc2c642011-07-02 00:01:44 +00002271static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002272 // weak_import only applies to variable & function declarations.
2273 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002274 if (!D->canBeWeakImported(isDef)) {
2275 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002276 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2277 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002278 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002279 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002280 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002281 // Nothing to warn about here.
2282 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002283 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002284 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002285
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002286 return;
2287 }
2288
Michael Han99315932013-01-24 16:46:58 +00002289 D->addAttr(::new (S.Context)
2290 WeakImportAttr(Attr.getRange(), S.Context,
2291 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002292}
2293
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002294// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002295template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002296static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002297 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002298 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002299 for (unsigned i = 0; i < 3; ++i) {
2300 const Expr *E = Attr.getArgAsExpr(i);
2301 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002302 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002303 if (WGSize[i] == 0) {
2304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2305 << Attr.getName() << E->getSourceRange();
2306 return;
2307 }
2308 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002309
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002310 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2311 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2312 Existing->getYDim() == WGSize[1] &&
2313 Existing->getZDim() == WGSize[2]))
2314 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002315
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002316 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2317 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002318 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002319}
2320
Joey Goulyaba589c2013-03-08 09:42:32 +00002321static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002322 if (!Attr.hasParsedType()) {
2323 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2324 << Attr.getName() << 1;
2325 return;
2326 }
2327
Craig Topperc3ec1492014-05-26 06:22:03 +00002328 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002329 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2330 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002331
2332 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2333 (ParmType->isBooleanType() ||
2334 !ParmType->isIntegralType(S.getASTContext()))) {
2335 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2336 << ParmType;
2337 return;
2338 }
2339
Aaron Ballmana9e05402013-12-02 22:16:55 +00002340 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002341 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002342 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2343 return;
2344 }
2345 }
2346
2347 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002348 ParmTSI,
2349 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002350}
2351
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002352SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002353 StringRef Name,
2354 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002355 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2356 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002357 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002358 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2359 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002360 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002361 }
Michael Han99315932013-01-24 16:46:58 +00002362 return ::new (Context) SectionAttr(Range, Context, Name,
2363 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002364}
2365
Reid Kleckner2a133222015-03-04 23:39:17 +00002366bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2367 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2368 if (!Error.empty()) {
2369 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2370 return false;
2371 }
2372 return true;
2373}
2374
Chandler Carruthedc2c642011-07-02 00:01:44 +00002375static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002376 // Make sure that there is a string literal as the sections's single
2377 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002378 StringRef Str;
2379 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002380 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002381 return;
Mike Stump11289f42009-09-09 15:08:12 +00002382
Reid Kleckner2a133222015-03-04 23:39:17 +00002383 if (!S.checkSectionName(LiteralLoc, Str))
2384 return;
2385
Chris Lattner30ba6742009-08-10 19:03:04 +00002386 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002387 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002388 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002389 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002390 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002391 return;
2392 }
Mike Stump11289f42009-09-09 15:08:12 +00002393
Michael Han99315932013-01-24 16:46:58 +00002394 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002395 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002396 if (NewAttr)
2397 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002398}
2399
Eric Christopher11acf732015-06-12 01:35:52 +00002400static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2401 // TODO: Validation should use a backend target library that specifies
2402 // the allowable subtarget features and cpus. We could use something like a
2403 // TargetCodeGenInfo hook here to do validation.
2404 StringRef Str;
2405 SourceLocation LiteralLoc;
2406 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2407 return;
2408 unsigned Index = Attr.getAttributeSpellingListIndex();
2409 TargetAttr *NewAttr =
2410 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2411 D->addAttr(NewAttr);
2412}
2413
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002414
Chandler Carruthedc2c642011-07-02 00:01:44 +00002415static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002416 VarDecl *VD = cast<VarDecl>(D);
2417 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002418 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002419 return;
2420 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002421
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002422 Expr *E = Attr.getArgAsExpr(0);
2423 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002424 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002425 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002426
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002427 // gcc only allows for simple identifiers. Since we support more than gcc, we
2428 // will warn the user.
2429 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2430 if (DRE->hasQualifier())
2431 S.Diag(Loc, diag::warn_cleanup_ext);
2432 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2433 NI = DRE->getNameInfo();
2434 if (!FD) {
2435 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2436 << NI.getName();
2437 return;
2438 }
2439 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2440 if (ULE->hasExplicitTemplateArgs())
2441 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002442 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2443 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002444 if (!FD) {
2445 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2446 << NI.getName();
2447 if (ULE->getType() == S.Context.OverloadTy)
2448 S.NoteAllOverloadCandidates(ULE);
2449 return;
2450 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002451 } else {
2452 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002453 return;
2454 }
2455
Anders Carlssond277d792009-01-31 01:16:18 +00002456 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002457 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2458 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002459 return;
2460 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002461
Anders Carlsson723f55d2009-02-07 23:16:50 +00002462 // We're currently more strict than GCC about what function types we accept.
2463 // If this ever proves to be a problem it should be easy to fix.
2464 QualType Ty = S.Context.getPointerType(VD->getType());
2465 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002466 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2467 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002468 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2469 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002470 return;
2471 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002472
Michael Han99315932013-01-24 16:46:58 +00002473 D->addAttr(::new (S.Context)
2474 CleanupAttr(Attr.getRange(), S.Context, FD,
2475 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002476}
2477
Mike Stumpd3bb5572009-07-24 19:02:52 +00002478/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002479/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002480static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002481 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002482 uint64_t Idx;
2483 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002484 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002485
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002486 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002487 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002488
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002489 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2490 if (not_nsstring_type &&
2491 !isCFStringType(Ty, S.Context) &&
2492 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002493 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002494 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002495 << (not_nsstring_type ? "a string type" : "an NSString")
2496 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002497 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002498 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002499 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002500 if (!isNSStringType(Ty, S.Context) &&
2501 !isCFStringType(Ty, S.Context) &&
2502 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002503 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002504 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002505 << (not_nsstring_type ? "string type" : "NSString")
2506 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002507 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002508 }
2509
Alp Toker601b22c2014-01-21 23:35:24 +00002510 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002511 // because that has corrected for the implicit this parameter, and is zero-
2512 // based. The attribute expects what the user wrote explicitly.
2513 llvm::APSInt Val;
2514 IdxExpr->EvaluateAsInt(Val, S.Context);
2515
Michael Han99315932013-01-24 16:46:58 +00002516 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002517 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002518 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002519}
2520
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002521enum FormatAttrKind {
2522 CFStringFormat,
2523 NSStringFormat,
2524 StrftimeFormat,
2525 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002526 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002527 InvalidFormat
2528};
2529
2530/// getFormatAttrKind - Map from format attribute names to supported format
2531/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002532static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002533 return llvm::StringSwitch<FormatAttrKind>(Format)
2534 // Check for formats that get handled specially.
2535 .Case("NSString", NSStringFormat)
2536 .Case("CFString", CFStringFormat)
2537 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002538
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002539 // Otherwise, check for supported formats.
2540 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2541 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2542 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002543 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002544 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002545
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002546 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2547 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002548}
2549
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002550/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002551/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002552static void handleInitPriorityAttr(Sema &S, Decl *D,
2553 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002554 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002555 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2556 return;
2557 }
2558
Aaron Ballman4a611152013-11-27 16:34:09 +00002559 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002560 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2561 Attr.setInvalid();
2562 return;
2563 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002564 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002565 if (S.Context.getAsArrayType(T))
2566 T = S.Context.getBaseElementType(T);
2567 if (!T->getAs<RecordType>()) {
2568 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2569 Attr.setInvalid();
2570 return;
2571 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002572
2573 Expr *E = Attr.getArgAsExpr(0);
2574 uint32_t prioritynum;
2575 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002576 Attr.setInvalid();
2577 return;
2578 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002579
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002580 if (prioritynum < 101 || prioritynum > 65535) {
2581 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002582 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002583 Attr.setInvalid();
2584 return;
2585 }
Michael Han99315932013-01-24 16:46:58 +00002586 D->addAttr(::new (S.Context)
2587 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2588 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002589}
2590
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002591FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2592 IdentifierInfo *Format, int FormatIdx,
2593 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002594 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002595 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002596 for (auto *F : D->specific_attrs<FormatAttr>()) {
2597 if (F->getType() == Format &&
2598 F->getFormatIdx() == FormatIdx &&
2599 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002600 // If we don't have a valid location for this attribute, adopt the
2601 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002602 if (F->getLocation().isInvalid())
2603 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002604 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002605 }
2606 }
2607
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002608 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2609 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002610}
2611
Mike Stumpd3bb5572009-07-24 19:02:52 +00002612/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002613/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002614static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002615 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002616 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002617 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618 return;
2619 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002620
Chandler Carruth743682b2010-11-16 08:35:43 +00002621 // In C++ the implicit 'this' function parameter also counts, and they are
2622 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002623 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002624 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002625
Aaron Ballman00e99962013-08-31 01:11:41 +00002626 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2627 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628
2629 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002630 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002631 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002632 // If we've modified the string name, we need a new identifier for it.
2633 II = &S.Context.Idents.get(Format);
2634 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002636 // Check for supported formats.
2637 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002638
2639 if (Kind == IgnoredFormat)
2640 return;
2641
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002642 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002643 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002644 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002645 return;
2646 }
2647
2648 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002649 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002650 uint32_t Idx;
2651 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002652 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002653
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002654 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002655 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002656 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657 return;
2658 }
2659
2660 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002661 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002662
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002663 if (HasImplicitThisParam) {
2664 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002665 S.Diag(Attr.getLoc(),
2666 diag::err_format_attribute_implicit_this_format_string)
2667 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002668 return;
2669 }
2670 ArgIdx--;
2671 }
Mike Stump11289f42009-09-09 15:08:12 +00002672
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002673 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002674 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002676 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002677 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002678 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002679 << "a CFString" << IdxExpr->getSourceRange()
2680 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002681 return;
2682 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002683 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002684 // FIXME: do we need to check if the type is NSString*? What are the
2685 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002686 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002687 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002688 << "an NSString" << IdxExpr->getSourceRange()
2689 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002690 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002691 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002692 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002693 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002694 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002695 << "a string type" << IdxExpr->getSourceRange()
2696 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002697 return;
2698 }
2699
2700 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002701 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002702 uint32_t FirstArg;
2703 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002704 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002705
2706 // check if the function is variadic if the 3rd argument non-zero
2707 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002708 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002709 ++NumArgs; // +1 for ...
2710 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002711 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002712 return;
2713 }
2714 }
2715
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002716 // strftime requires FirstArg to be 0 because it doesn't read from any
2717 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002718 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002719 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002720 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2721 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002722 return;
2723 }
2724 // if 0 it disables parameter checking (to use with e.g. va_list)
2725 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002726 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002727 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002728 return;
2729 }
2730
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002731 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002732 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002733 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002734 if (NewAttr)
2735 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002736}
2737
Chandler Carruthedc2c642011-07-02 00:01:44 +00002738static void handleTransparentUnionAttr(Sema &S, Decl *D,
2739 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002740 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002741 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002742 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002743 if (TD && TD->getUnderlyingType()->isUnionType())
2744 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2745 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002746 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002747
2748 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002750 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002751 return;
2752 }
2753
John McCallf937c022011-10-07 06:10:15 +00002754 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002755 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002756 diag::warn_transparent_union_attribute_not_definition);
2757 return;
2758 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002759
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002760 RecordDecl::field_iterator Field = RD->field_begin(),
2761 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002762 if (Field == FieldEnd) {
2763 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2764 return;
2765 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002766
David Blaikie40ed2972012-06-06 20:45:41 +00002767 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002768 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002769 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002770 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002771 diag::warn_transparent_union_attribute_floating)
2772 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002773 return;
2774 }
2775
2776 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2777 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2778 for (; Field != FieldEnd; ++Field) {
2779 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002780 // FIXME: this isn't fully correct; we also need to test whether the
2781 // members of the union would all have the same calling convention as the
2782 // first member of the union. Checking just the size and alignment isn't
2783 // sufficient (consider structs passed on the stack instead of in registers
2784 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002785 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002786 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002787 // Warn if we drop the attribute.
2788 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002789 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002790 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002791 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002792 diag::warn_transparent_union_attribute_field_size_align)
2793 << isSize << Field->getDeclName() << FieldBits;
2794 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002795 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002796 diag::note_transparent_union_first_field_size_align)
2797 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002798 return;
2799 }
2800 }
2801
Michael Han99315932013-01-24 16:46:58 +00002802 RD->addAttr(::new (S.Context)
2803 TransparentUnionAttr(Attr.getRange(), S.Context,
2804 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002805}
2806
Chandler Carruthedc2c642011-07-02 00:01:44 +00002807static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002808 // Make sure that there is a string literal as the annotation's single
2809 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002810 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002811 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002812 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002813
2814 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002815 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2816 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002817 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002818 }
Michael Han99315932013-01-24 16:46:58 +00002819
2820 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002821 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002822 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002823}
2824
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002825static void handleAlignValueAttr(Sema &S, Decl *D,
2826 const AttributeList &Attr) {
2827 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2828 Attr.getAttributeSpellingListIndex());
2829}
2830
2831void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2832 unsigned SpellingListIndex) {
2833 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2834 SourceLocation AttrLoc = AttrRange.getBegin();
2835
2836 QualType T;
2837 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2838 T = TD->getUnderlyingType();
2839 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2840 T = VD->getType();
2841 else
2842 llvm_unreachable("Unknown decl type for align_value");
2843
2844 if (!T->isDependentType() && !T->isAnyPointerType() &&
2845 !T->isReferenceType() && !T->isMemberPointerType()) {
2846 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2847 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2848 return;
2849 }
2850
2851 if (!E->isValueDependent()) {
2852 llvm::APSInt Alignment(32);
2853 ExprResult ICE
2854 = VerifyIntegerConstantExpression(E, &Alignment,
2855 diag::err_align_value_attribute_argument_not_int,
2856 /*AllowFold*/ false);
2857 if (ICE.isInvalid())
2858 return;
2859
2860 if (!Alignment.isPowerOf2()) {
2861 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2862 << E->getSourceRange();
2863 return;
2864 }
2865
2866 D->addAttr(::new (Context)
2867 AlignValueAttr(AttrRange, Context, ICE.get(),
2868 SpellingListIndex));
2869 return;
2870 }
2871
2872 // Save dependent expressions in the AST to be instantiated.
2873 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
2874 return;
2875}
2876
Chandler Carruthedc2c642011-07-02 00:01:44 +00002877static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002878 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002879 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002880 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2881 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002882 return;
2883 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002884
Richard Smith848e1f12013-02-01 08:12:08 +00002885 if (Attr.getNumArgs() == 0) {
2886 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002887 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002888 return;
2889 }
2890
Aaron Ballman00e99962013-08-31 01:11:41 +00002891 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002892 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2893 S.Diag(Attr.getEllipsisLoc(),
2894 diag::err_pack_expansion_without_parameter_packs);
2895 return;
2896 }
2897
2898 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2899 return;
2900
David Majnemer26a1e0e2015-04-07 02:37:09 +00002901 if (E->isValueDependent()) {
2902 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
2903 if (!TND->getUnderlyingType()->isDependentType()) {
2904 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
2905 << E->getSourceRange();
2906 return;
2907 }
2908 }
2909 }
2910
Richard Smith44c247f2013-02-22 08:32:16 +00002911 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2912 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002913}
2914
2915void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002916 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002917 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2918 SourceLocation AttrLoc = AttrRange.getBegin();
2919
Richard Smith1dba27c2013-01-29 09:02:09 +00002920 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002921 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002922 // C++11 [dcl.align]p1:
2923 // An alignment-specifier may be applied to a variable or to a class
2924 // data member, but it shall not be applied to a bit-field, a function
2925 // parameter, the formal parameter of a catch clause, or a variable
2926 // declared with the register storage class specifier. An
2927 // alignment-specifier may also be applied to the declaration of a class
2928 // or enumeration type.
2929 // C11 6.7.5/2:
2930 // An alignment attribute shall not be specified in a declaration of
2931 // a typedef, or a bit-field, or a function, or a parameter, or an
2932 // object declared with the register storage-class specifier.
2933 int DiagKind = -1;
2934 if (isa<ParmVarDecl>(D)) {
2935 DiagKind = 0;
2936 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2937 if (VD->getStorageClass() == SC_Register)
2938 DiagKind = 1;
2939 if (VD->isExceptionVariable())
2940 DiagKind = 2;
2941 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2942 if (FD->isBitField())
2943 DiagKind = 3;
2944 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002945 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002946 << (TmpAttr.isC11() ? ExpectedVariableOrField
2947 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002948 return;
2949 }
2950 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002951 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002952 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002953 return;
2954 }
2955 }
2956
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002957 if (E->isTypeDependent() || E->isValueDependent()) {
2958 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002959 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2960 AA->setPackExpansion(IsPackExpansion);
2961 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002962 return;
2963 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002964
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002965 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002966 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002967 ExprResult ICE
2968 = VerifyIntegerConstantExpression(E, &Alignment,
2969 diag::err_aligned_attribute_argument_not_int,
2970 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002971 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002972 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002973
2974 // C++11 [dcl.align]p2:
2975 // -- if the constant expression evaluates to zero, the alignment
2976 // specifier shall have no effect
2977 // C11 6.7.5p6:
2978 // An alignment specification of zero has no effect.
2979 if (!(TmpAttr.isAlignas() && !Alignment) &&
2980 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Hal Finkelbcc06082014-09-07 22:58:14 +00002981 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002982 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002983 return;
2984 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002985
David Majnemerabecae72014-02-12 20:36:10 +00002986 // Alignment calculations can wrap around if it's greater than 2**28.
2987 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2988 if (Alignment.getZExtValue() > MaxValidAlignment) {
2989 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2990 << E->getSourceRange();
2991 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002992 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002993
Richard Smith44c247f2013-02-22 08:32:16 +00002994 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002995 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002996 AA->setPackExpansion(IsPackExpansion);
2997 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002998}
2999
Michael Hanaf02bbe2013-02-01 01:19:17 +00003000void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003001 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003002 // FIXME: Cache the number on the Attr object if non-dependent?
3003 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003004 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3005 SpellingListIndex);
3006 AA->setPackExpansion(IsPackExpansion);
3007 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003008}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003009
Richard Smith848e1f12013-02-01 08:12:08 +00003010void Sema::CheckAlignasUnderalignment(Decl *D) {
3011 assert(D->hasAttrs() && "no attributes on decl");
3012
David Majnemer475b25e2015-01-21 10:54:38 +00003013 QualType UnderlyingTy, DiagTy;
3014 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3015 UnderlyingTy = DiagTy = VD->getType();
3016 } else {
3017 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3018 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3019 UnderlyingTy = ED->getIntegerType();
3020 }
3021 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003022 return;
3023
3024 // C++11 [dcl.align]p5, C11 6.7.5/4:
3025 // The combined effect of all alignment attributes in a declaration shall
3026 // not specify an alignment that is less strict than the alignment that
3027 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003028 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003029 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003030 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003031 if (I->isAlignmentDependent())
3032 return;
3033 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003034 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003035 Align = std::max(Align, I->getAlignment(Context));
3036 }
3037
3038 if (AlignasAttr && Align) {
3039 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003040 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003041 if (NaturalAlign > RequestedAlign)
3042 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003043 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003044 }
3045}
3046
David Majnemer2c4e00a2014-01-29 22:07:36 +00003047bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003048 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003049 MSInheritanceAttr::Spelling SemanticSpelling) {
3050 assert(RD->hasDefinition() && "RD has no definition!");
3051
David Majnemer98c9ee22014-02-07 00:43:07 +00003052 // We may not have seen base specifiers or any virtual methods yet. We will
3053 // have to wait until the record is defined to catch any mismatches.
3054 if (!RD->getDefinition()->isCompleteDefinition())
3055 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003056
David Majnemer98c9ee22014-02-07 00:43:07 +00003057 // The unspecified model never matches what a definition could need.
3058 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3059 return false;
3060
David Majnemer4bb09802014-02-10 19:50:15 +00003061 if (BestCase) {
3062 if (RD->calculateInheritanceModel() == SemanticSpelling)
3063 return false;
3064 } else {
3065 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3066 return false;
3067 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003068
3069 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3070 << 0 /*definition*/;
3071 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3072 << RD->getNameAsString();
3073 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003074}
3075
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003076/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003077/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003078///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003079/// Despite what would be logical, the mode attribute is a decl attribute, not a
3080/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3081/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003082static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003083 // This attribute isn't documented, but glibc uses it. It changes
3084 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003085 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3087 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003088 return;
3089 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003090
Aaron Ballman00e99962013-08-31 01:11:41 +00003091 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3092 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003093
3094 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003095 if (Str.startswith("__") && Str.endswith("__"))
3096 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003097
3098 unsigned DestWidth = 0;
3099 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003100 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003101 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003102 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003103 switch (Str[0]) {
3104 case 'Q': DestWidth = 8; break;
3105 case 'H': DestWidth = 16; break;
3106 case 'S': DestWidth = 32; break;
3107 case 'D': DestWidth = 64; break;
3108 case 'X': DestWidth = 96; break;
3109 case 'T': DestWidth = 128; break;
3110 }
3111 if (Str[1] == 'F') {
3112 IntegerMode = false;
3113 } else if (Str[1] == 'C') {
3114 IntegerMode = false;
3115 ComplexMode = true;
3116 } else if (Str[1] != 'I') {
3117 DestWidth = 0;
3118 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003119 break;
3120 case 4:
3121 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3122 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003123 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003124 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003125 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003126 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003127 break;
3128 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003129 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003130 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003131 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003132 case 11:
3133 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003134 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003135 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003136 }
3137
3138 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003139 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003140 OldTy = TD->getUnderlyingType();
3141 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3142 OldTy = VD->getType();
3143 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003144 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003145 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003146 return;
3147 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003148
John McCall9dd450b2009-09-21 23:43:11 +00003149 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003150 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3151 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003152 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003153 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3154 } else if (ComplexMode) {
3155 if (!OldTy->isComplexType())
3156 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3157 } else {
3158 if (!OldTy->isFloatingType())
3159 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3160 }
3161
Mike Stump87c57ac2009-05-16 07:39:55 +00003162 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3163 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003164 // FIXME: Make sure floating-point mappings are accurate
3165 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003166 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003167 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003168 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003169 }
3170
3171 QualType NewTy;
3172
3173 if (IntegerMode)
3174 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3175 OldTy->isSignedIntegerType());
3176 else
3177 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3178
3179 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003180 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003181 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003182 }
3183
Eli Friedman4735374e2009-03-03 06:41:03 +00003184 if (ComplexMode) {
3185 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003186 }
3187
3188 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003189 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3190 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3191 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003192 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003193
3194 D->addAttr(::new (S.Context)
3195 ModeAttr(Attr.getRange(), S.Context, Name,
3196 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003197}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003198
Chandler Carruthedc2c642011-07-02 00:01:44 +00003199static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003200 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3201 if (!VD->hasGlobalStorage())
3202 S.Diag(Attr.getLoc(),
3203 diag::warn_attribute_requires_functions_or_static_globals)
3204 << Attr.getName();
3205 } else if (!isFunctionOrMethod(D)) {
3206 S.Diag(Attr.getLoc(),
3207 diag::warn_attribute_requires_functions_or_static_globals)
3208 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003209 return;
3210 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003211
Michael Han99315932013-01-24 16:46:58 +00003212 D->addAttr(::new (S.Context)
3213 NoDebugAttr(Attr.getRange(), S.Context,
3214 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003215}
3216
Paul Robinson30e41fb2014-12-15 18:57:28 +00003217AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003218 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003219 unsigned AttrSpellingListIndex) {
3220 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003221 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003222 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3223 return nullptr;
3224 }
3225
3226 if (D->hasAttr<AlwaysInlineAttr>())
3227 return nullptr;
3228
3229 return ::new (Context) AlwaysInlineAttr(Range, Context,
3230 AttrSpellingListIndex);
3231}
3232
3233MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3234 unsigned AttrSpellingListIndex) {
3235 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3236 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3237 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3238 return nullptr;
3239 }
3240
3241 if (D->hasAttr<MinSizeAttr>())
3242 return nullptr;
3243
3244 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3245}
3246
3247OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3248 unsigned AttrSpellingListIndex) {
3249 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3250 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3251 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3252 D->dropAttr<AlwaysInlineAttr>();
3253 }
3254 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3255 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3256 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3257 D->dropAttr<MinSizeAttr>();
3258 }
3259
3260 if (D->hasAttr<OptimizeNoneAttr>())
3261 return nullptr;
3262
3263 return ::new (Context) OptimizeNoneAttr(Range, Context,
3264 AttrSpellingListIndex);
3265}
3266
Paul Robinsonf0674352014-03-31 22:29:15 +00003267static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3268 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003269 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3270 D, Attr.getRange(), Attr.getName(),
3271 Attr.getAttributeSpellingListIndex()))
3272 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003273}
3274
Paul Robinson080b1f32015-01-13 18:34:56 +00003275static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3276 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3277 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3278 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003279}
3280
Paul Robinsonf0674352014-03-31 22:29:15 +00003281static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3282 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003283 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3284 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3285 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003286}
3287
Chandler Carruthedc2c642011-07-02 00:01:44 +00003288static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003289 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003290 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003291 SourceRange RTRange = FD->getReturnTypeSourceRange();
3292 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003293 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003294 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3295 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003296 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003297 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003298
Aaron Ballman3aff6332013-12-02 19:30:36 +00003299 D->addAttr(::new (S.Context)
3300 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003301 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003302}
3303
Chandler Carruthedc2c642011-07-02 00:01:44 +00003304static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003305 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003306 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003307 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003308 return;
3309 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003310
Michael Han99315932013-01-24 16:46:58 +00003311 D->addAttr(::new (S.Context)
3312 GNUInlineAttr(Attr.getRange(), S.Context,
3313 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003314}
3315
Chandler Carruthedc2c642011-07-02 00:01:44 +00003316static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003317 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003318
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003319 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003320 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3321 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003322 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003323 return;
3324
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003325 if (!isa<ObjCMethodDecl>(D)) {
3326 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3327 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003328 return;
3329 }
3330
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003331 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003332 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003333 D->addAttr(::new (S.Context)
3334 FastCallAttr(Attr.getRange(), S.Context,
3335 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003336 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003337 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003338 D->addAttr(::new (S.Context)
3339 StdCallAttr(Attr.getRange(), S.Context,
3340 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003341 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003342 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003343 D->addAttr(::new (S.Context)
3344 ThisCallAttr(Attr.getRange(), S.Context,
3345 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003346 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003347 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003348 D->addAttr(::new (S.Context)
3349 CDeclAttr(Attr.getRange(), S.Context,
3350 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003351 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003352 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003353 D->addAttr(::new (S.Context)
3354 PascalAttr(Attr.getRange(), S.Context,
3355 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003356 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003357 case AttributeList::AT_VectorCall:
3358 D->addAttr(::new (S.Context)
3359 VectorCallAttr(Attr.getRange(), S.Context,
3360 Attr.getAttributeSpellingListIndex()));
3361 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003362 case AttributeList::AT_MSABI:
3363 D->addAttr(::new (S.Context)
3364 MSABIAttr(Attr.getRange(), S.Context,
3365 Attr.getAttributeSpellingListIndex()));
3366 return;
3367 case AttributeList::AT_SysVABI:
3368 D->addAttr(::new (S.Context)
3369 SysVABIAttr(Attr.getRange(), S.Context,
3370 Attr.getAttributeSpellingListIndex()));
3371 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003372 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003373 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003374 switch (CC) {
3375 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003376 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003377 break;
3378 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003379 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003380 break;
3381 default:
3382 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003383 }
3384
Michael Han99315932013-01-24 16:46:58 +00003385 D->addAttr(::new (S.Context)
3386 PcsAttr(Attr.getRange(), S.Context, PCS,
3387 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003388 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003389 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003390 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003391 D->addAttr(::new (S.Context)
3392 IntelOclBiccAttr(Attr.getRange(), S.Context,
3393 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003394 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003395
Abramo Bagnara50099372010-04-30 13:10:51 +00003396 default:
3397 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003398 }
3399}
3400
Aaron Ballman02df2e02012-12-09 17:45:41 +00003401bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3402 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003403 if (attr.isInvalid())
3404 return true;
3405
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003406 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003407 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003408 attr.setInvalid();
3409 return true;
3410 }
3411
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003412 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003413 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003414 case AttributeList::AT_CDecl: CC = CC_C; break;
3415 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3416 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3417 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3418 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003419 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003420 case AttributeList::AT_MSABI:
3421 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3422 CC_X86_64Win64;
3423 break;
3424 case AttributeList::AT_SysVABI:
3425 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3426 CC_C;
3427 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003428 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003429 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003430 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003431 attr.setInvalid();
3432 return true;
3433 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003434 if (StrRef == "aapcs") {
3435 CC = CC_AAPCS;
3436 break;
3437 } else if (StrRef == "aapcs-vfp") {
3438 CC = CC_AAPCS_VFP;
3439 break;
3440 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003441
3442 attr.setInvalid();
3443 Diag(attr.getLoc(), diag::err_invalid_pcs);
3444 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003445 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003446 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003447 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003448 }
3449
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003450 const TargetInfo &TI = Context.getTargetInfo();
3451 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003452 if (A != TargetInfo::CCCR_OK) {
3453 if (A == TargetInfo::CCCR_Warning)
3454 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003455
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003456 // This convention is not valid for the target. Use the default function or
3457 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003458 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3459 if (FD)
3460 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3461 TargetInfo::CCMT_NonMember;
3462 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003463 }
3464
John McCall3882ace2011-01-05 12:14:39 +00003465 return false;
3466}
3467
John McCall3882ace2011-01-05 12:14:39 +00003468/// Checks a regparm attribute, returning true if it is ill-formed and
3469/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003470bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3471 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003472 return true;
3473
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003474 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003475 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003476 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003477 }
Eli Friedman7044b762009-03-27 21:06:47 +00003478
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003479 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003480 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003481 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003482 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003483 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003484 }
3485
Douglas Gregore8bbc122011-09-02 00:18:52 +00003486 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003487 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003488 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003489 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003490 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003491 }
3492
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003493 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003494 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003495 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003496 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003497 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003498 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003499 }
3500
John McCall3882ace2011-01-05 12:14:39 +00003501 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003502}
3503
Artem Belevich7093e402015-04-21 22:55:54 +00003504// Checks whether an argument of launch_bounds attribute is acceptable
3505// May output an error.
3506static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3507 const CUDALaunchBoundsAttr &Attr,
3508 const unsigned Idx) {
3509
3510 if (S.DiagnoseUnexpandedParameterPack(E))
3511 return false;
3512
3513 // Accept template arguments for now as they depend on something else.
3514 // We'll get to check them when they eventually get instantiated.
3515 if (E->isValueDependent())
3516 return true;
3517
3518 llvm::APSInt I(64);
3519 if (!E->isIntegerConstantExpr(I, S.Context)) {
3520 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3521 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3522 return false;
3523 }
3524 // Make sure we can fit it in 32 bits.
3525 if (!I.isIntN(32)) {
3526 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3527 << 32 << /* Unsigned */ 1;
3528 return false;
3529 }
3530 if (I < 0)
3531 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3532 << &Attr << Idx << E->getSourceRange();
3533
3534 return true;
3535}
3536
3537void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3538 Expr *MinBlocks, unsigned SpellingListIndex) {
3539 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3540 SpellingListIndex);
3541
3542 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003543 return;
3544
Artem Belevich7093e402015-04-21 22:55:54 +00003545 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3546 return;
3547
3548 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3549 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3550}
3551
3552static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3553 const AttributeList &Attr) {
3554 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3555 !checkAttributeAtMostNumArgs(S, Attr, 2))
3556 return;
3557
3558 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3559 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3560 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003561}
3562
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003563static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3564 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003565 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003566 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003567 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003568 return;
3569 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003570
3571 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003572 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003573
Aaron Ballman00e99962013-08-31 01:11:41 +00003574 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003575
3576 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3577 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3578 << Attr.getName() << ExpectedFunctionOrMethod;
3579 return;
3580 }
3581
3582 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003583 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3584 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003585 return;
3586
3587 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003588 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3589 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003590 return;
3591
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003592 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003593 if (IsPointer) {
3594 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003595 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003596 if (!BufferTy->isPointerType()) {
3597 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003598 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003599 }
3600 }
3601
Michael Han99315932013-01-24 16:46:58 +00003602 D->addAttr(::new (S.Context)
3603 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3604 ArgumentIdx, TypeTagIdx, IsPointer,
3605 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003606}
3607
3608static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3609 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003610 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003611 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003612 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003613 return;
3614 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003615
3616 if (!checkAttributeNumArgs(S, Attr, 1))
3617 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003618
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003619 if (!isa<VarDecl>(D)) {
3620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3621 << Attr.getName() << ExpectedVariable;
3622 return;
3623 }
3624
Aaron Ballman00e99962013-08-31 01:11:41 +00003625 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003626 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003627 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3628 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003629
Michael Han99315932013-01-24 16:46:58 +00003630 D->addAttr(::new (S.Context)
3631 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003632 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003633 Attr.getLayoutCompatible(),
3634 Attr.getMustBeNull(),
3635 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003636}
3637
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003638//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003639// Checker-specific attribute handlers.
3640//===----------------------------------------------------------------------===//
3641
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003642static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003643 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003644 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003645}
3646
John McCalled433932011-01-25 03:31:58 +00003647static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003648 return type->isDependentType() ||
3649 type->isObjCObjectPointerType() ||
3650 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003651}
3652static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003653 return type->isDependentType() ||
3654 type->isPointerType() ||
3655 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003656}
3657
Chandler Carruthedc2c642011-07-02 00:01:44 +00003658static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003659 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003660 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003661
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003662 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003663 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3664 cf = false;
3665 } else {
3666 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3667 cf = true;
3668 }
3669
3670 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003671 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003672 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003673 return;
3674 }
3675
3676 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003677 param->addAttr(::new (S.Context)
3678 CFConsumedAttr(Attr.getRange(), S.Context,
3679 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003680 else
Michael Han99315932013-01-24 16:46:58 +00003681 param->addAttr(::new (S.Context)
3682 NSConsumedAttr(Attr.getRange(), S.Context,
3683 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003684}
3685
Chandler Carruthedc2c642011-07-02 00:01:44 +00003686static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3687 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003688
John McCalled433932011-01-25 03:31:58 +00003689 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003690
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003691 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003692 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003693 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003694 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003695 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003696 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3697 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003698 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003699 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003700 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003701 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003702 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003703 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003704 return;
3705 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003706
John McCalled433932011-01-25 03:31:58 +00003707 bool typeOK;
3708 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003709 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003710 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003711 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003712 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003713 cf = false;
3714 break;
3715
3716 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003717 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003718 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3719 cf = false;
3720 break;
3721
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003722 case AttributeList::AT_CFReturnsRetained:
3723 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003724 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3725 cf = true;
3726 break;
3727 }
3728
3729 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003730 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003731 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003732 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003733 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003734
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003735 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003736 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003737 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003738 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00003739 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
3740 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003741 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003742 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003743 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
3744 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003745 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003746 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003747 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
3748 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003749 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003750 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003751 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
3752 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003753 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003754 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003755 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
3756 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003757 return;
3758 };
3759}
3760
John McCallcf166702011-07-22 08:53:00 +00003761static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3762 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003763 const int EP_ObjCMethod = 1;
3764 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003765
John McCallcf166702011-07-22 08:53:00 +00003766 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003767 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003768 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003769 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003770 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003771 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003772
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003773 if (!resultType->isReferenceType() &&
3774 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003775 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003776 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003777 << attr.getName()
3778 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003779 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003780
3781 // Drop the attribute.
3782 return;
3783 }
3784
Nico Weber462fd1e2015-01-07 23:50:05 +00003785 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
3786 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003787}
3788
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003789static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3790 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003791 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003792
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003793 DeclContext *DC = method->getDeclContext();
3794 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3795 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3796 << attr.getName() << 0;
3797 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3798 return;
3799 }
3800 if (method->getMethodFamily() == OMF_dealloc) {
3801 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3802 << attr.getName() << 1;
3803 return;
3804 }
3805
Michael Han99315932013-01-24 16:46:58 +00003806 method->addAttr(::new (S.Context)
3807 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3808 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003809}
3810
Aaron Ballmanfb763042013-12-02 18:05:46 +00003811static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3812 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003813 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003814 return;
John McCall32f5fe12011-09-30 05:12:12 +00003815
Aaron Ballmanfb763042013-12-02 18:05:46 +00003816 D->addAttr(::new (S.Context)
3817 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3818 Attr.getAttributeSpellingListIndex()));
3819}
3820
3821static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3822 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003823 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003824 return;
3825
3826 D->addAttr(::new (S.Context)
3827 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3828 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003829}
3830
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003831static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3832 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003833 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003834
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003835 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003836 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003837 return;
3838 }
John McCall28592582015-02-01 22:34:06 +00003839
3840 // Typedefs only allow objc_bridge(id) and have some additional checking.
3841 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
3842 if (!Parm->Ident->isStr("id")) {
3843 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
3844 << Attr.getName();
3845 return;
3846 }
3847
3848 // Only allow 'cv void *'.
3849 QualType T = TD->getUnderlyingType();
3850 if (!T->isVoidPointerType()) {
3851 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
3852 return;
3853 }
3854 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003855
3856 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003857 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003858 Attr.getAttributeSpellingListIndex()));
3859}
3860
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003861static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3862 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003863 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3864
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003865 if (!Parm) {
3866 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3867 return;
3868 }
3869
3870 D->addAttr(::new (S.Context)
3871 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3872 Attr.getAttributeSpellingListIndex()));
3873}
3874
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003875static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3876 const AttributeList &Attr) {
3877 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003878 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003879 if (!RelatedClass) {
3880 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3881 return;
3882 }
3883 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003884 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003885 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003886 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003887 D->addAttr(::new (S.Context)
3888 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3889 ClassMethod, InstanceMethod,
3890 Attr.getAttributeSpellingListIndex()));
3891}
3892
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003893static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3894 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003895 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00003896 if (ObjCCategoryDecl *CatDecl =
3897 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003898 IFace = CatDecl->getClassInterface();
3899 else
3900 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00003901
3902 if (!IFace)
3903 return;
3904
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003905 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003906 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003907 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3908 Attr.getAttributeSpellingListIndex()));
3909}
3910
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003911static void handleObjCRuntimeName(Sema &S, Decl *D,
3912 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003913 StringRef MetaDataName;
3914 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3915 return;
3916 D->addAttr(::new (S.Context)
3917 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3918 MetaDataName,
3919 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003920}
3921
Chandler Carruthedc2c642011-07-02 00:01:44 +00003922static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3923 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003924 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003925
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003926 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003927 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003928}
3929
Chandler Carruthedc2c642011-07-02 00:01:44 +00003930static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3931 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003932 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003933 QualType type = vd->getType();
3934
3935 if (!type->isDependentType() &&
3936 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003937 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003938 << type;
3939 return;
3940 }
3941
3942 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3943
3944 // If we have no lifetime yet, check the lifetime we're presumably
3945 // going to infer.
3946 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3947 lifetime = type->getObjCARCImplicitLifetime();
3948
3949 switch (lifetime) {
3950 case Qualifiers::OCL_None:
3951 assert(type->isDependentType() &&
3952 "didn't infer lifetime for non-dependent type?");
3953 break;
3954
3955 case Qualifiers::OCL_Weak: // meaningful
3956 case Qualifiers::OCL_Strong: // meaningful
3957 break;
3958
3959 case Qualifiers::OCL_ExplicitNone:
3960 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003961 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003962 << (lifetime == Qualifiers::OCL_Autoreleasing);
3963 break;
3964 }
3965
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003966 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003967 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3968 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003969}
3970
Francois Picheta83957a2010-12-19 06:50:37 +00003971//===----------------------------------------------------------------------===//
3972// Microsoft specific attribute handlers.
3973//===----------------------------------------------------------------------===//
3974
Chandler Carruthedc2c642011-07-02 00:01:44 +00003975static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003976 if (!S.LangOpts.CPlusPlus) {
3977 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3978 << Attr.getName() << AttributeLangSupport::C;
3979 return;
3980 }
3981
Aaron Ballman60e705e2013-11-24 20:58:02 +00003982 if (!isa<CXXRecordDecl>(D)) {
3983 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3984 << Attr.getName() << ExpectedClass;
3985 return;
3986 }
3987
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003988 StringRef StrRef;
3989 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003990 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003991 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003992
David Majnemer89085342013-08-09 08:56:20 +00003993 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3994 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003995 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3996 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003997
Reid Kleckner140c4a72013-05-17 14:04:52 +00003998 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003999 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004000 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004001 return;
4002 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004003
David Majnemer89085342013-08-09 08:56:20 +00004004 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004005 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004006 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004007 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004008 return;
4009 }
David Majnemer89085342013-08-09 08:56:20 +00004010 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004011 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004012 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004013 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004014 }
Francois Picheta83957a2010-12-19 06:50:37 +00004015
David Majnemer89085342013-08-09 08:56:20 +00004016 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4017 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004018}
4019
David Majnemer2c4e00a2014-01-29 22:07:36 +00004020static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4021 if (!S.LangOpts.CPlusPlus) {
4022 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4023 << Attr.getName() << AttributeLangSupport::C;
4024 return;
4025 }
4026 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004027 D, Attr.getRange(), /*BestCase=*/true,
4028 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004029 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4030 if (IA)
4031 D->addAttr(IA);
4032}
4033
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004034static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4035 const AttributeList &Attr) {
4036 VarDecl *VD = cast<VarDecl>(D);
4037 if (!S.Context.getTargetInfo().isTLSSupported()) {
4038 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4039 return;
4040 }
4041 if (VD->getTSCSpec() != TSCS_unspecified) {
4042 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4043 return;
4044 }
4045 if (VD->hasLocalStorage()) {
4046 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4047 return;
4048 }
4049 VD->addAttr(::new (S.Context) ThreadAttr(
4050 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4051}
4052
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004053static void handleARMInterruptAttr(Sema &S, Decl *D,
4054 const AttributeList &Attr) {
4055 // Check the attribute arguments.
4056 if (Attr.getNumArgs() > 1) {
4057 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4058 << Attr.getName() << 1;
4059 return;
4060 }
4061
4062 StringRef Str;
4063 SourceLocation ArgLoc;
4064
4065 if (Attr.getNumArgs() == 0)
4066 Str = "";
4067 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4068 return;
4069
4070 ARMInterruptAttr::InterruptType Kind;
4071 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4072 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4073 << Attr.getName() << Str << ArgLoc;
4074 return;
4075 }
4076
4077 unsigned Index = Attr.getAttributeSpellingListIndex();
4078 D->addAttr(::new (S.Context)
4079 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4080}
4081
4082static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4083 const AttributeList &Attr) {
4084 if (!checkAttributeNumArgs(S, Attr, 1))
4085 return;
4086
4087 if (!Attr.isArgExpr(0)) {
4088 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4089 << AANT_ArgumentIntegerConstant;
4090 return;
4091 }
4092
4093 // FIXME: Check for decl - it should be void ()(void).
4094
4095 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4096 llvm::APSInt NumParams(32);
4097 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4098 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4099 << Attr.getName() << AANT_ArgumentIntegerConstant
4100 << NumParamsExpr->getSourceRange();
4101 return;
4102 }
4103
4104 unsigned Num = NumParams.getLimitedValue(255);
4105 if ((Num & 1) || Num > 30) {
4106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4107 << Attr.getName() << (int)NumParams.getSExtValue()
4108 << NumParamsExpr->getSourceRange();
4109 return;
4110 }
4111
Aaron Ballman36a53502014-01-16 13:03:14 +00004112 D->addAttr(::new (S.Context)
4113 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4114 Attr.getAttributeSpellingListIndex()));
4115 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004116}
4117
4118static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4119 // Dispatch the interrupt attribute based on the current target.
4120 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4121 handleMSP430InterruptAttr(S, D, Attr);
4122 else
4123 handleARMInterruptAttr(S, D, Attr);
4124}
4125
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004126static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4127 const AttributeList &Attr) {
4128 uint32_t NumRegs;
4129 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4130 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4131 return;
4132
4133 D->addAttr(::new (S.Context)
4134 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4135 NumRegs,
4136 Attr.getAttributeSpellingListIndex()));
4137}
4138
4139static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4140 const AttributeList &Attr) {
4141 uint32_t NumRegs;
4142 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4143 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4144 return;
4145
4146 D->addAttr(::new (S.Context)
4147 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4148 NumRegs,
4149 Attr.getAttributeSpellingListIndex()));
4150}
4151
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004152static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4153 const AttributeList& Attr) {
4154 // If we try to apply it to a function pointer, don't warn, but don't
4155 // do anything, either. It doesn't matter anyway, because there's nothing
4156 // special about calling a force_align_arg_pointer function.
4157 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4158 if (VD && VD->getType()->isFunctionPointerType())
4159 return;
4160 // Also don't warn on function pointer typedefs.
4161 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4162 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4163 TD->getUnderlyingType()->isFunctionType()))
4164 return;
4165 // Attribute can only be applied to function types.
4166 if (!isa<FunctionDecl>(D)) {
4167 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4168 << Attr.getName() << /* function */0;
4169 return;
4170 }
4171
Aaron Ballman36a53502014-01-16 13:03:14 +00004172 D->addAttr(::new (S.Context)
4173 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4174 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004175}
4176
4177DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4178 unsigned AttrSpellingListIndex) {
4179 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004180 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004181 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004182 }
4183
4184 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004185 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004186
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004187 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004188}
4189
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004190DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4191 unsigned AttrSpellingListIndex) {
4192 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004193 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004194 D->dropAttr<DLLImportAttr>();
4195 }
4196
4197 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004198 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004199
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004200 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004201}
4202
Hans Wennborge82f19c2014-06-24 23:57:05 +00004203static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004204 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4205 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4206 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4207 << A.getName();
4208 return;
4209 }
4210
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004211 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4212 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4213 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4214 // MinGW doesn't allow dllimport on inline functions.
4215 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4216 << A.getName();
4217 return;
4218 }
4219 }
4220
Hans Wennborge82f19c2014-06-24 23:57:05 +00004221 unsigned Index = A.getAttributeSpellingListIndex();
4222 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4223 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4224 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004225 if (NewAttr)
4226 D->addAttr(NewAttr);
4227}
4228
David Majnemer2c4e00a2014-01-29 22:07:36 +00004229MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004230Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004231 unsigned AttrSpellingListIndex,
4232 MSInheritanceAttr::Spelling SemanticSpelling) {
4233 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4234 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004235 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004236 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4237 << 1 /*previous declaration*/;
4238 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4239 D->dropAttr<MSInheritanceAttr>();
4240 }
4241
4242 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4243 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004244 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4245 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004246 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004247 }
4248 } else {
4249 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4250 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4251 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004252 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004253 }
4254 if (RD->getDescribedClassTemplate()) {
4255 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4256 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004257 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004258 }
4259 }
4260
4261 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004262 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004263}
4264
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004265static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4266 // The capability attributes take a single string parameter for the name of
4267 // the capability they represent. The lockable attribute does not take any
4268 // parameters. However, semantically, both attributes represent the same
4269 // concept, and so they use the same semantic attribute. Eventually, the
4270 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004271 //
Alp Toker958027b2014-07-14 19:42:55 +00004272 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004273 // literal will be considered a "mutex."
4274 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004275 SourceLocation LiteralLoc;
4276 if (Attr.getKind() == AttributeList::AT_Capability &&
4277 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4278 return;
4279
Aaron Ballman6c810072014-03-05 21:47:13 +00004280 // Currently, there are only two names allowed for a capability: role and
4281 // mutex (case insensitive). Diagnose other capability names.
4282 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4283 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4284
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004285 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4286 Attr.getAttributeSpellingListIndex()));
4287}
4288
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004289static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4290 const AttributeList &Attr) {
4291 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4292 Attr.getArgAsExpr(0),
4293 Attr.getAttributeSpellingListIndex()));
4294}
4295
4296static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4297 const AttributeList &Attr) {
4298 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004299 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004300 return;
4301
4302 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4303 S.Context,
4304 Args.data(), Args.size(),
4305 Attr.getAttributeSpellingListIndex()));
4306}
4307
4308static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4309 const AttributeList &Attr) {
4310 SmallVector<Expr*, 2> Args;
4311 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4312 return;
4313
4314 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4315 S.Context,
4316 Attr.getArgAsExpr(0),
4317 Args.data(),
4318 Args.size(),
4319 Attr.getAttributeSpellingListIndex()));
4320}
4321
4322static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4323 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004324 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004325 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004326 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004327
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004328 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4329 Attr.getRange(), S.Context, Args.data(), Args.size(),
4330 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004331}
4332
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004333static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4334 const AttributeList &Attr) {
4335 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4336 return;
4337
4338 // check that all arguments are lockable objects
4339 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004340 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004341 if (Args.empty())
4342 return;
4343
4344 RequiresCapabilityAttr *RCA = ::new (S.Context)
4345 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4346 Args.size(), Attr.getAttributeSpellingListIndex());
4347
4348 D->addAttr(RCA);
4349}
4350
Aaron Ballman43f40102014-11-14 22:34:56 +00004351static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4352 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4353 if (NSD->isAnonymousNamespace()) {
4354 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4355 // Do not want to attach the attribute to the namespace because that will
4356 // cause confusing diagnostic reports for uses of declarations within the
4357 // namespace.
4358 return;
4359 }
4360 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004361
4362 if (!S.getLangOpts().CPlusPlus14)
4363 if (Attr.isCXX11Attribute() &&
4364 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004365 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004366
Aaron Ballman43f40102014-11-14 22:34:56 +00004367 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4368}
4369
Peter Collingbourne915df992015-05-15 18:33:32 +00004370static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4371 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4372 return;
4373
4374 std::vector<std::string> Sanitizers;
4375
4376 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4377 StringRef SanitizerName;
4378 SourceLocation LiteralLoc;
4379
4380 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4381 return;
4382
4383 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4384 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4385
4386 Sanitizers.push_back(SanitizerName);
4387 }
4388
4389 D->addAttr(::new (S.Context) NoSanitizeAttr(
4390 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4391 Attr.getAttributeSpellingListIndex()));
4392}
4393
4394static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4395 const AttributeList &Attr) {
4396 std::string SanitizerName =
Peter Collingbourne94410942015-05-15 20:11:18 +00004397 llvm::StringSwitch<std::string>(Attr.getName()->getName())
Peter Collingbourne915df992015-05-15 18:33:32 +00004398 .Case("no_address_safety_analysis", "address")
4399 .Case("no_sanitize_address", "address")
4400 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004401 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004402 D->addAttr(::new (S.Context)
4403 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4404 Attr.getAttributeSpellingListIndex()));
4405}
4406
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004407/// Handles semantic checking for features that are common to all attributes,
4408/// such as checking whether a parameter was properly specified, or the correct
4409/// number of arguments were passed, etc.
4410static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4411 const AttributeList &Attr) {
4412 // Several attributes carry different semantics than the parsing requires, so
4413 // those are opted out of the common handling.
4414 //
4415 // We also bail on unknown and ignored attributes because those are handled
4416 // as part of the target-specific handling logic.
4417 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004418 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004419 return false;
4420
Aaron Ballman3aff6332013-12-02 19:30:36 +00004421 // Check whether the attribute requires specific language extensions to be
4422 // enabled.
4423 if (!Attr.diagnoseLangOpts(S))
4424 return true;
4425
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004426 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4427 // If there are no optional arguments, then checking for the argument count
4428 // is trivial.
4429 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4430 return true;
4431 } else {
4432 // There are optional arguments, so checking is slightly more involved.
4433 if (Attr.getMinArgs() &&
4434 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4435 return true;
4436 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4437 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4438 return true;
4439 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004440
4441 // Check whether the attribute appertains to the given subject.
4442 if (!Attr.diagnoseAppertainsTo(S, D))
4443 return true;
4444
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004445 return false;
4446}
4447
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004448//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004449// Top Level Sema Entry Points
4450//===----------------------------------------------------------------------===//
4451
Richard Smithf8a75c32013-08-29 00:47:48 +00004452/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4453/// the attribute applies to decls. If the attribute is a type attribute, just
4454/// silently ignore it if a GNU attribute.
4455static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4456 const AttributeList &Attr,
4457 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004458 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004459 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004460
Richard Smithf8a75c32013-08-29 00:47:48 +00004461 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4462 // instead.
4463 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4464 return;
4465
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004466 // Unknown attributes are automatically warned on. Target-specific attributes
4467 // which do not apply to the current target architecture are treated as
4468 // though they were unknown attributes.
4469 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4470 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004471 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4472 ? diag::warn_unhandled_ms_attribute_ignored
4473 : diag::warn_unknown_attribute_ignored)
4474 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004475 return;
4476 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004477
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004478 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4479 return;
4480
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004481 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004482 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004483 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004484 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004485 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004486 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004487 handleInterruptAttr(S, D, Attr);
4488 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004489 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004490 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4491 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004492 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004493 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004494 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004495 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004496 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004497 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4498 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004499 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004500 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4501 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004502 case AttributeList::AT_AMDGPUNumVGPR:
4503 handleAMDGPUNumVGPRAttr(S, D, Attr);
4504 break;
4505 case AttributeList::AT_AMDGPUNumSGPR:
4506 handleAMDGPUNumSGPRAttr(S, D, Attr);
4507 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004508 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004509 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4510 break;
4511 case AttributeList::AT_IBOutlet:
4512 handleIBOutlet(S, D, Attr);
4513 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004514 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004515 handleIBOutletCollection(S, D, Attr);
4516 break;
4517 case AttributeList::AT_Alias:
4518 handleAliasAttr(S, D, Attr);
4519 break;
4520 case AttributeList::AT_Aligned:
4521 handleAlignedAttr(S, D, Attr);
4522 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004523 case AttributeList::AT_AlignValue:
4524 handleAlignValueAttr(S, D, Attr);
4525 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004526 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004527 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004528 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004529 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004530 handleAnalyzerNoReturnAttr(S, D, Attr);
4531 break;
4532 case AttributeList::AT_TLSModel:
4533 handleTLSModelAttr(S, D, Attr);
4534 break;
4535 case AttributeList::AT_Annotate:
4536 handleAnnotateAttr(S, D, Attr);
4537 break;
4538 case AttributeList::AT_Availability:
4539 handleAvailabilityAttr(S, D, Attr);
4540 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004541 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004542 handleDependencyAttr(S, scope, D, Attr);
4543 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004544 case AttributeList::AT_Common:
4545 handleCommonAttr(S, D, Attr);
4546 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004547 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004548 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4549 break;
4550 case AttributeList::AT_Constructor:
4551 handleConstructorAttr(S, D, Attr);
4552 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004553 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004554 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4555 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004556 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004557 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004558 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004559 case AttributeList::AT_Destructor:
4560 handleDestructorAttr(S, D, Attr);
4561 break;
4562 case AttributeList::AT_EnableIf:
4563 handleEnableIfAttr(S, D, Attr);
4564 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004565 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004566 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004567 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004568 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004569 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004570 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004571 case AttributeList::AT_OptimizeNone:
4572 handleOptimizeNoneAttr(S, D, Attr);
4573 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004574 case AttributeList::AT_FlagEnum:
4575 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4576 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004577 case AttributeList::AT_Flatten:
4578 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4579 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004580 case AttributeList::AT_Format:
4581 handleFormatAttr(S, D, Attr);
4582 break;
4583 case AttributeList::AT_FormatArg:
4584 handleFormatArgAttr(S, D, Attr);
4585 break;
4586 case AttributeList::AT_CUDAGlobal:
4587 handleGlobalAttr(S, D, Attr);
4588 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004589 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004590 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4591 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004592 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004593 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4594 break;
4595 case AttributeList::AT_GNUInline:
4596 handleGNUInlineAttr(S, D, Attr);
4597 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004598 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004599 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004600 break;
David Majnemer631a90b2015-02-04 07:23:21 +00004601 case AttributeList::AT_Restrict:
4602 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004603 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004604 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004605 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4606 break;
4607 case AttributeList::AT_Mode:
4608 handleModeAttr(S, D, Attr);
4609 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004610 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004611 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4612 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004613 case AttributeList::AT_NoSplitStack:
4614 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4615 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004616 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004617 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4618 handleNonNullAttrParameter(S, PVD, Attr);
4619 else
4620 handleNonNullAttr(S, D, Attr);
4621 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004622 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004623 handleReturnsNonNullAttr(S, D, Attr);
4624 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004625 case AttributeList::AT_AssumeAligned:
4626 handleAssumeAlignedAttr(S, D, Attr);
4627 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004628 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004629 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4630 break;
4631 case AttributeList::AT_Ownership:
4632 handleOwnershipAttr(S, D, Attr);
4633 break;
4634 case AttributeList::AT_Cold:
4635 handleColdAttr(S, D, Attr);
4636 break;
4637 case AttributeList::AT_Hot:
4638 handleHotAttr(S, D, Attr);
4639 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004640 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004641 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4642 break;
4643 case AttributeList::AT_NoReturn:
4644 handleNoReturnAttr(S, D, Attr);
4645 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004646 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004647 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4648 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004649 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004650 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4651 break;
4652 case AttributeList::AT_VecReturn:
4653 handleVecReturnAttr(S, D, Attr);
4654 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004655
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004656 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004657 handleObjCOwnershipAttr(S, D, Attr);
4658 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004659 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004660 handleObjCPreciseLifetimeAttr(S, D, Attr);
4661 break;
John McCall31168b02011-06-15 23:02:42 +00004662
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004663 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004664 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4665 break;
John McCallcf166702011-07-22 08:53:00 +00004666
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004667 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004668 handleObjCRequiresSuperAttr(S, D, Attr);
4669 break;
4670
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004671 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004672 handleObjCBridgeAttr(S, scope, D, Attr);
4673 break;
4674
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004675 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004676 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4677 break;
4678
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004679 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004680 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4681 break;
John McCallf1e8b342011-09-29 07:17:38 +00004682
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004683 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004684 handleObjCDesignatedInitializer(S, D, Attr);
4685 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004686
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004687 case AttributeList::AT_ObjCRuntimeName:
4688 handleObjCRuntimeName(S, D, Attr);
4689 break;
4690
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004691 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004692 handleCFAuditedTransferAttr(S, D, Attr);
4693 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004694 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004695 handleCFUnknownTransferAttr(S, D, Attr);
4696 break;
John McCall32f5fe12011-09-30 05:12:12 +00004697
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004698 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004699 case AttributeList::AT_NSConsumed:
4700 handleNSConsumedAttr(S, D, Attr);
4701 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004702 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004703 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4704 break;
John McCalled433932011-01-25 03:31:58 +00004705
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004706 case AttributeList::AT_NSReturnsAutoreleased:
4707 case AttributeList::AT_NSReturnsNotRetained:
4708 case AttributeList::AT_CFReturnsNotRetained:
4709 case AttributeList::AT_NSReturnsRetained:
4710 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004711 handleNSReturnsRetainedAttr(S, D, Attr);
4712 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004713 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004714 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4715 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004716 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004717 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4718 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004719 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004720 handleVecTypeHint(S, D, Attr);
4721 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004722
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004723 case AttributeList::AT_InitPriority:
4724 handleInitPriorityAttr(S, D, Attr);
4725 break;
4726
4727 case AttributeList::AT_Packed:
4728 handlePackedAttr(S, D, Attr);
4729 break;
4730 case AttributeList::AT_Section:
4731 handleSectionAttr(S, D, Attr);
4732 break;
Eric Christopher11acf732015-06-12 01:35:52 +00004733 case AttributeList::AT_Target:
4734 handleTargetAttr(S, D, Attr);
4735 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004736 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004737 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004738 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004739 case AttributeList::AT_ArcWeakrefUnavailable:
4740 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4741 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004742 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004743 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4744 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004745 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004746 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004747 break;
4748 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004749 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4750 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004751 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004752 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4753 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004754 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004755 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4756 break;
4757 case AttributeList::AT_Used:
4758 handleUsedAttr(S, D, Attr);
4759 break;
John McCalld041a9b2013-02-20 01:54:26 +00004760 case AttributeList::AT_Visibility:
4761 handleVisibilityAttr(S, D, Attr, false);
4762 break;
4763 case AttributeList::AT_TypeVisibility:
4764 handleVisibilityAttr(S, D, Attr, true);
4765 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004766 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004767 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4768 break;
4769 case AttributeList::AT_WarnUnusedResult:
4770 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004771 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004772 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004773 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4774 break;
4775 case AttributeList::AT_WeakRef:
4776 handleWeakRefAttr(S, D, Attr);
4777 break;
4778 case AttributeList::AT_WeakImport:
4779 handleWeakImportAttr(S, D, Attr);
4780 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004781 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004782 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004783 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004784 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004785 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4786 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004787 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004788 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004789 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004790 case AttributeList::AT_ObjCNSObject:
4791 handleObjCNSObject(S, D, Attr);
4792 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00004793 case AttributeList::AT_ObjCIndependentClass:
4794 handleObjCIndependentClass(S, D, Attr);
4795 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004796 case AttributeList::AT_Blocks:
4797 handleBlocksAttr(S, D, Attr);
4798 break;
4799 case AttributeList::AT_Sentinel:
4800 handleSentinelAttr(S, D, Attr);
4801 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004802 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004803 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4804 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004805 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004806 handleSimpleAttribute<PureAttr>(S, D, Attr);
4807 break;
4808 case AttributeList::AT_Cleanup:
4809 handleCleanupAttr(S, D, Attr);
4810 break;
4811 case AttributeList::AT_NoDebug:
4812 handleNoDebugAttr(S, D, Attr);
4813 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004814 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004815 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4816 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004817 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004818 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4819 break;
4820 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4821 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4822 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004823 case AttributeList::AT_StdCall:
4824 case AttributeList::AT_CDecl:
4825 case AttributeList::AT_FastCall:
4826 case AttributeList::AT_ThisCall:
4827 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004828 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00004829 case AttributeList::AT_MSABI:
4830 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004831 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004832 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004833 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004834 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004835 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004836 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4837 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004838 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004839 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4840 break;
John McCall8d32c052012-05-22 21:28:12 +00004841
4842 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00004843 case AttributeList::AT_MSNoVTable:
4844 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00004845 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00004846 case AttributeList::AT_MSStruct:
4847 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004848 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004849 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004850 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004851 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004852 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004853 handleMSInheritanceAttr(S, D, Attr);
4854 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004855 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004856 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4857 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004858 case AttributeList::AT_Thread:
4859 handleDeclspecThreadAttr(S, D, Attr);
4860 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004861
4862 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004863 case AttributeList::AT_AssertExclusiveLock:
4864 handleAssertExclusiveLockAttr(S, D, Attr);
4865 break;
4866 case AttributeList::AT_AssertSharedLock:
4867 handleAssertSharedLockAttr(S, D, Attr);
4868 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004869 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004870 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4871 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004872 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004873 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004874 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004875 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004876 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4877 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00004878 case AttributeList::AT_NoSanitize:
4879 handleNoSanitizeAttr(S, D, Attr);
4880 break;
4881 case AttributeList::AT_NoSanitizeSpecific:
4882 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004883 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004884 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004885 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004886 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004887 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004888 handleGuardedByAttr(S, D, Attr);
4889 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004890 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004891 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004892 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004893 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004894 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004895 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004896 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004897 handleLockReturnedAttr(S, D, Attr);
4898 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004899 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004900 handleLocksExcludedAttr(S, D, Attr);
4901 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004902 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004903 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004904 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004905 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004906 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004907 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004908 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004909 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004910 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004911
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004912 // Capability analysis attributes.
4913 case AttributeList::AT_Capability:
4914 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004915 handleCapabilityAttr(S, D, Attr);
4916 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004917 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004918 handleRequiresCapabilityAttr(S, D, Attr);
4919 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004920
4921 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004922 handleAssertCapabilityAttr(S, D, Attr);
4923 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004924 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004925 handleAcquireCapabilityAttr(S, D, Attr);
4926 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004927 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004928 handleReleaseCapabilityAttr(S, D, Attr);
4929 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004930 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004931 handleTryAcquireCapabilityAttr(S, D, Attr);
4932 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004933
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004934 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004935 case AttributeList::AT_Consumable:
4936 handleConsumableAttr(S, D, Attr);
4937 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004938 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004939 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4940 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004941 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004942 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4943 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004944 case AttributeList::AT_CallableWhen:
4945 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004946 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004947 case AttributeList::AT_ParamTypestate:
4948 handleParamTypestateAttr(S, D, Attr);
4949 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004950 case AttributeList::AT_ReturnTypestate:
4951 handleReturnTypestateAttr(S, D, Attr);
4952 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004953 case AttributeList::AT_SetTypestate:
4954 handleSetTypestateAttr(S, D, Attr);
4955 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004956 case AttributeList::AT_TestTypestate:
4957 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004958 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004959
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004960 // Type safety attributes.
4961 case AttributeList::AT_ArgumentWithTypeTag:
4962 handleArgumentWithTypeTagAttr(S, D, Attr);
4963 break;
4964 case AttributeList::AT_TypeTagForDatatype:
4965 handleTypeTagForDatatypeAttr(S, D, Attr);
4966 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004967 }
4968}
4969
4970/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4971/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004972void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004973 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004974 bool IncludeCXX11Attributes) {
4975 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004976 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004977
Joey Gouly2cd9db12013-12-13 16:15:28 +00004978 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004979 // GCC accepts
4980 // static int a9 __attribute__((weakref));
4981 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004982 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004983 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4984 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004985 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004986 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004987 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004988
Aaron Ballmanbe243a72014-12-04 22:45:31 +00004989 // FIXME: We should be able to handle this in TableGen as well. It would be
4990 // good to have a way to specify "these attributes must appear as a group",
4991 // for these. Additionally, it would be good to have a way to specify "these
4992 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00004993 if (!D->hasAttr<OpenCLKernelAttr>()) {
4994 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004995 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00004996 // FIXME: This emits a different error message than
4997 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004998 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004999 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005000 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005001 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005002 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005003 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005004 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005005 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005006 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5007 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5008 << A << ExpectedKernelFunction;
5009 D->setInvalidDecl();
5010 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5011 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5012 << A << ExpectedKernelFunction;
5013 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005014 }
5015 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005016}
5017
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005018// Annotation attributes are the only attributes allowed after an access
5019// specifier.
5020bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5021 const AttributeList *AttrList) {
5022 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005023 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005024 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005025 } else {
5026 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5027 return true;
5028 }
5029 }
5030
5031 return false;
5032}
5033
John McCall42856de2011-10-01 05:17:03 +00005034/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5035/// contains any decl attributes that we should warn about.
5036static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5037 for ( ; A; A = A->getNext()) {
5038 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005039 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005040 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5041
5042 if (A->getKind() == AttributeList::UnknownAttribute) {
5043 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5044 << A->getName() << A->getRange();
5045 } else {
5046 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5047 << A->getName() << A->getRange();
5048 }
5049 }
5050}
5051
5052/// checkUnusedDeclAttributes - Given a declarator which is not being
5053/// used to build a declaration, complain about any decl attributes
5054/// which might be lying around on it.
5055void Sema::checkUnusedDeclAttributes(Declarator &D) {
5056 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5057 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5058 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5059 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5060}
5061
Ryan Flynn7d470f32009-07-30 03:15:39 +00005062/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005063/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005064NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5065 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005066 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005067 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005068 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005069 FunctionDecl *NewFD;
5070 // FIXME: Missing call to CheckFunctionDeclaration().
5071 // FIXME: Mangling?
5072 // FIXME: Is the qualifier info correct?
5073 // FIXME: Is the DeclContext correct?
5074 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5075 Loc, Loc, DeclarationName(II),
5076 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005077 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005078 FD->hasPrototype(),
5079 false/*isConstexprSpecified*/);
5080 NewD = NewFD;
5081
5082 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005083 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005084
5085 // Fake up parameter variables; they are declared as if this were
5086 // a typedef.
5087 QualType FDTy = FD->getType();
5088 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5089 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005090 for (const auto &AI : FT->param_types()) {
5091 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005092 Param->setScopeInfo(0, Params.size());
5093 Params.push_back(Param);
5094 }
David Blaikie9c70e042011-09-21 18:16:56 +00005095 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005096 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005097 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5098 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005099 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005100 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005101 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005102 if (VD->getQualifier()) {
5103 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005104 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005105 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005106 }
5107 return NewD;
5108}
5109
James Dennett634962f2012-06-14 21:40:34 +00005110/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005111/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005112void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005113 if (W.getUsed()) return; // only do this once
5114 W.setUsed(true);
5115 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5116 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005117 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005118 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5119 W.getLocation()));
5120 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005121 WeakTopLevelDecl.push_back(NewD);
5122 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5123 // to insert Decl at TU scope, sorry.
5124 DeclContext *SavedContext = CurContext;
5125 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005126 NewD->setDeclContext(CurContext);
5127 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005128 PushOnScopeChains(NewD, S);
5129 CurContext = SavedContext;
5130 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005131 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005132 }
5133}
5134
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005135void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5136 // It's valid to "forward-declare" #pragma weak, in which case we
5137 // have to do this.
5138 LoadExternalWeakUndeclaredIdentifiers();
5139 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005140 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005141 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5142 if (VD->isExternC())
5143 ND = VD;
5144 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5145 if (FD->isExternC())
5146 ND = FD;
5147 if (ND) {
5148 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005149 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005150 if (I != WeakUndeclaredIdentifiers.end()) {
5151 WeakInfo W = I->second;
5152 DeclApplyPragmaWeak(S, ND, W);
5153 WeakUndeclaredIdentifiers[Id] = W;
5154 }
5155 }
5156 }
5157 }
5158}
5159
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005160/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5161/// it, apply them to D. This is a bit tricky because PD can have attributes
5162/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005163void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005164 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005165 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005166 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005167
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005168 // Walk the declarator structure, applying decl attributes that were in a type
5169 // position to the decl itself. This handles cases like:
5170 // int *__attr__(x)** D;
5171 // when X is a decl attribute.
5172 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5173 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005174 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005175
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005176 // Finally, apply any attributes on the decl itself.
5177 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005178 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005179}
John McCall28a6aea2009-11-04 02:18:39 +00005180
John McCall31168b02011-06-15 23:02:42 +00005181/// Is the given declaration allowed to use a forbidden type?
5182static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5183 // Private ivars are always okay. Unfortunately, people don't
5184 // always properly make their ivars private, even in system headers.
5185 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005186 // Function declarations in sys headers will be marked unavailable.
5187 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5188 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005189 return false;
5190
5191 // Require it to be declared in a system header.
5192 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5193}
5194
5195/// Handle a delayed forbidden-type diagnostic.
5196static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5197 Decl *decl) {
5198 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00005199 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
5200 "this system declaration uses an unsupported type",
5201 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005202 return;
5203 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005204 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005205 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005206 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005207 // kind of forbidden type messages on unavailable functions.
5208 if (FD->hasAttr<UnavailableAttr>() &&
5209 diag.getForbiddenTypeDiagnostic() ==
5210 diag::err_arc_array_param_no_ownership) {
5211 diag.Triggered = true;
5212 return;
5213 }
5214 }
John McCall31168b02011-06-15 23:02:42 +00005215
5216 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5217 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5218 diag.Triggered = true;
5219}
5220
Aaron Ballmanfb237522014-10-15 15:37:51 +00005221
5222static bool isDeclDeprecated(Decl *D) {
5223 do {
5224 if (D->isDeprecated())
5225 return true;
5226 // A category implicitly has the availability of the interface.
5227 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005228 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5229 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005230 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5231 return false;
5232}
5233
5234static bool isDeclUnavailable(Decl *D) {
5235 do {
5236 if (D->isUnavailable())
5237 return true;
5238 // A category implicitly has the availability of the interface.
5239 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005240 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5241 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005242 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5243 return false;
5244}
5245
Nico Weber0055a192015-03-19 19:18:22 +00005246static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005247 Decl *Ctx, const NamedDecl *D,
5248 StringRef Message, SourceLocation Loc,
5249 const ObjCInterfaceDecl *UnknownObjCClass,
5250 const ObjCPropertyDecl *ObjCProperty,
5251 bool ObjCPropertyAccess) {
5252 // Diagnostics for deprecated or unavailable.
5253 unsigned diag, diag_message, diag_fwdclass_message;
5254
5255 // Matches 'diag::note_property_attribute' options.
5256 unsigned property_note_select;
5257
5258 // Matches diag::note_availability_specified_here.
5259 unsigned available_here_select_kind;
5260
5261 // Don't warn if our current context is deprecated or unavailable.
5262 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005263 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005264 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005265 return;
5266 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5267 : diag::warn_property_method_deprecated;
5268 diag_message = diag::warn_deprecated_message;
5269 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5270 property_note_select = /* deprecated */ 0;
5271 available_here_select_kind = /* deprecated */ 2;
5272 break;
5273
Nico Weber0055a192015-03-19 19:18:22 +00005274 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005275 if (isDeclUnavailable(Ctx))
5276 return;
5277 diag = !ObjCPropertyAccess ? diag::err_unavailable
5278 : diag::err_property_method_unavailable;
5279 diag_message = diag::err_unavailable_message;
5280 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5281 property_note_select = /* unavailable */ 1;
5282 available_here_select_kind = /* unavailable */ 0;
5283 break;
5284
Nico Weber0055a192015-03-19 19:18:22 +00005285 case Sema::AD_Partial:
5286 diag = diag::warn_partial_availability;
5287 diag_message = diag::warn_partial_message;
5288 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5289 property_note_select = /* partial */ 2;
5290 available_here_select_kind = /* partial */ 3;
5291 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005292 }
5293
Aaron Ballmanfb237522014-10-15 15:37:51 +00005294 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005295 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005296 if (ObjCProperty)
5297 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5298 << ObjCProperty->getDeclName() << property_note_select;
5299 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005300 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005301 if (ObjCProperty)
5302 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5303 << ObjCProperty->getDeclName() << property_note_select;
5304 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005305 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005306 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5307 }
5308
5309 S.Diag(D->getLocation(), diag::note_availability_specified_here)
5310 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005311 if (K == Sema::AD_Partial)
5312 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005313}
5314
5315static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5316 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005317 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5318 DD.Kind == DelayedDiagnostic::Unavailable);
5319 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5320 ? Sema::AD_Deprecation
5321 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005322 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005323 DoEmitAvailabilityWarning(
5324 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5325 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005326}
5327
John McCall2ec85372012-05-07 06:16:41 +00005328void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5329 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005330 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005331 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005332
John McCall2ec85372012-05-07 06:16:41 +00005333 // When delaying diagnostics to run in the context of a parsed
5334 // declaration, we only want to actually emit anything if parsing
5335 // succeeds.
5336 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005337
John McCall2ec85372012-05-07 06:16:41 +00005338 // We emit all the active diagnostics in this pool or any of its
5339 // parents. In general, we'll get one pool for the decl spec
5340 // and a child pool for each declarator; in a decl group like:
5341 // deprecated_typedef foo, *bar, baz();
5342 // only the declarator pops will be passed decls. This is correct;
5343 // we really do need to consider delayed diagnostics from the decl spec
5344 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005345 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005346 do {
John McCall6347b682012-05-07 06:16:58 +00005347 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005348 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5349 // This const_cast is a bit lame. Really, Triggered should be mutable.
5350 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005351 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005352 continue;
5353
John McCallc1465822011-02-14 07:13:47 +00005354 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005355 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005356 case DelayedDiagnostic::Unavailable:
5357 // Don't bother giving deprecation/unavailable diagnostics if
5358 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005359 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005360 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005361 break;
5362
5363 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005364 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005365 break;
John McCall31168b02011-06-15 23:02:42 +00005366
5367 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005368 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005369 break;
John McCall86121512010-01-27 03:50:35 +00005370 }
5371 }
John McCall2ec85372012-05-07 06:16:41 +00005372 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005373}
5374
John McCall6347b682012-05-07 06:16:58 +00005375/// Given a set of delayed diagnostics, re-emit them as if they had
5376/// been delayed in the current context instead of in the given pool.
5377/// Essentially, this just moves them to the current pool.
5378void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5379 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5380 assert(curPool && "re-emitting in undelayed context not supported");
5381 curPool->steal(pool);
5382}
5383
Ted Kremenekb79ee572013-12-18 23:30:06 +00005384void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5385 NamedDecl *D, StringRef Message,
5386 SourceLocation Loc,
5387 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005388 const ObjCPropertyDecl *ObjCProperty,
5389 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005390 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00005391 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005392 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5393 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5394 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005395 return;
5396 }
5397
Ted Kremenekb79ee572013-12-18 23:30:06 +00005398 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00005399 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
5400 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005401}