blob: 09cc8becca1a5fb016b4e81b9a89d1e671722603 [file] [log] [blame]
Chris Lattner6b6b5372008-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 McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindolad7a60ad2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1a343e22013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall19510852010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000032using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000034
John McCall883cc2c2011-03-02 12:29:23 +000035/// These constants match the enumerated choices of
36/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000037enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000038 ExpectedFunction,
39 ExpectedUnion,
40 ExpectedVariableOrFunction,
41 ExpectedFunctionOrMethod,
42 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000044 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000045 ExpectedFunctionMethodOrParameter,
46 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000047 ExpectedVariable,
48 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000049 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000050 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000051 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000052 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000053 ExpectedTLSVar,
54 ExpectedVariableOrField,
John McCalld4c3d662013-02-20 01:54:26 +000055 ExpectedVariableFieldOrTag,
Aaron Ballman37a89532013-07-18 14:56:42 +000056 ExpectedTypeOrNamespace,
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +000057 ExpectedObjectiveCInterface,
58 ExpectedMethodOrProperty
John McCall883cc2c2011-03-02 12:29:23 +000059};
60
Chris Lattnere5c5ee12008-06-29 00:16:31 +000061//===----------------------------------------------------------------------===//
62// Helper functions
63//===----------------------------------------------------------------------===//
64
Chandler Carruth87c44602011-07-01 23:49:12 +000065static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000066 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000067 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000068 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000069 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000070 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000071 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000072 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000073 Ty = decl->getUnderlyingType();
74 else
75 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000076
Chris Lattner6b6b5372008-06-26 18:38:35 +000077 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000078 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000079 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000080 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000081
John McCall183700f2009-09-21 23:43:11 +000082 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000083}
84
Daniel Dunbar35682492008-09-26 04:12:28 +000085// FIXME: We should provide an abstraction around a method or function
86// to provide the following bits of information.
87
Nuno Lopesd20254f2009-12-20 23:11:08 +000088/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000089/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000090static bool isFunction(const Decl *D) {
91 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000092}
93
94/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000095/// type (function or function-typed variable) or an Objective-C
96/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000097static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000098 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000101/// isFunctionOrMethodOrBlock - Return true if the given decl has function
102/// type (function or function-typed variable) or an Objective-C
103/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000104static bool isFunctionOrMethodOrBlock(const Decl *D) {
105 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000106 return true;
107 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000109 QualType Ty = V->getType();
110 return Ty->isBlockPointerType();
111 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000112 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000113}
114
John McCall711c52b2011-01-05 12:14:39 +0000115/// Return true if the given decl has a declarator that should have
116/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000117static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000118 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
120 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000121}
122
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000123/// hasFunctionProto - Return true if the given decl has a argument
124/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000125/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000126static bool hasFunctionProto(const Decl *D) {
127 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000128 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000129 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000131 return true;
132 }
133}
134
135/// getFunctionOrMethodNumArgs - Return number of function or method
136/// arguments. It is an error to call this on a K&R function (use
137/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000138static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
139 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000140 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000142 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000143 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000144}
145
Chandler Carruth87c44602011-07-01 23:49:12 +0000146static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
147 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000148 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000149 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000150 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Chandler Carruth87c44602011-07-01 23:49:12 +0000152 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000153}
154
Chandler Carruth87c44602011-07-01 23:49:12 +0000155static QualType getFunctionOrMethodResultType(const Decl *D) {
156 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000157 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000158 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isFunctionOrMethodVariadic(const Decl *D) {
162 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000163 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000164 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000165 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000166 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000167 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000168 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000169 }
170}
171
Chandler Carruth87c44602011-07-01 23:49:12 +0000172static bool isInstanceMethod(const Decl *D) {
173 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000174 return MethodDecl->isInstance();
175 return false;
176}
177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000179 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000180 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000181 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000182
John McCall506b57e2010-05-17 21:00:27 +0000183 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
184 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000185 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000186
John McCall506b57e2010-05-17 21:00:27 +0000187 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000188
Chris Lattner6b6b5372008-06-26 18:38:35 +0000189 // FIXME: Should we walk the chain of classes?
190 return ClsName == &Ctx.Idents.get("NSString") ||
191 ClsName == &Ctx.Idents.get("NSMutableString");
192}
193
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000195 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000196 if (!PT)
197 return false;
198
Ted Kremenek6217b802009-07-29 21:53:49 +0000199 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000200 if (!RT)
201 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000202
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000203 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000204 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000205 return false;
206
207 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
208}
209
Richard Smithddc2a532013-10-31 21:23:20 +0000210static unsigned getNumAttributeArgs(const AttributeList &Attr) {
211 // FIXME: Include the type in the argument list.
212 return Attr.getNumArgs() + Attr.hasParsedType();
213}
214
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000215/// \brief Check if the attribute has exactly as many args as Num. May
216/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000217static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithddc2a532013-10-31 21:23:20 +0000218 unsigned Num) {
219 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000220 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
221 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000222 return false;
223 }
224
225 return true;
226}
227
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000228
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000229/// \brief Check if the attribute has at least as many args as Num. May
230/// output an error.
231static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithddc2a532013-10-31 21:23:20 +0000232 unsigned Num) {
233 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
235 return false;
236 }
237
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000238 return true;
239}
240
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000241/// \brief Check if IdxExpr is a valid argument index for a function or
242/// instance method D. May output an error.
243///
244/// \returns true if IdxExpr is a valid index.
245static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
246 StringRef AttrName,
247 SourceLocation AttrLoc,
248 unsigned AttrArgNum,
249 const Expr *IdxExpr,
250 uint64_t &Idx)
251{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000252 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000253
254 // In C++ the implicit 'this' function parameter also counts.
255 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000256 bool HP = hasFunctionProto(D);
257 bool HasImplicitThisParam = isInstanceMethod(D);
258 bool IV = HP && isFunctionOrMethodVariadic(D);
259 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
260 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000261
262 llvm::APSInt IdxInt;
263 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
264 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000265 std::string Name = std::string("'") + AttrName.str() + std::string("'");
266 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000267 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000268 return false;
269 }
270
271 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000272 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000273 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
274 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
275 return false;
276 }
277 Idx--; // Convert to zero-based.
278 if (HasImplicitThisParam) {
279 if (Idx == 0) {
280 S.Diag(AttrLoc,
281 diag::err_attribute_invalid_implicit_this_argument)
282 << AttrName << IdxExpr->getSourceRange();
283 return false;
284 }
285 --Idx;
286 }
287
288 return true;
289}
290
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000291/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
292/// If not emit an error and return false. If the argument is an identifier it
293/// will emit an error with a fixit hint and treat it as if it was a string
294/// literal.
Tim Northoverf8aebef2013-10-01 14:34:18 +0000295bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
296 unsigned ArgNum, StringRef &Str,
Tim Northover64eac852013-10-01 14:34:25 +0000297 SourceLocation *ArgLocation) {
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000298 // Look for identifiers. If we have one emit a hint to fix it to a literal.
299 if (Attr.isArgIdent(ArgNum)) {
300 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northoverf8aebef2013-10-01 14:34:18 +0000301 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000302 << Attr.getName() << AANT_ArgumentString
303 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northoverf8aebef2013-10-01 14:34:18 +0000304 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000305 Str = Loc->Ident->getName();
306 if (ArgLocation)
307 *ArgLocation = Loc->Loc;
308 return true;
309 }
310
311 // Now check for an actual string literal.
312 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
313 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
314 if (ArgLocation)
315 *ArgLocation = ArgExpr->getLocStart();
316
317 if (!Literal || !Literal->isAscii()) {
Tim Northoverf8aebef2013-10-01 14:34:18 +0000318 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000319 << Attr.getName() << AANT_ArgumentString;
320 return false;
321 }
322
323 Str = Literal->getString();
324 return true;
325}
326
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000327///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000328/// \brief Check if passed in Decl is a field or potentially shared global var
329/// \return true if the Decl is a field or potentially shared global variable
330///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000331static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000332 if (isa<FieldDecl>(D))
333 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000334 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000335 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000336
337 return false;
338}
339
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000340/// \brief Check if the passed-in expression is of type int or bool.
341static bool isIntOrBool(Expr *Exp) {
342 QualType QT = Exp->getType();
343 return QT->isBooleanType() || QT->isIntegerType();
344}
345
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000346
347// Check to see if the type is a smart pointer of some kind. We assume
348// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000349static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
350 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
351 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000352 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000353 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000354
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000355 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
356 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000357 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000358 return false;
359
360 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000361}
362
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000363/// \brief Check if passed in Decl is a pointer type.
364/// Note that this function may produce an error message.
365/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000366static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
367 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000368 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000369 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000370 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000371 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000372
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000373 if (const RecordType *RT = QT->getAs<RecordType>()) {
374 // If it's an incomplete type, it could be a smart pointer; skip it.
375 // (We don't want to force template instantiation if we can avoid it,
376 // since that would alter the order in which templates are instantiated.)
377 if (RT->isIncompleteType())
378 return true;
379
380 if (threadSafetyCheckIsSmartPointer(S, RT))
381 return true;
382 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000383
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000384 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000385 << Attr.getName()->getName() << QT;
386 } else {
387 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
388 << Attr.getName();
389 }
390 return false;
391}
392
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000393/// \brief Checks that the passed in QualType either is of RecordType or points
394/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000395static const RecordType *getRecordType(QualType QT) {
396 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000397 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000398
399 // Now check if we point to record type.
400 if (const PointerType *PT = QT->getAs<PointerType>())
401 return PT->getPointeeType()->getAs<RecordType>();
402
403 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000404}
405
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000406
Jordy Rosefad5de92012-05-08 03:27:22 +0000407static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
408 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000409 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
410 if (RT->getDecl()->getAttr<LockableAttr>())
411 return true;
412 return false;
413}
414
415
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000416/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000417/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000418static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
419 QualType Ty) {
420 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000421
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000422 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000423 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000424 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000425 << Attr.getName() << Ty.getAsString();
426 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000427 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000428
Michael Hanf1aae3b2012-08-03 17:40:43 +0000429 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000430 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000431 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000432
433 // Allow smart pointers to be used as lockable objects.
434 // FIXME -- Check the type that the smart pointer points to.
435 if (threadSafetyCheckIsSmartPointer(S, RT))
436 return;
437
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000438 // Check if the type is lockable.
439 RecordDecl *RD = RT->getDecl();
440 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000441 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000442
443 // Else check if any base classes are lockable.
444 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
445 CXXBasePaths BPaths(false, false);
446 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
447 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000448 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000449
450 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
451 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000452}
453
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000454/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000455/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000456/// \param Sidx The attribute argument index to start checking with.
457/// \param ParamIdxOk Whether an argument can be indexing into a function
458/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000459static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000460 const AttributeList &Attr,
461 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 int Sidx = 0,
463 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000464 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000465 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000466
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000467 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000468 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000469 Args.push_back(ArgExp);
470 continue;
471 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000472
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000473 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000474 if (StrLit->getLength() == 0 ||
Benjamin Kramerf37e4f22013-09-13 16:30:12 +0000475 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000476 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000477 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000478 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000479 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000480 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000481
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000482 // We allow constant strings to be used as a placeholder for expressions
483 // that are not valid C++ syntax, but warn that they are ignored.
484 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
485 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000486 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000487 continue;
488 }
489
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000490 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000491
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000492 // A pointer to member expression of the form &MyClass::mu is treated
493 // specially -- we need to look at the type of the member.
494 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
495 if (UOp->getOpcode() == UO_AddrOf)
496 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
497 if (DRE->getDecl()->isCXXInstanceMember())
498 ArgTy = DRE->getDecl()->getType();
499
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000500 // First see if we can just cast to record type, or point to record type.
501 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000502
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000503 // Now check if we index into a record type function param.
504 if(!RT && ParamIdxOk) {
505 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000506 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
507 if(FD && IL) {
508 unsigned int NumParams = FD->getNumParams();
509 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000510 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
511 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
512 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
514 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000515 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000516 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000517 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000518 }
519 }
520
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000521 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000522
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000523 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000524 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000525}
526
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000527//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000528// Attribute Implementations
529//===----------------------------------------------------------------------===//
530
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000531// FIXME: All this manual attribute parsing code is gross. At the
532// least add some helper functions to check most argument patterns (#
533// and types of args).
534
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000535enum ThreadAttributeDeclKind {
536 ThreadExpectedFieldOrGlobalVar,
537 ThreadExpectedFunctionOrMethod,
538 ThreadExpectedClassOrStruct
539};
540
Michael Hanf1aae3b2012-08-03 17:40:43 +0000541static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000542 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000543 // D must be either a member field or global (potentially shared) variable.
544 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000545 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
546 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000547 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000548 }
549
Michael Handc691572012-07-23 18:48:41 +0000550 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000551}
552
Michael Handc691572012-07-23 18:48:41 +0000553static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
554 if (!checkGuardedVarAttrCommon(S, D, Attr))
555 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000556
Michael Han51d8c522013-01-24 16:46:58 +0000557 D->addAttr(::new (S.Context)
558 GuardedVarAttr(Attr.getRange(), S.Context,
559 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000560}
561
Michael Hanf1aae3b2012-08-03 17:40:43 +0000562static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000563 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000564 if (!checkGuardedVarAttrCommon(S, D, Attr))
565 return;
566
567 if (!threadSafetyCheckIsPointer(S, D, Attr))
568 return;
569
Michael Han51d8c522013-01-24 16:46:58 +0000570 D->addAttr(::new (S.Context)
571 PtGuardedVarAttr(Attr.getRange(), S.Context,
572 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000573}
574
Michael Hanf1aae3b2012-08-03 17:40:43 +0000575static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
576 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000577 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000578 // D must be either a member field or global (potentially shared) variable.
579 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000580 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
581 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000582 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000583 }
584
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000585 SmallVector<Expr*, 1> Args;
586 // check that all arguments are lockable objects
587 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
588 unsigned Size = Args.size();
589 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000590 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000591
Michael Handc691572012-07-23 18:48:41 +0000592 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000593
Michael Handc691572012-07-23 18:48:41 +0000594 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000595}
596
Michael Handc691572012-07-23 18:48:41 +0000597static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
598 Expr *Arg = 0;
599 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
600 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000601
Michael Handc691572012-07-23 18:48:41 +0000602 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
603}
604
Michael Hanf1aae3b2012-08-03 17:40:43 +0000605static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000606 const AttributeList &Attr) {
607 Expr *Arg = 0;
608 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
609 return;
610
611 if (!threadSafetyCheckIsPointer(S, D, Attr))
612 return;
613
614 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
615 S.Context, Arg));
616}
617
Michael Hanf1aae3b2012-08-03 17:40:43 +0000618static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000619 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000620 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000621 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000622 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
623 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000624 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000625 }
626
Michael Handc691572012-07-23 18:48:41 +0000627 return true;
628}
629
630static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
631 if (!checkLockableAttrCommon(S, D, Attr))
632 return;
633
634 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
635}
636
Michael Hanf1aae3b2012-08-03 17:40:43 +0000637static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000638 const AttributeList &Attr) {
639 if (!checkLockableAttrCommon(S, D, Attr))
640 return;
641
Michael Han51d8c522013-01-24 16:46:58 +0000642 D->addAttr(::new (S.Context)
643 ScopedLockableAttr(Attr.getRange(), S.Context,
644 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000645}
646
Kostya Serebryany85aee962013-02-26 06:58:27 +0000647static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
648 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000649 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000650 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
651 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000652 return;
653 }
654
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000655 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000656 S.Context));
657}
658
Kostya Serebryany85aee962013-02-26 06:58:27 +0000659static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000660 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000661 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000662 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000663 << Attr.getName() << ExpectedFunctionOrMethod;
664 return;
665 }
666
Michael Han51d8c522013-01-24 16:46:58 +0000667 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000668 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
669 Attr.getAttributeSpellingListIndex()));
670}
671
672static void handleNoSanitizeMemory(Sema &S, Decl *D,
673 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000674 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
676 << Attr.getName() << ExpectedFunctionOrMethod;
677 return;
678 }
679
680 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
681 S.Context));
682}
683
684static void handleNoSanitizeThread(Sema &S, Decl *D,
685 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000686 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
687 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
688 << Attr.getName() << ExpectedFunctionOrMethod;
689 return;
690 }
691
692 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
693 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000694}
695
Michael Hanf1aae3b2012-08-03 17:40:43 +0000696static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
697 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000698 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000699 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000700 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000701
702 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000703 ValueDecl *VD = dyn_cast<ValueDecl>(D);
704 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000705 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
706 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000707 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000708 }
709
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000710 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000711 QualType QT = VD->getType();
712 if (!QT->isDependentType()) {
713 const RecordType *RT = getRecordType(QT);
714 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000715 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000716 << Attr.getName();
717 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000718 }
719 }
720
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000721 // Check that all arguments are lockable objects.
722 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000723 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000724 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000725
Michael Handc691572012-07-23 18:48:41 +0000726 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000727}
728
Michael Hanf1aae3b2012-08-03 17:40:43 +0000729static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000730 const AttributeList &Attr) {
731 SmallVector<Expr*, 1> Args;
732 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
733 return;
734
735 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000736 D->addAttr(::new (S.Context)
737 AcquiredAfterAttr(Attr.getRange(), S.Context,
738 StartArg, Args.size(),
739 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000740}
741
Michael Hanf1aae3b2012-08-03 17:40:43 +0000742static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000743 const AttributeList &Attr) {
744 SmallVector<Expr*, 1> Args;
745 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
746 return;
747
748 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000749 D->addAttr(::new (S.Context)
750 AcquiredBeforeAttr(Attr.getRange(), S.Context,
751 StartArg, Args.size(),
752 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000753}
754
Michael Hanf1aae3b2012-08-03 17:40:43 +0000755static bool checkLockFunAttrCommon(Sema &S, Decl *D,
756 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000757 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000758 // zero or more arguments ok
759
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000760 // check that the attribute is applied to a function
761 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000762 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
763 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000764 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000765 }
766
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000767 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000768 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000769
Michael Handc691572012-07-23 18:48:41 +0000770 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000771}
772
Michael Hanf1aae3b2012-08-03 17:40:43 +0000773static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000774 const AttributeList &Attr) {
775 SmallVector<Expr*, 1> Args;
776 if (!checkLockFunAttrCommon(S, D, Attr, Args))
777 return;
778
779 unsigned Size = Args.size();
780 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000781 D->addAttr(::new (S.Context)
782 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
783 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000784}
785
Michael Hanf1aae3b2012-08-03 17:40:43 +0000786static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000787 const AttributeList &Attr) {
788 SmallVector<Expr*, 1> Args;
789 if (!checkLockFunAttrCommon(S, D, Attr, Args))
790 return;
791
792 unsigned Size = Args.size();
793 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000794 D->addAttr(::new (S.Context)
795 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
796 StartArg, Size,
797 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000798}
799
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000800static void handleAssertSharedLockAttr(Sema &S, Decl *D,
801 const AttributeList &Attr) {
802 SmallVector<Expr*, 1> Args;
803 if (!checkLockFunAttrCommon(S, D, Attr, Args))
804 return;
805
806 unsigned Size = Args.size();
807 Expr **StartArg = Size == 0 ? 0 : &Args[0];
808 D->addAttr(::new (S.Context)
809 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
810 Attr.getAttributeSpellingListIndex()));
811}
812
813static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
814 const AttributeList &Attr) {
815 SmallVector<Expr*, 1> Args;
816 if (!checkLockFunAttrCommon(S, D, Attr, Args))
817 return;
818
819 unsigned Size = Args.size();
820 Expr **StartArg = Size == 0 ? 0 : &Args[0];
821 D->addAttr(::new (S.Context)
822 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
823 StartArg, Size,
824 Attr.getAttributeSpellingListIndex()));
825}
826
827
Michael Hanf1aae3b2012-08-03 17:40:43 +0000828static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
829 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000830 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000831 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000832 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000833
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000834 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000835 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
836 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000837 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000838 }
839
Aaron Ballman624421f2013-08-31 01:11:41 +0000840 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000841 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000842 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000843 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000844 }
845
846 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000847 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000848
Michael Handc691572012-07-23 18:48:41 +0000849 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000850}
851
Michael Hanf1aae3b2012-08-03 17:40:43 +0000852static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000853 const AttributeList &Attr) {
854 SmallVector<Expr*, 2> Args;
855 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
856 return;
857
Michael Han51d8c522013-01-24 16:46:58 +0000858 D->addAttr(::new (S.Context)
859 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000860 Attr.getArgAsExpr(0),
861 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000862 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000863}
864
Michael Hanf1aae3b2012-08-03 17:40:43 +0000865static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000866 const AttributeList &Attr) {
867 SmallVector<Expr*, 2> Args;
868 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
869 return;
870
Michael Han51d8c522013-01-24 16:46:58 +0000871 D->addAttr(::new (S.Context)
872 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000873 Attr.getArgAsExpr(0),
874 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000875 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000876}
877
Michael Hanf1aae3b2012-08-03 17:40:43 +0000878static bool checkLocksRequiredCommon(Sema &S, Decl *D,
879 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000880 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000881 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000882 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000883
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000884 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000885 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
886 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000887 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000888 }
889
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000890 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000891 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000892 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000893 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000894
Michael Handc691572012-07-23 18:48:41 +0000895 return true;
896}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000897
Michael Hanf1aae3b2012-08-03 17:40:43 +0000898static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000899 const AttributeList &Attr) {
900 SmallVector<Expr*, 1> Args;
901 if (!checkLocksRequiredCommon(S, D, Attr, Args))
902 return;
903
904 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000905 D->addAttr(::new (S.Context)
906 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
907 StartArg, Args.size(),
908 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000909}
910
Michael Hanf1aae3b2012-08-03 17:40:43 +0000911static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000912 const AttributeList &Attr) {
913 SmallVector<Expr*, 1> Args;
914 if (!checkLocksRequiredCommon(S, D, Attr, Args))
915 return;
916
917 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000918 D->addAttr(::new (S.Context)
919 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
920 StartArg, Args.size(),
921 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000922}
923
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000924static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000925 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926 // zero or more arguments ok
927
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000928 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000929 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
930 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000931 return;
932 }
933
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000934 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000935 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000936 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000937 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000938 Expr **StartArg = Size == 0 ? 0 : &Args[0];
939
Michael Han51d8c522013-01-24 16:46:58 +0000940 D->addAttr(::new (S.Context)
941 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
942 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000943}
944
945static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000946 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000947 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000948 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
949 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000950 return;
951 }
952
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000953 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000954 SmallVector<Expr*, 1> Args;
955 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
956 unsigned Size = Args.size();
957 if (Size == 0)
958 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000959
Michael Han51d8c522013-01-24 16:46:58 +0000960 D->addAttr(::new (S.Context)
961 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
962 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000963}
964
965static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000966 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000967 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000968 return;
969
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000970 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000971 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
972 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000973 return;
974 }
975
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000976 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000977 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000978 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000979 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000980 if (Size == 0)
981 return;
982 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000983
Michael Han51d8c522013-01-24 16:46:58 +0000984 D->addAttr(::new (S.Context)
985 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
986 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000987}
988
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000989static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000990 ConsumableAttr::ConsumedState DefaultState;
991
992 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000993 IdentifierLoc *IL = Attr.getArgAsIdent(0);
994 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
995 DefaultState)) {
996 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
997 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +0000998 return;
999 }
David Blaikiea33ab602013-09-06 01:28:43 +00001000 } else {
1001 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1002 << Attr.getName() << AANT_ArgumentIdentifier;
1003 return;
1004 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001005
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001006 if (!isa<CXXRecordDecl>(D)) {
1007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1008 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001009 return;
1010 }
1011
1012 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +00001013 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001014 Attr.getAttributeSpellingListIndex()));
1015}
1016
1017static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1018 const AttributeList &Attr) {
1019 ASTContext &CurrContext = S.getASTContext();
1020 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1021
1022 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1023 if (!RD->hasAttr<ConsumableAttr>()) {
1024 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1025 RD->getNameAsString();
1026
1027 return false;
1028 }
1029 }
1030
1031 return true;
1032}
1033
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001034
DeLesley Hutchins66540852013-10-04 21:28:06 +00001035static void handleCallableWhenAttr(Sema &S, Decl *D,
1036 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001037 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1038 return;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001039
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001040 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001041 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1042 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001043 return;
1044 }
1045
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001046 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1047 return;
1048
DeLesley Hutchins66540852013-10-04 21:28:06 +00001049 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1050 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1051 CallableWhenAttr::ConsumedState CallableState;
1052
Aaron Ballman9025a182013-10-05 22:45:34 +00001053 StringRef StateString;
1054 SourceLocation Loc;
1055 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1056 return;
1057
1058 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00001059 CallableState)) {
Aaron Ballman9025a182013-10-05 22:45:34 +00001060 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1061 << Attr.getName() << StateString;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001062 return;
1063 }
Aaron Ballman9025a182013-10-05 22:45:34 +00001064
1065 States.push_back(CallableState);
DeLesley Hutchins66540852013-10-04 21:28:06 +00001066 }
1067
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001068 D->addAttr(::new (S.Context)
DeLesley Hutchins66540852013-10-04 21:28:06 +00001069 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1070 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001071}
1072
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001073
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00001074static void handleParamTypestateAttr(Sema &S, Decl *D,
1075 const AttributeList &Attr) {
1076 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1077
1078 if (!isa<ParmVarDecl>(D)) {
1079 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1080 Attr.getName() << ExpectedParameter;
1081 return;
1082 }
1083
1084 ParamTypestateAttr::ConsumedState ParamState;
1085
1086 if (Attr.isArgIdent(0)) {
1087 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1088 StringRef StateString = Ident->Ident->getName();
1089
1090 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1091 ParamState)) {
1092 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1093 << Attr.getName() << StateString;
1094 return;
1095 }
1096 } else {
1097 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1098 Attr.getName() << AANT_ArgumentIdentifier;
1099 return;
1100 }
1101
1102 // FIXME: This check is currently being done in the analysis. It can be
1103 // enabled here only after the parser propagates attributes at
1104 // template specialization definition, not declaration.
1105 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1106 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1107 //
1108 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1109 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1110 // ReturnType.getAsString();
1111 // return;
1112 //}
1113
1114 D->addAttr(::new (S.Context)
1115 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1116 Attr.getAttributeSpellingListIndex()));
1117}
1118
1119
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001120static void handleReturnTypestateAttr(Sema &S, Decl *D,
1121 const AttributeList &Attr) {
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001122 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1123
1124 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1125 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1126 Attr.getName() << ExpectedFunctionMethodOrParameter;
1127 return;
1128 }
1129
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001130 ReturnTypestateAttr::ConsumedState ReturnState;
1131
1132 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001133 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1134 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1135 ReturnState)) {
1136 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1137 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001138 return;
1139 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001140 } else {
1141 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1142 Attr.getName() << AANT_ArgumentIdentifier;
1143 return;
1144 }
1145
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001146 // FIXME: This check is currently being done in the analysis. It can be
1147 // enabled here only after the parser propagates attributes at
1148 // template specialization definition, not declaration.
1149 //QualType ReturnType;
1150 //
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001151 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1152 // ReturnType = Param->getType();
1153 //
1154 //} else if (const CXXConstructorDecl *Constructor =
1155 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001156 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1157 //
1158 //} else {
1159 //
1160 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1161 //}
1162 //
1163 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1164 //
1165 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1166 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1167 // ReturnType.getAsString();
1168 // return;
1169 //}
1170
1171 D->addAttr(::new (S.Context)
1172 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1173 Attr.getAttributeSpellingListIndex()));
1174}
1175
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001176
1177static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001178 if (!checkAttributeNumArgs(S, Attr, 1))
1179 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001180
1181 if (!isa<CXXMethodDecl>(D)) {
1182 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1183 Attr.getName() << ExpectedMethod;
1184 return;
1185 }
1186
1187 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1188 return;
1189
1190 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001191 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001192 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1193 StringRef Param = Ident->Ident->getName();
1194 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1195 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1196 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001197 return;
1198 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001199 } else {
1200 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1201 Attr.getName() << AANT_ArgumentIdentifier;
1202 return;
1203 }
1204
1205 D->addAttr(::new (S.Context)
1206 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1207 Attr.getAttributeSpellingListIndex()));
1208}
1209
Chris Wailes0e429f12013-10-29 20:28:41 +00001210static void handleTestTypestateAttr(Sema &S, Decl *D,
1211 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001212 if (!checkAttributeNumArgs(S, Attr, 1))
1213 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001214
1215 if (!isa<CXXMethodDecl>(D)) {
1216 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1217 Attr.getName() << ExpectedMethod;
1218 return;
1219 }
1220
1221 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1222 return;
1223
Chris Wailes0e429f12013-10-29 20:28:41 +00001224 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001225 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001226 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1227 StringRef Param = Ident->Ident->getName();
Chris Wailes0e429f12013-10-29 20:28:41 +00001228 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001229 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1230 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001231 return;
1232 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001233 } else {
1234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1235 Attr.getName() << AANT_ArgumentIdentifier;
1236 return;
1237 }
1238
1239 D->addAttr(::new (S.Context)
Chris Wailes0e429f12013-10-29 20:28:41 +00001240 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001241 Attr.getAttributeSpellingListIndex()));
1242}
1243
Chandler Carruth1b03c872011-07-02 00:01:44 +00001244static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1245 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001246 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1247 if (TD == 0) {
1248 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1249 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001250 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001251 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001252 }
Mike Stumpbf916502009-07-24 19:02:52 +00001253
Richard Smitha4fa9002013-01-13 02:11:23 +00001254 // Remember this typedef decl, we will need it later for diagnostics.
1255 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001256}
1257
Chandler Carruth1b03c872011-07-02 00:01:44 +00001258static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001259 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001260 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001261 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001262 // If the alignment is less than or equal to 8 bits, the packed attribute
1263 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001264 if (!FD->getType()->isDependentType() &&
1265 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001266 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001267 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001268 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001269 else
Michael Han51d8c522013-01-24 16:46:58 +00001270 FD->addAttr(::new (S.Context)
1271 PackedAttr(Attr.getRange(), S.Context,
1272 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001273 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001274 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001275}
1276
Chandler Carruth1b03c872011-07-02 00:01:44 +00001277static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001278 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001279 RD->addAttr(::new (S.Context)
1280 MsStructAttr(Attr.getRange(), S.Context,
1281 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001282 else
1283 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1284}
1285
Chandler Carruth1b03c872011-07-02 00:01:44 +00001286static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001287 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001288 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001289 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001290 D->addAttr(::new (S.Context)
1291 IBActionAttr(Attr.getRange(), S.Context,
1292 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001293 return;
1294 }
1295
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001296 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001297}
1298
Ted Kremenek2f041d02011-09-29 07:02:25 +00001299static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1300 // The IBOutlet/IBOutletCollection attributes only apply to instance
1301 // variables or properties of Objective-C classes. The outlet must also
1302 // have an object reference type.
1303 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1304 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001305 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001306 << Attr.getName() << VD->getType() << 0;
1307 return false;
1308 }
1309 }
1310 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1311 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001312 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001313 << Attr.getName() << PD->getType() << 1;
1314 return false;
1315 }
1316 }
1317 else {
1318 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1319 return false;
1320 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001321
Ted Kremenek2f041d02011-09-29 07:02:25 +00001322 return true;
1323}
1324
Chandler Carruth1b03c872011-07-02 00:01:44 +00001325static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001326 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001327 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001328
Michael Han51d8c522013-01-24 16:46:58 +00001329 D->addAttr(::new (S.Context)
1330 IBOutletAttr(Attr.getRange(), S.Context,
1331 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001332}
1333
Chandler Carruth1b03c872011-07-02 00:01:44 +00001334static void handleIBOutletCollection(Sema &S, Decl *D,
1335 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001336
1337 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001338 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1340 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001341 return;
1342 }
1343
Ted Kremenek2f041d02011-09-29 07:02:25 +00001344 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001345 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001346
Richard Smithd386fef2013-10-31 01:56:18 +00001347 ParsedType PT;
1348
1349 if (Attr.hasParsedType())
1350 PT = Attr.getTypeArg();
1351 else {
1352 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1353 S.getScopeForContext(D->getDeclContext()->getParent()));
1354 if (!PT) {
1355 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1356 return;
1357 }
Aaron Ballman624421f2013-08-31 01:11:41 +00001358 }
Richard Smithd386fef2013-10-31 01:56:18 +00001359
Richard Smithddc2a532013-10-31 21:23:20 +00001360 TypeSourceInfo *QTLoc = 0;
1361 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1362 if (!QTLoc)
1363 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithd386fef2013-10-31 01:56:18 +00001364
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001365 // Diagnose use of non-object type in iboutletcollection attribute.
1366 // FIXME. Gnu attribute extension ignores use of builtin types in
1367 // attributes. So, __attribute__((iboutletcollection(char))) will be
1368 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001369 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithd386fef2013-10-31 01:56:18 +00001370 S.Diag(Attr.getLoc(),
1371 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1372 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001373 return;
1374 }
Richard Smithd386fef2013-10-31 01:56:18 +00001375
Michael Han51d8c522013-01-24 16:46:58 +00001376 D->addAttr(::new (S.Context)
Richard Smithddc2a532013-10-31 21:23:20 +00001377 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han51d8c522013-01-24 16:46:58 +00001378 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001379}
1380
Chandler Carruthd309c812011-07-01 23:49:16 +00001381static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001382 if (const RecordType *UT = T->getAsUnionType())
1383 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1384 RecordDecl *UD = UT->getDecl();
1385 for (RecordDecl::field_iterator it = UD->field_begin(),
1386 itend = UD->field_end(); it != itend; ++it) {
1387 QualType QT = it->getType();
1388 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1389 T = QT;
1390 return;
1391 }
1392 }
1393 }
1394}
1395
Nuno Lopes587de5b2012-05-24 00:22:00 +00001396static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001397 if (!isFunctionOrMethod(D)) {
1398 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001399 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001400 return;
1401 }
1402
Nuno Lopes587de5b2012-05-24 00:22:00 +00001403 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1404 return;
1405
Nuno Lopes587de5b2012-05-24 00:22:00 +00001406 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001407 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001408 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001409 uint64_t Idx;
1410 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1411 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001412 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001413
1414 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001415 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001416 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001417 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1418 << Attr.getName() << AANT_ArgumentIntegerConstant
1419 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001420 return;
1421 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001422 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001423 }
1424
1425 // check if the function returns a pointer
1426 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1427 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001428 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001429 }
1430
Michael Han51d8c522013-01-24 16:46:58 +00001431 D->addAttr(::new (S.Context)
1432 AllocSizeAttr(Attr.getRange(), S.Context,
1433 SizeArgs.data(), SizeArgs.size(),
1434 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001435}
1436
Chandler Carruth1b03c872011-07-02 00:01:44 +00001437static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001438 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1439 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001440 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001441 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001442 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001443 return;
1444 }
Mike Stumpbf916502009-07-24 19:02:52 +00001445
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001446 SmallVector<unsigned, 8> NonNullArgs;
1447 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001448 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001449 uint64_t Idx;
1450 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1451 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001452 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001453
1454 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001455 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001456 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001457
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001458 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001459 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001460 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001461 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001462 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001463 }
Mike Stumpbf916502009-07-24 19:02:52 +00001464
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001465 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001466 }
Mike Stumpbf916502009-07-24 19:02:52 +00001467
1468 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1469 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001470 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001471 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1472 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001473 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001474 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001475 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001476 }
Mike Stumpbf916502009-07-24 19:02:52 +00001477
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001478 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001479 if (NonNullArgs.empty()) {
1480 // Warn the trivial case only if attribute is not coming from a
1481 // macro instantiation.
1482 if (Attr.getLoc().isFileID())
1483 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001484 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001485 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001486 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001487
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001488 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001489 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001490 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001491 D->addAttr(::new (S.Context)
1492 NonNullAttr(Attr.getRange(), S.Context, start, size,
1493 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001494}
1495
Aaron Ballmane1668a32013-09-16 18:11:41 +00001496static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1497 switch (K) {
1498 case OwnershipAttr::Holds: return "'ownership_holds'";
1499 case OwnershipAttr::Takes: return "'ownership_takes'";
1500 case OwnershipAttr::Returns: return "'ownership_returns'";
1501 }
1502 llvm_unreachable("unknown ownership");
1503}
1504
Chandler Carruth1b03c872011-07-02 00:01:44 +00001505static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001506 // This attribute must be applied to a function declaration. The first
1507 // argument to the attribute must be an identifier, the name of the resource,
1508 // for example: malloc. The following arguments must be argument indexes, the
1509 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001510 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballmane1668a32013-09-16 18:11:41 +00001511 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose2a479922010-08-12 08:54:03 +00001512 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001513
Aaron Ballman624421f2013-08-31 01:11:41 +00001514 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001515 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmane1668a32013-09-16 18:11:41 +00001516 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001517 return;
1518 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001519
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001520 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001521 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001522 switch (AL.getKind()) {
1523 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001524 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001525 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001526 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001527 return;
1528 }
Jordy Rose2a479922010-08-12 08:54:03 +00001529 break;
1530 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001531 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001532 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001533 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001534 return;
1535 }
Jordy Rose2a479922010-08-12 08:54:03 +00001536 break;
1537 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001538 K = OwnershipAttr::Returns;
Aaron Ballmane1668a32013-09-16 18:11:41 +00001539
Aaron Ballman624421f2013-08-31 01:11:41 +00001540 if (AL.getNumArgs() > 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001541 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001542 return;
1543 }
Jordy Rose2a479922010-08-12 08:54:03 +00001544 break;
1545 default:
1546 // This should never happen given how we are called.
1547 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001548 }
1549
Chandler Carruth87c44602011-07-01 23:49:12 +00001550 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001551 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1552 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001553 return;
1554 }
1555
Aaron Ballman624421f2013-08-31 01:11:41 +00001556 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001557
1558 // Normalize the argument, __foo__ becomes foo.
1559 if (Module.startswith("__") && Module.endswith("__"))
1560 Module = Module.substr(2, Module.size() - 4);
1561
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001562 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001563 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1564 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001565 uint64_t Idx;
1566 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001567 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001568 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001569
Aaron Ballmane1668a32013-09-16 18:11:41 +00001570 // Is the function argument a pointer type?
1571 QualType T = getFunctionOrMethodArgType(D, Idx);
1572 int Err = -1; // No error
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001573 switch (K) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001574 case OwnershipAttr::Takes:
1575 case OwnershipAttr::Holds:
1576 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1577 Err = 0;
1578 break;
1579 case OwnershipAttr::Returns:
1580 if (!T->isIntegerType())
1581 Err = 1;
1582 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001583 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001584 if (-1 != Err) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001585 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballmane1668a32013-09-16 18:11:41 +00001586 << Ex->getSourceRange();
1587 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001588 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001589
1590 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001591 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballmane1668a32013-09-16 18:11:41 +00001592 i = D->specific_attr_begin<OwnershipAttr>(),
1593 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1594 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1595 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1596 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1597 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1598 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001599 }
1600 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001601 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001602 }
1603
1604 unsigned* start = OwnershipArgs.data();
1605 unsigned size = OwnershipArgs.size();
1606 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001607
Michael Han51d8c522013-01-24 16:46:58 +00001608 D->addAttr(::new (S.Context)
1609 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1610 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001611}
1612
Chandler Carruth1b03c872011-07-02 00:01:44 +00001613static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001614 // Check the attribute arguments.
1615 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1617 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001618 return;
1619 }
1620
Chandler Carruth87c44602011-07-01 23:49:12 +00001621 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001622 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001623 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001624 return;
1625 }
1626
Chandler Carruth87c44602011-07-01 23:49:12 +00001627 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001628
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001629 // gcc rejects
1630 // class c {
1631 // static int a __attribute__((weakref ("v2")));
1632 // static int b() __attribute__((weakref ("f3")));
1633 // };
1634 // and ignores the attributes of
1635 // void f(void) {
1636 // static int a __attribute__((weakref ("v2")));
1637 // }
1638 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001639 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001640 if (!Ctx->isFileContext()) {
1641 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001642 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001643 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001644 }
1645
1646 // The GCC manual says
1647 //
1648 // At present, a declaration to which `weakref' is attached can only
1649 // be `static'.
1650 //
1651 // It also says
1652 //
1653 // Without a TARGET,
1654 // given as an argument to `weakref' or to `alias', `weakref' is
1655 // equivalent to `weak'.
1656 //
1657 // gcc 4.4.1 will accept
1658 // int a7 __attribute__((weakref));
1659 // as
1660 // int a7 __attribute__((weak));
1661 // This looks like a bug in gcc. We reject that for now. We should revisit
1662 // it if this behaviour is actually used.
1663
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001664 // GCC rejects
1665 // static ((alias ("y"), weakref)).
1666 // Should we? How to check that weakref is before or after alias?
1667
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001668 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1669 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1670 // StringRef parameter it was given anyway.
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001671 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001672 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001673 // GCC will accept anything as the argument of weakref. Should we
1674 // check for an existing decl?
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001675 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1676 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001677
Michael Han51d8c522013-01-24 16:46:58 +00001678 D->addAttr(::new (S.Context)
1679 WeakRefAttr(Attr.getRange(), S.Context,
1680 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001681}
1682
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001683static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1684 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001685 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001686 return;
1687
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001688 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001689 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1690 return;
1691 }
1692
Chris Lattner6b6b5372008-06-26 18:38:35 +00001693 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001694
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001695 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001696 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001697}
1698
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001699static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001700 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1701 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1702 << Attr.getName() << ExpectedFunctionOrMethod;
1703 return;
1704 }
1705
Michael Han51d8c522013-01-24 16:46:58 +00001706 D->addAttr(::new (S.Context)
1707 MinSizeAttr(Attr.getRange(), S.Context,
1708 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001709}
1710
Benjamin Krameree409a92012-05-12 21:10:52 +00001711static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001712 if (!isa<FunctionDecl>(D)) {
1713 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1714 << Attr.getName() << ExpectedFunction;
1715 return;
1716 }
1717
1718 if (D->hasAttr<HotAttr>()) {
1719 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1720 << Attr.getName() << "hot";
1721 return;
1722 }
1723
Michael Han51d8c522013-01-24 16:46:58 +00001724 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1725 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001726}
1727
1728static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001729 if (!isa<FunctionDecl>(D)) {
1730 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1731 << Attr.getName() << ExpectedFunction;
1732 return;
1733 }
1734
1735 if (D->hasAttr<ColdAttr>()) {
1736 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1737 << Attr.getName() << "cold";
1738 return;
1739 }
1740
Michael Han51d8c522013-01-24 16:46:58 +00001741 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1742 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001743}
1744
Chandler Carruth1b03c872011-07-02 00:01:44 +00001745static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001746 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001747 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001748 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001749 return;
1750 }
1751
Michael Han51d8c522013-01-24 16:46:58 +00001752 D->addAttr(::new (S.Context)
1753 NakedAttr(Attr.getRange(), S.Context,
1754 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001755}
1756
Chandler Carruth1b03c872011-07-02 00:01:44 +00001757static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1758 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001759 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001760 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001761 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001762 return;
1763 }
Mike Stumpbf916502009-07-24 19:02:52 +00001764
Michael Han51d8c522013-01-24 16:46:58 +00001765 D->addAttr(::new (S.Context)
1766 AlwaysInlineAttr(Attr.getRange(), S.Context,
1767 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001768}
1769
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001770static void handleTLSModelAttr(Sema &S, Decl *D,
1771 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001772 StringRef Model;
1773 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001774 // Check that it is a string.
Tim Northoverf8aebef2013-10-01 14:34:18 +00001775 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001776 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001777
Richard Smith38afbc72013-04-13 02:43:54 +00001778 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001779 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1780 << Attr.getName() << ExpectedTLSVar;
1781 return;
1782 }
1783
1784 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001785 if (Model != "global-dynamic" && Model != "local-dynamic"
1786 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001787 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001788 return;
1789 }
1790
Michael Han51d8c522013-01-24 16:46:58 +00001791 D->addAttr(::new (S.Context)
1792 TLSModelAttr(Attr.getRange(), S.Context, Model,
1793 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001794}
1795
Chandler Carruth1b03c872011-07-02 00:01:44 +00001796static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001797 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001798 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001799 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001800 D->addAttr(::new (S.Context)
1801 MallocAttr(Attr.getRange(), S.Context,
1802 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001803 return;
1804 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001805 }
1806
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001807 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001808}
1809
Chandler Carruth1b03c872011-07-02 00:01:44 +00001810static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001811 D->addAttr(::new (S.Context)
1812 MayAliasAttr(Attr.getRange(), S.Context,
1813 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001814}
1815
Chandler Carruth1b03c872011-07-02 00:01:44 +00001816static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001817 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001818 D->addAttr(::new (S.Context)
1819 NoCommonAttr(Attr.getRange(), S.Context,
1820 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001821 else
1822 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001823 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001824}
1825
Chandler Carruth1b03c872011-07-02 00:01:44 +00001826static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001827 if (S.LangOpts.CPlusPlus) {
1828 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1829 return;
1830 }
1831
Chandler Carruth87c44602011-07-01 23:49:12 +00001832 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001833 D->addAttr(::new (S.Context)
1834 CommonAttr(Attr.getRange(), S.Context,
1835 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001836 else
1837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001838 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001839}
1840
Chandler Carruth1b03c872011-07-02 00:01:44 +00001841static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001842 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001843
1844 if (S.CheckNoReturnAttr(attr)) return;
1845
Chandler Carruth87c44602011-07-01 23:49:12 +00001846 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001847 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001848 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001849 return;
1850 }
1851
Michael Han51d8c522013-01-24 16:46:58 +00001852 D->addAttr(::new (S.Context)
1853 NoReturnAttr(attr.getRange(), S.Context,
1854 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001855}
1856
1857bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001858 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001859 attr.setInvalid();
1860 return true;
1861 }
1862
1863 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001864}
1865
Chandler Carruth1b03c872011-07-02 00:01:44 +00001866static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1867 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001868
1869 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1870 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001871 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1872 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001873 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1874 && !VD->getType()->isFunctionPointerType())) {
1875 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001876 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001877 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001878 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001879 return;
1880 }
1881 }
1882
Michael Han51d8c522013-01-24 16:46:58 +00001883 D->addAttr(::new (S.Context)
1884 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1885 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001886}
1887
Richard Smithcd8ab512013-01-17 01:30:42 +00001888static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1889 const AttributeList &Attr) {
1890 // C++11 [dcl.attr.noreturn]p1:
1891 // The attribute may be applied to the declarator-id in a function
1892 // declaration.
1893 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1894 if (!FD) {
1895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1896 << Attr.getName() << ExpectedFunctionOrMethod;
1897 return;
1898 }
1899
Michael Han51d8c522013-01-24 16:46:58 +00001900 D->addAttr(::new (S.Context)
1901 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1902 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001903}
1904
John Thompson35cc9622010-08-09 21:53:52 +00001905// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001906static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001907/*
1908 Returning a Vector Class in Registers
1909
Eric Christopherf48f3672010-12-01 22:13:54 +00001910 According to the PPU ABI specifications, a class with a single member of
1911 vector type is returned in memory when used as the return value of a function.
1912 This results in inefficient code when implementing vector classes. To return
1913 the value in a single vector register, add the vecreturn attribute to the
1914 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001915
1916 Example:
1917
1918 struct Vector
1919 {
1920 __vector float xyzw;
1921 } __attribute__((vecreturn));
1922
1923 Vector Add(Vector lhs, Vector rhs)
1924 {
1925 Vector result;
1926 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1927 return result; // This will be returned in a register
1928 }
1929*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001930 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001931 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001932 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001933 return;
1934 }
1935
Chandler Carruth87c44602011-07-01 23:49:12 +00001936 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001937 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1938 return;
1939 }
1940
Chandler Carruth87c44602011-07-01 23:49:12 +00001941 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001942 int count = 0;
1943
1944 if (!isa<CXXRecordDecl>(record)) {
1945 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1946 return;
1947 }
1948
1949 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1950 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1951 return;
1952 }
1953
Eric Christopherf48f3672010-12-01 22:13:54 +00001954 for (RecordDecl::field_iterator iter = record->field_begin();
1955 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001956 if ((count == 1) || !iter->getType()->isVectorType()) {
1957 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1958 return;
1959 }
1960 count++;
1961 }
1962
Michael Han51d8c522013-01-24 16:46:58 +00001963 D->addAttr(::new (S.Context)
1964 VecReturnAttr(Attr.getRange(), S.Context,
1965 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001966}
1967
Richard Smith3a2b7a12013-01-28 22:42:45 +00001968static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1969 const AttributeList &Attr) {
1970 if (isa<ParmVarDecl>(D)) {
1971 // [[carries_dependency]] can only be applied to a parameter if it is a
1972 // parameter of a function declaration or lambda.
1973 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1974 S.Diag(Attr.getLoc(),
1975 diag::err_carries_dependency_param_not_function_decl);
1976 return;
1977 }
1978 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001980 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001981 return;
1982 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001983
1984 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1985 Attr.getRange(), S.Context,
1986 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001987}
1988
Chandler Carruth1b03c872011-07-02 00:01:44 +00001989static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001990 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001991 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001992 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001993 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001994 return;
1995 }
Mike Stumpbf916502009-07-24 19:02:52 +00001996
Michael Han51d8c522013-01-24 16:46:58 +00001997 D->addAttr(::new (S.Context)
1998 UnusedAttr(Attr.getRange(), S.Context,
1999 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00002000}
2001
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002002static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2003 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002004 if (!isa<FunctionDecl>(D)) {
2005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2006 << Attr.getName() << ExpectedFunction;
2007 return;
2008 }
2009
Michael Han51d8c522013-01-24 16:46:58 +00002010 D->addAttr(::new (S.Context)
2011 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2012 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002013}
2014
Chandler Carruth1b03c872011-07-02 00:01:44 +00002015static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002016 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00002017 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002018 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2019 return;
2020 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002021 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002023 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002024 return;
2025 }
Mike Stumpbf916502009-07-24 19:02:52 +00002026
Michael Han51d8c522013-01-24 16:46:58 +00002027 D->addAttr(::new (S.Context)
2028 UsedAttr(Attr.getRange(), S.Context,
2029 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002030}
2031
Chandler Carruth1b03c872011-07-02 00:01:44 +00002032static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002033 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002034 if (Attr.getNumArgs() > 1) {
2035 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002036 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002037 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002038
2039 int priority = 65535; // FIXME: Do not hardcode such constants.
2040 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002041 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002042 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002043 if (E->isTypeDependent() || E->isValueDependent() ||
2044 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002045 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002046 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002047 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002048 return;
2049 }
2050 priority = Idx.getZExtValue();
2051 }
Mike Stumpbf916502009-07-24 19:02:52 +00002052
Chandler Carruth87c44602011-07-01 23:49:12 +00002053 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002054 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002055 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002056 return;
2057 }
2058
Michael Han51d8c522013-01-24 16:46:58 +00002059 D->addAttr(::new (S.Context)
2060 ConstructorAttr(Attr.getRange(), S.Context, priority,
2061 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002062}
2063
Chandler Carruth1b03c872011-07-02 00:01:44 +00002064static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002065 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002066 if (Attr.getNumArgs() > 1) {
2067 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002068 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002069 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002070
2071 int priority = 65535; // FIXME: Do not hardcode such constants.
2072 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002073 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002074 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002075 if (E->isTypeDependent() || E->isValueDependent() ||
2076 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002078 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002079 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002080 return;
2081 }
2082 priority = Idx.getZExtValue();
2083 }
Mike Stumpbf916502009-07-24 19:02:52 +00002084
Chandler Carruth87c44602011-07-01 23:49:12 +00002085 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002086 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002087 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002088 return;
2089 }
2090
Michael Han51d8c522013-01-24 16:46:58 +00002091 D->addAttr(::new (S.Context)
2092 DestructorAttr(Attr.getRange(), S.Context, priority,
2093 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002094}
2095
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002096template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002097static void handleAttrWithMessage(Sema &S, Decl *D,
2098 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002099 unsigned NumArgs = Attr.getNumArgs();
2100 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002101 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002102 return;
2103 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002104
2105 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002106 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002107 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002108 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002109
Michael Han51d8c522013-01-24 16:46:58 +00002110 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2111 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002112}
2113
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002114static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2115 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002116 D->addAttr(::new (S.Context)
2117 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2118 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002119}
2120
Patrick Beardb2f68202012-04-06 18:12:22 +00002121static void handleObjCRootClassAttr(Sema &S, Decl *D,
2122 const AttributeList &Attr) {
2123 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002124 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2125 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002126 return;
2127 }
2128
Michael Han51d8c522013-01-24 16:46:58 +00002129 D->addAttr(::new (S.Context)
2130 ObjCRootClassAttr(Attr.getRange(), S.Context,
2131 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002132}
2133
Michael Han51d8c522013-01-24 16:46:58 +00002134static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2135 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002136 if (!isa<ObjCInterfaceDecl>(D)) {
2137 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2138 return;
2139 }
2140
Michael Han51d8c522013-01-24 16:46:58 +00002141 D->addAttr(::new (S.Context)
2142 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2143 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002144}
2145
Jordy Rosefad5de92012-05-08 03:27:22 +00002146static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2147 IdentifierInfo *Platform,
2148 VersionTuple Introduced,
2149 VersionTuple Deprecated,
2150 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002151 StringRef PlatformName
2152 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2153 if (PlatformName.empty())
2154 PlatformName = Platform->getName();
2155
2156 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2157 // of these steps are needed).
2158 if (!Introduced.empty() && !Deprecated.empty() &&
2159 !(Introduced <= Deprecated)) {
2160 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2161 << 1 << PlatformName << Deprecated.getAsString()
2162 << 0 << Introduced.getAsString();
2163 return true;
2164 }
2165
2166 if (!Introduced.empty() && !Obsoleted.empty() &&
2167 !(Introduced <= Obsoleted)) {
2168 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2169 << 2 << PlatformName << Obsoleted.getAsString()
2170 << 0 << Introduced.getAsString();
2171 return true;
2172 }
2173
2174 if (!Deprecated.empty() && !Obsoleted.empty() &&
2175 !(Deprecated <= Obsoleted)) {
2176 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2177 << 2 << PlatformName << Obsoleted.getAsString()
2178 << 1 << Deprecated.getAsString();
2179 return true;
2180 }
2181
2182 return false;
2183}
2184
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002185/// \brief Check whether the two versions match.
2186///
2187/// If either version tuple is empty, then they are assumed to match. If
2188/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2189static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2190 bool BeforeIsOkay) {
2191 if (X.empty() || Y.empty())
2192 return true;
2193
2194 if (X == Y)
2195 return true;
2196
2197 if (BeforeIsOkay && X < Y)
2198 return true;
2199
2200 return false;
2201}
2202
Rafael Espindola51be6e32013-01-08 22:04:34 +00002203AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002204 IdentifierInfo *Platform,
2205 VersionTuple Introduced,
2206 VersionTuple Deprecated,
2207 VersionTuple Obsoleted,
2208 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002209 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002210 bool Override,
2211 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002212 VersionTuple MergedIntroduced = Introduced;
2213 VersionTuple MergedDeprecated = Deprecated;
2214 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002215 bool FoundAny = false;
2216
Rafael Espindola98ae8342012-05-10 02:50:16 +00002217 if (D->hasAttrs()) {
2218 AttrVec &Attrs = D->getAttrs();
2219 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2220 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2221 if (!OldAA) {
2222 ++i;
2223 continue;
2224 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002225
Rafael Espindola98ae8342012-05-10 02:50:16 +00002226 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2227 if (OldPlatform != Platform) {
2228 ++i;
2229 continue;
2230 }
2231
2232 FoundAny = true;
2233 VersionTuple OldIntroduced = OldAA->getIntroduced();
2234 VersionTuple OldDeprecated = OldAA->getDeprecated();
2235 VersionTuple OldObsoleted = OldAA->getObsoleted();
2236 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002237
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002238 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2239 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2240 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2241 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002242 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002243 if (Override) {
2244 int Which = -1;
2245 VersionTuple FirstVersion;
2246 VersionTuple SecondVersion;
2247 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2248 Which = 0;
2249 FirstVersion = OldIntroduced;
2250 SecondVersion = Introduced;
2251 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2252 Which = 1;
2253 FirstVersion = Deprecated;
2254 SecondVersion = OldDeprecated;
2255 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2256 Which = 2;
2257 FirstVersion = Obsoleted;
2258 SecondVersion = OldObsoleted;
2259 }
2260
2261 if (Which == -1) {
2262 Diag(OldAA->getLocation(),
2263 diag::warn_mismatched_availability_override_unavail)
2264 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2265 } else {
2266 Diag(OldAA->getLocation(),
2267 diag::warn_mismatched_availability_override)
2268 << Which
2269 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2270 << FirstVersion.getAsString() << SecondVersion.getAsString();
2271 }
2272 Diag(Range.getBegin(), diag::note_overridden_method);
2273 } else {
2274 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2275 Diag(Range.getBegin(), diag::note_previous_attribute);
2276 }
2277
Rafael Espindola98ae8342012-05-10 02:50:16 +00002278 Attrs.erase(Attrs.begin() + i);
2279 --e;
2280 continue;
2281 }
2282
2283 VersionTuple MergedIntroduced2 = MergedIntroduced;
2284 VersionTuple MergedDeprecated2 = MergedDeprecated;
2285 VersionTuple MergedObsoleted2 = MergedObsoleted;
2286
2287 if (MergedIntroduced2.empty())
2288 MergedIntroduced2 = OldIntroduced;
2289 if (MergedDeprecated2.empty())
2290 MergedDeprecated2 = OldDeprecated;
2291 if (MergedObsoleted2.empty())
2292 MergedObsoleted2 = OldObsoleted;
2293
2294 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2295 MergedIntroduced2, MergedDeprecated2,
2296 MergedObsoleted2)) {
2297 Attrs.erase(Attrs.begin() + i);
2298 --e;
2299 continue;
2300 }
2301
2302 MergedIntroduced = MergedIntroduced2;
2303 MergedDeprecated = MergedDeprecated2;
2304 MergedObsoleted = MergedObsoleted2;
2305 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002306 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002307 }
2308
2309 if (FoundAny &&
2310 MergedIntroduced == Introduced &&
2311 MergedDeprecated == Deprecated &&
2312 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002313 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002314
Ted Kremenekcb344392013-04-06 00:34:27 +00002315 // Only create a new attribute if !Override, but we want to do
2316 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002317 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002318 MergedDeprecated, MergedObsoleted) &&
2319 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002320 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2321 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002322 Obsoleted, IsUnavailable, Message,
2323 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002324 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002325 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002326}
2327
Chandler Carruth1b03c872011-07-02 00:01:44 +00002328static void handleAvailabilityAttr(Sema &S, Decl *D,
2329 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002330 if (!checkAttributeNumArgs(S, Attr, 1))
2331 return;
2332 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002333 unsigned Index = Attr.getAttributeSpellingListIndex();
2334
Aaron Ballman624421f2013-08-31 01:11:41 +00002335 IdentifierInfo *II = Platform->Ident;
2336 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2337 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2338 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002339
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002340 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2341 if (!ND) {
2342 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2343 return;
2344 }
2345
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002346 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2347 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2348 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002349 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002350 StringRef Str;
Benjamin Kramerc5617142013-09-13 17:31:48 +00002351 if (const StringLiteral *SE =
2352 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002353 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002354
Aaron Ballman624421f2013-08-31 01:11:41 +00002355 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002356 Introduced.Version,
2357 Deprecated.Version,
2358 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002359 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002360 /*Override=*/false,
2361 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002362 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002363 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002364}
2365
John McCalld4c3d662013-02-20 01:54:26 +00002366template <class T>
2367static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2368 typename T::VisibilityType value,
2369 unsigned attrSpellingListIndex) {
2370 T *existingAttr = D->getAttr<T>();
2371 if (existingAttr) {
2372 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2373 if (existingValue == value)
2374 return NULL;
2375 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2376 S.Diag(range.getBegin(), diag::note_previous_attribute);
2377 D->dropAttr<T>();
2378 }
2379 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2380}
2381
Rafael Espindola599f1b72012-05-13 03:25:18 +00002382VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002383 VisibilityAttr::VisibilityType Vis,
2384 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002385 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2386 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002387}
2388
John McCalld4c3d662013-02-20 01:54:26 +00002389TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2390 TypeVisibilityAttr::VisibilityType Vis,
2391 unsigned AttrSpellingListIndex) {
2392 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2393 AttrSpellingListIndex);
2394}
2395
2396static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2397 bool isTypeVisibility) {
2398 // Visibility attributes don't mean anything on a typedef.
2399 if (isa<TypedefNameDecl>(D)) {
2400 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2401 << Attr.getName();
2402 return;
2403 }
2404
2405 // 'type_visibility' can only go on a type or namespace.
2406 if (isTypeVisibility &&
2407 !(isa<TagDecl>(D) ||
2408 isa<ObjCInterfaceDecl>(D) ||
2409 isa<NamespaceDecl>(D))) {
2410 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2411 << Attr.getName() << ExpectedTypeOrNamespace;
2412 return;
2413 }
2414
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002415 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002416 StringRef TypeStr;
2417 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002418 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002419 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002420
Sean Huntcf807c42010-08-18 23:23:40 +00002421 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002422 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002423 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002424 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002425 return;
2426 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002427
2428 // Complain about attempts to use protected visibility on targets
2429 // (like Darwin) that don't support it.
2430 if (type == VisibilityAttr::Protected &&
2431 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2432 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2433 type = VisibilityAttr::Default;
2434 }
Mike Stumpbf916502009-07-24 19:02:52 +00002435
Michael Han51d8c522013-01-24 16:46:58 +00002436 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002437 clang::Attr *newAttr;
2438 if (isTypeVisibility) {
2439 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2440 (TypeVisibilityAttr::VisibilityType) type,
2441 Index);
2442 } else {
2443 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2444 }
2445 if (newAttr)
2446 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002447}
2448
Chandler Carruth1b03c872011-07-02 00:01:44 +00002449static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2450 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002451 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2452 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002454 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002455 return;
2456 }
2457
Aaron Ballman624421f2013-08-31 01:11:41 +00002458 if (!Attr.isArgIdent(0)) {
2459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2460 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002461 return;
2462 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002463
Aaron Ballmand0686072013-09-11 19:47:58 +00002464 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2465 ObjCMethodFamilyAttr::FamilyKind F;
2466 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2467 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2468 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002469 return;
2470 }
2471
Aaron Ballmand0686072013-09-11 19:47:58 +00002472 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002473 !method->getResultType()->isObjCObjectPointerType()) {
2474 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2475 << method->getResultType();
2476 // Ignore the attribute.
2477 return;
2478 }
2479
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002480 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002481 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002482}
2483
Chandler Carruth1b03c872011-07-02 00:01:44 +00002484static void handleObjCExceptionAttr(Sema &S, Decl *D,
2485 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002486 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2487 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2489 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002490 return;
2491 }
Mike Stumpbf916502009-07-24 19:02:52 +00002492
Michael Han51d8c522013-01-24 16:46:58 +00002493 D->addAttr(::new (S.Context)
2494 ObjCExceptionAttr(Attr.getRange(), S.Context,
2495 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002496}
2497
Chandler Carruth1b03c872011-07-02 00:01:44 +00002498static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002499 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002500 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002501 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002502 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2503 return;
2504 }
2505 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002506 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2507 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002508 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002509 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2510 return;
2511 }
2512 }
2513 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002514 // It is okay to include this attribute on properties, e.g.:
2515 //
2516 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2517 //
2518 // In this case it follows tradition and suppresses an error in the above
2519 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002520 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002521 }
Michael Han51d8c522013-01-24 16:46:58 +00002522 D->addAttr(::new (S.Context)
2523 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2524 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002525}
2526
Mike Stumpbf916502009-07-24 19:02:52 +00002527static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002528handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002529 if (!isa<FunctionDecl>(D)) {
2530 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2531 return;
2532 }
2533
Michael Han51d8c522013-01-24 16:46:58 +00002534 D->addAttr(::new (S.Context)
2535 OverloadableAttr(Attr.getRange(), S.Context,
2536 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002537}
2538
Chandler Carruth1b03c872011-07-02 00:01:44 +00002539static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002540 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002541 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002542 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002543 return;
2544 }
Mike Stumpbf916502009-07-24 19:02:52 +00002545
Aaron Ballman624421f2013-08-31 01:11:41 +00002546 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002547 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002548 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2549 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2550 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002551 return;
2552 }
Mike Stumpbf916502009-07-24 19:02:52 +00002553
Michael Han51d8c522013-01-24 16:46:58 +00002554 D->addAttr(::new (S.Context)
2555 BlocksAttr(Attr.getRange(), S.Context, type,
2556 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002557}
2558
Chandler Carruth1b03c872011-07-02 00:01:44 +00002559static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002560 // check the attribute arguments.
2561 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002562 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002563 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002564 }
2565
John McCall3323fad2011-09-09 07:56:05 +00002566 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002567 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002568 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002569 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002570 if (E->isTypeDependent() || E->isValueDependent() ||
2571 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002572 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002573 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002574 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002575 return;
2576 }
Mike Stumpbf916502009-07-24 19:02:52 +00002577
John McCall3323fad2011-09-09 07:56:05 +00002578 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002579 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2580 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002581 return;
2582 }
John McCall3323fad2011-09-09 07:56:05 +00002583
2584 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002585 }
2586
John McCall3323fad2011-09-09 07:56:05 +00002587 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002588 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002589 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002590 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002591 if (E->isTypeDependent() || E->isValueDependent() ||
2592 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002593 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002594 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002595 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002596 return;
2597 }
2598 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002599
John McCall3323fad2011-09-09 07:56:05 +00002600 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002601 // FIXME: This error message could be improved, it would be nice
2602 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002603 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2604 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002605 return;
2606 }
2607 }
2608
Chandler Carruth87c44602011-07-01 23:49:12 +00002609 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002610 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002611 if (isa<FunctionNoProtoType>(FT)) {
2612 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2613 return;
2614 }
Mike Stumpbf916502009-07-24 19:02:52 +00002615
Chris Lattner897cd902009-03-17 23:03:47 +00002616 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002617 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002618 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002619 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002620 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002621 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002622 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002623 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002624 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002625 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2626 if (!BD->isVariadic()) {
2627 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2628 return;
2629 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002630 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002631 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002632 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002633 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002634 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002635 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002636 int m = Ty->isFunctionPointerType() ? 0 : 1;
2637 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002638 return;
2639 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002640 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002642 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002643 return;
2644 }
Anders Carlsson77091822008-10-05 18:05:59 +00002645 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002646 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002647 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002648 return;
2649 }
Michael Han51d8c522013-01-24 16:46:58 +00002650 D->addAttr(::new (S.Context)
2651 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2652 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002653}
2654
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002655static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002656 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2657 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2658 else
2659 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2660}
2661
Chandler Carruth1b03c872011-07-02 00:01:44 +00002662static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002663 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002664 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002665 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002666 return;
2667 }
Mike Stumpbf916502009-07-24 19:02:52 +00002668
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002669 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2670 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2671 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002672 return;
2673 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002674 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2675 if (MD->getResultType()->isVoidType()) {
2676 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2677 << Attr.getName() << 1;
2678 return;
2679 }
2680
Michael Han51d8c522013-01-24 16:46:58 +00002681 D->addAttr(::new (S.Context)
2682 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2683 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002684}
2685
Chandler Carruth1b03c872011-07-02 00:01:44 +00002686static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002687 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002688 if (isa<CXXRecordDecl>(D)) {
2689 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2690 return;
2691 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002692 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2693 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002694 return;
2695 }
2696
Chandler Carruth87c44602011-07-01 23:49:12 +00002697 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002698
Michael Han51d8c522013-01-24 16:46:58 +00002699 nd->addAttr(::new (S.Context)
2700 WeakAttr(Attr.getRange(), S.Context,
2701 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002702}
2703
Chandler Carruth1b03c872011-07-02 00:01:44 +00002704static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002705 // weak_import only applies to variable & function declarations.
2706 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002707 if (!D->canBeWeakImported(isDef)) {
2708 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002709 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2710 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002711 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002712 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002713 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002714 // Nothing to warn about here.
2715 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002716 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002717 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002718
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002719 return;
2720 }
2721
Michael Han51d8c522013-01-24 16:46:58 +00002722 D->addAttr(::new (S.Context)
2723 WeakImportAttr(Attr.getRange(), S.Context,
2724 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002725}
2726
Tanya Lattner0df579e2012-07-09 22:06:01 +00002727// Handles reqd_work_group_size and work_group_size_hint.
2728static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002729 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002730 unsigned WGSize[3];
2731 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002732 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002733 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002734 if (E->isTypeDependent() || E->isValueDependent() ||
2735 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002736 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2737 << Attr.getName() << AANT_ArgumentIntegerConstant
2738 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002739 return;
2740 }
2741 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2742 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002743
2744 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2745 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2746 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2747 if (!(A->getXDim() == WGSize[0] &&
2748 A->getYDim() == WGSize[1] &&
2749 A->getZDim() == WGSize[2])) {
2750 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2751 Attr.getName();
2752 }
2753 }
2754
2755 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2756 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2757 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2758 if (!(A->getXDim() == WGSize[0] &&
2759 A->getYDim() == WGSize[1] &&
2760 A->getZDim() == WGSize[2])) {
2761 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2762 Attr.getName();
2763 }
2764 }
2765
2766 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2767 D->addAttr(::new (S.Context)
2768 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002769 WGSize[0], WGSize[1], WGSize[2],
2770 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002771 else
2772 D->addAttr(::new (S.Context)
2773 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002774 WGSize[0], WGSize[1], WGSize[2],
2775 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002776}
2777
Joey Gouly37453b92013-03-08 09:42:32 +00002778static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2779 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2780
Aaron Ballman624421f2013-08-31 01:11:41 +00002781 if (!Attr.hasParsedType()) {
2782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2783 << Attr.getName() << 1;
2784 return;
2785 }
2786
Richard Smithddc2a532013-10-31 21:23:20 +00002787 TypeSourceInfo *ParmTSI = 0;
2788 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2789 assert(ParmTSI && "no type source info for attribute argument");
Joey Gouly37453b92013-03-08 09:42:32 +00002790
2791 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2792 (ParmType->isBooleanType() ||
2793 !ParmType->isIntegralType(S.getASTContext()))) {
2794 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2795 << ParmType;
2796 return;
2797 }
2798
2799 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2800 D->hasAttr<VecTypeHintAttr>()) {
2801 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithddc2a532013-10-31 21:23:20 +00002802 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Gouly37453b92013-03-08 09:42:32 +00002803 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2804 return;
2805 }
2806 }
2807
2808 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithddc2a532013-10-31 21:23:20 +00002809 ParmTSI));
Joey Gouly37453b92013-03-08 09:42:32 +00002810}
2811
Rafael Espindola599f1b72012-05-13 03:25:18 +00002812SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002813 StringRef Name,
2814 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002815 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2816 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002817 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002818 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2819 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002820 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002821 }
Michael Han51d8c522013-01-24 16:46:58 +00002822 return ::new (Context) SectionAttr(Range, Context, Name,
2823 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002824}
2825
Chandler Carruth1b03c872011-07-02 00:01:44 +00002826static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002827 // Make sure that there is a string literal as the sections's single
2828 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002829 StringRef Str;
2830 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002831 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002832 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Chris Lattner797c3c42009-08-10 19:03:04 +00002834 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002835 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002836 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002837 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002838 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002839 return;
2840 }
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002842 // This attribute cannot be applied to local variables.
2843 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002844 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002845 return;
2846 }
Michael Han51d8c522013-01-24 16:46:58 +00002847
2848 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002849 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002850 if (NewAttr)
2851 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002852}
2853
Chris Lattner6b6b5372008-06-26 18:38:35 +00002854
Chandler Carruth1b03c872011-07-02 00:01:44 +00002855static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002856 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002857 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002858 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002859 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002860 D->addAttr(::new (S.Context)
2861 NoThrowAttr(Attr.getRange(), S.Context,
2862 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002863 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002864}
2865
Chandler Carruth1b03c872011-07-02 00:01:44 +00002866static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002867 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002868 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002869 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002870 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002871 D->addAttr(::new (S.Context)
2872 ConstAttr(Attr.getRange(), S.Context,
2873 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002874 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002875}
2876
Chandler Carruth1b03c872011-07-02 00:01:44 +00002877static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002878 D->addAttr(::new (S.Context)
2879 PureAttr(Attr.getRange(), S.Context,
2880 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002881}
2882
Chandler Carruth1b03c872011-07-02 00:01:44 +00002883static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002884 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002885 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002886 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002887 return;
2888 }
Mike Stumpbf916502009-07-24 19:02:52 +00002889
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002890 Expr *E = Attr.getArgAsExpr(0);
2891 SourceLocation Loc = E->getExprLoc();
2892 FunctionDecl *FD = 0;
2893 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002894
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002895 // gcc only allows for simple identifiers. Since we support more than gcc, we
2896 // will warn the user.
2897 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2898 if (DRE->hasQualifier())
2899 S.Diag(Loc, diag::warn_cleanup_ext);
2900 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2901 NI = DRE->getNameInfo();
2902 if (!FD) {
2903 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2904 << NI.getName();
2905 return;
2906 }
2907 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2908 if (ULE->hasExplicitTemplateArgs())
2909 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002910 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2911 NI = ULE->getNameInfo();
Alp Toker8b407722013-10-20 18:48:56 +00002912 if (!FD) {
2913 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2914 << NI.getName();
2915 if (ULE->getType() == S.Context.OverloadTy)
2916 S.NoteAllOverloadCandidates(ULE);
2917 return;
2918 }
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002919 } else {
2920 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002921 return;
2922 }
2923
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002924 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002925 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2926 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002927 return;
2928 }
Mike Stumpbf916502009-07-24 19:02:52 +00002929
Anders Carlsson89941c12009-02-07 23:16:50 +00002930 // We're currently more strict than GCC about what function types we accept.
2931 // If this ever proves to be a problem it should be easy to fix.
2932 QualType Ty = S.Context.getPointerType(VD->getType());
2933 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002934 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2935 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002936 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2937 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002938 return;
2939 }
Mike Stumpbf916502009-07-24 19:02:52 +00002940
Michael Han51d8c522013-01-24 16:46:58 +00002941 D->addAttr(::new (S.Context)
2942 CleanupAttr(Attr.getRange(), S.Context, FD,
2943 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002944}
2945
Mike Stumpbf916502009-07-24 19:02:52 +00002946/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002947/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002948static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002949 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002950 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002951 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002952 return;
2953 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002954
Aaron Ballman624421f2013-08-31 01:11:41 +00002955 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002956 uint64_t ArgIdx;
2957 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2958 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002959 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002960
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002961 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002962 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002963
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002964 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2965 if (not_nsstring_type &&
2966 !isCFStringType(Ty, S.Context) &&
2967 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002968 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002969 // FIXME: Should highlight the actual expression that has the wrong type.
2970 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002971 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002972 << IdxExpr->getSourceRange();
2973 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002974 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002975 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002976 if (!isNSStringType(Ty, S.Context) &&
2977 !isCFStringType(Ty, S.Context) &&
2978 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002979 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002980 // FIXME: Should highlight the actual expression that has the wrong type.
2981 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002982 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002983 << IdxExpr->getSourceRange();
2984 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002985 }
2986
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002987 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2988 // because that has corrected for the implicit this parameter, and is zero-
2989 // based. The attribute expects what the user wrote explicitly.
2990 llvm::APSInt Val;
2991 IdxExpr->EvaluateAsInt(Val, S.Context);
2992
Michael Han51d8c522013-01-24 16:46:58 +00002993 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002994 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002995 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002996}
2997
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002998enum FormatAttrKind {
2999 CFStringFormat,
3000 NSStringFormat,
3001 StrftimeFormat,
3002 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003003 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003004 InvalidFormat
3005};
3006
3007/// getFormatAttrKind - Map from format attribute names to supported format
3008/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003009static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003010 return llvm::StringSwitch<FormatAttrKind>(Format)
3011 // Check for formats that get handled specially.
3012 .Case("NSString", NSStringFormat)
3013 .Case("CFString", CFStringFormat)
3014 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003015
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003016 // Otherwise, check for supported formats.
3017 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3018 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3019 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003020
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003021 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3022 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003023}
3024
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003025/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003026/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003027static void handleInitPriorityAttr(Sema &S, Decl *D,
3028 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003029 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003030 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3031 return;
3032 }
3033
Chandler Carruth87c44602011-07-01 23:49:12 +00003034 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003035 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3036 Attr.setInvalid();
3037 return;
3038 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003039 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003040 if (S.Context.getAsArrayType(T))
3041 T = S.Context.getBaseElementType(T);
3042 if (!T->getAs<RecordType>()) {
3043 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3044 Attr.setInvalid();
3045 return;
3046 }
3047
Aaron Ballman624421f2013-08-31 01:11:41 +00003048 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003049
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003050 llvm::APSInt priority(32);
3051 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3052 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3054 << Attr.getName() << AANT_ArgumentIntegerConstant
3055 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003056 Attr.setInvalid();
3057 return;
3058 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003059 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003060 if (prioritynum < 101 || prioritynum > 65535) {
3061 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3062 << priorityExpr->getSourceRange();
3063 Attr.setInvalid();
3064 return;
3065 }
Michael Han51d8c522013-01-24 16:46:58 +00003066 D->addAttr(::new (S.Context)
3067 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3068 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003069}
3070
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003071FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3072 IdentifierInfo *Format, int FormatIdx,
3073 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003074 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003075 // Check whether we already have an equivalent format attribute.
3076 for (specific_attr_iterator<FormatAttr>
3077 i = D->specific_attr_begin<FormatAttr>(),
3078 e = D->specific_attr_end<FormatAttr>();
3079 i != e ; ++i) {
3080 FormatAttr *f = *i;
3081 if (f->getType() == Format &&
3082 f->getFormatIdx() == FormatIdx &&
3083 f->getFirstArg() == FirstArg) {
3084 // If we don't have a valid location for this attribute, adopt the
3085 // location.
3086 if (f->getLocation().isInvalid())
3087 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003088 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003089 }
3090 }
3091
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003092 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3093 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003094}
3095
Mike Stumpbf916502009-07-24 19:02:52 +00003096/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003097/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003098static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003099 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003100 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003101 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003102 return;
3103 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003104
Chandler Carruth87c44602011-07-01 23:49:12 +00003105 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003106 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003107 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003108 return;
3109 }
3110
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003111 // In C++ the implicit 'this' function parameter also counts, and they are
3112 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003113 bool HasImplicitThisParam = isInstanceMethod(D);
3114 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003115 unsigned FirstIdx = 1;
3116
Aaron Ballman624421f2013-08-31 01:11:41 +00003117 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3118 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003119
3120 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003121 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003122 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003123 // If we've modified the string name, we need a new identifier for it.
3124 II = &S.Context.Idents.get(Format);
3125 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003126
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003127 // Check for supported formats.
3128 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003129
3130 if (Kind == IgnoredFormat)
3131 return;
3132
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003133 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003134 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003135 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003136 return;
3137 }
3138
3139 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003140 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003141 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003142 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3143 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003144 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003145 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003146 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003147 return;
3148 }
3149
3150 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003151 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003152 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003153 return;
3154 }
3155
3156 // FIXME: Do we need to bounds check?
3157 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003158
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003159 if (HasImplicitThisParam) {
3160 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003161 S.Diag(Attr.getLoc(),
3162 diag::err_format_attribute_implicit_this_format_string)
3163 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003164 return;
3165 }
3166 ArgIdx--;
3167 }
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Chris Lattner6b6b5372008-06-26 18:38:35 +00003169 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003170 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003171
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003172 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003173 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003174 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3175 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003176 return;
3177 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003178 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003179 // FIXME: do we need to check if the type is NSString*? What are the
3180 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003181 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003182 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003183 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3184 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003185 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003186 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003187 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003188 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003189 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003190 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3191 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003192 return;
3193 }
3194
3195 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003196 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003197 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003198 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3199 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003200 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003201 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003202 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003203 return;
3204 }
3205
3206 // check if the function is variadic if the 3rd argument non-zero
3207 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003208 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003209 ++NumArgs; // +1 for ...
3210 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003211 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003212 return;
3213 }
3214 }
3215
Chris Lattner3c73c412008-11-19 08:23:25 +00003216 // strftime requires FirstArg to be 0 because it doesn't read from any
3217 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003218 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003219 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003220 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3221 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003222 return;
3223 }
3224 // if 0 it disables parameter checking (to use with e.g. va_list)
3225 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003226 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003227 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003228 return;
3229 }
3230
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003231 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003232 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003233 FirstArg.getZExtValue(),
3234 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003235 if (NewAttr)
3236 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003237}
3238
Chandler Carruth1b03c872011-07-02 00:01:44 +00003239static void handleTransparentUnionAttr(Sema &S, Decl *D,
3240 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003241 // Try to find the underlying union declaration.
3242 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003243 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003244 if (TD && TD->getUnderlyingType()->isUnionType())
3245 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3246 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003247 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003248
3249 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003251 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003252 return;
3253 }
3254
John McCall5e1cdac2011-10-07 06:10:15 +00003255 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003256 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003257 diag::warn_transparent_union_attribute_not_definition);
3258 return;
3259 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003260
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003261 RecordDecl::field_iterator Field = RD->field_begin(),
3262 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003263 if (Field == FieldEnd) {
3264 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3265 return;
3266 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003267
David Blaikie581deb32012-06-06 20:45:41 +00003268 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003269 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003270 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003271 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003272 diag::warn_transparent_union_attribute_floating)
3273 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003274 return;
3275 }
3276
3277 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3278 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3279 for (; Field != FieldEnd; ++Field) {
3280 QualType FieldType = Field->getType();
3281 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3282 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3283 // Warn if we drop the attribute.
3284 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003285 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003286 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003287 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003288 diag::warn_transparent_union_attribute_field_size_align)
3289 << isSize << Field->getDeclName() << FieldBits;
3290 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003291 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003292 diag::note_transparent_union_first_field_size_align)
3293 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003294 return;
3295 }
3296 }
3297
Michael Han51d8c522013-01-24 16:46:58 +00003298 RD->addAttr(::new (S.Context)
3299 TransparentUnionAttr(Attr.getRange(), S.Context,
3300 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003301}
3302
Chandler Carruth1b03c872011-07-02 00:01:44 +00003303static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003304 // Make sure that there is a string literal as the annotation's single
3305 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003306 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003307 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003308 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003309
3310 // Don't duplicate annotations that are already set.
3311 for (specific_attr_iterator<AnnotateAttr>
3312 i = D->specific_attr_begin<AnnotateAttr>(),
3313 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003314 if ((*i)->getAnnotation() == Str)
3315 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003316 }
Michael Han51d8c522013-01-24 16:46:58 +00003317
3318 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003319 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003320 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003321}
3322
Chandler Carruth1b03c872011-07-02 00:01:44 +00003323static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003324 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003325 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003326 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3327 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003328 return;
3329 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003330
Richard Smithbe507b62013-02-01 08:12:08 +00003331 if (Attr.getNumArgs() == 0) {
3332 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3333 true, 0, Attr.getAttributeSpellingListIndex()));
3334 return;
3335 }
3336
Aaron Ballman624421f2013-08-31 01:11:41 +00003337 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003338 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3339 S.Diag(Attr.getEllipsisLoc(),
3340 diag::err_pack_expansion_without_parameter_packs);
3341 return;
3342 }
3343
3344 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3345 return;
3346
3347 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3348 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003349}
3350
3351void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003352 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003353 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3354 SourceLocation AttrLoc = AttrRange.getBegin();
3355
Richard Smith4cd81c52013-01-29 09:02:09 +00003356 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003357 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003358 // C++11 [dcl.align]p1:
3359 // An alignment-specifier may be applied to a variable or to a class
3360 // data member, but it shall not be applied to a bit-field, a function
3361 // parameter, the formal parameter of a catch clause, or a variable
3362 // declared with the register storage class specifier. An
3363 // alignment-specifier may also be applied to the declaration of a class
3364 // or enumeration type.
3365 // C11 6.7.5/2:
3366 // An alignment attribute shall not be specified in a declaration of
3367 // a typedef, or a bit-field, or a function, or a parameter, or an
3368 // object declared with the register storage-class specifier.
3369 int DiagKind = -1;
3370 if (isa<ParmVarDecl>(D)) {
3371 DiagKind = 0;
3372 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3373 if (VD->getStorageClass() == SC_Register)
3374 DiagKind = 1;
3375 if (VD->isExceptionVariable())
3376 DiagKind = 2;
3377 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3378 if (FD->isBitField())
3379 DiagKind = 3;
3380 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003381 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3382 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003383 << (TmpAttr.isC11() ? ExpectedVariableOrField
3384 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003385 return;
3386 }
3387 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003388 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003389 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003390 return;
3391 }
3392 }
3393
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003394 if (E->isTypeDependent() || E->isValueDependent()) {
3395 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003396 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3397 AA->setPackExpansion(IsPackExpansion);
3398 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003399 return;
3400 }
Michael Hana31f65b2013-02-01 01:19:17 +00003401
Sean Huntcf807c42010-08-18 23:23:40 +00003402 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003403 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003404 ExprResult ICE
3405 = VerifyIntegerConstantExpression(E, &Alignment,
3406 diag::err_aligned_attribute_argument_not_int,
3407 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003408 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003409 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003410
3411 // C++11 [dcl.align]p2:
3412 // -- if the constant expression evaluates to zero, the alignment
3413 // specifier shall have no effect
3414 // C11 6.7.5p6:
3415 // An alignment specification of zero has no effect.
3416 if (!(TmpAttr.isAlignas() && !Alignment) &&
3417 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003418 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3419 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003420 return;
3421 }
Michael Hana31f65b2013-02-01 01:19:17 +00003422
Richard Smithbe507b62013-02-01 08:12:08 +00003423 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003424 // We've already verified it's a power of 2, now let's make sure it's
3425 // 8192 or less.
3426 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003427 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003428 << E->getSourceRange();
3429 return;
3430 }
3431 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003432
Richard Smithf6565a92013-02-22 08:32:16 +00003433 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3434 ICE.take(), SpellingListIndex);
3435 AA->setPackExpansion(IsPackExpansion);
3436 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003437}
3438
Michael Hana31f65b2013-02-01 01:19:17 +00003439void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003440 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003441 // FIXME: Cache the number on the Attr object if non-dependent?
3442 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003443 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3444 SpellingListIndex);
3445 AA->setPackExpansion(IsPackExpansion);
3446 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003447}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003448
Richard Smithbe507b62013-02-01 08:12:08 +00003449void Sema::CheckAlignasUnderalignment(Decl *D) {
3450 assert(D->hasAttrs() && "no attributes on decl");
3451
3452 QualType Ty;
3453 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3454 Ty = VD->getType();
3455 else
3456 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003457 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003458 return;
3459
3460 // C++11 [dcl.align]p5, C11 6.7.5/4:
3461 // The combined effect of all alignment attributes in a declaration shall
3462 // not specify an alignment that is less strict than the alignment that
3463 // would otherwise be required for the entity being declared.
3464 AlignedAttr *AlignasAttr = 0;
3465 unsigned Align = 0;
3466 for (specific_attr_iterator<AlignedAttr>
3467 I = D->specific_attr_begin<AlignedAttr>(),
3468 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3469 if (I->isAlignmentDependent())
3470 return;
3471 if (I->isAlignas())
3472 AlignasAttr = *I;
3473 Align = std::max(Align, I->getAlignment(Context));
3474 }
3475
3476 if (AlignasAttr && Align) {
3477 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3478 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3479 if (NaturalAlign > RequestedAlign)
3480 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3481 << Ty << (unsigned)NaturalAlign.getQuantity();
3482 }
3483}
3484
Chandler Carruthd309c812011-07-01 23:49:16 +00003485/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003486/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003487///
Mike Stumpbf916502009-07-24 19:02:52 +00003488/// Despite what would be logical, the mode attribute is a decl attribute, not a
3489/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3490/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003491static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003492 // This attribute isn't documented, but glibc uses it. It changes
3493 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003494 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003495 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3496 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003497 return;
3498 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003499
Aaron Ballman624421f2013-08-31 01:11:41 +00003500 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3501 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003502
3503 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003504 if (Str.startswith("__") && Str.endswith("__"))
3505 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003506
3507 unsigned DestWidth = 0;
3508 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003509 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003510 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003511 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003512 switch (Str[0]) {
3513 case 'Q': DestWidth = 8; break;
3514 case 'H': DestWidth = 16; break;
3515 case 'S': DestWidth = 32; break;
3516 case 'D': DestWidth = 64; break;
3517 case 'X': DestWidth = 96; break;
3518 case 'T': DestWidth = 128; break;
3519 }
3520 if (Str[1] == 'F') {
3521 IntegerMode = false;
3522 } else if (Str[1] == 'C') {
3523 IntegerMode = false;
3524 ComplexMode = true;
3525 } else if (Str[1] != 'I') {
3526 DestWidth = 0;
3527 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003528 break;
3529 case 4:
3530 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3531 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003532 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003533 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003534 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003535 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003536 break;
3537 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003538 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003539 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003540 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003541 case 11:
3542 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003543 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003544 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003545 }
3546
3547 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003548 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003549 OldTy = TD->getUnderlyingType();
3550 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3551 OldTy = VD->getType();
3552 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003553 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003554 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003555 return;
3556 }
Eli Friedman73397492009-03-03 06:41:03 +00003557
John McCall183700f2009-09-21 23:43:11 +00003558 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003559 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3560 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003561 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003562 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3563 } else if (ComplexMode) {
3564 if (!OldTy->isComplexType())
3565 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3566 } else {
3567 if (!OldTy->isFloatingType())
3568 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3569 }
3570
Mike Stump390b4cc2009-05-16 07:39:55 +00003571 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3572 // and friends, at least with glibc.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003573 // FIXME: Make sure floating-point mappings are accurate
3574 // FIXME: Support XF and TF types
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003575 if (!DestWidth) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003576 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003577 return;
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003578 }
3579
3580 QualType NewTy;
3581
3582 if (IntegerMode)
3583 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3584 OldTy->isSignedIntegerType());
3585 else
3586 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3587
3588 if (NewTy.isNull()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003589 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003590 return;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003591 }
3592
Eli Friedman73397492009-03-03 06:41:03 +00003593 if (ComplexMode) {
3594 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003595 }
3596
3597 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003598 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3599 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3600 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003601 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003602
3603 D->addAttr(::new (S.Context)
3604 ModeAttr(Attr.getRange(), S.Context, Name,
3605 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003606}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003607
Chandler Carruth1b03c872011-07-02 00:01:44 +00003608static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003609 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3610 if (!VD->hasGlobalStorage())
3611 S.Diag(Attr.getLoc(),
3612 diag::warn_attribute_requires_functions_or_static_globals)
3613 << Attr.getName();
3614 } else if (!isFunctionOrMethod(D)) {
3615 S.Diag(Attr.getLoc(),
3616 diag::warn_attribute_requires_functions_or_static_globals)
3617 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003618 return;
3619 }
Mike Stumpbf916502009-07-24 19:02:52 +00003620
Michael Han51d8c522013-01-24 16:46:58 +00003621 D->addAttr(::new (S.Context)
3622 NoDebugAttr(Attr.getRange(), S.Context,
3623 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003624}
3625
Chandler Carruth1b03c872011-07-02 00:01:44 +00003626static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003627 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003629 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003630 return;
3631 }
Mike Stumpbf916502009-07-24 19:02:52 +00003632
Michael Han51d8c522013-01-24 16:46:58 +00003633 D->addAttr(::new (S.Context)
3634 NoInlineAttr(Attr.getRange(), S.Context,
3635 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003636}
3637
Chandler Carruth1b03c872011-07-02 00:01:44 +00003638static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3639 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003640 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003642 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003643 return;
3644 }
3645
Michael Han51d8c522013-01-24 16:46:58 +00003646 D->addAttr(::new (S.Context)
3647 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3648 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003649}
3650
Chandler Carruth1b03c872011-07-02 00:01:44 +00003651static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003652 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003653 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003654 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003655 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003656 return;
3657 }
3658
Michael Han51d8c522013-01-24 16:46:58 +00003659 D->addAttr(::new (S.Context)
3660 CUDAConstantAttr(Attr.getRange(), S.Context,
3661 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003662 } else {
3663 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3664 }
3665}
3666
Chandler Carruth1b03c872011-07-02 00:01:44 +00003667static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003668 if (S.LangOpts.CUDA) {
3669 // check the attribute arguments.
3670 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3672 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003673 return;
3674 }
3675
Chandler Carruth87c44602011-07-01 23:49:12 +00003676 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003678 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003679 return;
3680 }
3681
Michael Han51d8c522013-01-24 16:46:58 +00003682 D->addAttr(::new (S.Context)
3683 CUDADeviceAttr(Attr.getRange(), S.Context,
3684 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003685 } else {
3686 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3687 }
3688}
3689
Chandler Carruth1b03c872011-07-02 00:01:44 +00003690static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003691 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003692 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003693 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003694 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003695 return;
3696 }
3697
Chandler Carruth87c44602011-07-01 23:49:12 +00003698 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003699 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003700 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003701 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003702 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3703 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003704 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003705 "void");
3706 } else {
3707 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3708 << FD->getType();
3709 }
3710 return;
3711 }
3712
Michael Han51d8c522013-01-24 16:46:58 +00003713 D->addAttr(::new (S.Context)
3714 CUDAGlobalAttr(Attr.getRange(), S.Context,
3715 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003716 } else {
3717 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3718 }
3719}
3720
Chandler Carruth1b03c872011-07-02 00:01:44 +00003721static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003722 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003723 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003725 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003726 return;
3727 }
3728
Michael Han51d8c522013-01-24 16:46:58 +00003729 D->addAttr(::new (S.Context)
3730 CUDAHostAttr(Attr.getRange(), S.Context,
3731 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003732 } else {
3733 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3734 }
3735}
3736
Chandler Carruth1b03c872011-07-02 00:01:44 +00003737static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003738 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003739 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003741 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003742 return;
3743 }
3744
Michael Han51d8c522013-01-24 16:46:58 +00003745 D->addAttr(::new (S.Context)
3746 CUDASharedAttr(Attr.getRange(), S.Context,
3747 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003748 } else {
3749 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3750 }
3751}
3752
Chandler Carruth1b03c872011-07-02 00:01:44 +00003753static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003754 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003755 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003756 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003757 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003758 return;
3759 }
Mike Stumpbf916502009-07-24 19:02:52 +00003760
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003761 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003762 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003763 return;
3764 }
Mike Stumpbf916502009-07-24 19:02:52 +00003765
Michael Han51d8c522013-01-24 16:46:58 +00003766 D->addAttr(::new (S.Context)
3767 GNUInlineAttr(Attr.getRange(), S.Context,
3768 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003769}
3770
Chandler Carruth1b03c872011-07-02 00:01:44 +00003771static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003772 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003773
Aaron Ballmanfff32482012-12-09 17:45:41 +00003774 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003775 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003776 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3777 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003778 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003779 return;
3780
Chandler Carruth87c44602011-07-01 23:49:12 +00003781 if (!isa<ObjCMethodDecl>(D)) {
3782 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3783 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003784 return;
3785 }
3786
Chandler Carruth87c44602011-07-01 23:49:12 +00003787 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003788 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003789 D->addAttr(::new (S.Context)
3790 FastCallAttr(Attr.getRange(), S.Context,
3791 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003792 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003793 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003794 D->addAttr(::new (S.Context)
3795 StdCallAttr(Attr.getRange(), S.Context,
3796 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003797 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003798 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003799 D->addAttr(::new (S.Context)
3800 ThisCallAttr(Attr.getRange(), S.Context,
3801 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003802 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003803 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003804 D->addAttr(::new (S.Context)
3805 CDeclAttr(Attr.getRange(), S.Context,
3806 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003807 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003808 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003809 D->addAttr(::new (S.Context)
3810 PascalAttr(Attr.getRange(), S.Context,
3811 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003812 return;
Charles Davise8519c32013-08-30 04:39:01 +00003813 case AttributeList::AT_MSABI:
3814 D->addAttr(::new (S.Context)
3815 MSABIAttr(Attr.getRange(), S.Context,
3816 Attr.getAttributeSpellingListIndex()));
3817 return;
3818 case AttributeList::AT_SysVABI:
3819 D->addAttr(::new (S.Context)
3820 SysVABIAttr(Attr.getRange(), S.Context,
3821 Attr.getAttributeSpellingListIndex()));
3822 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003823 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003824 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003825 switch (CC) {
3826 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003827 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003828 break;
3829 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003830 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003831 break;
3832 default:
3833 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003834 }
3835
Michael Han51d8c522013-01-24 16:46:58 +00003836 D->addAttr(::new (S.Context)
3837 PcsAttr(Attr.getRange(), S.Context, PCS,
3838 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003839 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003840 }
Derek Schuff263366f2012-10-16 22:30:41 +00003841 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003842 D->addAttr(::new (S.Context)
3843 PnaclCallAttr(Attr.getRange(), S.Context,
3844 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003845 return;
Guy Benyei38980082012-12-25 08:53:55 +00003846 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003847 D->addAttr(::new (S.Context)
3848 IntelOclBiccAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003850 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003851
Abramo Bagnarae215f722010-04-30 13:10:51 +00003852 default:
3853 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003854 }
3855}
3856
Chandler Carruth1b03c872011-07-02 00:01:44 +00003857static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003858 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003859}
3860
Guy Benyei1db70402013-03-24 13:58:12 +00003861static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003862 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003863 llvm::APSInt ArgNum(32);
3864 if (E->isTypeDependent() || E->isValueDependent() ||
3865 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003866 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3867 << Attr.getName() << AANT_ArgumentIntegerConstant
3868 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003869 return;
3870 }
3871
3872 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3873 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3874}
3875
Aaron Ballmanfff32482012-12-09 17:45:41 +00003876bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3877 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003878 if (attr.isInvalid())
3879 return true;
3880
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003881 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003882 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003883 attr.setInvalid();
3884 return true;
3885 }
3886
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003887 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3888 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003889 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003890 case AttributeList::AT_CDecl: CC = CC_C; break;
3891 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3892 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3893 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3894 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003895 case AttributeList::AT_MSABI:
3896 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3897 CC_X86_64Win64;
3898 break;
3899 case AttributeList::AT_SysVABI:
3900 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3901 CC_C;
3902 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003903 case AttributeList::AT_Pcs: {
Aaron Ballmanfbf6f5c2013-09-13 17:48:25 +00003904 StringRef StrRef;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003905 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003906 attr.setInvalid();
3907 return true;
3908 }
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003909 if (StrRef == "aapcs") {
3910 CC = CC_AAPCS;
3911 break;
3912 } else if (StrRef == "aapcs-vfp") {
3913 CC = CC_AAPCS_VFP;
3914 break;
3915 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003916
3917 attr.setInvalid();
3918 Diag(attr.getLoc(), diag::err_invalid_pcs);
3919 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003920 }
Derek Schuff263366f2012-10-16 22:30:41 +00003921 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003922 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003923 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003924 }
3925
Aaron Ballman82bfa192012-10-02 14:26:08 +00003926 const TargetInfo &TI = Context.getTargetInfo();
3927 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3928 if (A == TargetInfo::CCCR_Warning) {
3929 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003930
3931 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3932 if (FD)
3933 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3934 TargetInfo::CCMT_NonMember;
3935 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003936 }
3937
John McCall711c52b2011-01-05 12:14:39 +00003938 return false;
3939}
3940
Chandler Carruth1b03c872011-07-02 00:01:44 +00003941static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003942 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003943
3944 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003945 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003946 return;
3947
Chandler Carruth87c44602011-07-01 23:49:12 +00003948 if (!isa<ObjCMethodDecl>(D)) {
3949 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3950 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003951 return;
3952 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003953
Michael Han51d8c522013-01-24 16:46:58 +00003954 D->addAttr(::new (S.Context)
3955 RegparmAttr(Attr.getRange(), S.Context, numParams,
3956 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003957}
3958
3959/// Checks a regparm attribute, returning true if it is ill-formed and
3960/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003961bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3962 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003963 return true;
3964
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003965 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003966 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003967 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003968 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003969
Aaron Ballman624421f2013-08-31 01:11:41 +00003970 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003971 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003972 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003973 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003974 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3975 << Attr.getName() << AANT_ArgumentIntegerConstant
3976 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003977 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003978 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003979 }
3980
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003981 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003982 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003983 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003984 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003985 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003986 }
3987
John McCall711c52b2011-01-05 12:14:39 +00003988 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003989 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003990 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003991 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003992 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003993 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003994 }
3995
John McCall711c52b2011-01-05 12:14:39 +00003996 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003997}
3998
Chandler Carruth1b03c872011-07-02 00:01:44 +00003999static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004000 if (S.LangOpts.CUDA) {
4001 // check the attribute arguments.
4002 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004003 // FIXME: 0 is not okay.
4004 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004005 return;
4006 }
4007
Chandler Carruth87c44602011-07-01 23:49:12 +00004008 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004009 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004010 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004011 return;
4012 }
4013
Aaron Ballman624421f2013-08-31 01:11:41 +00004014 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004015 llvm::APSInt MaxThreads(32);
4016 if (MaxThreadsExpr->isTypeDependent() ||
4017 MaxThreadsExpr->isValueDependent() ||
4018 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004019 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004020 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004021 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004022 return;
4023 }
4024
4025 llvm::APSInt MinBlocks(32);
4026 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004027 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004028 if (MinBlocksExpr->isTypeDependent() ||
4029 MinBlocksExpr->isValueDependent() ||
4030 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004031 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004032 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004033 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004034 return;
4035 }
4036 }
4037
Michael Han51d8c522013-01-24 16:46:58 +00004038 D->addAttr(::new (S.Context)
4039 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4040 MaxThreads.getZExtValue(),
4041 MinBlocks.getZExtValue(),
4042 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004043 } else {
4044 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4045 }
4046}
4047
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004048static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4049 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004050 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004051 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004052 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004053 return;
4054 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004055
4056 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004057 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004058
Aaron Ballman624421f2013-08-31 01:11:41 +00004059 StringRef AttrName = Attr.getName()->getName();
4060 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004061
4062 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4063 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4064 << Attr.getName() << ExpectedFunctionOrMethod;
4065 return;
4066 }
4067
4068 uint64_t ArgumentIdx;
4069 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4070 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004071 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004072 return;
4073
4074 uint64_t TypeTagIdx;
4075 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4076 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004077 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004078 return;
4079
4080 bool IsPointer = (AttrName == "pointer_with_type_tag");
4081 if (IsPointer) {
4082 // Ensure that buffer has a pointer type.
4083 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4084 if (!BufferTy->isPointerType()) {
4085 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004086 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004087 }
4088 }
4089
Michael Han51d8c522013-01-24 16:46:58 +00004090 D->addAttr(::new (S.Context)
4091 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4092 ArgumentIdx, TypeTagIdx, IsPointer,
4093 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004094}
4095
4096static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4097 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004098 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004099 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004100 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004101 return;
4102 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004103
4104 if (!checkAttributeNumArgs(S, Attr, 1))
4105 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004106
Aaron Ballman624421f2013-08-31 01:11:41 +00004107 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithddc2a532013-10-31 21:23:20 +00004108 TypeSourceInfo *MatchingCTypeLoc = 0;
4109 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4110 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004111
Michael Han51d8c522013-01-24 16:46:58 +00004112 D->addAttr(::new (S.Context)
4113 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithddc2a532013-10-31 21:23:20 +00004114 MatchingCTypeLoc,
Michael Han51d8c522013-01-24 16:46:58 +00004115 Attr.getLayoutCompatible(),
4116 Attr.getMustBeNull(),
4117 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004118}
4119
Chris Lattner0744e5f2008-06-29 00:23:49 +00004120//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004121// Checker-specific attribute handlers.
4122//===----------------------------------------------------------------------===//
4123
John McCallc7ad3812011-01-25 03:31:58 +00004124static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004125 return type->isDependentType() ||
4126 type->isObjCObjectPointerType() ||
4127 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004128}
4129static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004130 return type->isDependentType() ||
4131 type->isPointerType() ||
4132 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004133}
4134
Chandler Carruth1b03c872011-07-02 00:01:44 +00004135static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004136 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004137 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004138 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004139 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004140 return;
4141 }
4142
4143 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004144 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004145 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4146 cf = false;
4147 } else {
4148 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4149 cf = true;
4150 }
4151
4152 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004153 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004154 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004155 return;
4156 }
4157
4158 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004159 param->addAttr(::new (S.Context)
4160 CFConsumedAttr(Attr.getRange(), S.Context,
4161 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004162 else
Michael Han51d8c522013-01-24 16:46:58 +00004163 param->addAttr(::new (S.Context)
4164 NSConsumedAttr(Attr.getRange(), S.Context,
4165 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004166}
4167
Chandler Carruth1b03c872011-07-02 00:01:44 +00004168static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4169 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004170 if (!isa<ObjCMethodDecl>(D)) {
4171 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004172 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004173 return;
4174 }
4175
Michael Han51d8c522013-01-24 16:46:58 +00004176 D->addAttr(::new (S.Context)
4177 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4178 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004179}
4180
Chandler Carruth1b03c872011-07-02 00:01:44 +00004181static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4182 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004183
John McCallc7ad3812011-01-25 03:31:58 +00004184 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004185
Chandler Carruth87c44602011-07-01 23:49:12 +00004186 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004187 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004188 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004189 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004190 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004191 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4192 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004193 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004194 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004195 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004196 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004197 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004198 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004199 return;
4200 }
Mike Stumpbf916502009-07-24 19:02:52 +00004201
John McCallc7ad3812011-01-25 03:31:58 +00004202 bool typeOK;
4203 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004204 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004205 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004206 case AttributeList::AT_NSReturnsAutoreleased:
4207 case AttributeList::AT_NSReturnsRetained:
4208 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004209 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4210 cf = false;
4211 break;
4212
Sean Hunt8e083e72012-06-19 23:57:03 +00004213 case AttributeList::AT_CFReturnsRetained:
4214 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004215 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4216 cf = true;
4217 break;
4218 }
4219
4220 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004221 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004222 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004223 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004224 }
Mike Stumpbf916502009-07-24 19:02:52 +00004225
Chandler Carruth87c44602011-07-01 23:49:12 +00004226 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004227 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004228 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004229 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004230 D->addAttr(::new (S.Context)
4231 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4232 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004233 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004234 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004235 D->addAttr(::new (S.Context)
4236 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4237 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004238 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004239 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004240 D->addAttr(::new (S.Context)
4241 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4242 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004243 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004244 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004245 D->addAttr(::new (S.Context)
4246 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4247 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004248 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004249 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004250 D->addAttr(::new (S.Context)
4251 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4252 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004253 return;
4254 };
4255}
4256
John McCalldc7c5ad2011-07-22 08:53:00 +00004257static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4258 const AttributeList &attr) {
Fariborz Jahanianaf1d28f2013-09-19 17:52:50 +00004259 const int EP_ObjCMethod = 1;
4260 const int EP_ObjCProperty = 2;
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004261
John McCalldc7c5ad2011-07-22 08:53:00 +00004262 SourceLocation loc = attr.getLoc();
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004263 QualType resultType;
4264
John McCalldc7c5ad2011-07-22 08:53:00 +00004265 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4266
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004267 if (!method) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004268 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4269 if (!property) {
4270 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4271 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4272 return;
4273 }
4274 resultType = property->getType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004275 }
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004276 else
4277 // Check that the method returns a normal pointer.
4278 resultType = method->getResultType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004279
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004280 if (!resultType->isReferenceType() &&
4281 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004282 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalldc7c5ad2011-07-22 08:53:00 +00004283 << SourceRange(loc)
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004284 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4285 << /*non-retainable pointer*/ 2;
John McCalldc7c5ad2011-07-22 08:53:00 +00004286
4287 // Drop the attribute.
4288 return;
4289 }
4290
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004291 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004292 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4293 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004294}
4295
Fariborz Jahanian84101132012-09-07 23:46:23 +00004296static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4297 const AttributeList &attr) {
4298 SourceLocation loc = attr.getLoc();
4299 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4300
4301 if (!method) {
4302 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4303 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4304 return;
4305 }
4306 DeclContext *DC = method->getDeclContext();
4307 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4308 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4309 << attr.getName() << 0;
4310 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4311 return;
4312 }
4313 if (method->getMethodFamily() == OMF_dealloc) {
4314 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4315 << attr.getName() << 1;
4316 return;
4317 }
4318
Michael Han51d8c522013-01-24 16:46:58 +00004319 method->addAttr(::new (S.Context)
4320 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4321 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004322}
4323
John McCall8dfac0b2011-09-30 05:12:12 +00004324/// Handle cf_audited_transfer and cf_unknown_transfer.
4325static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4326 if (!isa<FunctionDecl>(D)) {
4327 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004328 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004329 return;
4330 }
4331
Sean Hunt8e083e72012-06-19 23:57:03 +00004332 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004333
4334 // Check whether there's a conflicting attribute already present.
4335 Attr *Existing;
4336 if (IsAudited) {
4337 Existing = D->getAttr<CFUnknownTransferAttr>();
4338 } else {
4339 Existing = D->getAttr<CFAuditedTransferAttr>();
4340 }
4341 if (Existing) {
4342 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4343 << A.getName()
4344 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4345 << A.getRange() << Existing->getRange();
4346 return;
4347 }
4348
4349 // All clear; add the attribute.
4350 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004351 D->addAttr(::new (S.Context)
4352 CFAuditedTransferAttr(A.getRange(), S.Context,
4353 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004354 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004355 D->addAttr(::new (S.Context)
4356 CFUnknownTransferAttr(A.getRange(), S.Context,
4357 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004358 }
4359}
4360
John McCallfe98da02011-09-29 07:17:38 +00004361static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4362 const AttributeList &Attr) {
4363 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4364 if (!RD || RD->isUnion()) {
4365 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004366 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004367 }
4368
Aaron Ballman624421f2013-08-31 01:11:41 +00004369 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004370
4371 // In Objective-C, verify that the type names an Objective-C type.
4372 // We don't want to check this outside of ObjC because people sometimes
4373 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004374 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004375 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004376 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004377 Sema::LookupOrdinaryName);
4378 if (S.LookupName(R, Sc)) {
4379 NamedDecl *Target = R.getFoundDecl();
4380 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4381 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4382 S.Diag(Target->getLocStart(), diag::note_declared_at);
4383 }
4384 }
4385 }
4386
Michael Han51d8c522013-01-24 16:46:58 +00004387 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004388 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004389 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004390}
4391
Chandler Carruth1b03c872011-07-02 00:01:44 +00004392static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4393 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004394 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004395
Chandler Carruth87c44602011-07-01 23:49:12 +00004396 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004397 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004398}
4399
Chandler Carruth1b03c872011-07-02 00:01:44 +00004400static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4401 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004402 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004403 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004404 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004405 return;
4406 }
4407
Chandler Carruth87c44602011-07-01 23:49:12 +00004408 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004409 QualType type = vd->getType();
4410
4411 if (!type->isDependentType() &&
4412 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004413 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004414 << type;
4415 return;
4416 }
4417
4418 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4419
4420 // If we have no lifetime yet, check the lifetime we're presumably
4421 // going to infer.
4422 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4423 lifetime = type->getObjCARCImplicitLifetime();
4424
4425 switch (lifetime) {
4426 case Qualifiers::OCL_None:
4427 assert(type->isDependentType() &&
4428 "didn't infer lifetime for non-dependent type?");
4429 break;
4430
4431 case Qualifiers::OCL_Weak: // meaningful
4432 case Qualifiers::OCL_Strong: // meaningful
4433 break;
4434
4435 case Qualifiers::OCL_ExplicitNone:
4436 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004437 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004438 << (lifetime == Qualifiers::OCL_Autoreleasing);
4439 break;
4440 }
4441
Chandler Carruth87c44602011-07-01 23:49:12 +00004442 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004443 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4444 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004445}
4446
Francois Pichet11542142010-12-19 06:50:37 +00004447//===----------------------------------------------------------------------===//
4448// Microsoft specific attribute handlers.
4449//===----------------------------------------------------------------------===//
4450
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004451// Check if MS extensions or some other language extensions are enabled. If
4452// not, issue a diagnostic that the given attribute is unused.
4453static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4454 bool OtherExtension = false) {
4455 if (S.LangOpts.MicrosoftExt || OtherExtension)
4456 return true;
4457 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4458 return false;
4459}
4460
Chandler Carruth1b03c872011-07-02 00:01:44 +00004461static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004462 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4463 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004464
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004465 StringRef StrRef;
4466 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00004467 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004468 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004469
David Majnemerbb0ed292013-08-09 08:56:20 +00004470 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4471 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004472 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4473 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004474
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004475 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004476 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004477 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004478 return;
4479 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004480
David Majnemerbb0ed292013-08-09 08:56:20 +00004481 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004482 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004483 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004484 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004485 return;
4486 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004487 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004488 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004489 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004490 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004491 }
Francois Pichet11542142010-12-19 06:50:37 +00004492
David Majnemerbb0ed292013-08-09 08:56:20 +00004493 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4494 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004495}
4496
John McCallc052dbb2012-05-22 21:28:12 +00004497static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004498 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004499 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004500
4501 AttributeList::Kind Kind = Attr.getKind();
4502 if (Kind == AttributeList::AT_SingleInheritance)
4503 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004504 ::new (S.Context)
4505 SingleInheritanceAttr(Attr.getRange(), S.Context,
4506 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004507 else if (Kind == AttributeList::AT_MultipleInheritance)
4508 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004509 ::new (S.Context)
4510 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4511 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004512 else if (Kind == AttributeList::AT_VirtualInheritance)
4513 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004514 ::new (S.Context)
4515 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4516 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004517}
4518
4519static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004520 if (!checkMicrosoftExt(S, Attr))
4521 return;
4522
4523 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004524 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004525 D->addAttr(
4526 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4527 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004528}
4529
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004530static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004531 if (!checkMicrosoftExt(S, Attr))
4532 return;
4533 D->addAttr(::new (S.Context)
4534 ForceInlineAttr(Attr.getRange(), S.Context,
4535 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004536}
4537
Reid Klecknera7225342013-05-20 14:02:37 +00004538static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4539 if (!checkMicrosoftExt(S, Attr))
4540 return;
4541 // Check linkage after possibly merging declaratinos. See
4542 // checkAttributesAfterMerging().
4543 D->addAttr(::new (S.Context)
4544 SelectAnyAttr(Attr.getRange(), S.Context,
4545 Attr.getAttributeSpellingListIndex()));
4546}
4547
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004548/// Handles semantic checking for features that are common to all attributes,
4549/// such as checking whether a parameter was properly specified, or the correct
4550/// number of arguments were passed, etc.
4551static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4552 const AttributeList &Attr) {
4553 // Several attributes carry different semantics than the parsing requires, so
4554 // those are opted out of the common handling.
4555 //
4556 // We also bail on unknown and ignored attributes because those are handled
4557 // as part of the target-specific handling logic.
4558 if (Attr.hasCustomParsing() ||
4559 Attr.getKind() == AttributeList::UnknownAttribute ||
4560 Attr.getKind() == AttributeList::IgnoredAttribute)
4561 return false;
4562
4563 // If there are no optional arguments, then checking for the argument count
4564 // is trivial.
4565 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4566 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4567 return true;
4568 return false;
4569}
4570
Ted Kremenekb71368d2009-05-09 02:44:38 +00004571//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004572// Top Level Sema Entry Points
4573//===----------------------------------------------------------------------===//
4574
Richard Smith4a97b8e2013-08-29 00:47:48 +00004575/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4576/// the attribute applies to decls. If the attribute is a type attribute, just
4577/// silently ignore it if a GNU attribute.
4578static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4579 const AttributeList &Attr,
4580 bool IncludeCXX11Attributes) {
4581 if (Attr.isInvalid())
4582 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004583
Richard Smith4a97b8e2013-08-29 00:47:48 +00004584 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4585 // instead.
4586 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4587 return;
4588
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004589 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4590 return;
4591
Chris Lattner803d0802008-06-29 00:43:07 +00004592 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004593 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4594 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4595 case AttributeList::AT_IBOutletCollection:
4596 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004597 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004598 case AttributeList::AT_ObjCGC:
4599 case AttributeList::AT_VectorSize:
4600 case AttributeList::AT_NeonVectorType:
4601 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004602 case AttributeList::AT_Ptr32:
4603 case AttributeList::AT_Ptr64:
4604 case AttributeList::AT_SPtr:
4605 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004606 // Ignore these, these are type attributes, handled by
4607 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004608 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004609 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4610 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4611 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4612 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004613 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004614 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004615 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004616 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004617 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4618 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4619 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004620 handleDependencyAttr(S, scope, D, Attr);
4621 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004622 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4623 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4624 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004625 case AttributeList::AT_CXX11NoReturn:
4626 handleCXX11NoReturnAttr(S, D, Attr);
4627 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004628 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004629 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004630 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004631 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4632 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004633 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004634 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004635 case AttributeList::AT_MinSize:
4636 handleMinSizeAttr(S, D, Attr);
4637 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004638 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4639 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4640 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004641 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4642 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004643 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4644 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004645 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004646 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004647 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4648 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004649 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004650 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4651 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004652 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004653 case AttributeList::AT_ownership_returns:
4654 case AttributeList::AT_ownership_takes:
4655 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004656 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004657 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4658 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4659 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4660 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4661 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4662 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4663 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004664
Sean Hunt8e083e72012-06-19 23:57:03 +00004665 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004666 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004667 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004668 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004669
Sean Hunt8e083e72012-06-19 23:57:03 +00004670 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004671 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4672
Fariborz Jahanian84101132012-09-07 23:46:23 +00004673 case AttributeList::AT_ObjCRequiresSuper:
4674 handleObjCRequiresSuperAttr(S, D, Attr); break;
4675
Sean Hunt8e083e72012-06-19 23:57:03 +00004676 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004677 handleNSBridgedAttr(S, scope, D, Attr); break;
4678
Sean Hunt8e083e72012-06-19 23:57:03 +00004679 case AttributeList::AT_CFAuditedTransfer:
4680 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004681 handleCFTransferAttr(S, D, Attr); break;
4682
Ted Kremenekb71368d2009-05-09 02:44:38 +00004683 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004684 case AttributeList::AT_CFConsumed:
4685 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4686 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004687 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004688
Sean Hunt8e083e72012-06-19 23:57:03 +00004689 case AttributeList::AT_NSReturnsAutoreleased:
4690 case AttributeList::AT_NSReturnsNotRetained:
4691 case AttributeList::AT_CFReturnsNotRetained:
4692 case AttributeList::AT_NSReturnsRetained:
4693 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004694 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004695
Tanya Lattner0df579e2012-07-09 22:06:01 +00004696 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004697 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004698 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004699
Joey Gouly37453b92013-03-08 09:42:32 +00004700 case AttributeList::AT_VecTypeHint:
4701 handleVecTypeHint(S, D, Attr); break;
4702
Sean Hunt8e083e72012-06-19 23:57:03 +00004703 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004704 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004705
Sean Hunt8e083e72012-06-19 23:57:03 +00004706 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4707 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4708 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004709 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004710 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004711 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004712 handleArcWeakrefUnavailableAttr (S, D, Attr);
4713 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004714 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004715 handleObjCRootClassAttr(S, D, Attr);
4716 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004717 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004718 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004719 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004720 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4721 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004722 handleReturnsTwiceAttr(S, D, Attr);
4723 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004724 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004725 case AttributeList::AT_Visibility:
4726 handleVisibilityAttr(S, D, Attr, false);
4727 break;
4728 case AttributeList::AT_TypeVisibility:
4729 handleVisibilityAttr(S, D, Attr, true);
4730 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004731 case AttributeList::AT_WarnUnused:
4732 handleWarnUnusedAttr(S, D, Attr);
4733 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004734 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004735 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004736 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4737 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4738 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4739 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004740 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004741 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004742 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004743 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004744 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004746 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004747 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004748 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4749 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4750 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4751 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4752 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4753 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4754 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4755 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4756 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004757 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004758 // Just ignore
4759 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004761 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004762 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004763 case AttributeList::AT_StdCall:
4764 case AttributeList::AT_CDecl:
4765 case AttributeList::AT_FastCall:
4766 case AttributeList::AT_ThisCall:
4767 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004768 case AttributeList::AT_MSABI:
4769 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004770 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004771 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004772 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004773 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004774 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004775 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004776 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004777 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004778 case AttributeList::AT_OpenCLImageAccess:
4779 handleOpenCLImageAccessAttr(S, D, Attr);
4780 break;
John McCallc052dbb2012-05-22 21:28:12 +00004781
4782 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004784 handleMsStructAttr(S, D, Attr);
4785 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004786 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004787 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004788 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004789 case AttributeList::AT_SingleInheritance:
4790 case AttributeList::AT_MultipleInheritance:
4791 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004792 handleInheritanceAttr(S, D, Attr);
4793 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004794 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004795 handlePortabilityAttr(S, D, Attr);
4796 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004798 handleForceInlineAttr(S, D, Attr);
4799 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004800 case AttributeList::AT_SelectAny:
4801 handleSelectAnyAttr(S, D, Attr);
4802 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004803
4804 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004805 case AttributeList::AT_AssertExclusiveLock:
4806 handleAssertExclusiveLockAttr(S, D, Attr);
4807 break;
4808 case AttributeList::AT_AssertSharedLock:
4809 handleAssertSharedLockAttr(S, D, Attr);
4810 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004811 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004812 handleGuardedVarAttr(S, D, Attr);
4813 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004814 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004815 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004816 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004818 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004819 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004820 case AttributeList::AT_NoSanitizeAddress:
4821 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004822 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004823 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004824 handleNoThreadSafetyAnalysis(S, D, Attr);
4825 break;
4826 case AttributeList::AT_NoSanitizeThread:
4827 handleNoSanitizeThread(S, D, Attr);
4828 break;
4829 case AttributeList::AT_NoSanitizeMemory:
4830 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004831 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004832 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004833 handleLockableAttr(S, D, Attr);
4834 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004835 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004836 handleGuardedByAttr(S, D, Attr);
4837 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004838 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004839 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004840 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004841 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004842 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004843 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004845 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004846 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004848 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004849 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004851 handleLockReturnedAttr(S, D, Attr);
4852 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004853 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004854 handleLocksExcludedAttr(S, D, Attr);
4855 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004857 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004858 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004859 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004860 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004861 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004862 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004863 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004864 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004865 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004866 handleUnlockFunAttr(S, D, Attr);
4867 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004869 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004870 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004871 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004872 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004873 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004874
DeLesley Hutchins1bf63432013-10-11 22:30:48 +00004875 // Consumed analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004876 case AttributeList::AT_Consumable:
4877 handleConsumableAttr(S, D, Attr);
4878 break;
DeLesley Hutchins66540852013-10-04 21:28:06 +00004879 case AttributeList::AT_CallableWhen:
4880 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004881 break;
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00004882 case AttributeList::AT_ParamTypestate:
4883 handleParamTypestateAttr(S, D, Attr);
4884 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004885 case AttributeList::AT_ReturnTypestate:
4886 handleReturnTypestateAttr(S, D, Attr);
4887 break;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004888 case AttributeList::AT_SetTypestate:
4889 handleSetTypestateAttr(S, D, Attr);
4890 break;
Chris Wailes0e429f12013-10-29 20:28:41 +00004891 case AttributeList::AT_TestTypestate:
4892 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004893 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004894
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004895 // Type safety attributes.
4896 case AttributeList::AT_ArgumentWithTypeTag:
4897 handleArgumentWithTypeTagAttr(S, D, Attr);
4898 break;
4899 case AttributeList::AT_TypeTagForDatatype:
4900 handleTypeTagForDatatypeAttr(S, D, Attr);
4901 break;
4902
Chris Lattner803d0802008-06-29 00:43:07 +00004903 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004904 // Ask target about the attribute.
4905 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4906 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004907 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4908 diag::warn_unhandled_ms_attribute_ignored :
4909 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004910 break;
4911 }
4912}
4913
4914/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4915/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004916void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004917 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004918 bool IncludeCXX11Attributes) {
4919 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004920 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004921
4922 // GCC accepts
4923 // static int a9 __attribute__((weakref));
4924 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004925 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004926 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004927 cast<NamedDecl>(D)->getNameAsString();
4928 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004929 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004930 }
4931}
4932
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004933// Annotation attributes are the only attributes allowed after an access
4934// specifier.
4935bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4936 const AttributeList *AttrList) {
4937 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004938 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004939 handleAnnotateAttr(*this, ASDecl, *l);
4940 } else {
4941 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4942 return true;
4943 }
4944 }
4945
4946 return false;
4947}
4948
John McCalle82247a2011-10-01 05:17:03 +00004949/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4950/// contains any decl attributes that we should warn about.
4951static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4952 for ( ; A; A = A->getNext()) {
4953 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004954 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004955 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4956
4957 if (A->getKind() == AttributeList::UnknownAttribute) {
4958 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4959 << A->getName() << A->getRange();
4960 } else {
4961 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4962 << A->getName() << A->getRange();
4963 }
4964 }
4965}
4966
4967/// checkUnusedDeclAttributes - Given a declarator which is not being
4968/// used to build a declaration, complain about any decl attributes
4969/// which might be lying around on it.
4970void Sema::checkUnusedDeclAttributes(Declarator &D) {
4971 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4972 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4973 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4974 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4975}
4976
Ryan Flynne25ff832009-07-30 03:15:39 +00004977/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004978/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004979NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4980 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004981 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004982 NamedDecl *NewD = 0;
4983 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004984 FunctionDecl *NewFD;
4985 // FIXME: Missing call to CheckFunctionDeclaration().
4986 // FIXME: Mangling?
4987 // FIXME: Is the qualifier info correct?
4988 // FIXME: Is the DeclContext correct?
4989 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4990 Loc, Loc, DeclarationName(II),
4991 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004992 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004993 FD->hasPrototype(),
4994 false/*isConstexprSpecified*/);
4995 NewD = NewFD;
4996
4997 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004998 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004999
5000 // Fake up parameter variables; they are declared as if this were
5001 // a typedef.
5002 QualType FDTy = FD->getType();
5003 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5004 SmallVector<ParmVarDecl*, 16> Params;
5005 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5006 AE = FT->arg_type_end(); AI != AE; ++AI) {
5007 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5008 Param->setScopeInfo(0, Params.size());
5009 Params.push_back(Param);
5010 }
David Blaikie4278c652011-09-21 18:16:56 +00005011 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005012 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005013 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5014 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005015 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005016 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005017 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005018 if (VD->getQualifier()) {
5019 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005020 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005021 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005022 }
5023 return NewD;
5024}
5025
James Dennett1dfbd922012-06-14 21:40:34 +00005026/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005027/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005028void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005029 if (W.getUsed()) return; // only do this once
5030 W.setUsed(true);
5031 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5032 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005033 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005034 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5035 NDId->getName()));
5036 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005037 WeakTopLevelDecl.push_back(NewD);
5038 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5039 // to insert Decl at TU scope, sorry.
5040 DeclContext *SavedContext = CurContext;
5041 CurContext = Context.getTranslationUnitDecl();
5042 PushOnScopeChains(NewD, S);
5043 CurContext = SavedContext;
5044 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005045 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005046 }
5047}
5048
Rafael Espindola65611bf2013-03-02 21:41:48 +00005049void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5050 // It's valid to "forward-declare" #pragma weak, in which case we
5051 // have to do this.
5052 LoadExternalWeakUndeclaredIdentifiers();
5053 if (!WeakUndeclaredIdentifiers.empty()) {
5054 NamedDecl *ND = NULL;
5055 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5056 if (VD->isExternC())
5057 ND = VD;
5058 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5059 if (FD->isExternC())
5060 ND = FD;
5061 if (ND) {
5062 if (IdentifierInfo *Id = ND->getIdentifier()) {
5063 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5064 = WeakUndeclaredIdentifiers.find(Id);
5065 if (I != WeakUndeclaredIdentifiers.end()) {
5066 WeakInfo W = I->second;
5067 DeclApplyPragmaWeak(S, ND, W);
5068 WeakUndeclaredIdentifiers[Id] = W;
5069 }
5070 }
5071 }
5072 }
5073}
5074
Chris Lattner0744e5f2008-06-29 00:23:49 +00005075/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5076/// it, apply them to D. This is a bit tricky because PD can have attributes
5077/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005078void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005079 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005080 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005081 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005082
Chris Lattner0744e5f2008-06-29 00:23:49 +00005083 // Walk the declarator structure, applying decl attributes that were in a type
5084 // position to the decl itself. This handles cases like:
5085 // int *__attr__(x)** D;
5086 // when X is a decl attribute.
5087 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5088 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005089 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005090
Chris Lattner0744e5f2008-06-29 00:23:49 +00005091 // Finally, apply any attributes on the decl itself.
5092 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005093 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005094}
John McCall54abf7d2009-11-04 02:18:39 +00005095
John McCallf85e1932011-06-15 23:02:42 +00005096/// Is the given declaration allowed to use a forbidden type?
5097static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5098 // Private ivars are always okay. Unfortunately, people don't
5099 // always properly make their ivars private, even in system headers.
5100 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005101 // Function declarations in sys headers will be marked unavailable.
5102 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5103 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005104 return false;
5105
5106 // Require it to be declared in a system header.
5107 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5108}
5109
5110/// Handle a delayed forbidden-type diagnostic.
5111static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5112 Decl *decl) {
5113 if (decl && isForbiddenTypeAllowed(S, decl)) {
5114 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5115 "this system declaration uses an unsupported type"));
5116 return;
5117 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005118 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005119 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005120 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005121 // kind of forbidden type messages on unavailable functions.
5122 if (FD->hasAttr<UnavailableAttr>() &&
5123 diag.getForbiddenTypeDiagnostic() ==
5124 diag::err_arc_array_param_no_ownership) {
5125 diag.Triggered = true;
5126 return;
5127 }
5128 }
John McCallf85e1932011-06-15 23:02:42 +00005129
5130 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5131 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5132 diag.Triggered = true;
5133}
5134
John McCall92576642012-05-07 06:16:41 +00005135void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5136 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005137 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005138 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005139
John McCall92576642012-05-07 06:16:41 +00005140 // When delaying diagnostics to run in the context of a parsed
5141 // declaration, we only want to actually emit anything if parsing
5142 // succeeds.
5143 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005144
John McCall92576642012-05-07 06:16:41 +00005145 // We emit all the active diagnostics in this pool or any of its
5146 // parents. In general, we'll get one pool for the decl spec
5147 // and a child pool for each declarator; in a decl group like:
5148 // deprecated_typedef foo, *bar, baz();
5149 // only the declarator pops will be passed decls. This is correct;
5150 // we really do need to consider delayed diagnostics from the decl spec
5151 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005152 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005153 do {
John McCall13489672012-05-07 06:16:58 +00005154 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005155 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5156 // This const_cast is a bit lame. Really, Triggered should be mutable.
5157 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005158 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005159 continue;
5160
John McCalleee1d542011-02-14 07:13:47 +00005161 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005162 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005163 // Don't bother giving deprecation diagnostics if the decl is invalid.
5164 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005165 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005166 break;
5167
5168 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005169 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005170 break;
John McCallf85e1932011-06-15 23:02:42 +00005171
5172 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005173 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005174 break;
John McCall2f514482010-01-27 03:50:35 +00005175 }
5176 }
John McCall92576642012-05-07 06:16:41 +00005177 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005178}
5179
John McCall13489672012-05-07 06:16:58 +00005180/// Given a set of delayed diagnostics, re-emit them as if they had
5181/// been delayed in the current context instead of in the given pool.
5182/// Essentially, this just moves them to the current pool.
5183void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5184 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5185 assert(curPool && "re-emitting in undelayed context not supported");
5186 curPool->steal(pool);
5187}
5188
John McCall54abf7d2009-11-04 02:18:39 +00005189static bool isDeclDeprecated(Decl *D) {
5190 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005191 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005192 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005193 // A category implicitly has the availability of the interface.
5194 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5195 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005196 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5197 return false;
5198}
5199
Eli Friedmanc3b23082012-08-08 21:52:41 +00005200static void
5201DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5202 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005203 const ObjCInterfaceDecl *UnknownObjCClass,
5204 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005205 DeclarationName Name = D->getDeclName();
5206 if (!Message.empty()) {
5207 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5208 S.Diag(D->getLocation(),
5209 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5210 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005211 if (ObjCPropery)
5212 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5213 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005214 } else if (!UnknownObjCClass) {
5215 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5216 S.Diag(D->getLocation(),
5217 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5218 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005219 if (ObjCPropery)
5220 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5221 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005222 } else {
5223 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5224 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5225 }
5226}
5227
John McCall9c3087b2010-08-26 02:13:20 +00005228void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005229 Decl *Ctx) {
5230 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005231 return;
5232
John McCall2f514482010-01-27 03:50:35 +00005233 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005234 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5235 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005236 DD.getUnknownObjCClass(),
5237 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005238}
5239
Chris Lattner5f9e2722011-07-23 10:55:15 +00005240void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005241 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005242 const ObjCInterfaceDecl *UnknownObjCClass,
5243 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005244 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005245 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005246 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5247 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005248 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005249 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005250 return;
5251 }
5252
5253 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005254 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005255 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005256 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005257}