blob: c08767574ceb260388d8035259febde0fcffe988 [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
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000210/// \brief Check if the attribute has exactly as many args as Num. May
211/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000212static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
213 unsigned int Num) {
214 if (Attr.getNumArgs() != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000215 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
216 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000217 return false;
218 }
219
220 return true;
221}
222
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000223
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000224/// \brief Check if the attribute has at least as many args as Num. May
225/// output an error.
226static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
227 unsigned int Num) {
228 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000229 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
230 return false;
231 }
232
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000233 return true;
234}
235
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000236/// \brief Check if IdxExpr is a valid argument index for a function or
237/// instance method D. May output an error.
238///
239/// \returns true if IdxExpr is a valid index.
240static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
241 StringRef AttrName,
242 SourceLocation AttrLoc,
243 unsigned AttrArgNum,
244 const Expr *IdxExpr,
245 uint64_t &Idx)
246{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000247 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000248
249 // In C++ the implicit 'this' function parameter also counts.
250 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000251 bool HP = hasFunctionProto(D);
252 bool HasImplicitThisParam = isInstanceMethod(D);
253 bool IV = HP && isFunctionOrMethodVariadic(D);
254 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
255 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000256
257 llvm::APSInt IdxInt;
258 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
259 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000260 std::string Name = std::string("'") + AttrName.str() + std::string("'");
261 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000262 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000263 return false;
264 }
265
266 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000267 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000268 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
269 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
270 return false;
271 }
272 Idx--; // Convert to zero-based.
273 if (HasImplicitThisParam) {
274 if (Idx == 0) {
275 S.Diag(AttrLoc,
276 diag::err_attribute_invalid_implicit_this_argument)
277 << AttrName << IdxExpr->getSourceRange();
278 return false;
279 }
280 --Idx;
281 }
282
283 return true;
284}
285
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000286/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
287/// If not emit an error and return false. If the argument is an identifier it
288/// will emit an error with a fixit hint and treat it as if it was a string
289/// literal.
Tim Northoverf8aebef2013-10-01 14:34:18 +0000290bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
291 unsigned ArgNum, StringRef &Str,
Tim Northover64eac852013-10-01 14:34:25 +0000292 SourceLocation *ArgLocation) {
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000293 // Look for identifiers. If we have one emit a hint to fix it to a literal.
294 if (Attr.isArgIdent(ArgNum)) {
295 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northoverf8aebef2013-10-01 14:34:18 +0000296 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000297 << Attr.getName() << AANT_ArgumentString
298 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northoverf8aebef2013-10-01 14:34:18 +0000299 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000300 Str = Loc->Ident->getName();
301 if (ArgLocation)
302 *ArgLocation = Loc->Loc;
303 return true;
304 }
305
306 // Now check for an actual string literal.
307 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
308 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
309 if (ArgLocation)
310 *ArgLocation = ArgExpr->getLocStart();
311
312 if (!Literal || !Literal->isAscii()) {
Tim Northoverf8aebef2013-10-01 14:34:18 +0000313 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000314 << Attr.getName() << AANT_ArgumentString;
315 return false;
316 }
317
318 Str = Literal->getString();
319 return true;
320}
321
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000322///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000323/// \brief Check if passed in Decl is a field or potentially shared global var
324/// \return true if the Decl is a field or potentially shared global variable
325///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000326static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000327 if (isa<FieldDecl>(D))
328 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000329 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000330 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000331
332 return false;
333}
334
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000335/// \brief Check if the passed-in expression is of type int or bool.
336static bool isIntOrBool(Expr *Exp) {
337 QualType QT = Exp->getType();
338 return QT->isBooleanType() || QT->isIntegerType();
339}
340
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000341
342// Check to see if the type is a smart pointer of some kind. We assume
343// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000344static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
345 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
346 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000347 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000348 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000349
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000350 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
351 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000352 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000353 return false;
354
355 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000356}
357
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000358/// \brief Check if passed in Decl is a pointer type.
359/// Note that this function may produce an error message.
360/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000361static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
362 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000363 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000364 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000365 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000366 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000367
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000368 if (const RecordType *RT = QT->getAs<RecordType>()) {
369 // If it's an incomplete type, it could be a smart pointer; skip it.
370 // (We don't want to force template instantiation if we can avoid it,
371 // since that would alter the order in which templates are instantiated.)
372 if (RT->isIncompleteType())
373 return true;
374
375 if (threadSafetyCheckIsSmartPointer(S, RT))
376 return true;
377 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000378
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000379 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000380 << Attr.getName()->getName() << QT;
381 } else {
382 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
383 << Attr.getName();
384 }
385 return false;
386}
387
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000388/// \brief Checks that the passed in QualType either is of RecordType or points
389/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000390static const RecordType *getRecordType(QualType QT) {
391 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000392 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000393
394 // Now check if we point to record type.
395 if (const PointerType *PT = QT->getAs<PointerType>())
396 return PT->getPointeeType()->getAs<RecordType>();
397
398 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000399}
400
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000401
Jordy Rosefad5de92012-05-08 03:27:22 +0000402static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
403 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000404 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
405 if (RT->getDecl()->getAttr<LockableAttr>())
406 return true;
407 return false;
408}
409
410
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000411/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000413static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
414 QualType Ty) {
415 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000416
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000417 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000418 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000419 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000420 << Attr.getName() << Ty.getAsString();
421 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000422 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000423
Michael Hanf1aae3b2012-08-03 17:40:43 +0000424 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000425 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000426 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000427
428 // Allow smart pointers to be used as lockable objects.
429 // FIXME -- Check the type that the smart pointer points to.
430 if (threadSafetyCheckIsSmartPointer(S, RT))
431 return;
432
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000433 // Check if the type is lockable.
434 RecordDecl *RD = RT->getDecl();
435 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000436 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000437
438 // Else check if any base classes are lockable.
439 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
440 CXXBasePaths BPaths(false, false);
441 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
442 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000443 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000444
445 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
446 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447}
448
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000449/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000450/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000451/// \param Sidx The attribute argument index to start checking with.
452/// \param ParamIdxOk Whether an argument can be indexing into a function
453/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000454static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000455 const AttributeList &Attr,
456 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457 int Sidx = 0,
458 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000459 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000460 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000461
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000462 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000463 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000464 Args.push_back(ArgExp);
465 continue;
466 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000467
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000468 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000469 if (StrLit->getLength() == 0 ||
Benjamin Kramerf37e4f22013-09-13 16:30:12 +0000470 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000471 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000472 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000473 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000474 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000475 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000476
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000477 // We allow constant strings to be used as a placeholder for expressions
478 // that are not valid C++ syntax, but warn that they are ignored.
479 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
480 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000481 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000482 continue;
483 }
484
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000485 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000486
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000487 // A pointer to member expression of the form &MyClass::mu is treated
488 // specially -- we need to look at the type of the member.
489 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
490 if (UOp->getOpcode() == UO_AddrOf)
491 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
492 if (DRE->getDecl()->isCXXInstanceMember())
493 ArgTy = DRE->getDecl()->getType();
494
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000495 // First see if we can just cast to record type, or point to record type.
496 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000497
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000498 // Now check if we index into a record type function param.
499 if(!RT && ParamIdxOk) {
500 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000501 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
502 if(FD && IL) {
503 unsigned int NumParams = FD->getNumParams();
504 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000505 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
506 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
507 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
509 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000510 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000511 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000512 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000513 }
514 }
515
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000516 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000517
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000518 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000519 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000520}
521
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000522//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000523// Attribute Implementations
524//===----------------------------------------------------------------------===//
525
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000526// FIXME: All this manual attribute parsing code is gross. At the
527// least add some helper functions to check most argument patterns (#
528// and types of args).
529
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000530enum ThreadAttributeDeclKind {
531 ThreadExpectedFieldOrGlobalVar,
532 ThreadExpectedFunctionOrMethod,
533 ThreadExpectedClassOrStruct
534};
535
Michael Hanf1aae3b2012-08-03 17:40:43 +0000536static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000537 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000538 // D must be either a member field or global (potentially shared) variable.
539 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000540 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
541 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000542 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000543 }
544
Michael Handc691572012-07-23 18:48:41 +0000545 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000546}
547
Michael Handc691572012-07-23 18:48:41 +0000548static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
549 if (!checkGuardedVarAttrCommon(S, D, Attr))
550 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000551
Michael Han51d8c522013-01-24 16:46:58 +0000552 D->addAttr(::new (S.Context)
553 GuardedVarAttr(Attr.getRange(), S.Context,
554 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000555}
556
Michael Hanf1aae3b2012-08-03 17:40:43 +0000557static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000558 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000559 if (!checkGuardedVarAttrCommon(S, D, Attr))
560 return;
561
562 if (!threadSafetyCheckIsPointer(S, D, Attr))
563 return;
564
Michael Han51d8c522013-01-24 16:46:58 +0000565 D->addAttr(::new (S.Context)
566 PtGuardedVarAttr(Attr.getRange(), S.Context,
567 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000568}
569
Michael Hanf1aae3b2012-08-03 17:40:43 +0000570static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
571 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000572 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000573 // D must be either a member field or global (potentially shared) variable.
574 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000575 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
576 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000577 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000578 }
579
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000580 SmallVector<Expr*, 1> Args;
581 // check that all arguments are lockable objects
582 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
583 unsigned Size = Args.size();
584 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000585 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000586
Michael Handc691572012-07-23 18:48:41 +0000587 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000588
Michael Handc691572012-07-23 18:48:41 +0000589 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000590}
591
Michael Handc691572012-07-23 18:48:41 +0000592static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
593 Expr *Arg = 0;
594 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
595 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000596
Michael Handc691572012-07-23 18:48:41 +0000597 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
598}
599
Michael Hanf1aae3b2012-08-03 17:40:43 +0000600static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000601 const AttributeList &Attr) {
602 Expr *Arg = 0;
603 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
604 return;
605
606 if (!threadSafetyCheckIsPointer(S, D, Attr))
607 return;
608
609 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
610 S.Context, Arg));
611}
612
Michael Hanf1aae3b2012-08-03 17:40:43 +0000613static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000614 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000615 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000616 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000617 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
618 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000619 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000620 }
621
Michael Handc691572012-07-23 18:48:41 +0000622 return true;
623}
624
625static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
626 if (!checkLockableAttrCommon(S, D, Attr))
627 return;
628
629 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
630}
631
Michael Hanf1aae3b2012-08-03 17:40:43 +0000632static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000633 const AttributeList &Attr) {
634 if (!checkLockableAttrCommon(S, D, Attr))
635 return;
636
Michael Han51d8c522013-01-24 16:46:58 +0000637 D->addAttr(::new (S.Context)
638 ScopedLockableAttr(Attr.getRange(), S.Context,
639 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000640}
641
Kostya Serebryany85aee962013-02-26 06:58:27 +0000642static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
643 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000644 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000645 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
646 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000647 return;
648 }
649
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000650 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000651 S.Context));
652}
653
Kostya Serebryany85aee962013-02-26 06:58:27 +0000654static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000655 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000656 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000658 << Attr.getName() << ExpectedFunctionOrMethod;
659 return;
660 }
661
Michael Han51d8c522013-01-24 16:46:58 +0000662 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000663 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
664 Attr.getAttributeSpellingListIndex()));
665}
666
667static void handleNoSanitizeMemory(Sema &S, Decl *D,
668 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000669 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
670 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
671 << Attr.getName() << ExpectedFunctionOrMethod;
672 return;
673 }
674
675 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
676 S.Context));
677}
678
679static void handleNoSanitizeThread(Sema &S, Decl *D,
680 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000681 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
683 << Attr.getName() << ExpectedFunctionOrMethod;
684 return;
685 }
686
687 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
688 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000689}
690
Michael Hanf1aae3b2012-08-03 17:40:43 +0000691static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
692 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000693 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000694 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000695 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000696
697 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000698 ValueDecl *VD = dyn_cast<ValueDecl>(D);
699 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000700 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
701 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000702 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000703 }
704
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000705 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000706 QualType QT = VD->getType();
707 if (!QT->isDependentType()) {
708 const RecordType *RT = getRecordType(QT);
709 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000710 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000711 << Attr.getName();
712 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000713 }
714 }
715
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000716 // Check that all arguments are lockable objects.
717 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000718 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000719 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000720
Michael Handc691572012-07-23 18:48:41 +0000721 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000722}
723
Michael Hanf1aae3b2012-08-03 17:40:43 +0000724static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 1> Args;
727 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
728 return;
729
730 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000731 D->addAttr(::new (S.Context)
732 AcquiredAfterAttr(Attr.getRange(), S.Context,
733 StartArg, Args.size(),
734 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000735}
736
Michael Hanf1aae3b2012-08-03 17:40:43 +0000737static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000738 const AttributeList &Attr) {
739 SmallVector<Expr*, 1> Args;
740 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
741 return;
742
743 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000744 D->addAttr(::new (S.Context)
745 AcquiredBeforeAttr(Attr.getRange(), S.Context,
746 StartArg, Args.size(),
747 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000748}
749
Michael Hanf1aae3b2012-08-03 17:40:43 +0000750static bool checkLockFunAttrCommon(Sema &S, Decl *D,
751 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000752 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000753 // zero or more arguments ok
754
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000755 // check that the attribute is applied to a function
756 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000757 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
758 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000759 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000760 }
761
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000762 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000763 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000764
Michael Handc691572012-07-23 18:48:41 +0000765 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000766}
767
Michael Hanf1aae3b2012-08-03 17:40:43 +0000768static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000769 const AttributeList &Attr) {
770 SmallVector<Expr*, 1> Args;
771 if (!checkLockFunAttrCommon(S, D, Attr, Args))
772 return;
773
774 unsigned Size = Args.size();
775 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000776 D->addAttr(::new (S.Context)
777 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
778 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000779}
780
Michael Hanf1aae3b2012-08-03 17:40:43 +0000781static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000782 const AttributeList &Attr) {
783 SmallVector<Expr*, 1> Args;
784 if (!checkLockFunAttrCommon(S, D, Attr, Args))
785 return;
786
787 unsigned Size = Args.size();
788 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000789 D->addAttr(::new (S.Context)
790 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
791 StartArg, Size,
792 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000793}
794
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000795static void handleAssertSharedLockAttr(Sema &S, Decl *D,
796 const AttributeList &Attr) {
797 SmallVector<Expr*, 1> Args;
798 if (!checkLockFunAttrCommon(S, D, Attr, Args))
799 return;
800
801 unsigned Size = Args.size();
802 Expr **StartArg = Size == 0 ? 0 : &Args[0];
803 D->addAttr(::new (S.Context)
804 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
805 Attr.getAttributeSpellingListIndex()));
806}
807
808static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
809 const AttributeList &Attr) {
810 SmallVector<Expr*, 1> Args;
811 if (!checkLockFunAttrCommon(S, D, Attr, Args))
812 return;
813
814 unsigned Size = Args.size();
815 Expr **StartArg = Size == 0 ? 0 : &Args[0];
816 D->addAttr(::new (S.Context)
817 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
818 StartArg, Size,
819 Attr.getAttributeSpellingListIndex()));
820}
821
822
Michael Hanf1aae3b2012-08-03 17:40:43 +0000823static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
824 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000825 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000826 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000827 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000828
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000829 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000830 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
831 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000832 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000833 }
834
Aaron Ballman624421f2013-08-31 01:11:41 +0000835 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000836 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000837 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000838 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000839 }
840
841 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000842 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000843
Michael Handc691572012-07-23 18:48:41 +0000844 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000845}
846
Michael Hanf1aae3b2012-08-03 17:40:43 +0000847static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000848 const AttributeList &Attr) {
849 SmallVector<Expr*, 2> Args;
850 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
851 return;
852
Michael Han51d8c522013-01-24 16:46:58 +0000853 D->addAttr(::new (S.Context)
854 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000855 Attr.getArgAsExpr(0),
856 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000857 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000858}
859
Michael Hanf1aae3b2012-08-03 17:40:43 +0000860static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000861 const AttributeList &Attr) {
862 SmallVector<Expr*, 2> Args;
863 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
864 return;
865
Michael Han51d8c522013-01-24 16:46:58 +0000866 D->addAttr(::new (S.Context)
867 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000868 Attr.getArgAsExpr(0),
869 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000870 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000871}
872
Michael Hanf1aae3b2012-08-03 17:40:43 +0000873static bool checkLocksRequiredCommon(Sema &S, Decl *D,
874 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000875 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000876 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000877 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000878
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000879 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000880 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
881 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000882 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000883 }
884
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000885 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000886 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000887 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000888 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000889
Michael Handc691572012-07-23 18:48:41 +0000890 return true;
891}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000892
Michael Hanf1aae3b2012-08-03 17:40:43 +0000893static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000894 const AttributeList &Attr) {
895 SmallVector<Expr*, 1> Args;
896 if (!checkLocksRequiredCommon(S, D, Attr, Args))
897 return;
898
899 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000900 D->addAttr(::new (S.Context)
901 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
902 StartArg, Args.size(),
903 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000904}
905
Michael Hanf1aae3b2012-08-03 17:40:43 +0000906static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000907 const AttributeList &Attr) {
908 SmallVector<Expr*, 1> Args;
909 if (!checkLocksRequiredCommon(S, D, Attr, Args))
910 return;
911
912 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000913 D->addAttr(::new (S.Context)
914 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
915 StartArg, Args.size(),
916 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000917}
918
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000919static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000920 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000921 // zero or more arguments ok
922
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000923 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000924 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
925 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926 return;
927 }
928
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000929 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000930 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000931 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000932 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000933 Expr **StartArg = Size == 0 ? 0 : &Args[0];
934
Michael Han51d8c522013-01-24 16:46:58 +0000935 D->addAttr(::new (S.Context)
936 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
937 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000938}
939
940static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000941 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000942 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000943 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
944 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000945 return;
946 }
947
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000948 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000949 SmallVector<Expr*, 1> Args;
950 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
951 unsigned Size = Args.size();
952 if (Size == 0)
953 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000954
Michael Han51d8c522013-01-24 16:46:58 +0000955 D->addAttr(::new (S.Context)
956 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
957 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000958}
959
960static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000961 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000962 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000963 return;
964
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000965 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000966 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
967 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000968 return;
969 }
970
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000971 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000972 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000973 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000974 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000975 if (Size == 0)
976 return;
977 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000978
Michael Han51d8c522013-01-24 16:46:58 +0000979 D->addAttr(::new (S.Context)
980 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
981 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000982}
983
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000984static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000985 ConsumableAttr::ConsumedState DefaultState;
986
987 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000988 IdentifierLoc *IL = Attr.getArgAsIdent(0);
989 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
990 DefaultState)) {
991 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
992 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +0000993 return;
994 }
David Blaikiea33ab602013-09-06 01:28:43 +0000995 } else {
996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
997 << Attr.getName() << AANT_ArgumentIdentifier;
998 return;
999 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001000
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001001 if (!isa<CXXRecordDecl>(D)) {
1002 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1003 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001004 return;
1005 }
1006
1007 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +00001008 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001009 Attr.getAttributeSpellingListIndex()));
1010}
1011
1012static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1013 const AttributeList &Attr) {
1014 ASTContext &CurrContext = S.getASTContext();
1015 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1016
1017 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1018 if (!RD->hasAttr<ConsumableAttr>()) {
1019 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1020 RD->getNameAsString();
1021
1022 return false;
1023 }
1024 }
1025
1026 return true;
1027}
1028
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001029
DeLesley Hutchins66540852013-10-04 21:28:06 +00001030static void handleCallableWhenAttr(Sema &S, Decl *D,
1031 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001032 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1033 return;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001034
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001035 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001036 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1037 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001038 return;
1039 }
1040
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001041 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1042 return;
1043
DeLesley Hutchins66540852013-10-04 21:28:06 +00001044 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1045 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1046 CallableWhenAttr::ConsumedState CallableState;
1047
Aaron Ballman9025a182013-10-05 22:45:34 +00001048 StringRef StateString;
1049 SourceLocation Loc;
1050 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1051 return;
1052
1053 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
1054 CallableState)) {
1055 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1056 << Attr.getName() << StateString;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001057 return;
1058 }
Aaron Ballman9025a182013-10-05 22:45:34 +00001059
1060 States.push_back(CallableState);
DeLesley Hutchins66540852013-10-04 21:28:06 +00001061 }
1062
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001063 D->addAttr(::new (S.Context)
DeLesley Hutchins66540852013-10-04 21:28:06 +00001064 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1065 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001066}
1067
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001068
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001069static void handleReturnTypestateAttr(Sema &S, Decl *D,
1070 const AttributeList &Attr) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001071 ReturnTypestateAttr::ConsumedState ReturnState;
1072
1073 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001074 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1075 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1076 ReturnState)) {
1077 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1078 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001079 return;
1080 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001081 } else {
1082 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1083 Attr.getName() << AANT_ArgumentIdentifier;
1084 return;
1085 }
1086
1087 if (!isa<FunctionDecl>(D)) {
1088 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1089 Attr.getName() << ExpectedFunction;
1090 return;
1091 }
1092
1093 // FIXME: This check is currently being done in the analysis. It can be
1094 // enabled here only after the parser propagates attributes at
1095 // template specialization definition, not declaration.
1096 //QualType ReturnType;
1097 //
1098 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1099 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1100 //
1101 //} else {
1102 //
1103 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1104 //}
1105 //
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 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1116 Attr.getAttributeSpellingListIndex()));
1117}
1118
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001119
1120static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001121 if (!checkAttributeNumArgs(S, Attr, 1))
1122 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001123
1124 if (!isa<CXXMethodDecl>(D)) {
1125 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1126 Attr.getName() << ExpectedMethod;
1127 return;
1128 }
1129
1130 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1131 return;
1132
1133 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001134 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001135 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1136 StringRef Param = Ident->Ident->getName();
1137 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1138 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1139 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001140 return;
1141 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001142 } else {
1143 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1144 Attr.getName() << AANT_ArgumentIdentifier;
1145 return;
1146 }
1147
1148 D->addAttr(::new (S.Context)
1149 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1150 Attr.getAttributeSpellingListIndex()));
1151}
1152
1153static void handleTestsTypestateAttr(Sema &S, Decl *D,
1154 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001155 if (!checkAttributeNumArgs(S, Attr, 1))
1156 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001157
1158 if (!isa<CXXMethodDecl>(D)) {
1159 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1160 Attr.getName() << ExpectedMethod;
1161 return;
1162 }
1163
1164 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1165 return;
1166
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001167 TestsTypestateAttr::ConsumedState TestState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001168 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001169 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1170 StringRef Param = Ident->Ident->getName();
1171 if (!TestsTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
1172 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1173 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001174 return;
1175 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001176 } else {
1177 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1178 Attr.getName() << AANT_ArgumentIdentifier;
1179 return;
1180 }
1181
1182 D->addAttr(::new (S.Context)
1183 TestsTypestateAttr(Attr.getRange(), S.Context, TestState,
1184 Attr.getAttributeSpellingListIndex()));
1185}
1186
Chandler Carruth1b03c872011-07-02 00:01:44 +00001187static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1188 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001189 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1190 if (TD == 0) {
1191 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1192 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001193 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001194 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001195 }
Mike Stumpbf916502009-07-24 19:02:52 +00001196
Richard Smitha4fa9002013-01-13 02:11:23 +00001197 // Remember this typedef decl, we will need it later for diagnostics.
1198 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001199}
1200
Chandler Carruth1b03c872011-07-02 00:01:44 +00001201static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001202 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001203 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001204 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001205 // If the alignment is less than or equal to 8 bits, the packed attribute
1206 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001207 if (!FD->getType()->isDependentType() &&
1208 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001209 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001210 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001211 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001212 else
Michael Han51d8c522013-01-24 16:46:58 +00001213 FD->addAttr(::new (S.Context)
1214 PackedAttr(Attr.getRange(), S.Context,
1215 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001216 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001217 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001218}
1219
Chandler Carruth1b03c872011-07-02 00:01:44 +00001220static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001221 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001222 RD->addAttr(::new (S.Context)
1223 MsStructAttr(Attr.getRange(), S.Context,
1224 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001225 else
1226 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1227}
1228
Chandler Carruth1b03c872011-07-02 00:01:44 +00001229static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001230 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001231 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001232 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001233 D->addAttr(::new (S.Context)
1234 IBActionAttr(Attr.getRange(), S.Context,
1235 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001236 return;
1237 }
1238
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001239 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001240}
1241
Ted Kremenek2f041d02011-09-29 07:02:25 +00001242static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1243 // The IBOutlet/IBOutletCollection attributes only apply to instance
1244 // variables or properties of Objective-C classes. The outlet must also
1245 // have an object reference type.
1246 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1247 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001248 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001249 << Attr.getName() << VD->getType() << 0;
1250 return false;
1251 }
1252 }
1253 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1254 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001255 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001256 << Attr.getName() << PD->getType() << 1;
1257 return false;
1258 }
1259 }
1260 else {
1261 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1262 return false;
1263 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001264
Ted Kremenek2f041d02011-09-29 07:02:25 +00001265 return true;
1266}
1267
Chandler Carruth1b03c872011-07-02 00:01:44 +00001268static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001269 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001270 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001271
Michael Han51d8c522013-01-24 16:46:58 +00001272 D->addAttr(::new (S.Context)
1273 IBOutletAttr(Attr.getRange(), S.Context,
1274 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001275}
1276
Chandler Carruth1b03c872011-07-02 00:01:44 +00001277static void handleIBOutletCollection(Sema &S, Decl *D,
1278 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001279
1280 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001281 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001282 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1283 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001284 return;
1285 }
1286
Ted Kremenek2f041d02011-09-29 07:02:25 +00001287 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001288 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001289
Aaron Ballman624421f2013-08-31 01:11:41 +00001290 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1291 IdentifierInfo *II;
1292 SourceLocation ILS;
1293 if (IL) {
1294 II = IL->Ident;
1295 ILS = IL->Loc;
1296 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001297 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001298 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001299
John McCallb3d87482010-08-24 05:47:05 +00001300 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001301 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001302 if (!TypeRep) {
1303 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1304 return;
1305 }
John McCallb3d87482010-08-24 05:47:05 +00001306 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001307 // Diagnose use of non-object type in iboutletcollection attribute.
1308 // FIXME. Gnu attribute extension ignores use of builtin types in
1309 // attributes. So, __attribute__((iboutletcollection(char))) will be
1310 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001311 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001312 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1313 return;
1314 }
Michael Han51d8c522013-01-24 16:46:58 +00001315 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001316 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001317 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001318}
1319
Chandler Carruthd309c812011-07-01 23:49:16 +00001320static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001321 if (const RecordType *UT = T->getAsUnionType())
1322 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1323 RecordDecl *UD = UT->getDecl();
1324 for (RecordDecl::field_iterator it = UD->field_begin(),
1325 itend = UD->field_end(); it != itend; ++it) {
1326 QualType QT = it->getType();
1327 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1328 T = QT;
1329 return;
1330 }
1331 }
1332 }
1333}
1334
Nuno Lopes587de5b2012-05-24 00:22:00 +00001335static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001336 if (!isFunctionOrMethod(D)) {
1337 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001338 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001339 return;
1340 }
1341
Nuno Lopes587de5b2012-05-24 00:22:00 +00001342 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1343 return;
1344
Nuno Lopes587de5b2012-05-24 00:22:00 +00001345 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001346 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001347 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001348 uint64_t Idx;
1349 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1350 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001351 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001352
1353 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001354 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001355 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001356 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1357 << Attr.getName() << AANT_ArgumentIntegerConstant
1358 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001359 return;
1360 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001361 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001362 }
1363
1364 // check if the function returns a pointer
1365 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1366 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001367 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001368 }
1369
Michael Han51d8c522013-01-24 16:46:58 +00001370 D->addAttr(::new (S.Context)
1371 AllocSizeAttr(Attr.getRange(), S.Context,
1372 SizeArgs.data(), SizeArgs.size(),
1373 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001374}
1375
Chandler Carruth1b03c872011-07-02 00:01:44 +00001376static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001377 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1378 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001379 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001380 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001381 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001382 return;
1383 }
Mike Stumpbf916502009-07-24 19:02:52 +00001384
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001385 SmallVector<unsigned, 8> NonNullArgs;
1386 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001387 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001388 uint64_t Idx;
1389 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1390 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001391 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001392
1393 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001394 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001395 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001396
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001397 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001398 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001399 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001400 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001401 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001402 }
Mike Stumpbf916502009-07-24 19:02:52 +00001403
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001404 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001405 }
Mike Stumpbf916502009-07-24 19:02:52 +00001406
1407 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1408 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001409 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001410 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1411 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001412 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001413 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001414 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001415 }
Mike Stumpbf916502009-07-24 19:02:52 +00001416
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001417 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001418 if (NonNullArgs.empty()) {
1419 // Warn the trivial case only if attribute is not coming from a
1420 // macro instantiation.
1421 if (Attr.getLoc().isFileID())
1422 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001423 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001424 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001425 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001426
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001427 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001428 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001429 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001430 D->addAttr(::new (S.Context)
1431 NonNullAttr(Attr.getRange(), S.Context, start, size,
1432 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001433}
1434
Aaron Ballmane1668a32013-09-16 18:11:41 +00001435static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1436 switch (K) {
1437 case OwnershipAttr::Holds: return "'ownership_holds'";
1438 case OwnershipAttr::Takes: return "'ownership_takes'";
1439 case OwnershipAttr::Returns: return "'ownership_returns'";
1440 }
1441 llvm_unreachable("unknown ownership");
1442}
1443
Chandler Carruth1b03c872011-07-02 00:01:44 +00001444static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001445 // This attribute must be applied to a function declaration. The first
1446 // argument to the attribute must be an identifier, the name of the resource,
1447 // for example: malloc. The following arguments must be argument indexes, the
1448 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001449 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballmane1668a32013-09-16 18:11:41 +00001450 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose2a479922010-08-12 08:54:03 +00001451 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001452
Aaron Ballman624421f2013-08-31 01:11:41 +00001453 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001454 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmane1668a32013-09-16 18:11:41 +00001455 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001456 return;
1457 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001458
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001459 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001460 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001461 switch (AL.getKind()) {
1462 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001463 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001464 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001465 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001466 return;
1467 }
Jordy Rose2a479922010-08-12 08:54:03 +00001468 break;
1469 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001470 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001471 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001472 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001473 return;
1474 }
Jordy Rose2a479922010-08-12 08:54:03 +00001475 break;
1476 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001477 K = OwnershipAttr::Returns;
Aaron Ballmane1668a32013-09-16 18:11:41 +00001478
Aaron Ballman624421f2013-08-31 01:11:41 +00001479 if (AL.getNumArgs() > 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001480 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001481 return;
1482 }
Jordy Rose2a479922010-08-12 08:54:03 +00001483 break;
1484 default:
1485 // This should never happen given how we are called.
1486 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001487 }
1488
Chandler Carruth87c44602011-07-01 23:49:12 +00001489 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001490 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1491 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001492 return;
1493 }
1494
Aaron Ballman624421f2013-08-31 01:11:41 +00001495 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001496
1497 // Normalize the argument, __foo__ becomes foo.
1498 if (Module.startswith("__") && Module.endswith("__"))
1499 Module = Module.substr(2, Module.size() - 4);
1500
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001501 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001502 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1503 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001504 uint64_t Idx;
1505 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001506 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001507 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001508
Aaron Ballmane1668a32013-09-16 18:11:41 +00001509 // Is the function argument a pointer type?
1510 QualType T = getFunctionOrMethodArgType(D, Idx);
1511 int Err = -1; // No error
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001512 switch (K) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001513 case OwnershipAttr::Takes:
1514 case OwnershipAttr::Holds:
1515 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1516 Err = 0;
1517 break;
1518 case OwnershipAttr::Returns:
1519 if (!T->isIntegerType())
1520 Err = 1;
1521 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001522 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001523 if (-1 != Err) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001524 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballmane1668a32013-09-16 18:11:41 +00001525 << Ex->getSourceRange();
1526 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001527 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001528
1529 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001530 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballmane1668a32013-09-16 18:11:41 +00001531 i = D->specific_attr_begin<OwnershipAttr>(),
1532 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1533 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1534 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1535 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1536 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1537 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001538 }
1539 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001540 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001541 }
1542
1543 unsigned* start = OwnershipArgs.data();
1544 unsigned size = OwnershipArgs.size();
1545 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001546
Michael Han51d8c522013-01-24 16:46:58 +00001547 D->addAttr(::new (S.Context)
1548 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1549 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001550}
1551
Chandler Carruth1b03c872011-07-02 00:01:44 +00001552static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001553 // Check the attribute arguments.
1554 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001555 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1556 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001557 return;
1558 }
1559
Chandler Carruth87c44602011-07-01 23:49:12 +00001560 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001561 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001562 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001563 return;
1564 }
1565
Chandler Carruth87c44602011-07-01 23:49:12 +00001566 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001567
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001568 // gcc rejects
1569 // class c {
1570 // static int a __attribute__((weakref ("v2")));
1571 // static int b() __attribute__((weakref ("f3")));
1572 // };
1573 // and ignores the attributes of
1574 // void f(void) {
1575 // static int a __attribute__((weakref ("v2")));
1576 // }
1577 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001578 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001579 if (!Ctx->isFileContext()) {
1580 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001581 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001582 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001583 }
1584
1585 // The GCC manual says
1586 //
1587 // At present, a declaration to which `weakref' is attached can only
1588 // be `static'.
1589 //
1590 // It also says
1591 //
1592 // Without a TARGET,
1593 // given as an argument to `weakref' or to `alias', `weakref' is
1594 // equivalent to `weak'.
1595 //
1596 // gcc 4.4.1 will accept
1597 // int a7 __attribute__((weakref));
1598 // as
1599 // int a7 __attribute__((weak));
1600 // This looks like a bug in gcc. We reject that for now. We should revisit
1601 // it if this behaviour is actually used.
1602
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001603 // GCC rejects
1604 // static ((alias ("y"), weakref)).
1605 // Should we? How to check that weakref is before or after alias?
1606
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001607 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1608 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1609 // StringRef parameter it was given anyway.
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001610 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001611 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001612 // GCC will accept anything as the argument of weakref. Should we
1613 // check for an existing decl?
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001614 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1615 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001616
Michael Han51d8c522013-01-24 16:46:58 +00001617 D->addAttr(::new (S.Context)
1618 WeakRefAttr(Attr.getRange(), S.Context,
1619 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001620}
1621
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001622static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1623 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001624 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001625 return;
1626
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001627 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001628 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1629 return;
1630 }
1631
Chris Lattner6b6b5372008-06-26 18:38:35 +00001632 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001633
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001634 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001635 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001636}
1637
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001638static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001639 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1641 << Attr.getName() << ExpectedFunctionOrMethod;
1642 return;
1643 }
1644
Michael Han51d8c522013-01-24 16:46:58 +00001645 D->addAttr(::new (S.Context)
1646 MinSizeAttr(Attr.getRange(), S.Context,
1647 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001648}
1649
Benjamin Krameree409a92012-05-12 21:10:52 +00001650static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001651 if (!isa<FunctionDecl>(D)) {
1652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1653 << Attr.getName() << ExpectedFunction;
1654 return;
1655 }
1656
1657 if (D->hasAttr<HotAttr>()) {
1658 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1659 << Attr.getName() << "hot";
1660 return;
1661 }
1662
Michael Han51d8c522013-01-24 16:46:58 +00001663 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1664 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001665}
1666
1667static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001668 if (!isa<FunctionDecl>(D)) {
1669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1670 << Attr.getName() << ExpectedFunction;
1671 return;
1672 }
1673
1674 if (D->hasAttr<ColdAttr>()) {
1675 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1676 << Attr.getName() << "cold";
1677 return;
1678 }
1679
Michael Han51d8c522013-01-24 16:46:58 +00001680 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1681 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001682}
1683
Chandler Carruth1b03c872011-07-02 00:01:44 +00001684static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001685 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001686 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001687 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001688 return;
1689 }
1690
Michael Han51d8c522013-01-24 16:46:58 +00001691 D->addAttr(::new (S.Context)
1692 NakedAttr(Attr.getRange(), S.Context,
1693 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001694}
1695
Chandler Carruth1b03c872011-07-02 00:01:44 +00001696static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1697 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001698 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001699 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001700 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001701 return;
1702 }
Mike Stumpbf916502009-07-24 19:02:52 +00001703
Michael Han51d8c522013-01-24 16:46:58 +00001704 D->addAttr(::new (S.Context)
1705 AlwaysInlineAttr(Attr.getRange(), S.Context,
1706 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001707}
1708
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001709static void handleTLSModelAttr(Sema &S, Decl *D,
1710 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001711 StringRef Model;
1712 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001713 // Check that it is a string.
Tim Northoverf8aebef2013-10-01 14:34:18 +00001714 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001715 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001716
Richard Smith38afbc72013-04-13 02:43:54 +00001717 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001718 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1719 << Attr.getName() << ExpectedTLSVar;
1720 return;
1721 }
1722
1723 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001724 if (Model != "global-dynamic" && Model != "local-dynamic"
1725 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001726 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001727 return;
1728 }
1729
Michael Han51d8c522013-01-24 16:46:58 +00001730 D->addAttr(::new (S.Context)
1731 TLSModelAttr(Attr.getRange(), S.Context, Model,
1732 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001733}
1734
Chandler Carruth1b03c872011-07-02 00:01:44 +00001735static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001736 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001737 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001738 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001739 D->addAttr(::new (S.Context)
1740 MallocAttr(Attr.getRange(), S.Context,
1741 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001742 return;
1743 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001744 }
1745
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001746 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001747}
1748
Chandler Carruth1b03c872011-07-02 00:01:44 +00001749static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001750 D->addAttr(::new (S.Context)
1751 MayAliasAttr(Attr.getRange(), S.Context,
1752 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001753}
1754
Chandler Carruth1b03c872011-07-02 00:01:44 +00001755static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001756 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001757 D->addAttr(::new (S.Context)
1758 NoCommonAttr(Attr.getRange(), S.Context,
1759 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001760 else
1761 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001762 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001763}
1764
Chandler Carruth1b03c872011-07-02 00:01:44 +00001765static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001766 if (S.LangOpts.CPlusPlus) {
1767 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1768 return;
1769 }
1770
Chandler Carruth87c44602011-07-01 23:49:12 +00001771 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001772 D->addAttr(::new (S.Context)
1773 CommonAttr(Attr.getRange(), S.Context,
1774 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001775 else
1776 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001777 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001778}
1779
Chandler Carruth1b03c872011-07-02 00:01:44 +00001780static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001781 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001782
1783 if (S.CheckNoReturnAttr(attr)) return;
1784
Chandler Carruth87c44602011-07-01 23:49:12 +00001785 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001786 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001787 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001788 return;
1789 }
1790
Michael Han51d8c522013-01-24 16:46:58 +00001791 D->addAttr(::new (S.Context)
1792 NoReturnAttr(attr.getRange(), S.Context,
1793 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001794}
1795
1796bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001797 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001798 attr.setInvalid();
1799 return true;
1800 }
1801
1802 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001803}
1804
Chandler Carruth1b03c872011-07-02 00:01:44 +00001805static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1806 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001807
1808 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1809 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001810 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1811 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001812 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1813 && !VD->getType()->isFunctionPointerType())) {
1814 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001815 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001816 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001817 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001818 return;
1819 }
1820 }
1821
Michael Han51d8c522013-01-24 16:46:58 +00001822 D->addAttr(::new (S.Context)
1823 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1824 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001825}
1826
Richard Smithcd8ab512013-01-17 01:30:42 +00001827static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1828 const AttributeList &Attr) {
1829 // C++11 [dcl.attr.noreturn]p1:
1830 // The attribute may be applied to the declarator-id in a function
1831 // declaration.
1832 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1833 if (!FD) {
1834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1835 << Attr.getName() << ExpectedFunctionOrMethod;
1836 return;
1837 }
1838
Michael Han51d8c522013-01-24 16:46:58 +00001839 D->addAttr(::new (S.Context)
1840 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1841 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001842}
1843
John Thompson35cc9622010-08-09 21:53:52 +00001844// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001845static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001846/*
1847 Returning a Vector Class in Registers
1848
Eric Christopherf48f3672010-12-01 22:13:54 +00001849 According to the PPU ABI specifications, a class with a single member of
1850 vector type is returned in memory when used as the return value of a function.
1851 This results in inefficient code when implementing vector classes. To return
1852 the value in a single vector register, add the vecreturn attribute to the
1853 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001854
1855 Example:
1856
1857 struct Vector
1858 {
1859 __vector float xyzw;
1860 } __attribute__((vecreturn));
1861
1862 Vector Add(Vector lhs, Vector rhs)
1863 {
1864 Vector result;
1865 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1866 return result; // This will be returned in a register
1867 }
1868*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001869 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001870 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001871 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001872 return;
1873 }
1874
Chandler Carruth87c44602011-07-01 23:49:12 +00001875 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001876 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1877 return;
1878 }
1879
Chandler Carruth87c44602011-07-01 23:49:12 +00001880 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001881 int count = 0;
1882
1883 if (!isa<CXXRecordDecl>(record)) {
1884 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1885 return;
1886 }
1887
1888 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1889 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1890 return;
1891 }
1892
Eric Christopherf48f3672010-12-01 22:13:54 +00001893 for (RecordDecl::field_iterator iter = record->field_begin();
1894 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001895 if ((count == 1) || !iter->getType()->isVectorType()) {
1896 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1897 return;
1898 }
1899 count++;
1900 }
1901
Michael Han51d8c522013-01-24 16:46:58 +00001902 D->addAttr(::new (S.Context)
1903 VecReturnAttr(Attr.getRange(), S.Context,
1904 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001905}
1906
Richard Smith3a2b7a12013-01-28 22:42:45 +00001907static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1908 const AttributeList &Attr) {
1909 if (isa<ParmVarDecl>(D)) {
1910 // [[carries_dependency]] can only be applied to a parameter if it is a
1911 // parameter of a function declaration or lambda.
1912 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1913 S.Diag(Attr.getLoc(),
1914 diag::err_carries_dependency_param_not_function_decl);
1915 return;
1916 }
1917 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001919 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001920 return;
1921 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001922
1923 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1924 Attr.getRange(), S.Context,
1925 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001926}
1927
Chandler Carruth1b03c872011-07-02 00:01:44 +00001928static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001929 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001930 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001931 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001932 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001933 return;
1934 }
Mike Stumpbf916502009-07-24 19:02:52 +00001935
Michael Han51d8c522013-01-24 16:46:58 +00001936 D->addAttr(::new (S.Context)
1937 UnusedAttr(Attr.getRange(), S.Context,
1938 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001939}
1940
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001941static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1942 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001943 if (!isa<FunctionDecl>(D)) {
1944 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1945 << Attr.getName() << ExpectedFunction;
1946 return;
1947 }
1948
Michael Han51d8c522013-01-24 16:46:58 +00001949 D->addAttr(::new (S.Context)
1950 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1951 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001952}
1953
Chandler Carruth1b03c872011-07-02 00:01:44 +00001954static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001955 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001956 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001957 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1958 return;
1959 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001960 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001961 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001962 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001963 return;
1964 }
Mike Stumpbf916502009-07-24 19:02:52 +00001965
Michael Han51d8c522013-01-24 16:46:58 +00001966 D->addAttr(::new (S.Context)
1967 UsedAttr(Attr.getRange(), S.Context,
1968 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001969}
1970
Chandler Carruth1b03c872011-07-02 00:01:44 +00001971static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001972 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001973 if (Attr.getNumArgs() > 1) {
1974 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001975 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001976 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001977
1978 int priority = 65535; // FIXME: Do not hardcode such constants.
1979 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001980 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001981 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001982 if (E->isTypeDependent() || E->isValueDependent() ||
1983 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001984 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001985 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001986 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001987 return;
1988 }
1989 priority = Idx.getZExtValue();
1990 }
Mike Stumpbf916502009-07-24 19:02:52 +00001991
Chandler Carruth87c44602011-07-01 23:49:12 +00001992 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001993 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001994 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001995 return;
1996 }
1997
Michael Han51d8c522013-01-24 16:46:58 +00001998 D->addAttr(::new (S.Context)
1999 ConstructorAttr(Attr.getRange(), S.Context, priority,
2000 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002001}
2002
Chandler Carruth1b03c872011-07-02 00:01:44 +00002003static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002004 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002005 if (Attr.getNumArgs() > 1) {
2006 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002007 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002008 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002009
2010 int priority = 65535; // FIXME: Do not hardcode such constants.
2011 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002012 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002013 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002014 if (E->isTypeDependent() || E->isValueDependent() ||
2015 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002016 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002017 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002018 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002019 return;
2020 }
2021 priority = Idx.getZExtValue();
2022 }
Mike Stumpbf916502009-07-24 19:02:52 +00002023
Chandler Carruth87c44602011-07-01 23:49:12 +00002024 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002025 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002026 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002027 return;
2028 }
2029
Michael Han51d8c522013-01-24 16:46:58 +00002030 D->addAttr(::new (S.Context)
2031 DestructorAttr(Attr.getRange(), S.Context, priority,
2032 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002033}
2034
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002035template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002036static void handleAttrWithMessage(Sema &S, Decl *D,
2037 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002038 unsigned NumArgs = Attr.getNumArgs();
2039 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002040 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002041 return;
2042 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002043
2044 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002045 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002046 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002047 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002048
Michael Han51d8c522013-01-24 16:46:58 +00002049 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2050 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002051}
2052
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002053static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2054 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002055 D->addAttr(::new (S.Context)
2056 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2057 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002058}
2059
Patrick Beardb2f68202012-04-06 18:12:22 +00002060static void handleObjCRootClassAttr(Sema &S, Decl *D,
2061 const AttributeList &Attr) {
2062 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002063 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2064 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002065 return;
2066 }
2067
Michael Han51d8c522013-01-24 16:46:58 +00002068 D->addAttr(::new (S.Context)
2069 ObjCRootClassAttr(Attr.getRange(), S.Context,
2070 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002071}
2072
Michael Han51d8c522013-01-24 16:46:58 +00002073static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2074 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002075 if (!isa<ObjCInterfaceDecl>(D)) {
2076 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2077 return;
2078 }
2079
Michael Han51d8c522013-01-24 16:46:58 +00002080 D->addAttr(::new (S.Context)
2081 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2082 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002083}
2084
Jordy Rosefad5de92012-05-08 03:27:22 +00002085static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2086 IdentifierInfo *Platform,
2087 VersionTuple Introduced,
2088 VersionTuple Deprecated,
2089 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002090 StringRef PlatformName
2091 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2092 if (PlatformName.empty())
2093 PlatformName = Platform->getName();
2094
2095 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2096 // of these steps are needed).
2097 if (!Introduced.empty() && !Deprecated.empty() &&
2098 !(Introduced <= Deprecated)) {
2099 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2100 << 1 << PlatformName << Deprecated.getAsString()
2101 << 0 << Introduced.getAsString();
2102 return true;
2103 }
2104
2105 if (!Introduced.empty() && !Obsoleted.empty() &&
2106 !(Introduced <= Obsoleted)) {
2107 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2108 << 2 << PlatformName << Obsoleted.getAsString()
2109 << 0 << Introduced.getAsString();
2110 return true;
2111 }
2112
2113 if (!Deprecated.empty() && !Obsoleted.empty() &&
2114 !(Deprecated <= Obsoleted)) {
2115 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2116 << 2 << PlatformName << Obsoleted.getAsString()
2117 << 1 << Deprecated.getAsString();
2118 return true;
2119 }
2120
2121 return false;
2122}
2123
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002124/// \brief Check whether the two versions match.
2125///
2126/// If either version tuple is empty, then they are assumed to match. If
2127/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2128static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2129 bool BeforeIsOkay) {
2130 if (X.empty() || Y.empty())
2131 return true;
2132
2133 if (X == Y)
2134 return true;
2135
2136 if (BeforeIsOkay && X < Y)
2137 return true;
2138
2139 return false;
2140}
2141
Rafael Espindola51be6e32013-01-08 22:04:34 +00002142AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002143 IdentifierInfo *Platform,
2144 VersionTuple Introduced,
2145 VersionTuple Deprecated,
2146 VersionTuple Obsoleted,
2147 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002148 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002149 bool Override,
2150 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002151 VersionTuple MergedIntroduced = Introduced;
2152 VersionTuple MergedDeprecated = Deprecated;
2153 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002154 bool FoundAny = false;
2155
Rafael Espindola98ae8342012-05-10 02:50:16 +00002156 if (D->hasAttrs()) {
2157 AttrVec &Attrs = D->getAttrs();
2158 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2159 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2160 if (!OldAA) {
2161 ++i;
2162 continue;
2163 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002164
Rafael Espindola98ae8342012-05-10 02:50:16 +00002165 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2166 if (OldPlatform != Platform) {
2167 ++i;
2168 continue;
2169 }
2170
2171 FoundAny = true;
2172 VersionTuple OldIntroduced = OldAA->getIntroduced();
2173 VersionTuple OldDeprecated = OldAA->getDeprecated();
2174 VersionTuple OldObsoleted = OldAA->getObsoleted();
2175 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002176
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002177 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2178 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2179 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2180 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002181 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002182 if (Override) {
2183 int Which = -1;
2184 VersionTuple FirstVersion;
2185 VersionTuple SecondVersion;
2186 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2187 Which = 0;
2188 FirstVersion = OldIntroduced;
2189 SecondVersion = Introduced;
2190 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2191 Which = 1;
2192 FirstVersion = Deprecated;
2193 SecondVersion = OldDeprecated;
2194 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2195 Which = 2;
2196 FirstVersion = Obsoleted;
2197 SecondVersion = OldObsoleted;
2198 }
2199
2200 if (Which == -1) {
2201 Diag(OldAA->getLocation(),
2202 diag::warn_mismatched_availability_override_unavail)
2203 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2204 } else {
2205 Diag(OldAA->getLocation(),
2206 diag::warn_mismatched_availability_override)
2207 << Which
2208 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2209 << FirstVersion.getAsString() << SecondVersion.getAsString();
2210 }
2211 Diag(Range.getBegin(), diag::note_overridden_method);
2212 } else {
2213 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2214 Diag(Range.getBegin(), diag::note_previous_attribute);
2215 }
2216
Rafael Espindola98ae8342012-05-10 02:50:16 +00002217 Attrs.erase(Attrs.begin() + i);
2218 --e;
2219 continue;
2220 }
2221
2222 VersionTuple MergedIntroduced2 = MergedIntroduced;
2223 VersionTuple MergedDeprecated2 = MergedDeprecated;
2224 VersionTuple MergedObsoleted2 = MergedObsoleted;
2225
2226 if (MergedIntroduced2.empty())
2227 MergedIntroduced2 = OldIntroduced;
2228 if (MergedDeprecated2.empty())
2229 MergedDeprecated2 = OldDeprecated;
2230 if (MergedObsoleted2.empty())
2231 MergedObsoleted2 = OldObsoleted;
2232
2233 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2234 MergedIntroduced2, MergedDeprecated2,
2235 MergedObsoleted2)) {
2236 Attrs.erase(Attrs.begin() + i);
2237 --e;
2238 continue;
2239 }
2240
2241 MergedIntroduced = MergedIntroduced2;
2242 MergedDeprecated = MergedDeprecated2;
2243 MergedObsoleted = MergedObsoleted2;
2244 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002245 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002246 }
2247
2248 if (FoundAny &&
2249 MergedIntroduced == Introduced &&
2250 MergedDeprecated == Deprecated &&
2251 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002252 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002253
Ted Kremenekcb344392013-04-06 00:34:27 +00002254 // Only create a new attribute if !Override, but we want to do
2255 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002256 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002257 MergedDeprecated, MergedObsoleted) &&
2258 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002259 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2260 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002261 Obsoleted, IsUnavailable, Message,
2262 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002263 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002264 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002265}
2266
Chandler Carruth1b03c872011-07-02 00:01:44 +00002267static void handleAvailabilityAttr(Sema &S, Decl *D,
2268 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002269 if (!checkAttributeNumArgs(S, Attr, 1))
2270 return;
2271 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002272 unsigned Index = Attr.getAttributeSpellingListIndex();
2273
Aaron Ballman624421f2013-08-31 01:11:41 +00002274 IdentifierInfo *II = Platform->Ident;
2275 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2276 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2277 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002278
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002279 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2280 if (!ND) {
2281 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2282 return;
2283 }
2284
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002285 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2286 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2287 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002288 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002289 StringRef Str;
Benjamin Kramerc5617142013-09-13 17:31:48 +00002290 if (const StringLiteral *SE =
2291 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002292 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002293
Aaron Ballman624421f2013-08-31 01:11:41 +00002294 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002295 Introduced.Version,
2296 Deprecated.Version,
2297 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002298 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002299 /*Override=*/false,
2300 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002301 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002302 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002303}
2304
John McCalld4c3d662013-02-20 01:54:26 +00002305template <class T>
2306static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2307 typename T::VisibilityType value,
2308 unsigned attrSpellingListIndex) {
2309 T *existingAttr = D->getAttr<T>();
2310 if (existingAttr) {
2311 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2312 if (existingValue == value)
2313 return NULL;
2314 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2315 S.Diag(range.getBegin(), diag::note_previous_attribute);
2316 D->dropAttr<T>();
2317 }
2318 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2319}
2320
Rafael Espindola599f1b72012-05-13 03:25:18 +00002321VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002322 VisibilityAttr::VisibilityType Vis,
2323 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002324 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2325 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002326}
2327
John McCalld4c3d662013-02-20 01:54:26 +00002328TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2329 TypeVisibilityAttr::VisibilityType Vis,
2330 unsigned AttrSpellingListIndex) {
2331 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2332 AttrSpellingListIndex);
2333}
2334
2335static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2336 bool isTypeVisibility) {
2337 // Visibility attributes don't mean anything on a typedef.
2338 if (isa<TypedefNameDecl>(D)) {
2339 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2340 << Attr.getName();
2341 return;
2342 }
2343
2344 // 'type_visibility' can only go on a type or namespace.
2345 if (isTypeVisibility &&
2346 !(isa<TagDecl>(D) ||
2347 isa<ObjCInterfaceDecl>(D) ||
2348 isa<NamespaceDecl>(D))) {
2349 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2350 << Attr.getName() << ExpectedTypeOrNamespace;
2351 return;
2352 }
2353
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002354 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002355 StringRef TypeStr;
2356 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002357 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002358 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002359
Sean Huntcf807c42010-08-18 23:23:40 +00002360 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002361 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002362 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002363 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002364 return;
2365 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002366
2367 // Complain about attempts to use protected visibility on targets
2368 // (like Darwin) that don't support it.
2369 if (type == VisibilityAttr::Protected &&
2370 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2371 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2372 type = VisibilityAttr::Default;
2373 }
Mike Stumpbf916502009-07-24 19:02:52 +00002374
Michael Han51d8c522013-01-24 16:46:58 +00002375 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002376 clang::Attr *newAttr;
2377 if (isTypeVisibility) {
2378 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2379 (TypeVisibilityAttr::VisibilityType) type,
2380 Index);
2381 } else {
2382 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2383 }
2384 if (newAttr)
2385 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002386}
2387
Chandler Carruth1b03c872011-07-02 00:01:44 +00002388static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2389 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002390 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2391 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002392 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002393 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002394 return;
2395 }
2396
Aaron Ballman624421f2013-08-31 01:11:41 +00002397 if (!Attr.isArgIdent(0)) {
2398 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2399 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002400 return;
2401 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002402
Aaron Ballmand0686072013-09-11 19:47:58 +00002403 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2404 ObjCMethodFamilyAttr::FamilyKind F;
2405 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2406 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2407 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002408 return;
2409 }
2410
Aaron Ballmand0686072013-09-11 19:47:58 +00002411 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002412 !method->getResultType()->isObjCObjectPointerType()) {
2413 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2414 << method->getResultType();
2415 // Ignore the attribute.
2416 return;
2417 }
2418
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002419 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002420 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002421}
2422
Chandler Carruth1b03c872011-07-02 00:01:44 +00002423static void handleObjCExceptionAttr(Sema &S, Decl *D,
2424 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002425 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2426 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002427 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2428 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002429 return;
2430 }
Mike Stumpbf916502009-07-24 19:02:52 +00002431
Michael Han51d8c522013-01-24 16:46:58 +00002432 D->addAttr(::new (S.Context)
2433 ObjCExceptionAttr(Attr.getRange(), S.Context,
2434 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002435}
2436
Chandler Carruth1b03c872011-07-02 00:01:44 +00002437static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002438 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002439 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002440 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002441 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2442 return;
2443 }
2444 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002445 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2446 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002447 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002448 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2449 return;
2450 }
2451 }
2452 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002453 // It is okay to include this attribute on properties, e.g.:
2454 //
2455 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2456 //
2457 // In this case it follows tradition and suppresses an error in the above
2458 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002459 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002460 }
Michael Han51d8c522013-01-24 16:46:58 +00002461 D->addAttr(::new (S.Context)
2462 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2463 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002464}
2465
Mike Stumpbf916502009-07-24 19:02:52 +00002466static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002467handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002468 if (!isa<FunctionDecl>(D)) {
2469 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2470 return;
2471 }
2472
Michael Han51d8c522013-01-24 16:46:58 +00002473 D->addAttr(::new (S.Context)
2474 OverloadableAttr(Attr.getRange(), S.Context,
2475 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002476}
2477
Chandler Carruth1b03c872011-07-02 00:01:44 +00002478static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002479 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002481 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002482 return;
2483 }
Mike Stumpbf916502009-07-24 19:02:52 +00002484
Aaron Ballman624421f2013-08-31 01:11:41 +00002485 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002486 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002487 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2488 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2489 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +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 BlocksAttr(Attr.getRange(), S.Context, type,
2495 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002496}
2497
Chandler Carruth1b03c872011-07-02 00:01:44 +00002498static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002499 // check the attribute arguments.
2500 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002501 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002502 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002503 }
2504
John McCall3323fad2011-09-09 07:56:05 +00002505 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002506 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002507 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002508 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002509 if (E->isTypeDependent() || E->isValueDependent() ||
2510 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002512 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002513 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002514 return;
2515 }
Mike Stumpbf916502009-07-24 19:02:52 +00002516
John McCall3323fad2011-09-09 07:56:05 +00002517 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002518 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2519 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002520 return;
2521 }
John McCall3323fad2011-09-09 07:56:05 +00002522
2523 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002524 }
2525
John McCall3323fad2011-09-09 07:56:05 +00002526 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002527 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002528 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002529 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002530 if (E->isTypeDependent() || E->isValueDependent() ||
2531 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002532 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002533 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002534 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002535 return;
2536 }
2537 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002538
John McCall3323fad2011-09-09 07:56:05 +00002539 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002540 // FIXME: This error message could be improved, it would be nice
2541 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002542 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2543 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002544 return;
2545 }
2546 }
2547
Chandler Carruth87c44602011-07-01 23:49:12 +00002548 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002549 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002550 if (isa<FunctionNoProtoType>(FT)) {
2551 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2552 return;
2553 }
Mike Stumpbf916502009-07-24 19:02:52 +00002554
Chris Lattner897cd902009-03-17 23:03:47 +00002555 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002556 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002557 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002558 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002559 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002560 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002561 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002562 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002563 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002564 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2565 if (!BD->isVariadic()) {
2566 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2567 return;
2568 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002569 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002570 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002571 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002572 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002573 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002574 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002575 int m = Ty->isFunctionPointerType() ? 0 : 1;
2576 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002577 return;
2578 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002579 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002580 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002581 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002582 return;
2583 }
Anders Carlsson77091822008-10-05 18:05:59 +00002584 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002585 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002586 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002587 return;
2588 }
Michael Han51d8c522013-01-24 16:46:58 +00002589 D->addAttr(::new (S.Context)
2590 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2591 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002592}
2593
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002594static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002595 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2596 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2597 else
2598 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2599}
2600
Chandler Carruth1b03c872011-07-02 00:01:44 +00002601static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002602 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002603 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002604 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002605 return;
2606 }
Mike Stumpbf916502009-07-24 19:02:52 +00002607
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002608 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2609 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2610 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002611 return;
2612 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002613 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2614 if (MD->getResultType()->isVoidType()) {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2616 << Attr.getName() << 1;
2617 return;
2618 }
2619
Michael Han51d8c522013-01-24 16:46:58 +00002620 D->addAttr(::new (S.Context)
2621 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2622 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002623}
2624
Chandler Carruth1b03c872011-07-02 00:01:44 +00002625static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002626 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002627 if (isa<CXXRecordDecl>(D)) {
2628 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2629 return;
2630 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2632 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002633 return;
2634 }
2635
Chandler Carruth87c44602011-07-01 23:49:12 +00002636 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002637
Michael Han51d8c522013-01-24 16:46:58 +00002638 nd->addAttr(::new (S.Context)
2639 WeakAttr(Attr.getRange(), S.Context,
2640 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002641}
2642
Chandler Carruth1b03c872011-07-02 00:01:44 +00002643static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002644 // weak_import only applies to variable & function declarations.
2645 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002646 if (!D->canBeWeakImported(isDef)) {
2647 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002648 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2649 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002650 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002651 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002652 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002653 // Nothing to warn about here.
2654 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002656 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002657
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002658 return;
2659 }
2660
Michael Han51d8c522013-01-24 16:46:58 +00002661 D->addAttr(::new (S.Context)
2662 WeakImportAttr(Attr.getRange(), S.Context,
2663 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002664}
2665
Tanya Lattner0df579e2012-07-09 22:06:01 +00002666// Handles reqd_work_group_size and work_group_size_hint.
2667static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002668 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002669 unsigned WGSize[3];
2670 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002671 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002672 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002673 if (E->isTypeDependent() || E->isValueDependent() ||
2674 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002675 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2676 << Attr.getName() << AANT_ArgumentIntegerConstant
2677 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002678 return;
2679 }
2680 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2681 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002682
2683 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2684 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2685 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2686 if (!(A->getXDim() == WGSize[0] &&
2687 A->getYDim() == WGSize[1] &&
2688 A->getZDim() == WGSize[2])) {
2689 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2690 Attr.getName();
2691 }
2692 }
2693
2694 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2695 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2696 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2697 if (!(A->getXDim() == WGSize[0] &&
2698 A->getYDim() == WGSize[1] &&
2699 A->getZDim() == WGSize[2])) {
2700 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2701 Attr.getName();
2702 }
2703 }
2704
2705 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2706 D->addAttr(::new (S.Context)
2707 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002708 WGSize[0], WGSize[1], WGSize[2],
2709 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002710 else
2711 D->addAttr(::new (S.Context)
2712 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002713 WGSize[0], WGSize[1], WGSize[2],
2714 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002715}
2716
Joey Gouly37453b92013-03-08 09:42:32 +00002717static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2718 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2719
Aaron Ballman624421f2013-08-31 01:11:41 +00002720 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002721 return;
2722
Aaron Ballman624421f2013-08-31 01:11:41 +00002723 if (!Attr.hasParsedType()) {
2724 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2725 << Attr.getName() << 1;
2726 return;
2727 }
2728
Joey Gouly37453b92013-03-08 09:42:32 +00002729 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2730
2731 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2732 (ParmType->isBooleanType() ||
2733 !ParmType->isIntegralType(S.getASTContext()))) {
2734 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2735 << ParmType;
2736 return;
2737 }
2738
2739 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2740 D->hasAttr<VecTypeHintAttr>()) {
2741 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2742 if (A->getTypeHint() != ParmType) {
2743 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2744 return;
2745 }
2746 }
2747
2748 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2749 ParmType, Attr.getLoc()));
2750}
2751
Rafael Espindola599f1b72012-05-13 03:25:18 +00002752SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002753 StringRef Name,
2754 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002755 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2756 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002757 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002758 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2759 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002760 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002761 }
Michael Han51d8c522013-01-24 16:46:58 +00002762 return ::new (Context) SectionAttr(Range, Context, Name,
2763 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002764}
2765
Chandler Carruth1b03c872011-07-02 00:01:44 +00002766static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002767 // Make sure that there is a string literal as the sections's single
2768 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002769 StringRef Str;
2770 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002771 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002772 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Chris Lattner797c3c42009-08-10 19:03:04 +00002774 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002775 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002776 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002777 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002778 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002779 return;
2780 }
Mike Stump1eb44332009-09-09 15:08:12 +00002781
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002782 // This attribute cannot be applied to local variables.
2783 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002784 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002785 return;
2786 }
Michael Han51d8c522013-01-24 16:46:58 +00002787
2788 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002789 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002790 if (NewAttr)
2791 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002792}
2793
Chris Lattner6b6b5372008-06-26 18:38:35 +00002794
Chandler Carruth1b03c872011-07-02 00:01:44 +00002795static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002796 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002797 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002798 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002799 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002800 D->addAttr(::new (S.Context)
2801 NoThrowAttr(Attr.getRange(), S.Context,
2802 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002803 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002804}
2805
Chandler Carruth1b03c872011-07-02 00:01:44 +00002806static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002807 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002808 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002809 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002810 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002811 D->addAttr(::new (S.Context)
2812 ConstAttr(Attr.getRange(), S.Context,
2813 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002814 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002815}
2816
Chandler Carruth1b03c872011-07-02 00:01:44 +00002817static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002818 D->addAttr(::new (S.Context)
2819 PureAttr(Attr.getRange(), S.Context,
2820 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002821}
2822
Chandler Carruth1b03c872011-07-02 00:01:44 +00002823static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002824 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002825 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002826 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002827 return;
2828 }
Mike Stumpbf916502009-07-24 19:02:52 +00002829
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002830 Expr *E = Attr.getArgAsExpr(0);
2831 SourceLocation Loc = E->getExprLoc();
2832 FunctionDecl *FD = 0;
2833 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002834
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002835 // gcc only allows for simple identifiers. Since we support more than gcc, we
2836 // will warn the user.
2837 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2838 if (DRE->hasQualifier())
2839 S.Diag(Loc, diag::warn_cleanup_ext);
2840 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2841 NI = DRE->getNameInfo();
2842 if (!FD) {
2843 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2844 << NI.getName();
2845 return;
2846 }
2847 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2848 if (ULE->hasExplicitTemplateArgs())
2849 S.Diag(Loc, diag::warn_cleanup_ext);
Mike Stumpbf916502009-07-24 19:02:52 +00002850
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002851 // This will diagnose the case where the function cannot be found.
2852 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2853 NI = ULE->getNameInfo();
2854 } else {
2855 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002856 return;
2857 }
2858
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002859 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002860 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2861 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002862 return;
2863 }
Mike Stumpbf916502009-07-24 19:02:52 +00002864
Anders Carlsson89941c12009-02-07 23:16:50 +00002865 // We're currently more strict than GCC about what function types we accept.
2866 // If this ever proves to be a problem it should be easy to fix.
2867 QualType Ty = S.Context.getPointerType(VD->getType());
2868 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002869 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2870 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002871 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2872 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002873 return;
2874 }
Mike Stumpbf916502009-07-24 19:02:52 +00002875
Michael Han51d8c522013-01-24 16:46:58 +00002876 D->addAttr(::new (S.Context)
2877 CleanupAttr(Attr.getRange(), S.Context, FD,
2878 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002879}
2880
Mike Stumpbf916502009-07-24 19:02:52 +00002881/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002882/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002883static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002884 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002885 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002886 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002887 return;
2888 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002889
Aaron Ballman624421f2013-08-31 01:11:41 +00002890 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002891 uint64_t ArgIdx;
2892 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2893 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002894 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002895
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002896 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002897 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002898
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002899 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2900 if (not_nsstring_type &&
2901 !isCFStringType(Ty, S.Context) &&
2902 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002903 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002904 // FIXME: Should highlight the actual expression that has the wrong type.
2905 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002906 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002907 << IdxExpr->getSourceRange();
2908 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002909 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002910 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002911 if (!isNSStringType(Ty, S.Context) &&
2912 !isCFStringType(Ty, S.Context) &&
2913 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002914 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002915 // FIXME: Should highlight the actual expression that has the wrong type.
2916 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002917 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002918 << IdxExpr->getSourceRange();
2919 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002920 }
2921
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002922 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2923 // because that has corrected for the implicit this parameter, and is zero-
2924 // based. The attribute expects what the user wrote explicitly.
2925 llvm::APSInt Val;
2926 IdxExpr->EvaluateAsInt(Val, S.Context);
2927
Michael Han51d8c522013-01-24 16:46:58 +00002928 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002929 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002930 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002931}
2932
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002933enum FormatAttrKind {
2934 CFStringFormat,
2935 NSStringFormat,
2936 StrftimeFormat,
2937 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002938 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002939 InvalidFormat
2940};
2941
2942/// getFormatAttrKind - Map from format attribute names to supported format
2943/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002944static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002945 return llvm::StringSwitch<FormatAttrKind>(Format)
2946 // Check for formats that get handled specially.
2947 .Case("NSString", NSStringFormat)
2948 .Case("CFString", CFStringFormat)
2949 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002950
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002951 // Otherwise, check for supported formats.
2952 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2953 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2954 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002955
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002956 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2957 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002958}
2959
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002960/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002961/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002962static void handleInitPriorityAttr(Sema &S, Decl *D,
2963 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002964 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002965 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2966 return;
2967 }
2968
Chandler Carruth87c44602011-07-01 23:49:12 +00002969 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002970 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2971 Attr.setInvalid();
2972 return;
2973 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002974 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002975 if (S.Context.getAsArrayType(T))
2976 T = S.Context.getBaseElementType(T);
2977 if (!T->getAs<RecordType>()) {
2978 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2979 Attr.setInvalid();
2980 return;
2981 }
2982
Aaron Ballman624421f2013-08-31 01:11:41 +00002983 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002984
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002985 llvm::APSInt priority(32);
2986 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2987 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002988 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2989 << Attr.getName() << AANT_ArgumentIntegerConstant
2990 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002991 Attr.setInvalid();
2992 return;
2993 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002994 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002995 if (prioritynum < 101 || prioritynum > 65535) {
2996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2997 << priorityExpr->getSourceRange();
2998 Attr.setInvalid();
2999 return;
3000 }
Michael Han51d8c522013-01-24 16:46:58 +00003001 D->addAttr(::new (S.Context)
3002 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3003 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003004}
3005
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003006FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3007 IdentifierInfo *Format, int FormatIdx,
3008 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003009 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003010 // Check whether we already have an equivalent format attribute.
3011 for (specific_attr_iterator<FormatAttr>
3012 i = D->specific_attr_begin<FormatAttr>(),
3013 e = D->specific_attr_end<FormatAttr>();
3014 i != e ; ++i) {
3015 FormatAttr *f = *i;
3016 if (f->getType() == Format &&
3017 f->getFormatIdx() == FormatIdx &&
3018 f->getFirstArg() == FirstArg) {
3019 // If we don't have a valid location for this attribute, adopt the
3020 // location.
3021 if (f->getLocation().isInvalid())
3022 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003023 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003024 }
3025 }
3026
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003027 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3028 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003029}
3030
Mike Stumpbf916502009-07-24 19:02:52 +00003031/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003032/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003033static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003034 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003036 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003037 return;
3038 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003039
Chandler Carruth87c44602011-07-01 23:49:12 +00003040 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003041 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003042 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003043 return;
3044 }
3045
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003046 // In C++ the implicit 'this' function parameter also counts, and they are
3047 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003048 bool HasImplicitThisParam = isInstanceMethod(D);
3049 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003050 unsigned FirstIdx = 1;
3051
Aaron Ballman624421f2013-08-31 01:11:41 +00003052 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3053 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003054
3055 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003056 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003057 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003058 // If we've modified the string name, we need a new identifier for it.
3059 II = &S.Context.Idents.get(Format);
3060 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003061
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003062 // Check for supported formats.
3063 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003064
3065 if (Kind == IgnoredFormat)
3066 return;
3067
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003068 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003069 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003070 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003071 return;
3072 }
3073
3074 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003075 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003076 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003077 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3078 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003079 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003080 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003081 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003082 return;
3083 }
3084
3085 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003087 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003088 return;
3089 }
3090
3091 // FIXME: Do we need to bounds check?
3092 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003093
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003094 if (HasImplicitThisParam) {
3095 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003096 S.Diag(Attr.getLoc(),
3097 diag::err_format_attribute_implicit_this_format_string)
3098 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003099 return;
3100 }
3101 ArgIdx--;
3102 }
Mike Stump1eb44332009-09-09 15:08:12 +00003103
Chris Lattner6b6b5372008-06-26 18:38:35 +00003104 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003105 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003106
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003107 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003108 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003109 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3110 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003111 return;
3112 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003113 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003114 // FIXME: do we need to check if the type is NSString*? What are the
3115 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003116 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003117 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003118 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3119 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003120 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003121 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003122 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003123 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003124 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003125 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3126 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003127 return;
3128 }
3129
3130 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003131 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003132 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003133 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3134 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003136 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003137 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003138 return;
3139 }
3140
3141 // check if the function is variadic if the 3rd argument non-zero
3142 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003143 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003144 ++NumArgs; // +1 for ...
3145 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003146 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003147 return;
3148 }
3149 }
3150
Chris Lattner3c73c412008-11-19 08:23:25 +00003151 // strftime requires FirstArg to be 0 because it doesn't read from any
3152 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003153 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003154 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003155 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3156 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003157 return;
3158 }
3159 // if 0 it disables parameter checking (to use with e.g. va_list)
3160 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003162 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003163 return;
3164 }
3165
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003166 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003167 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003168 FirstArg.getZExtValue(),
3169 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003170 if (NewAttr)
3171 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003172}
3173
Chandler Carruth1b03c872011-07-02 00:01:44 +00003174static void handleTransparentUnionAttr(Sema &S, Decl *D,
3175 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003176 // Try to find the underlying union declaration.
3177 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003178 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003179 if (TD && TD->getUnderlyingType()->isUnionType())
3180 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3181 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003182 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003183
3184 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003185 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003186 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003187 return;
3188 }
3189
John McCall5e1cdac2011-10-07 06:10:15 +00003190 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003191 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003192 diag::warn_transparent_union_attribute_not_definition);
3193 return;
3194 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003195
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003196 RecordDecl::field_iterator Field = RD->field_begin(),
3197 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003198 if (Field == FieldEnd) {
3199 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3200 return;
3201 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003202
David Blaikie581deb32012-06-06 20:45:41 +00003203 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003204 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003205 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003206 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003207 diag::warn_transparent_union_attribute_floating)
3208 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003209 return;
3210 }
3211
3212 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3213 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3214 for (; Field != FieldEnd; ++Field) {
3215 QualType FieldType = Field->getType();
3216 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3217 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3218 // Warn if we drop the attribute.
3219 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003220 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003221 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003222 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003223 diag::warn_transparent_union_attribute_field_size_align)
3224 << isSize << Field->getDeclName() << FieldBits;
3225 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003226 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003227 diag::note_transparent_union_first_field_size_align)
3228 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003229 return;
3230 }
3231 }
3232
Michael Han51d8c522013-01-24 16:46:58 +00003233 RD->addAttr(::new (S.Context)
3234 TransparentUnionAttr(Attr.getRange(), S.Context,
3235 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003236}
3237
Chandler Carruth1b03c872011-07-02 00:01:44 +00003238static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003239 // Make sure that there is a string literal as the annotation's single
3240 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003241 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003242 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003243 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003244
3245 // Don't duplicate annotations that are already set.
3246 for (specific_attr_iterator<AnnotateAttr>
3247 i = D->specific_attr_begin<AnnotateAttr>(),
3248 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003249 if ((*i)->getAnnotation() == Str)
3250 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003251 }
Michael Han51d8c522013-01-24 16:46:58 +00003252
3253 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003254 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003255 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003256}
3257
Chandler Carruth1b03c872011-07-02 00:01:44 +00003258static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003259 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003260 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003261 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3262 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003263 return;
3264 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003265
Richard Smithbe507b62013-02-01 08:12:08 +00003266 if (Attr.getNumArgs() == 0) {
3267 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3268 true, 0, Attr.getAttributeSpellingListIndex()));
3269 return;
3270 }
3271
Aaron Ballman624421f2013-08-31 01:11:41 +00003272 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003273 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3274 S.Diag(Attr.getEllipsisLoc(),
3275 diag::err_pack_expansion_without_parameter_packs);
3276 return;
3277 }
3278
3279 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3280 return;
3281
3282 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3283 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003284}
3285
3286void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003287 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003288 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3289 SourceLocation AttrLoc = AttrRange.getBegin();
3290
Richard Smith4cd81c52013-01-29 09:02:09 +00003291 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003292 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003293 // C++11 [dcl.align]p1:
3294 // An alignment-specifier may be applied to a variable or to a class
3295 // data member, but it shall not be applied to a bit-field, a function
3296 // parameter, the formal parameter of a catch clause, or a variable
3297 // declared with the register storage class specifier. An
3298 // alignment-specifier may also be applied to the declaration of a class
3299 // or enumeration type.
3300 // C11 6.7.5/2:
3301 // An alignment attribute shall not be specified in a declaration of
3302 // a typedef, or a bit-field, or a function, or a parameter, or an
3303 // object declared with the register storage-class specifier.
3304 int DiagKind = -1;
3305 if (isa<ParmVarDecl>(D)) {
3306 DiagKind = 0;
3307 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3308 if (VD->getStorageClass() == SC_Register)
3309 DiagKind = 1;
3310 if (VD->isExceptionVariable())
3311 DiagKind = 2;
3312 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3313 if (FD->isBitField())
3314 DiagKind = 3;
3315 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003316 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3317 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003318 << (TmpAttr.isC11() ? ExpectedVariableOrField
3319 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003320 return;
3321 }
3322 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003323 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003324 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003325 return;
3326 }
3327 }
3328
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003329 if (E->isTypeDependent() || E->isValueDependent()) {
3330 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003331 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3332 AA->setPackExpansion(IsPackExpansion);
3333 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003334 return;
3335 }
Michael Hana31f65b2013-02-01 01:19:17 +00003336
Sean Huntcf807c42010-08-18 23:23:40 +00003337 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003338 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003339 ExprResult ICE
3340 = VerifyIntegerConstantExpression(E, &Alignment,
3341 diag::err_aligned_attribute_argument_not_int,
3342 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003343 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003344 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003345
3346 // C++11 [dcl.align]p2:
3347 // -- if the constant expression evaluates to zero, the alignment
3348 // specifier shall have no effect
3349 // C11 6.7.5p6:
3350 // An alignment specification of zero has no effect.
3351 if (!(TmpAttr.isAlignas() && !Alignment) &&
3352 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003353 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3354 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003355 return;
3356 }
Michael Hana31f65b2013-02-01 01:19:17 +00003357
Richard Smithbe507b62013-02-01 08:12:08 +00003358 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003359 // We've already verified it's a power of 2, now let's make sure it's
3360 // 8192 or less.
3361 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003362 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003363 << E->getSourceRange();
3364 return;
3365 }
3366 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003367
Richard Smithf6565a92013-02-22 08:32:16 +00003368 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3369 ICE.take(), SpellingListIndex);
3370 AA->setPackExpansion(IsPackExpansion);
3371 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003372}
3373
Michael Hana31f65b2013-02-01 01:19:17 +00003374void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003375 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003376 // FIXME: Cache the number on the Attr object if non-dependent?
3377 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003378 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3379 SpellingListIndex);
3380 AA->setPackExpansion(IsPackExpansion);
3381 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003382}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003383
Richard Smithbe507b62013-02-01 08:12:08 +00003384void Sema::CheckAlignasUnderalignment(Decl *D) {
3385 assert(D->hasAttrs() && "no attributes on decl");
3386
3387 QualType Ty;
3388 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3389 Ty = VD->getType();
3390 else
3391 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003392 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003393 return;
3394
3395 // C++11 [dcl.align]p5, C11 6.7.5/4:
3396 // The combined effect of all alignment attributes in a declaration shall
3397 // not specify an alignment that is less strict than the alignment that
3398 // would otherwise be required for the entity being declared.
3399 AlignedAttr *AlignasAttr = 0;
3400 unsigned Align = 0;
3401 for (specific_attr_iterator<AlignedAttr>
3402 I = D->specific_attr_begin<AlignedAttr>(),
3403 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3404 if (I->isAlignmentDependent())
3405 return;
3406 if (I->isAlignas())
3407 AlignasAttr = *I;
3408 Align = std::max(Align, I->getAlignment(Context));
3409 }
3410
3411 if (AlignasAttr && Align) {
3412 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3413 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3414 if (NaturalAlign > RequestedAlign)
3415 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3416 << Ty << (unsigned)NaturalAlign.getQuantity();
3417 }
3418}
3419
Chandler Carruthd309c812011-07-01 23:49:16 +00003420/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003421/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003422///
Mike Stumpbf916502009-07-24 19:02:52 +00003423/// Despite what would be logical, the mode attribute is a decl attribute, not a
3424/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3425/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003426static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003427 // This attribute isn't documented, but glibc uses it. It changes
3428 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003429 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003430 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3431 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003432 return;
3433 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003434
Aaron Ballman624421f2013-08-31 01:11:41 +00003435 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3436 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003437
3438 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003439 if (Str.startswith("__") && Str.endswith("__"))
3440 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003441
3442 unsigned DestWidth = 0;
3443 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003444 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003445 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003446 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003447 switch (Str[0]) {
3448 case 'Q': DestWidth = 8; break;
3449 case 'H': DestWidth = 16; break;
3450 case 'S': DestWidth = 32; break;
3451 case 'D': DestWidth = 64; break;
3452 case 'X': DestWidth = 96; break;
3453 case 'T': DestWidth = 128; break;
3454 }
3455 if (Str[1] == 'F') {
3456 IntegerMode = false;
3457 } else if (Str[1] == 'C') {
3458 IntegerMode = false;
3459 ComplexMode = true;
3460 } else if (Str[1] != 'I') {
3461 DestWidth = 0;
3462 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003463 break;
3464 case 4:
3465 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3466 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003467 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003468 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003469 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003470 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003471 break;
3472 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003473 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003474 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003475 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003476 case 11:
3477 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003478 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003479 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003480 }
3481
3482 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003483 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003484 OldTy = TD->getUnderlyingType();
3485 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3486 OldTy = VD->getType();
3487 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003488 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003489 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003490 return;
3491 }
Eli Friedman73397492009-03-03 06:41:03 +00003492
John McCall183700f2009-09-21 23:43:11 +00003493 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003494 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3495 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003496 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003497 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3498 } else if (ComplexMode) {
3499 if (!OldTy->isComplexType())
3500 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3501 } else {
3502 if (!OldTy->isFloatingType())
3503 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3504 }
3505
Mike Stump390b4cc2009-05-16 07:39:55 +00003506 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3507 // and friends, at least with glibc.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003508 // FIXME: Make sure floating-point mappings are accurate
3509 // FIXME: Support XF and TF types
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003510 if (!DestWidth) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003511 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003512 return;
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003513 }
3514
3515 QualType NewTy;
3516
3517 if (IntegerMode)
3518 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3519 OldTy->isSignedIntegerType());
3520 else
3521 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3522
3523 if (NewTy.isNull()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003524 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003525 return;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003526 }
3527
Eli Friedman73397492009-03-03 06:41:03 +00003528 if (ComplexMode) {
3529 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003530 }
3531
3532 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003533 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3534 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3535 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003536 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003537
3538 D->addAttr(::new (S.Context)
3539 ModeAttr(Attr.getRange(), S.Context, Name,
3540 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003541}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003542
Chandler Carruth1b03c872011-07-02 00:01:44 +00003543static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003544 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3545 if (!VD->hasGlobalStorage())
3546 S.Diag(Attr.getLoc(),
3547 diag::warn_attribute_requires_functions_or_static_globals)
3548 << Attr.getName();
3549 } else if (!isFunctionOrMethod(D)) {
3550 S.Diag(Attr.getLoc(),
3551 diag::warn_attribute_requires_functions_or_static_globals)
3552 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003553 return;
3554 }
Mike Stumpbf916502009-07-24 19:02:52 +00003555
Michael Han51d8c522013-01-24 16:46:58 +00003556 D->addAttr(::new (S.Context)
3557 NoDebugAttr(Attr.getRange(), S.Context,
3558 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003559}
3560
Chandler Carruth1b03c872011-07-02 00:01:44 +00003561static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003562 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003563 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003564 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003565 return;
3566 }
Mike Stumpbf916502009-07-24 19:02:52 +00003567
Michael Han51d8c522013-01-24 16:46:58 +00003568 D->addAttr(::new (S.Context)
3569 NoInlineAttr(Attr.getRange(), S.Context,
3570 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003571}
3572
Chandler Carruth1b03c872011-07-02 00:01:44 +00003573static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3574 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003575 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003576 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003577 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003578 return;
3579 }
3580
Michael Han51d8c522013-01-24 16:46:58 +00003581 D->addAttr(::new (S.Context)
3582 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3583 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003584}
3585
Chandler Carruth1b03c872011-07-02 00:01:44 +00003586static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003587 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003588 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003589 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003590 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003591 return;
3592 }
3593
Michael Han51d8c522013-01-24 16:46:58 +00003594 D->addAttr(::new (S.Context)
3595 CUDAConstantAttr(Attr.getRange(), S.Context,
3596 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003597 } else {
3598 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3599 }
3600}
3601
Chandler Carruth1b03c872011-07-02 00:01:44 +00003602static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003603 if (S.LangOpts.CUDA) {
3604 // check the attribute arguments.
3605 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003606 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3607 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003608 return;
3609 }
3610
Chandler Carruth87c44602011-07-01 23:49:12 +00003611 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003613 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003614 return;
3615 }
3616
Michael Han51d8c522013-01-24 16:46:58 +00003617 D->addAttr(::new (S.Context)
3618 CUDADeviceAttr(Attr.getRange(), S.Context,
3619 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003620 } else {
3621 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3622 }
3623}
3624
Chandler Carruth1b03c872011-07-02 00:01:44 +00003625static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003626 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003627 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003629 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003630 return;
3631 }
3632
Chandler Carruth87c44602011-07-01 23:49:12 +00003633 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003634 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003635 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003636 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003637 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3638 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003639 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003640 "void");
3641 } else {
3642 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3643 << FD->getType();
3644 }
3645 return;
3646 }
3647
Michael Han51d8c522013-01-24 16:46:58 +00003648 D->addAttr(::new (S.Context)
3649 CUDAGlobalAttr(Attr.getRange(), S.Context,
3650 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003651 } else {
3652 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3653 }
3654}
3655
Chandler Carruth1b03c872011-07-02 00:01:44 +00003656static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003657 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003658 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003660 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003661 return;
3662 }
3663
Michael Han51d8c522013-01-24 16:46:58 +00003664 D->addAttr(::new (S.Context)
3665 CUDAHostAttr(Attr.getRange(), S.Context,
3666 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003667 } else {
3668 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3669 }
3670}
3671
Chandler Carruth1b03c872011-07-02 00:01:44 +00003672static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003673 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003674 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003676 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003677 return;
3678 }
3679
Michael Han51d8c522013-01-24 16:46:58 +00003680 D->addAttr(::new (S.Context)
3681 CUDASharedAttr(Attr.getRange(), S.Context,
3682 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003683 } else {
3684 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3685 }
3686}
3687
Chandler Carruth1b03c872011-07-02 00:01:44 +00003688static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003689 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003690 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003692 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003693 return;
3694 }
Mike Stumpbf916502009-07-24 19:02:52 +00003695
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003696 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003697 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003698 return;
3699 }
Mike Stumpbf916502009-07-24 19:02:52 +00003700
Michael Han51d8c522013-01-24 16:46:58 +00003701 D->addAttr(::new (S.Context)
3702 GNUInlineAttr(Attr.getRange(), S.Context,
3703 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003704}
3705
Chandler Carruth1b03c872011-07-02 00:01:44 +00003706static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003707 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003708
Aaron Ballmanfff32482012-12-09 17:45:41 +00003709 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003710 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003711 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3712 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003713 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003714 return;
3715
Chandler Carruth87c44602011-07-01 23:49:12 +00003716 if (!isa<ObjCMethodDecl>(D)) {
3717 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3718 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003719 return;
3720 }
3721
Chandler Carruth87c44602011-07-01 23:49:12 +00003722 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003723 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003724 D->addAttr(::new (S.Context)
3725 FastCallAttr(Attr.getRange(), S.Context,
3726 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003727 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003728 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003729 D->addAttr(::new (S.Context)
3730 StdCallAttr(Attr.getRange(), S.Context,
3731 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003732 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003733 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003734 D->addAttr(::new (S.Context)
3735 ThisCallAttr(Attr.getRange(), S.Context,
3736 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003737 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003738 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003739 D->addAttr(::new (S.Context)
3740 CDeclAttr(Attr.getRange(), S.Context,
3741 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003742 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003743 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003744 D->addAttr(::new (S.Context)
3745 PascalAttr(Attr.getRange(), S.Context,
3746 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003747 return;
Charles Davise8519c32013-08-30 04:39:01 +00003748 case AttributeList::AT_MSABI:
3749 D->addAttr(::new (S.Context)
3750 MSABIAttr(Attr.getRange(), S.Context,
3751 Attr.getAttributeSpellingListIndex()));
3752 return;
3753 case AttributeList::AT_SysVABI:
3754 D->addAttr(::new (S.Context)
3755 SysVABIAttr(Attr.getRange(), S.Context,
3756 Attr.getAttributeSpellingListIndex()));
3757 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003758 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003759 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003760 switch (CC) {
3761 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003762 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003763 break;
3764 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003765 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003766 break;
3767 default:
3768 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003769 }
3770
Michael Han51d8c522013-01-24 16:46:58 +00003771 D->addAttr(::new (S.Context)
3772 PcsAttr(Attr.getRange(), S.Context, PCS,
3773 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003774 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003775 }
Derek Schuff263366f2012-10-16 22:30:41 +00003776 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003777 D->addAttr(::new (S.Context)
3778 PnaclCallAttr(Attr.getRange(), S.Context,
3779 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003780 return;
Guy Benyei38980082012-12-25 08:53:55 +00003781 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003782 D->addAttr(::new (S.Context)
3783 IntelOclBiccAttr(Attr.getRange(), S.Context,
3784 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003785 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003786
Abramo Bagnarae215f722010-04-30 13:10:51 +00003787 default:
3788 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003789 }
3790}
3791
Chandler Carruth1b03c872011-07-02 00:01:44 +00003792static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003793 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003794}
3795
Guy Benyei1db70402013-03-24 13:58:12 +00003796static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003797 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003798 llvm::APSInt ArgNum(32);
3799 if (E->isTypeDependent() || E->isValueDependent() ||
3800 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003801 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3802 << Attr.getName() << AANT_ArgumentIntegerConstant
3803 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003804 return;
3805 }
3806
3807 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3808 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3809}
3810
Aaron Ballmanfff32482012-12-09 17:45:41 +00003811bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3812 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003813 if (attr.isInvalid())
3814 return true;
3815
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003816 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003817 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003818 attr.setInvalid();
3819 return true;
3820 }
3821
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003822 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3823 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003824 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003825 case AttributeList::AT_CDecl: CC = CC_C; break;
3826 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3827 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3828 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3829 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003830 case AttributeList::AT_MSABI:
3831 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3832 CC_X86_64Win64;
3833 break;
3834 case AttributeList::AT_SysVABI:
3835 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3836 CC_C;
3837 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003838 case AttributeList::AT_Pcs: {
Aaron Ballmanfbf6f5c2013-09-13 17:48:25 +00003839 StringRef StrRef;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003840 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003841 attr.setInvalid();
3842 return true;
3843 }
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003844 if (StrRef == "aapcs") {
3845 CC = CC_AAPCS;
3846 break;
3847 } else if (StrRef == "aapcs-vfp") {
3848 CC = CC_AAPCS_VFP;
3849 break;
3850 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003851
3852 attr.setInvalid();
3853 Diag(attr.getLoc(), diag::err_invalid_pcs);
3854 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003855 }
Derek Schuff263366f2012-10-16 22:30:41 +00003856 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003857 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003858 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003859 }
3860
Aaron Ballman82bfa192012-10-02 14:26:08 +00003861 const TargetInfo &TI = Context.getTargetInfo();
3862 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3863 if (A == TargetInfo::CCCR_Warning) {
3864 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003865
3866 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3867 if (FD)
3868 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3869 TargetInfo::CCMT_NonMember;
3870 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003871 }
3872
John McCall711c52b2011-01-05 12:14:39 +00003873 return false;
3874}
3875
Chandler Carruth1b03c872011-07-02 00:01:44 +00003876static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003877 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003878
3879 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003880 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003881 return;
3882
Chandler Carruth87c44602011-07-01 23:49:12 +00003883 if (!isa<ObjCMethodDecl>(D)) {
3884 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3885 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003886 return;
3887 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003888
Michael Han51d8c522013-01-24 16:46:58 +00003889 D->addAttr(::new (S.Context)
3890 RegparmAttr(Attr.getRange(), S.Context, numParams,
3891 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003892}
3893
3894/// Checks a regparm attribute, returning true if it is ill-formed and
3895/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003896bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3897 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003898 return true;
3899
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003900 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003901 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003902 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003903 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003904
Aaron Ballman624421f2013-08-31 01:11:41 +00003905 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003906 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003907 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003908 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003909 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3910 << Attr.getName() << AANT_ArgumentIntegerConstant
3911 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003912 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003913 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003914 }
3915
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003916 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003917 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003918 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003919 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003920 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003921 }
3922
John McCall711c52b2011-01-05 12:14:39 +00003923 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003924 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003925 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003926 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003927 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003928 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003929 }
3930
John McCall711c52b2011-01-05 12:14:39 +00003931 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003932}
3933
Chandler Carruth1b03c872011-07-02 00:01:44 +00003934static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003935 if (S.LangOpts.CUDA) {
3936 // check the attribute arguments.
3937 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003938 // FIXME: 0 is not okay.
3939 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003940 return;
3941 }
3942
Chandler Carruth87c44602011-07-01 23:49:12 +00003943 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003944 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003945 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003946 return;
3947 }
3948
Aaron Ballman624421f2013-08-31 01:11:41 +00003949 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003950 llvm::APSInt MaxThreads(32);
3951 if (MaxThreadsExpr->isTypeDependent() ||
3952 MaxThreadsExpr->isValueDependent() ||
3953 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003954 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003955 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003956 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00003957 return;
3958 }
3959
3960 llvm::APSInt MinBlocks(32);
3961 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003962 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003963 if (MinBlocksExpr->isTypeDependent() ||
3964 MinBlocksExpr->isValueDependent() ||
3965 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003966 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003967 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003968 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00003969 return;
3970 }
3971 }
3972
Michael Han51d8c522013-01-24 16:46:58 +00003973 D->addAttr(::new (S.Context)
3974 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3975 MaxThreads.getZExtValue(),
3976 MinBlocks.getZExtValue(),
3977 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00003978 } else {
3979 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3980 }
3981}
3982
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003983static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3984 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003985 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003987 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003988 return;
3989 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003990
3991 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003992 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003993
Aaron Ballman624421f2013-08-31 01:11:41 +00003994 StringRef AttrName = Attr.getName()->getName();
3995 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003996
3997 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3998 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3999 << Attr.getName() << ExpectedFunctionOrMethod;
4000 return;
4001 }
4002
4003 uint64_t ArgumentIdx;
4004 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4005 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004006 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004007 return;
4008
4009 uint64_t TypeTagIdx;
4010 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4011 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004012 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004013 return;
4014
4015 bool IsPointer = (AttrName == "pointer_with_type_tag");
4016 if (IsPointer) {
4017 // Ensure that buffer has a pointer type.
4018 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4019 if (!BufferTy->isPointerType()) {
4020 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004021 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004022 }
4023 }
4024
Michael Han51d8c522013-01-24 16:46:58 +00004025 D->addAttr(::new (S.Context)
4026 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4027 ArgumentIdx, TypeTagIdx, IsPointer,
4028 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004029}
4030
4031static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4032 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004033 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004035 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004036 return;
4037 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004038
4039 if (!checkAttributeNumArgs(S, Attr, 1))
4040 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004041
Aaron Ballman624421f2013-08-31 01:11:41 +00004042 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004043 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4044
Michael Han51d8c522013-01-24 16:46:58 +00004045 D->addAttr(::new (S.Context)
4046 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4047 MatchingCType,
4048 Attr.getLayoutCompatible(),
4049 Attr.getMustBeNull(),
4050 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004051}
4052
Chris Lattner0744e5f2008-06-29 00:23:49 +00004053//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004054// Checker-specific attribute handlers.
4055//===----------------------------------------------------------------------===//
4056
John McCallc7ad3812011-01-25 03:31:58 +00004057static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004058 return type->isDependentType() ||
4059 type->isObjCObjectPointerType() ||
4060 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004061}
4062static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004063 return type->isDependentType() ||
4064 type->isPointerType() ||
4065 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004066}
4067
Chandler Carruth1b03c872011-07-02 00:01:44 +00004068static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004069 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004070 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004071 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004072 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004073 return;
4074 }
4075
4076 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004077 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004078 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4079 cf = false;
4080 } else {
4081 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4082 cf = true;
4083 }
4084
4085 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004086 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004087 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004088 return;
4089 }
4090
4091 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004092 param->addAttr(::new (S.Context)
4093 CFConsumedAttr(Attr.getRange(), S.Context,
4094 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004095 else
Michael Han51d8c522013-01-24 16:46:58 +00004096 param->addAttr(::new (S.Context)
4097 NSConsumedAttr(Attr.getRange(), S.Context,
4098 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004099}
4100
Chandler Carruth1b03c872011-07-02 00:01:44 +00004101static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4102 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004103 if (!isa<ObjCMethodDecl>(D)) {
4104 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004105 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004106 return;
4107 }
4108
Michael Han51d8c522013-01-24 16:46:58 +00004109 D->addAttr(::new (S.Context)
4110 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4111 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004112}
4113
Chandler Carruth1b03c872011-07-02 00:01:44 +00004114static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4115 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004116
John McCallc7ad3812011-01-25 03:31:58 +00004117 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004118
Chandler Carruth87c44602011-07-01 23:49:12 +00004119 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004120 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004121 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004122 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004123 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004124 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4125 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004126 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004127 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004128 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004129 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004130 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004131 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004132 return;
4133 }
Mike Stumpbf916502009-07-24 19:02:52 +00004134
John McCallc7ad3812011-01-25 03:31:58 +00004135 bool typeOK;
4136 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004137 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004138 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004139 case AttributeList::AT_NSReturnsAutoreleased:
4140 case AttributeList::AT_NSReturnsRetained:
4141 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004142 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4143 cf = false;
4144 break;
4145
Sean Hunt8e083e72012-06-19 23:57:03 +00004146 case AttributeList::AT_CFReturnsRetained:
4147 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004148 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4149 cf = true;
4150 break;
4151 }
4152
4153 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004154 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004155 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004156 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004157 }
Mike Stumpbf916502009-07-24 19:02:52 +00004158
Chandler Carruth87c44602011-07-01 23:49:12 +00004159 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004160 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004161 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004162 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004163 D->addAttr(::new (S.Context)
4164 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4165 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004166 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004167 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004168 D->addAttr(::new (S.Context)
4169 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4170 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004171 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004172 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004173 D->addAttr(::new (S.Context)
4174 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4175 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004176 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004177 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004178 D->addAttr(::new (S.Context)
4179 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4180 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004181 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004182 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004183 D->addAttr(::new (S.Context)
4184 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4185 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004186 return;
4187 };
4188}
4189
John McCalldc7c5ad2011-07-22 08:53:00 +00004190static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4191 const AttributeList &attr) {
Fariborz Jahanianaf1d28f2013-09-19 17:52:50 +00004192 const int EP_ObjCMethod = 1;
4193 const int EP_ObjCProperty = 2;
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004194
John McCalldc7c5ad2011-07-22 08:53:00 +00004195 SourceLocation loc = attr.getLoc();
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004196 QualType resultType;
4197
John McCalldc7c5ad2011-07-22 08:53:00 +00004198 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4199
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004200 if (!method) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004201 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4202 if (!property) {
4203 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4204 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4205 return;
4206 }
4207 resultType = property->getType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004208 }
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004209 else
4210 // Check that the method returns a normal pointer.
4211 resultType = method->getResultType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004212
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004213 if (!resultType->isReferenceType() &&
4214 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004215 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalldc7c5ad2011-07-22 08:53:00 +00004216 << SourceRange(loc)
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004217 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4218 << /*non-retainable pointer*/ 2;
John McCalldc7c5ad2011-07-22 08:53:00 +00004219
4220 // Drop the attribute.
4221 return;
4222 }
4223
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004224 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004225 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4226 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004227}
4228
Fariborz Jahanian84101132012-09-07 23:46:23 +00004229static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4230 const AttributeList &attr) {
4231 SourceLocation loc = attr.getLoc();
4232 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4233
4234 if (!method) {
4235 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4236 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4237 return;
4238 }
4239 DeclContext *DC = method->getDeclContext();
4240 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4241 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4242 << attr.getName() << 0;
4243 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4244 return;
4245 }
4246 if (method->getMethodFamily() == OMF_dealloc) {
4247 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4248 << attr.getName() << 1;
4249 return;
4250 }
4251
Michael Han51d8c522013-01-24 16:46:58 +00004252 method->addAttr(::new (S.Context)
4253 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4254 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004255}
4256
John McCall8dfac0b2011-09-30 05:12:12 +00004257/// Handle cf_audited_transfer and cf_unknown_transfer.
4258static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4259 if (!isa<FunctionDecl>(D)) {
4260 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004261 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004262 return;
4263 }
4264
Sean Hunt8e083e72012-06-19 23:57:03 +00004265 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004266
4267 // Check whether there's a conflicting attribute already present.
4268 Attr *Existing;
4269 if (IsAudited) {
4270 Existing = D->getAttr<CFUnknownTransferAttr>();
4271 } else {
4272 Existing = D->getAttr<CFAuditedTransferAttr>();
4273 }
4274 if (Existing) {
4275 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4276 << A.getName()
4277 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4278 << A.getRange() << Existing->getRange();
4279 return;
4280 }
4281
4282 // All clear; add the attribute.
4283 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004284 D->addAttr(::new (S.Context)
4285 CFAuditedTransferAttr(A.getRange(), S.Context,
4286 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004287 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004288 D->addAttr(::new (S.Context)
4289 CFUnknownTransferAttr(A.getRange(), S.Context,
4290 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004291 }
4292}
4293
John McCallfe98da02011-09-29 07:17:38 +00004294static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4295 const AttributeList &Attr) {
4296 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4297 if (!RD || RD->isUnion()) {
4298 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004299 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004300 }
4301
Aaron Ballman624421f2013-08-31 01:11:41 +00004302 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004303
4304 // In Objective-C, verify that the type names an Objective-C type.
4305 // We don't want to check this outside of ObjC because people sometimes
4306 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004307 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004308 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004309 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004310 Sema::LookupOrdinaryName);
4311 if (S.LookupName(R, Sc)) {
4312 NamedDecl *Target = R.getFoundDecl();
4313 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4314 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4315 S.Diag(Target->getLocStart(), diag::note_declared_at);
4316 }
4317 }
4318 }
4319
Michael Han51d8c522013-01-24 16:46:58 +00004320 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004321 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004322 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004323}
4324
Chandler Carruth1b03c872011-07-02 00:01:44 +00004325static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4326 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004327 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004328
Chandler Carruth87c44602011-07-01 23:49:12 +00004329 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004330 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004331}
4332
Chandler Carruth1b03c872011-07-02 00:01:44 +00004333static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4334 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004335 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004336 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004337 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004338 return;
4339 }
4340
Chandler Carruth87c44602011-07-01 23:49:12 +00004341 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004342 QualType type = vd->getType();
4343
4344 if (!type->isDependentType() &&
4345 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004346 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004347 << type;
4348 return;
4349 }
4350
4351 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4352
4353 // If we have no lifetime yet, check the lifetime we're presumably
4354 // going to infer.
4355 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4356 lifetime = type->getObjCARCImplicitLifetime();
4357
4358 switch (lifetime) {
4359 case Qualifiers::OCL_None:
4360 assert(type->isDependentType() &&
4361 "didn't infer lifetime for non-dependent type?");
4362 break;
4363
4364 case Qualifiers::OCL_Weak: // meaningful
4365 case Qualifiers::OCL_Strong: // meaningful
4366 break;
4367
4368 case Qualifiers::OCL_ExplicitNone:
4369 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004370 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004371 << (lifetime == Qualifiers::OCL_Autoreleasing);
4372 break;
4373 }
4374
Chandler Carruth87c44602011-07-01 23:49:12 +00004375 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004376 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4377 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004378}
4379
Francois Pichet11542142010-12-19 06:50:37 +00004380//===----------------------------------------------------------------------===//
4381// Microsoft specific attribute handlers.
4382//===----------------------------------------------------------------------===//
4383
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004384// Check if MS extensions or some other language extensions are enabled. If
4385// not, issue a diagnostic that the given attribute is unused.
4386static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4387 bool OtherExtension = false) {
4388 if (S.LangOpts.MicrosoftExt || OtherExtension)
4389 return true;
4390 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4391 return false;
4392}
4393
Chandler Carruth1b03c872011-07-02 00:01:44 +00004394static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004395 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4396 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004397
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004398 StringRef StrRef;
4399 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00004400 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004401 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004402
David Majnemerbb0ed292013-08-09 08:56:20 +00004403 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4404 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004405 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4406 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004407
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004408 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004409 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004410 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004411 return;
4412 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004413
David Majnemerbb0ed292013-08-09 08:56:20 +00004414 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004415 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004416 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004417 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004418 return;
4419 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004420 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004421 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004422 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004423 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004424 }
Francois Pichet11542142010-12-19 06:50:37 +00004425
David Majnemerbb0ed292013-08-09 08:56:20 +00004426 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4427 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004428}
4429
John McCallc052dbb2012-05-22 21:28:12 +00004430static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004431 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004432 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004433
4434 AttributeList::Kind Kind = Attr.getKind();
4435 if (Kind == AttributeList::AT_SingleInheritance)
4436 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004437 ::new (S.Context)
4438 SingleInheritanceAttr(Attr.getRange(), S.Context,
4439 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004440 else if (Kind == AttributeList::AT_MultipleInheritance)
4441 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004442 ::new (S.Context)
4443 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4444 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004445 else if (Kind == AttributeList::AT_VirtualInheritance)
4446 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004447 ::new (S.Context)
4448 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4449 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004450}
4451
4452static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004453 if (!checkMicrosoftExt(S, Attr))
4454 return;
4455
4456 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004457 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004458 D->addAttr(
4459 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4460 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004461}
4462
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004463static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004464 if (!checkMicrosoftExt(S, Attr))
4465 return;
4466 D->addAttr(::new (S.Context)
4467 ForceInlineAttr(Attr.getRange(), S.Context,
4468 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004469}
4470
Reid Klecknera7225342013-05-20 14:02:37 +00004471static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4472 if (!checkMicrosoftExt(S, Attr))
4473 return;
4474 // Check linkage after possibly merging declaratinos. See
4475 // checkAttributesAfterMerging().
4476 D->addAttr(::new (S.Context)
4477 SelectAnyAttr(Attr.getRange(), S.Context,
4478 Attr.getAttributeSpellingListIndex()));
4479}
4480
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004481/// Handles semantic checking for features that are common to all attributes,
4482/// such as checking whether a parameter was properly specified, or the correct
4483/// number of arguments were passed, etc.
4484static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4485 const AttributeList &Attr) {
4486 // Several attributes carry different semantics than the parsing requires, so
4487 // those are opted out of the common handling.
4488 //
4489 // We also bail on unknown and ignored attributes because those are handled
4490 // as part of the target-specific handling logic.
4491 if (Attr.hasCustomParsing() ||
4492 Attr.getKind() == AttributeList::UnknownAttribute ||
4493 Attr.getKind() == AttributeList::IgnoredAttribute)
4494 return false;
4495
4496 // If there are no optional arguments, then checking for the argument count
4497 // is trivial.
4498 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4499 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4500 return true;
4501 return false;
4502}
4503
Ted Kremenekb71368d2009-05-09 02:44:38 +00004504//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004505// Top Level Sema Entry Points
4506//===----------------------------------------------------------------------===//
4507
Richard Smith4a97b8e2013-08-29 00:47:48 +00004508/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4509/// the attribute applies to decls. If the attribute is a type attribute, just
4510/// silently ignore it if a GNU attribute.
4511static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4512 const AttributeList &Attr,
4513 bool IncludeCXX11Attributes) {
4514 if (Attr.isInvalid())
4515 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004516
Richard Smith4a97b8e2013-08-29 00:47:48 +00004517 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4518 // instead.
4519 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4520 return;
4521
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004522 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4523 return;
4524
Chris Lattner803d0802008-06-29 00:43:07 +00004525 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004526 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4527 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4528 case AttributeList::AT_IBOutletCollection:
4529 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004530 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004531 case AttributeList::AT_ObjCGC:
4532 case AttributeList::AT_VectorSize:
4533 case AttributeList::AT_NeonVectorType:
4534 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004535 case AttributeList::AT_Ptr32:
4536 case AttributeList::AT_Ptr64:
4537 case AttributeList::AT_SPtr:
4538 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004539 // Ignore these, these are type attributes, handled by
4540 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004541 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004542 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4543 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4544 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4545 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004546 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004547 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004548 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004549 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004550 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4551 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4552 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004553 handleDependencyAttr(S, scope, D, Attr);
4554 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004555 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4556 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4557 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004558 case AttributeList::AT_CXX11NoReturn:
4559 handleCXX11NoReturnAttr(S, D, Attr);
4560 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004561 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004562 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004563 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004564 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4565 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004566 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004567 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004568 case AttributeList::AT_MinSize:
4569 handleMinSizeAttr(S, D, Attr);
4570 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004571 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4572 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4573 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004574 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4575 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004576 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4577 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004578 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004579 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004580 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4581 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004582 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004583 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4584 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004585 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004586 case AttributeList::AT_ownership_returns:
4587 case AttributeList::AT_ownership_takes:
4588 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004589 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004590 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4591 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4592 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4593 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4594 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4595 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4596 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004597
Sean Hunt8e083e72012-06-19 23:57:03 +00004598 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004599 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004600 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004601 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004602
Sean Hunt8e083e72012-06-19 23:57:03 +00004603 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004604 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4605
Fariborz Jahanian84101132012-09-07 23:46:23 +00004606 case AttributeList::AT_ObjCRequiresSuper:
4607 handleObjCRequiresSuperAttr(S, D, Attr); break;
4608
Sean Hunt8e083e72012-06-19 23:57:03 +00004609 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004610 handleNSBridgedAttr(S, scope, D, Attr); break;
4611
Sean Hunt8e083e72012-06-19 23:57:03 +00004612 case AttributeList::AT_CFAuditedTransfer:
4613 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004614 handleCFTransferAttr(S, D, Attr); break;
4615
Ted Kremenekb71368d2009-05-09 02:44:38 +00004616 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004617 case AttributeList::AT_CFConsumed:
4618 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4619 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004620 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004621
Sean Hunt8e083e72012-06-19 23:57:03 +00004622 case AttributeList::AT_NSReturnsAutoreleased:
4623 case AttributeList::AT_NSReturnsNotRetained:
4624 case AttributeList::AT_CFReturnsNotRetained:
4625 case AttributeList::AT_NSReturnsRetained:
4626 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004627 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004628
Tanya Lattner0df579e2012-07-09 22:06:01 +00004629 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004630 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004631 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004632
Joey Gouly37453b92013-03-08 09:42:32 +00004633 case AttributeList::AT_VecTypeHint:
4634 handleVecTypeHint(S, D, Attr); break;
4635
Sean Hunt8e083e72012-06-19 23:57:03 +00004636 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004637 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004638
Sean Hunt8e083e72012-06-19 23:57:03 +00004639 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4640 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4641 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004642 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004643 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004644 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004645 handleArcWeakrefUnavailableAttr (S, D, Attr);
4646 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004647 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004648 handleObjCRootClassAttr(S, D, Attr);
4649 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004650 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004651 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004652 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004653 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4654 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004655 handleReturnsTwiceAttr(S, D, Attr);
4656 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004657 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004658 case AttributeList::AT_Visibility:
4659 handleVisibilityAttr(S, D, Attr, false);
4660 break;
4661 case AttributeList::AT_TypeVisibility:
4662 handleVisibilityAttr(S, D, Attr, true);
4663 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004664 case AttributeList::AT_WarnUnused:
4665 handleWarnUnusedAttr(S, D, Attr);
4666 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004667 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004668 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004669 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4670 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4671 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4672 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004673 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004674 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004675 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004676 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004677 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004678 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004679 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004680 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004681 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4682 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4683 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4684 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4685 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4686 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4687 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4688 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4689 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004690 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004691 // Just ignore
4692 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004693 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004694 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004695 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004696 case AttributeList::AT_StdCall:
4697 case AttributeList::AT_CDecl:
4698 case AttributeList::AT_FastCall:
4699 case AttributeList::AT_ThisCall:
4700 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004701 case AttributeList::AT_MSABI:
4702 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004703 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004704 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004705 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004706 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004707 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004708 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004709 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004710 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004711 case AttributeList::AT_OpenCLImageAccess:
4712 handleOpenCLImageAccessAttr(S, D, Attr);
4713 break;
John McCallc052dbb2012-05-22 21:28:12 +00004714
4715 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004716 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004717 handleMsStructAttr(S, D, Attr);
4718 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004719 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004720 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004721 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004722 case AttributeList::AT_SingleInheritance:
4723 case AttributeList::AT_MultipleInheritance:
4724 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004725 handleInheritanceAttr(S, D, Attr);
4726 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004727 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004728 handlePortabilityAttr(S, D, Attr);
4729 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004730 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004731 handleForceInlineAttr(S, D, Attr);
4732 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004733 case AttributeList::AT_SelectAny:
4734 handleSelectAnyAttr(S, D, Attr);
4735 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004736
4737 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004738 case AttributeList::AT_AssertExclusiveLock:
4739 handleAssertExclusiveLockAttr(S, D, Attr);
4740 break;
4741 case AttributeList::AT_AssertSharedLock:
4742 handleAssertSharedLockAttr(S, D, Attr);
4743 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004744 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004745 handleGuardedVarAttr(S, D, Attr);
4746 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004747 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004748 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004749 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004750 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004751 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004752 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004753 case AttributeList::AT_NoSanitizeAddress:
4754 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004755 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004757 handleNoThreadSafetyAnalysis(S, D, Attr);
4758 break;
4759 case AttributeList::AT_NoSanitizeThread:
4760 handleNoSanitizeThread(S, D, Attr);
4761 break;
4762 case AttributeList::AT_NoSanitizeMemory:
4763 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004764 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004765 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004766 handleLockableAttr(S, D, Attr);
4767 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004768 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004769 handleGuardedByAttr(S, D, Attr);
4770 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004771 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004772 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004773 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004774 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004775 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004776 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004777 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004778 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004779 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004780 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004781 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004782 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004783 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004784 handleLockReturnedAttr(S, D, Attr);
4785 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004786 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004787 handleLocksExcludedAttr(S, D, Attr);
4788 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004789 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004790 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004791 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004792 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004793 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004794 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004795 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004796 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004797 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004798 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004799 handleUnlockFunAttr(S, D, Attr);
4800 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004801 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004802 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004803 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004804 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004805 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004806 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004807
DeLesley Hutchins1bf63432013-10-11 22:30:48 +00004808 // Consumed analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004809 case AttributeList::AT_Consumable:
4810 handleConsumableAttr(S, D, Attr);
4811 break;
DeLesley Hutchins66540852013-10-04 21:28:06 +00004812 case AttributeList::AT_CallableWhen:
4813 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004814 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004815 case AttributeList::AT_ReturnTypestate:
4816 handleReturnTypestateAttr(S, D, Attr);
4817 break;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004818 case AttributeList::AT_SetTypestate:
4819 handleSetTypestateAttr(S, D, Attr);
4820 break;
4821 case AttributeList::AT_TestsTypestate:
4822 handleTestsTypestateAttr(S, D, Attr);
4823 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004824
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004825 // Type safety attributes.
4826 case AttributeList::AT_ArgumentWithTypeTag:
4827 handleArgumentWithTypeTagAttr(S, D, Attr);
4828 break;
4829 case AttributeList::AT_TypeTagForDatatype:
4830 handleTypeTagForDatatypeAttr(S, D, Attr);
4831 break;
4832
Chris Lattner803d0802008-06-29 00:43:07 +00004833 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004834 // Ask target about the attribute.
4835 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4836 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004837 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4838 diag::warn_unhandled_ms_attribute_ignored :
4839 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004840 break;
4841 }
4842}
4843
4844/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4845/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004846void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004847 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004848 bool IncludeCXX11Attributes) {
4849 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004850 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004851
4852 // GCC accepts
4853 // static int a9 __attribute__((weakref));
4854 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004855 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004856 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004857 cast<NamedDecl>(D)->getNameAsString();
4858 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004859 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004860 }
4861}
4862
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004863// Annotation attributes are the only attributes allowed after an access
4864// specifier.
4865bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4866 const AttributeList *AttrList) {
4867 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004869 handleAnnotateAttr(*this, ASDecl, *l);
4870 } else {
4871 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4872 return true;
4873 }
4874 }
4875
4876 return false;
4877}
4878
John McCalle82247a2011-10-01 05:17:03 +00004879/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4880/// contains any decl attributes that we should warn about.
4881static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4882 for ( ; A; A = A->getNext()) {
4883 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004884 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004885 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4886
4887 if (A->getKind() == AttributeList::UnknownAttribute) {
4888 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4889 << A->getName() << A->getRange();
4890 } else {
4891 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4892 << A->getName() << A->getRange();
4893 }
4894 }
4895}
4896
4897/// checkUnusedDeclAttributes - Given a declarator which is not being
4898/// used to build a declaration, complain about any decl attributes
4899/// which might be lying around on it.
4900void Sema::checkUnusedDeclAttributes(Declarator &D) {
4901 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4902 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4903 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4904 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4905}
4906
Ryan Flynne25ff832009-07-30 03:15:39 +00004907/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004908/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004909NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4910 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004911 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004912 NamedDecl *NewD = 0;
4913 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004914 FunctionDecl *NewFD;
4915 // FIXME: Missing call to CheckFunctionDeclaration().
4916 // FIXME: Mangling?
4917 // FIXME: Is the qualifier info correct?
4918 // FIXME: Is the DeclContext correct?
4919 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4920 Loc, Loc, DeclarationName(II),
4921 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004922 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004923 FD->hasPrototype(),
4924 false/*isConstexprSpecified*/);
4925 NewD = NewFD;
4926
4927 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004928 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004929
4930 // Fake up parameter variables; they are declared as if this were
4931 // a typedef.
4932 QualType FDTy = FD->getType();
4933 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4934 SmallVector<ParmVarDecl*, 16> Params;
4935 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4936 AE = FT->arg_type_end(); AI != AE; ++AI) {
4937 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4938 Param->setScopeInfo(0, Params.size());
4939 Params.push_back(Param);
4940 }
David Blaikie4278c652011-09-21 18:16:56 +00004941 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004942 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004943 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4944 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004945 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004946 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004947 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00004948 if (VD->getQualifier()) {
4949 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004950 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004951 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004952 }
4953 return NewD;
4954}
4955
James Dennett1dfbd922012-06-14 21:40:34 +00004956/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004957/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004958void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004959 if (W.getUsed()) return; // only do this once
4960 W.setUsed(true);
4961 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4962 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004963 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004964 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4965 NDId->getName()));
4966 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004967 WeakTopLevelDecl.push_back(NewD);
4968 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4969 // to insert Decl at TU scope, sorry.
4970 DeclContext *SavedContext = CurContext;
4971 CurContext = Context.getTranslationUnitDecl();
4972 PushOnScopeChains(NewD, S);
4973 CurContext = SavedContext;
4974 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004975 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004976 }
4977}
4978
Rafael Espindola65611bf2013-03-02 21:41:48 +00004979void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4980 // It's valid to "forward-declare" #pragma weak, in which case we
4981 // have to do this.
4982 LoadExternalWeakUndeclaredIdentifiers();
4983 if (!WeakUndeclaredIdentifiers.empty()) {
4984 NamedDecl *ND = NULL;
4985 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4986 if (VD->isExternC())
4987 ND = VD;
4988 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4989 if (FD->isExternC())
4990 ND = FD;
4991 if (ND) {
4992 if (IdentifierInfo *Id = ND->getIdentifier()) {
4993 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4994 = WeakUndeclaredIdentifiers.find(Id);
4995 if (I != WeakUndeclaredIdentifiers.end()) {
4996 WeakInfo W = I->second;
4997 DeclApplyPragmaWeak(S, ND, W);
4998 WeakUndeclaredIdentifiers[Id] = W;
4999 }
5000 }
5001 }
5002 }
5003}
5004
Chris Lattner0744e5f2008-06-29 00:23:49 +00005005/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5006/// it, apply them to D. This is a bit tricky because PD can have attributes
5007/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005008void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005009 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005010 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005011 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005012
Chris Lattner0744e5f2008-06-29 00:23:49 +00005013 // Walk the declarator structure, applying decl attributes that were in a type
5014 // position to the decl itself. This handles cases like:
5015 // int *__attr__(x)** D;
5016 // when X is a decl attribute.
5017 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5018 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005019 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005020
Chris Lattner0744e5f2008-06-29 00:23:49 +00005021 // Finally, apply any attributes on the decl itself.
5022 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005023 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005024}
John McCall54abf7d2009-11-04 02:18:39 +00005025
John McCallf85e1932011-06-15 23:02:42 +00005026/// Is the given declaration allowed to use a forbidden type?
5027static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5028 // Private ivars are always okay. Unfortunately, people don't
5029 // always properly make their ivars private, even in system headers.
5030 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005031 // Function declarations in sys headers will be marked unavailable.
5032 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5033 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005034 return false;
5035
5036 // Require it to be declared in a system header.
5037 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5038}
5039
5040/// Handle a delayed forbidden-type diagnostic.
5041static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5042 Decl *decl) {
5043 if (decl && isForbiddenTypeAllowed(S, decl)) {
5044 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5045 "this system declaration uses an unsupported type"));
5046 return;
5047 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005048 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005049 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005050 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005051 // kind of forbidden type messages on unavailable functions.
5052 if (FD->hasAttr<UnavailableAttr>() &&
5053 diag.getForbiddenTypeDiagnostic() ==
5054 diag::err_arc_array_param_no_ownership) {
5055 diag.Triggered = true;
5056 return;
5057 }
5058 }
John McCallf85e1932011-06-15 23:02:42 +00005059
5060 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5061 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5062 diag.Triggered = true;
5063}
5064
John McCall92576642012-05-07 06:16:41 +00005065void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5066 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005067 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005068 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005069
John McCall92576642012-05-07 06:16:41 +00005070 // When delaying diagnostics to run in the context of a parsed
5071 // declaration, we only want to actually emit anything if parsing
5072 // succeeds.
5073 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005074
John McCall92576642012-05-07 06:16:41 +00005075 // We emit all the active diagnostics in this pool or any of its
5076 // parents. In general, we'll get one pool for the decl spec
5077 // and a child pool for each declarator; in a decl group like:
5078 // deprecated_typedef foo, *bar, baz();
5079 // only the declarator pops will be passed decls. This is correct;
5080 // we really do need to consider delayed diagnostics from the decl spec
5081 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005082 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005083 do {
John McCall13489672012-05-07 06:16:58 +00005084 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005085 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5086 // This const_cast is a bit lame. Really, Triggered should be mutable.
5087 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005088 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005089 continue;
5090
John McCalleee1d542011-02-14 07:13:47 +00005091 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005092 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005093 // Don't bother giving deprecation diagnostics if the decl is invalid.
5094 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005095 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005096 break;
5097
5098 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005099 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005100 break;
John McCallf85e1932011-06-15 23:02:42 +00005101
5102 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005103 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005104 break;
John McCall2f514482010-01-27 03:50:35 +00005105 }
5106 }
John McCall92576642012-05-07 06:16:41 +00005107 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005108}
5109
John McCall13489672012-05-07 06:16:58 +00005110/// Given a set of delayed diagnostics, re-emit them as if they had
5111/// been delayed in the current context instead of in the given pool.
5112/// Essentially, this just moves them to the current pool.
5113void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5114 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5115 assert(curPool && "re-emitting in undelayed context not supported");
5116 curPool->steal(pool);
5117}
5118
John McCall54abf7d2009-11-04 02:18:39 +00005119static bool isDeclDeprecated(Decl *D) {
5120 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005121 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005122 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005123 // A category implicitly has the availability of the interface.
5124 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5125 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005126 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5127 return false;
5128}
5129
Eli Friedmanc3b23082012-08-08 21:52:41 +00005130static void
5131DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5132 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005133 const ObjCInterfaceDecl *UnknownObjCClass,
5134 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005135 DeclarationName Name = D->getDeclName();
5136 if (!Message.empty()) {
5137 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5138 S.Diag(D->getLocation(),
5139 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5140 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005141 if (ObjCPropery)
5142 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5143 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005144 } else if (!UnknownObjCClass) {
5145 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5146 S.Diag(D->getLocation(),
5147 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5148 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005149 if (ObjCPropery)
5150 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5151 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005152 } else {
5153 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5154 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5155 }
5156}
5157
John McCall9c3087b2010-08-26 02:13:20 +00005158void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005159 Decl *Ctx) {
5160 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005161 return;
5162
John McCall2f514482010-01-27 03:50:35 +00005163 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005164 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5165 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005166 DD.getUnknownObjCClass(),
5167 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005168}
5169
Chris Lattner5f9e2722011-07-23 10:55:15 +00005170void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005171 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005172 const ObjCInterfaceDecl *UnknownObjCClass,
5173 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005174 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005175 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005176 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5177 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005178 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005179 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005180 return;
5181 }
5182
5183 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005184 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005185 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005186 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005187}