blob: 3eabf87624c03dec9442af1ad2e2f3709b8822fa [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,
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00001054 CallableState)) {
Aaron Ballman9025a182013-10-05 22:45:34 +00001055 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 Hutchinsd4f0e192013-10-17 23:23:53 +00001069static void handleParamTypestateAttr(Sema &S, Decl *D,
1070 const AttributeList &Attr) {
1071 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1072
1073 if (!isa<ParmVarDecl>(D)) {
1074 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1075 Attr.getName() << ExpectedParameter;
1076 return;
1077 }
1078
1079 ParamTypestateAttr::ConsumedState ParamState;
1080
1081 if (Attr.isArgIdent(0)) {
1082 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1083 StringRef StateString = Ident->Ident->getName();
1084
1085 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1086 ParamState)) {
1087 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1088 << Attr.getName() << StateString;
1089 return;
1090 }
1091 } else {
1092 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1093 Attr.getName() << AANT_ArgumentIdentifier;
1094 return;
1095 }
1096
1097 // FIXME: This check is currently being done in the analysis. It can be
1098 // enabled here only after the parser propagates attributes at
1099 // template specialization definition, not declaration.
1100 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1101 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1102 //
1103 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1104 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1105 // ReturnType.getAsString();
1106 // return;
1107 //}
1108
1109 D->addAttr(::new (S.Context)
1110 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1111 Attr.getAttributeSpellingListIndex()));
1112}
1113
1114
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001115static void handleReturnTypestateAttr(Sema &S, Decl *D,
1116 const AttributeList &Attr) {
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001117 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1118
1119 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1120 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1121 Attr.getName() << ExpectedFunctionMethodOrParameter;
1122 return;
1123 }
1124
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001125 ReturnTypestateAttr::ConsumedState ReturnState;
1126
1127 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001128 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1129 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1130 ReturnState)) {
1131 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1132 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001133 return;
1134 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001135 } else {
1136 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1137 Attr.getName() << AANT_ArgumentIdentifier;
1138 return;
1139 }
1140
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001141 // FIXME: This check is currently being done in the analysis. It can be
1142 // enabled here only after the parser propagates attributes at
1143 // template specialization definition, not declaration.
1144 //QualType ReturnType;
1145 //
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001146 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1147 // ReturnType = Param->getType();
1148 //
1149 //} else if (const CXXConstructorDecl *Constructor =
1150 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001151 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1152 //
1153 //} else {
1154 //
1155 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1156 //}
1157 //
1158 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1159 //
1160 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1161 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1162 // ReturnType.getAsString();
1163 // return;
1164 //}
1165
1166 D->addAttr(::new (S.Context)
1167 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1168 Attr.getAttributeSpellingListIndex()));
1169}
1170
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001171
1172static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001173 if (!checkAttributeNumArgs(S, Attr, 1))
1174 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001175
1176 if (!isa<CXXMethodDecl>(D)) {
1177 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1178 Attr.getName() << ExpectedMethod;
1179 return;
1180 }
1181
1182 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1183 return;
1184
1185 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001186 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001187 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1188 StringRef Param = Ident->Ident->getName();
1189 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1190 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1191 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001192 return;
1193 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001194 } else {
1195 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1196 Attr.getName() << AANT_ArgumentIdentifier;
1197 return;
1198 }
1199
1200 D->addAttr(::new (S.Context)
1201 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1202 Attr.getAttributeSpellingListIndex()));
1203}
1204
1205static void handleTestsTypestateAttr(Sema &S, Decl *D,
1206 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001207 if (!checkAttributeNumArgs(S, Attr, 1))
1208 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001209
1210 if (!isa<CXXMethodDecl>(D)) {
1211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1212 Attr.getName() << ExpectedMethod;
1213 return;
1214 }
1215
1216 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1217 return;
1218
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001219 TestsTypestateAttr::ConsumedState TestState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001220 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001221 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1222 StringRef Param = Ident->Ident->getName();
1223 if (!TestsTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
1224 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1225 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001226 return;
1227 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001228 } else {
1229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1230 Attr.getName() << AANT_ArgumentIdentifier;
1231 return;
1232 }
1233
1234 D->addAttr(::new (S.Context)
1235 TestsTypestateAttr(Attr.getRange(), S.Context, TestState,
1236 Attr.getAttributeSpellingListIndex()));
1237}
1238
Chandler Carruth1b03c872011-07-02 00:01:44 +00001239static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1240 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001241 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1242 if (TD == 0) {
1243 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1244 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001245 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001246 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001247 }
Mike Stumpbf916502009-07-24 19:02:52 +00001248
Richard Smitha4fa9002013-01-13 02:11:23 +00001249 // Remember this typedef decl, we will need it later for diagnostics.
1250 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001251}
1252
Chandler Carruth1b03c872011-07-02 00:01:44 +00001253static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001254 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001255 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001256 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001257 // If the alignment is less than or equal to 8 bits, the packed attribute
1258 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001259 if (!FD->getType()->isDependentType() &&
1260 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001261 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001262 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001263 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001264 else
Michael Han51d8c522013-01-24 16:46:58 +00001265 FD->addAttr(::new (S.Context)
1266 PackedAttr(Attr.getRange(), S.Context,
1267 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001268 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001269 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001270}
1271
Chandler Carruth1b03c872011-07-02 00:01:44 +00001272static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001273 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001274 RD->addAttr(::new (S.Context)
1275 MsStructAttr(Attr.getRange(), S.Context,
1276 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001277 else
1278 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1279}
1280
Chandler Carruth1b03c872011-07-02 00:01:44 +00001281static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001282 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001283 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001284 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001285 D->addAttr(::new (S.Context)
1286 IBActionAttr(Attr.getRange(), S.Context,
1287 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001288 return;
1289 }
1290
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001291 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001292}
1293
Ted Kremenek2f041d02011-09-29 07:02:25 +00001294static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1295 // The IBOutlet/IBOutletCollection attributes only apply to instance
1296 // variables or properties of Objective-C classes. The outlet must also
1297 // have an object reference type.
1298 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1299 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001300 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001301 << Attr.getName() << VD->getType() << 0;
1302 return false;
1303 }
1304 }
1305 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1306 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001307 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001308 << Attr.getName() << PD->getType() << 1;
1309 return false;
1310 }
1311 }
1312 else {
1313 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1314 return false;
1315 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001316
Ted Kremenek2f041d02011-09-29 07:02:25 +00001317 return true;
1318}
1319
Chandler Carruth1b03c872011-07-02 00:01:44 +00001320static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001321 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001322 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001323
Michael Han51d8c522013-01-24 16:46:58 +00001324 D->addAttr(::new (S.Context)
1325 IBOutletAttr(Attr.getRange(), S.Context,
1326 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001327}
1328
Chandler Carruth1b03c872011-07-02 00:01:44 +00001329static void handleIBOutletCollection(Sema &S, Decl *D,
1330 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001331
1332 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001333 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001334 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1335 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001336 return;
1337 }
1338
Ted Kremenek2f041d02011-09-29 07:02:25 +00001339 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001340 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001341
Aaron Ballman624421f2013-08-31 01:11:41 +00001342 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1343 IdentifierInfo *II;
1344 SourceLocation ILS;
1345 if (IL) {
1346 II = IL->Ident;
1347 ILS = IL->Loc;
1348 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001349 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001350 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001351
John McCallb3d87482010-08-24 05:47:05 +00001352 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001353 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001354 if (!TypeRep) {
1355 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1356 return;
1357 }
John McCallb3d87482010-08-24 05:47:05 +00001358 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001359 // Diagnose use of non-object type in iboutletcollection attribute.
1360 // FIXME. Gnu attribute extension ignores use of builtin types in
1361 // attributes. So, __attribute__((iboutletcollection(char))) will be
1362 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001363 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001364 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1365 return;
1366 }
Michael Han51d8c522013-01-24 16:46:58 +00001367 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001368 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001369 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001370}
1371
Chandler Carruthd309c812011-07-01 23:49:16 +00001372static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001373 if (const RecordType *UT = T->getAsUnionType())
1374 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1375 RecordDecl *UD = UT->getDecl();
1376 for (RecordDecl::field_iterator it = UD->field_begin(),
1377 itend = UD->field_end(); it != itend; ++it) {
1378 QualType QT = it->getType();
1379 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1380 T = QT;
1381 return;
1382 }
1383 }
1384 }
1385}
1386
Nuno Lopes587de5b2012-05-24 00:22:00 +00001387static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001388 if (!isFunctionOrMethod(D)) {
1389 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001390 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001391 return;
1392 }
1393
Nuno Lopes587de5b2012-05-24 00:22:00 +00001394 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1395 return;
1396
Nuno Lopes587de5b2012-05-24 00:22:00 +00001397 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001398 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001399 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001400 uint64_t Idx;
1401 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1402 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001403 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001404
1405 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001406 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001407 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001408 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1409 << Attr.getName() << AANT_ArgumentIntegerConstant
1410 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001411 return;
1412 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001413 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001414 }
1415
1416 // check if the function returns a pointer
1417 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1418 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001419 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001420 }
1421
Michael Han51d8c522013-01-24 16:46:58 +00001422 D->addAttr(::new (S.Context)
1423 AllocSizeAttr(Attr.getRange(), S.Context,
1424 SizeArgs.data(), SizeArgs.size(),
1425 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001426}
1427
Chandler Carruth1b03c872011-07-02 00:01:44 +00001428static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001429 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1430 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001431 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001432 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001433 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001434 return;
1435 }
Mike Stumpbf916502009-07-24 19:02:52 +00001436
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001437 SmallVector<unsigned, 8> NonNullArgs;
1438 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001439 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001440 uint64_t Idx;
1441 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1442 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001443 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001444
1445 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001446 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001447 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001448
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001449 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001450 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001451 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001452 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001453 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001454 }
Mike Stumpbf916502009-07-24 19:02:52 +00001455
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001456 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001457 }
Mike Stumpbf916502009-07-24 19:02:52 +00001458
1459 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1460 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001461 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001462 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1463 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001464 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001465 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001466 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001467 }
Mike Stumpbf916502009-07-24 19:02:52 +00001468
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001469 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001470 if (NonNullArgs.empty()) {
1471 // Warn the trivial case only if attribute is not coming from a
1472 // macro instantiation.
1473 if (Attr.getLoc().isFileID())
1474 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001475 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001476 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001477 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001478
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001479 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001480 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001481 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001482 D->addAttr(::new (S.Context)
1483 NonNullAttr(Attr.getRange(), S.Context, start, size,
1484 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001485}
1486
Aaron Ballmane1668a32013-09-16 18:11:41 +00001487static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1488 switch (K) {
1489 case OwnershipAttr::Holds: return "'ownership_holds'";
1490 case OwnershipAttr::Takes: return "'ownership_takes'";
1491 case OwnershipAttr::Returns: return "'ownership_returns'";
1492 }
1493 llvm_unreachable("unknown ownership");
1494}
1495
Chandler Carruth1b03c872011-07-02 00:01:44 +00001496static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001497 // This attribute must be applied to a function declaration. The first
1498 // argument to the attribute must be an identifier, the name of the resource,
1499 // for example: malloc. The following arguments must be argument indexes, the
1500 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001501 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballmane1668a32013-09-16 18:11:41 +00001502 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose2a479922010-08-12 08:54:03 +00001503 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001504
Aaron Ballman624421f2013-08-31 01:11:41 +00001505 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001506 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmane1668a32013-09-16 18:11:41 +00001507 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001508 return;
1509 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001510
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001511 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001512 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001513 switch (AL.getKind()) {
1514 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001515 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001516 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001517 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001518 return;
1519 }
Jordy Rose2a479922010-08-12 08:54:03 +00001520 break;
1521 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001522 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001523 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001524 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001525 return;
1526 }
Jordy Rose2a479922010-08-12 08:54:03 +00001527 break;
1528 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001529 K = OwnershipAttr::Returns;
Aaron Ballmane1668a32013-09-16 18:11:41 +00001530
Aaron Ballman624421f2013-08-31 01:11:41 +00001531 if (AL.getNumArgs() > 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001532 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001533 return;
1534 }
Jordy Rose2a479922010-08-12 08:54:03 +00001535 break;
1536 default:
1537 // This should never happen given how we are called.
1538 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001539 }
1540
Chandler Carruth87c44602011-07-01 23:49:12 +00001541 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001542 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1543 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001544 return;
1545 }
1546
Aaron Ballman624421f2013-08-31 01:11:41 +00001547 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001548
1549 // Normalize the argument, __foo__ becomes foo.
1550 if (Module.startswith("__") && Module.endswith("__"))
1551 Module = Module.substr(2, Module.size() - 4);
1552
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001553 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001554 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1555 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001556 uint64_t Idx;
1557 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001558 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001559 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001560
Aaron Ballmane1668a32013-09-16 18:11:41 +00001561 // Is the function argument a pointer type?
1562 QualType T = getFunctionOrMethodArgType(D, Idx);
1563 int Err = -1; // No error
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001564 switch (K) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001565 case OwnershipAttr::Takes:
1566 case OwnershipAttr::Holds:
1567 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1568 Err = 0;
1569 break;
1570 case OwnershipAttr::Returns:
1571 if (!T->isIntegerType())
1572 Err = 1;
1573 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001574 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001575 if (-1 != Err) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001576 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballmane1668a32013-09-16 18:11:41 +00001577 << Ex->getSourceRange();
1578 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001579 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001580
1581 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001582 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballmane1668a32013-09-16 18:11:41 +00001583 i = D->specific_attr_begin<OwnershipAttr>(),
1584 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1585 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1586 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1587 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1588 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1589 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001590 }
1591 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001592 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001593 }
1594
1595 unsigned* start = OwnershipArgs.data();
1596 unsigned size = OwnershipArgs.size();
1597 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001598
Michael Han51d8c522013-01-24 16:46:58 +00001599 D->addAttr(::new (S.Context)
1600 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1601 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001602}
1603
Chandler Carruth1b03c872011-07-02 00:01:44 +00001604static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001605 // Check the attribute arguments.
1606 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001607 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1608 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001609 return;
1610 }
1611
Chandler Carruth87c44602011-07-01 23:49:12 +00001612 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001613 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001614 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001615 return;
1616 }
1617
Chandler Carruth87c44602011-07-01 23:49:12 +00001618 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001619
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001620 // gcc rejects
1621 // class c {
1622 // static int a __attribute__((weakref ("v2")));
1623 // static int b() __attribute__((weakref ("f3")));
1624 // };
1625 // and ignores the attributes of
1626 // void f(void) {
1627 // static int a __attribute__((weakref ("v2")));
1628 // }
1629 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001630 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001631 if (!Ctx->isFileContext()) {
1632 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001633 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001634 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001635 }
1636
1637 // The GCC manual says
1638 //
1639 // At present, a declaration to which `weakref' is attached can only
1640 // be `static'.
1641 //
1642 // It also says
1643 //
1644 // Without a TARGET,
1645 // given as an argument to `weakref' or to `alias', `weakref' is
1646 // equivalent to `weak'.
1647 //
1648 // gcc 4.4.1 will accept
1649 // int a7 __attribute__((weakref));
1650 // as
1651 // int a7 __attribute__((weak));
1652 // This looks like a bug in gcc. We reject that for now. We should revisit
1653 // it if this behaviour is actually used.
1654
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001655 // GCC rejects
1656 // static ((alias ("y"), weakref)).
1657 // Should we? How to check that weakref is before or after alias?
1658
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001659 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1660 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1661 // StringRef parameter it was given anyway.
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001662 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001663 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001664 // GCC will accept anything as the argument of weakref. Should we
1665 // check for an existing decl?
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001666 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1667 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001668
Michael Han51d8c522013-01-24 16:46:58 +00001669 D->addAttr(::new (S.Context)
1670 WeakRefAttr(Attr.getRange(), S.Context,
1671 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001672}
1673
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001674static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1675 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001676 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001677 return;
1678
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001679 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001680 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1681 return;
1682 }
1683
Chris Lattner6b6b5372008-06-26 18:38:35 +00001684 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001685
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001686 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001687 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001688}
1689
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001690static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001691 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1693 << Attr.getName() << ExpectedFunctionOrMethod;
1694 return;
1695 }
1696
Michael Han51d8c522013-01-24 16:46:58 +00001697 D->addAttr(::new (S.Context)
1698 MinSizeAttr(Attr.getRange(), S.Context,
1699 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001700}
1701
Benjamin Krameree409a92012-05-12 21:10:52 +00001702static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001703 if (!isa<FunctionDecl>(D)) {
1704 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1705 << Attr.getName() << ExpectedFunction;
1706 return;
1707 }
1708
1709 if (D->hasAttr<HotAttr>()) {
1710 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1711 << Attr.getName() << "hot";
1712 return;
1713 }
1714
Michael Han51d8c522013-01-24 16:46:58 +00001715 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1716 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001717}
1718
1719static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001720 if (!isa<FunctionDecl>(D)) {
1721 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1722 << Attr.getName() << ExpectedFunction;
1723 return;
1724 }
1725
1726 if (D->hasAttr<ColdAttr>()) {
1727 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1728 << Attr.getName() << "cold";
1729 return;
1730 }
1731
Michael Han51d8c522013-01-24 16:46:58 +00001732 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1733 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001734}
1735
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001737 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001739 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001740 return;
1741 }
1742
Michael Han51d8c522013-01-24 16:46:58 +00001743 D->addAttr(::new (S.Context)
1744 NakedAttr(Attr.getRange(), S.Context,
1745 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001746}
1747
Chandler Carruth1b03c872011-07-02 00:01:44 +00001748static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1749 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001750 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001751 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001752 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001753 return;
1754 }
Mike Stumpbf916502009-07-24 19:02:52 +00001755
Michael Han51d8c522013-01-24 16:46:58 +00001756 D->addAttr(::new (S.Context)
1757 AlwaysInlineAttr(Attr.getRange(), S.Context,
1758 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001759}
1760
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001761static void handleTLSModelAttr(Sema &S, Decl *D,
1762 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001763 StringRef Model;
1764 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001765 // Check that it is a string.
Tim Northoverf8aebef2013-10-01 14:34:18 +00001766 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001767 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001768
Richard Smith38afbc72013-04-13 02:43:54 +00001769 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001770 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1771 << Attr.getName() << ExpectedTLSVar;
1772 return;
1773 }
1774
1775 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001776 if (Model != "global-dynamic" && Model != "local-dynamic"
1777 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001778 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001779 return;
1780 }
1781
Michael Han51d8c522013-01-24 16:46:58 +00001782 D->addAttr(::new (S.Context)
1783 TLSModelAttr(Attr.getRange(), S.Context, Model,
1784 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001785}
1786
Chandler Carruth1b03c872011-07-02 00:01:44 +00001787static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001788 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001789 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001790 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001791 D->addAttr(::new (S.Context)
1792 MallocAttr(Attr.getRange(), S.Context,
1793 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001794 return;
1795 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001796 }
1797
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001798 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001799}
1800
Chandler Carruth1b03c872011-07-02 00:01:44 +00001801static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001802 D->addAttr(::new (S.Context)
1803 MayAliasAttr(Attr.getRange(), S.Context,
1804 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001805}
1806
Chandler Carruth1b03c872011-07-02 00:01:44 +00001807static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001808 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001809 D->addAttr(::new (S.Context)
1810 NoCommonAttr(Attr.getRange(), S.Context,
1811 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001812 else
1813 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001814 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001815}
1816
Chandler Carruth1b03c872011-07-02 00:01:44 +00001817static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001818 if (S.LangOpts.CPlusPlus) {
1819 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1820 return;
1821 }
1822
Chandler Carruth87c44602011-07-01 23:49:12 +00001823 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001824 D->addAttr(::new (S.Context)
1825 CommonAttr(Attr.getRange(), S.Context,
1826 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001827 else
1828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001829 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001830}
1831
Chandler Carruth1b03c872011-07-02 00:01:44 +00001832static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001833 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001834
1835 if (S.CheckNoReturnAttr(attr)) return;
1836
Chandler Carruth87c44602011-07-01 23:49:12 +00001837 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001838 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001839 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001840 return;
1841 }
1842
Michael Han51d8c522013-01-24 16:46:58 +00001843 D->addAttr(::new (S.Context)
1844 NoReturnAttr(attr.getRange(), S.Context,
1845 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001846}
1847
1848bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001849 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001850 attr.setInvalid();
1851 return true;
1852 }
1853
1854 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001855}
1856
Chandler Carruth1b03c872011-07-02 00:01:44 +00001857static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1858 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001859
1860 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1861 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001862 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1863 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001864 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1865 && !VD->getType()->isFunctionPointerType())) {
1866 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001867 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001868 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001869 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001870 return;
1871 }
1872 }
1873
Michael Han51d8c522013-01-24 16:46:58 +00001874 D->addAttr(::new (S.Context)
1875 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1876 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001877}
1878
Richard Smithcd8ab512013-01-17 01:30:42 +00001879static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1880 const AttributeList &Attr) {
1881 // C++11 [dcl.attr.noreturn]p1:
1882 // The attribute may be applied to the declarator-id in a function
1883 // declaration.
1884 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1885 if (!FD) {
1886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1887 << Attr.getName() << ExpectedFunctionOrMethod;
1888 return;
1889 }
1890
Michael Han51d8c522013-01-24 16:46:58 +00001891 D->addAttr(::new (S.Context)
1892 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1893 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001894}
1895
John Thompson35cc9622010-08-09 21:53:52 +00001896// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001897static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001898/*
1899 Returning a Vector Class in Registers
1900
Eric Christopherf48f3672010-12-01 22:13:54 +00001901 According to the PPU ABI specifications, a class with a single member of
1902 vector type is returned in memory when used as the return value of a function.
1903 This results in inefficient code when implementing vector classes. To return
1904 the value in a single vector register, add the vecreturn attribute to the
1905 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001906
1907 Example:
1908
1909 struct Vector
1910 {
1911 __vector float xyzw;
1912 } __attribute__((vecreturn));
1913
1914 Vector Add(Vector lhs, Vector rhs)
1915 {
1916 Vector result;
1917 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1918 return result; // This will be returned in a register
1919 }
1920*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001921 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001922 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001923 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001924 return;
1925 }
1926
Chandler Carruth87c44602011-07-01 23:49:12 +00001927 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001928 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1929 return;
1930 }
1931
Chandler Carruth87c44602011-07-01 23:49:12 +00001932 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001933 int count = 0;
1934
1935 if (!isa<CXXRecordDecl>(record)) {
1936 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1937 return;
1938 }
1939
1940 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1941 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1942 return;
1943 }
1944
Eric Christopherf48f3672010-12-01 22:13:54 +00001945 for (RecordDecl::field_iterator iter = record->field_begin();
1946 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001947 if ((count == 1) || !iter->getType()->isVectorType()) {
1948 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1949 return;
1950 }
1951 count++;
1952 }
1953
Michael Han51d8c522013-01-24 16:46:58 +00001954 D->addAttr(::new (S.Context)
1955 VecReturnAttr(Attr.getRange(), S.Context,
1956 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001957}
1958
Richard Smith3a2b7a12013-01-28 22:42:45 +00001959static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1960 const AttributeList &Attr) {
1961 if (isa<ParmVarDecl>(D)) {
1962 // [[carries_dependency]] can only be applied to a parameter if it is a
1963 // parameter of a function declaration or lambda.
1964 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1965 S.Diag(Attr.getLoc(),
1966 diag::err_carries_dependency_param_not_function_decl);
1967 return;
1968 }
1969 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001970 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001971 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001972 return;
1973 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001974
1975 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1976 Attr.getRange(), S.Context,
1977 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001978}
1979
Chandler Carruth1b03c872011-07-02 00:01:44 +00001980static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001981 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001982 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001983 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001984 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001985 return;
1986 }
Mike Stumpbf916502009-07-24 19:02:52 +00001987
Michael Han51d8c522013-01-24 16:46:58 +00001988 D->addAttr(::new (S.Context)
1989 UnusedAttr(Attr.getRange(), S.Context,
1990 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001991}
1992
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001993static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1994 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001995 if (!isa<FunctionDecl>(D)) {
1996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1997 << Attr.getName() << ExpectedFunction;
1998 return;
1999 }
2000
Michael Han51d8c522013-01-24 16:46:58 +00002001 D->addAttr(::new (S.Context)
2002 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2003 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002004}
2005
Chandler Carruth1b03c872011-07-02 00:01:44 +00002006static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002007 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00002008 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002009 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2010 return;
2011 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002012 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002013 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002014 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002015 return;
2016 }
Mike Stumpbf916502009-07-24 19:02:52 +00002017
Michael Han51d8c522013-01-24 16:46:58 +00002018 D->addAttr(::new (S.Context)
2019 UsedAttr(Attr.getRange(), S.Context,
2020 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002021}
2022
Chandler Carruth1b03c872011-07-02 00:01:44 +00002023static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002024 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002025 if (Attr.getNumArgs() > 1) {
2026 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002027 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002028 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002029
2030 int priority = 65535; // FIXME: Do not hardcode such constants.
2031 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002032 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002033 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002034 if (E->isTypeDependent() || E->isValueDependent() ||
2035 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002036 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002037 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002038 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002039 return;
2040 }
2041 priority = Idx.getZExtValue();
2042 }
Mike Stumpbf916502009-07-24 19:02:52 +00002043
Chandler Carruth87c44602011-07-01 23:49:12 +00002044 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002045 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002046 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002047 return;
2048 }
2049
Michael Han51d8c522013-01-24 16:46:58 +00002050 D->addAttr(::new (S.Context)
2051 ConstructorAttr(Attr.getRange(), S.Context, priority,
2052 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002053}
2054
Chandler Carruth1b03c872011-07-02 00:01:44 +00002055static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002056 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002057 if (Attr.getNumArgs() > 1) {
2058 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002059 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002060 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002061
2062 int priority = 65535; // FIXME: Do not hardcode such constants.
2063 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002064 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002065 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002066 if (E->isTypeDependent() || E->isValueDependent() ||
2067 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002068 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002069 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002070 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002071 return;
2072 }
2073 priority = Idx.getZExtValue();
2074 }
Mike Stumpbf916502009-07-24 19:02:52 +00002075
Chandler Carruth87c44602011-07-01 23:49:12 +00002076 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002077 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002078 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002079 return;
2080 }
2081
Michael Han51d8c522013-01-24 16:46:58 +00002082 D->addAttr(::new (S.Context)
2083 DestructorAttr(Attr.getRange(), S.Context, priority,
2084 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002085}
2086
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002087template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002088static void handleAttrWithMessage(Sema &S, Decl *D,
2089 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002090 unsigned NumArgs = Attr.getNumArgs();
2091 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002092 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002093 return;
2094 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002095
2096 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002097 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002098 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002099 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002100
Michael Han51d8c522013-01-24 16:46:58 +00002101 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2102 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002103}
2104
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002105static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2106 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002107 D->addAttr(::new (S.Context)
2108 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2109 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002110}
2111
Patrick Beardb2f68202012-04-06 18:12:22 +00002112static void handleObjCRootClassAttr(Sema &S, Decl *D,
2113 const AttributeList &Attr) {
2114 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002115 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2116 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002117 return;
2118 }
2119
Michael Han51d8c522013-01-24 16:46:58 +00002120 D->addAttr(::new (S.Context)
2121 ObjCRootClassAttr(Attr.getRange(), S.Context,
2122 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002123}
2124
Michael Han51d8c522013-01-24 16:46:58 +00002125static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2126 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002127 if (!isa<ObjCInterfaceDecl>(D)) {
2128 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2129 return;
2130 }
2131
Michael Han51d8c522013-01-24 16:46:58 +00002132 D->addAttr(::new (S.Context)
2133 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2134 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002135}
2136
Jordy Rosefad5de92012-05-08 03:27:22 +00002137static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2138 IdentifierInfo *Platform,
2139 VersionTuple Introduced,
2140 VersionTuple Deprecated,
2141 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002142 StringRef PlatformName
2143 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2144 if (PlatformName.empty())
2145 PlatformName = Platform->getName();
2146
2147 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2148 // of these steps are needed).
2149 if (!Introduced.empty() && !Deprecated.empty() &&
2150 !(Introduced <= Deprecated)) {
2151 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2152 << 1 << PlatformName << Deprecated.getAsString()
2153 << 0 << Introduced.getAsString();
2154 return true;
2155 }
2156
2157 if (!Introduced.empty() && !Obsoleted.empty() &&
2158 !(Introduced <= Obsoleted)) {
2159 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2160 << 2 << PlatformName << Obsoleted.getAsString()
2161 << 0 << Introduced.getAsString();
2162 return true;
2163 }
2164
2165 if (!Deprecated.empty() && !Obsoleted.empty() &&
2166 !(Deprecated <= Obsoleted)) {
2167 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2168 << 2 << PlatformName << Obsoleted.getAsString()
2169 << 1 << Deprecated.getAsString();
2170 return true;
2171 }
2172
2173 return false;
2174}
2175
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002176/// \brief Check whether the two versions match.
2177///
2178/// If either version tuple is empty, then they are assumed to match. If
2179/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2180static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2181 bool BeforeIsOkay) {
2182 if (X.empty() || Y.empty())
2183 return true;
2184
2185 if (X == Y)
2186 return true;
2187
2188 if (BeforeIsOkay && X < Y)
2189 return true;
2190
2191 return false;
2192}
2193
Rafael Espindola51be6e32013-01-08 22:04:34 +00002194AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002195 IdentifierInfo *Platform,
2196 VersionTuple Introduced,
2197 VersionTuple Deprecated,
2198 VersionTuple Obsoleted,
2199 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002200 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002201 bool Override,
2202 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002203 VersionTuple MergedIntroduced = Introduced;
2204 VersionTuple MergedDeprecated = Deprecated;
2205 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002206 bool FoundAny = false;
2207
Rafael Espindola98ae8342012-05-10 02:50:16 +00002208 if (D->hasAttrs()) {
2209 AttrVec &Attrs = D->getAttrs();
2210 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2211 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2212 if (!OldAA) {
2213 ++i;
2214 continue;
2215 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002216
Rafael Espindola98ae8342012-05-10 02:50:16 +00002217 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2218 if (OldPlatform != Platform) {
2219 ++i;
2220 continue;
2221 }
2222
2223 FoundAny = true;
2224 VersionTuple OldIntroduced = OldAA->getIntroduced();
2225 VersionTuple OldDeprecated = OldAA->getDeprecated();
2226 VersionTuple OldObsoleted = OldAA->getObsoleted();
2227 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002228
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002229 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2230 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2231 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2232 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002233 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002234 if (Override) {
2235 int Which = -1;
2236 VersionTuple FirstVersion;
2237 VersionTuple SecondVersion;
2238 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2239 Which = 0;
2240 FirstVersion = OldIntroduced;
2241 SecondVersion = Introduced;
2242 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2243 Which = 1;
2244 FirstVersion = Deprecated;
2245 SecondVersion = OldDeprecated;
2246 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2247 Which = 2;
2248 FirstVersion = Obsoleted;
2249 SecondVersion = OldObsoleted;
2250 }
2251
2252 if (Which == -1) {
2253 Diag(OldAA->getLocation(),
2254 diag::warn_mismatched_availability_override_unavail)
2255 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2256 } else {
2257 Diag(OldAA->getLocation(),
2258 diag::warn_mismatched_availability_override)
2259 << Which
2260 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2261 << FirstVersion.getAsString() << SecondVersion.getAsString();
2262 }
2263 Diag(Range.getBegin(), diag::note_overridden_method);
2264 } else {
2265 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2266 Diag(Range.getBegin(), diag::note_previous_attribute);
2267 }
2268
Rafael Espindola98ae8342012-05-10 02:50:16 +00002269 Attrs.erase(Attrs.begin() + i);
2270 --e;
2271 continue;
2272 }
2273
2274 VersionTuple MergedIntroduced2 = MergedIntroduced;
2275 VersionTuple MergedDeprecated2 = MergedDeprecated;
2276 VersionTuple MergedObsoleted2 = MergedObsoleted;
2277
2278 if (MergedIntroduced2.empty())
2279 MergedIntroduced2 = OldIntroduced;
2280 if (MergedDeprecated2.empty())
2281 MergedDeprecated2 = OldDeprecated;
2282 if (MergedObsoleted2.empty())
2283 MergedObsoleted2 = OldObsoleted;
2284
2285 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2286 MergedIntroduced2, MergedDeprecated2,
2287 MergedObsoleted2)) {
2288 Attrs.erase(Attrs.begin() + i);
2289 --e;
2290 continue;
2291 }
2292
2293 MergedIntroduced = MergedIntroduced2;
2294 MergedDeprecated = MergedDeprecated2;
2295 MergedObsoleted = MergedObsoleted2;
2296 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002297 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002298 }
2299
2300 if (FoundAny &&
2301 MergedIntroduced == Introduced &&
2302 MergedDeprecated == Deprecated &&
2303 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002304 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002305
Ted Kremenekcb344392013-04-06 00:34:27 +00002306 // Only create a new attribute if !Override, but we want to do
2307 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002308 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002309 MergedDeprecated, MergedObsoleted) &&
2310 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002311 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2312 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002313 Obsoleted, IsUnavailable, Message,
2314 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002315 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002316 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002317}
2318
Chandler Carruth1b03c872011-07-02 00:01:44 +00002319static void handleAvailabilityAttr(Sema &S, Decl *D,
2320 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002321 if (!checkAttributeNumArgs(S, Attr, 1))
2322 return;
2323 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002324 unsigned Index = Attr.getAttributeSpellingListIndex();
2325
Aaron Ballman624421f2013-08-31 01:11:41 +00002326 IdentifierInfo *II = Platform->Ident;
2327 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2328 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2329 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002330
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002331 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2332 if (!ND) {
2333 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2334 return;
2335 }
2336
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002337 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2338 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2339 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002340 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002341 StringRef Str;
Benjamin Kramerc5617142013-09-13 17:31:48 +00002342 if (const StringLiteral *SE =
2343 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002344 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002345
Aaron Ballman624421f2013-08-31 01:11:41 +00002346 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002347 Introduced.Version,
2348 Deprecated.Version,
2349 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002350 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002351 /*Override=*/false,
2352 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002353 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002354 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002355}
2356
John McCalld4c3d662013-02-20 01:54:26 +00002357template <class T>
2358static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2359 typename T::VisibilityType value,
2360 unsigned attrSpellingListIndex) {
2361 T *existingAttr = D->getAttr<T>();
2362 if (existingAttr) {
2363 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2364 if (existingValue == value)
2365 return NULL;
2366 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2367 S.Diag(range.getBegin(), diag::note_previous_attribute);
2368 D->dropAttr<T>();
2369 }
2370 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2371}
2372
Rafael Espindola599f1b72012-05-13 03:25:18 +00002373VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002374 VisibilityAttr::VisibilityType Vis,
2375 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002376 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2377 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002378}
2379
John McCalld4c3d662013-02-20 01:54:26 +00002380TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2381 TypeVisibilityAttr::VisibilityType Vis,
2382 unsigned AttrSpellingListIndex) {
2383 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2384 AttrSpellingListIndex);
2385}
2386
2387static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2388 bool isTypeVisibility) {
2389 // Visibility attributes don't mean anything on a typedef.
2390 if (isa<TypedefNameDecl>(D)) {
2391 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2392 << Attr.getName();
2393 return;
2394 }
2395
2396 // 'type_visibility' can only go on a type or namespace.
2397 if (isTypeVisibility &&
2398 !(isa<TagDecl>(D) ||
2399 isa<ObjCInterfaceDecl>(D) ||
2400 isa<NamespaceDecl>(D))) {
2401 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2402 << Attr.getName() << ExpectedTypeOrNamespace;
2403 return;
2404 }
2405
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002406 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002407 StringRef TypeStr;
2408 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002409 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002410 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002411
Sean Huntcf807c42010-08-18 23:23:40 +00002412 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002413 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002414 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002415 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002416 return;
2417 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002418
2419 // Complain about attempts to use protected visibility on targets
2420 // (like Darwin) that don't support it.
2421 if (type == VisibilityAttr::Protected &&
2422 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2423 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2424 type = VisibilityAttr::Default;
2425 }
Mike Stumpbf916502009-07-24 19:02:52 +00002426
Michael Han51d8c522013-01-24 16:46:58 +00002427 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002428 clang::Attr *newAttr;
2429 if (isTypeVisibility) {
2430 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2431 (TypeVisibilityAttr::VisibilityType) type,
2432 Index);
2433 } else {
2434 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2435 }
2436 if (newAttr)
2437 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002438}
2439
Chandler Carruth1b03c872011-07-02 00:01:44 +00002440static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2441 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002442 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2443 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002444 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002445 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002446 return;
2447 }
2448
Aaron Ballman624421f2013-08-31 01:11:41 +00002449 if (!Attr.isArgIdent(0)) {
2450 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2451 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002452 return;
2453 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002454
Aaron Ballmand0686072013-09-11 19:47:58 +00002455 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2456 ObjCMethodFamilyAttr::FamilyKind F;
2457 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2458 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2459 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002460 return;
2461 }
2462
Aaron Ballmand0686072013-09-11 19:47:58 +00002463 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002464 !method->getResultType()->isObjCObjectPointerType()) {
2465 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2466 << method->getResultType();
2467 // Ignore the attribute.
2468 return;
2469 }
2470
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002471 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002472 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002473}
2474
Chandler Carruth1b03c872011-07-02 00:01:44 +00002475static void handleObjCExceptionAttr(Sema &S, Decl *D,
2476 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002477 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2478 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002479 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2480 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002481 return;
2482 }
Mike Stumpbf916502009-07-24 19:02:52 +00002483
Michael Han51d8c522013-01-24 16:46:58 +00002484 D->addAttr(::new (S.Context)
2485 ObjCExceptionAttr(Attr.getRange(), S.Context,
2486 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002487}
2488
Chandler Carruth1b03c872011-07-02 00:01:44 +00002489static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002490 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002491 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002492 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002493 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2494 return;
2495 }
2496 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002497 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2498 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002499 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002500 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2501 return;
2502 }
2503 }
2504 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002505 // It is okay to include this attribute on properties, e.g.:
2506 //
2507 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2508 //
2509 // In this case it follows tradition and suppresses an error in the above
2510 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002511 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002512 }
Michael Han51d8c522013-01-24 16:46:58 +00002513 D->addAttr(::new (S.Context)
2514 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2515 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002516}
2517
Mike Stumpbf916502009-07-24 19:02:52 +00002518static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002519handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002520 if (!isa<FunctionDecl>(D)) {
2521 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2522 return;
2523 }
2524
Michael Han51d8c522013-01-24 16:46:58 +00002525 D->addAttr(::new (S.Context)
2526 OverloadableAttr(Attr.getRange(), S.Context,
2527 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002528}
2529
Chandler Carruth1b03c872011-07-02 00:01:44 +00002530static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002531 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002532 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002533 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002534 return;
2535 }
Mike Stumpbf916502009-07-24 19:02:52 +00002536
Aaron Ballman624421f2013-08-31 01:11:41 +00002537 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002538 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002539 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2540 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2541 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002542 return;
2543 }
Mike Stumpbf916502009-07-24 19:02:52 +00002544
Michael Han51d8c522013-01-24 16:46:58 +00002545 D->addAttr(::new (S.Context)
2546 BlocksAttr(Attr.getRange(), S.Context, type,
2547 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002548}
2549
Chandler Carruth1b03c872011-07-02 00:01:44 +00002550static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002551 // check the attribute arguments.
2552 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002553 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002554 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002555 }
2556
John McCall3323fad2011-09-09 07:56:05 +00002557 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002558 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002559 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002560 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002561 if (E->isTypeDependent() || E->isValueDependent() ||
2562 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002563 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002564 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002565 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002566 return;
2567 }
Mike Stumpbf916502009-07-24 19:02:52 +00002568
John McCall3323fad2011-09-09 07:56:05 +00002569 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002570 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2571 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002572 return;
2573 }
John McCall3323fad2011-09-09 07:56:05 +00002574
2575 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002576 }
2577
John McCall3323fad2011-09-09 07:56:05 +00002578 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002579 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002580 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002581 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002582 if (E->isTypeDependent() || E->isValueDependent() ||
2583 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002584 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002585 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002586 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002587 return;
2588 }
2589 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002590
John McCall3323fad2011-09-09 07:56:05 +00002591 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002592 // FIXME: This error message could be improved, it would be nice
2593 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002594 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2595 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002596 return;
2597 }
2598 }
2599
Chandler Carruth87c44602011-07-01 23:49:12 +00002600 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002601 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002602 if (isa<FunctionNoProtoType>(FT)) {
2603 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2604 return;
2605 }
Mike Stumpbf916502009-07-24 19:02:52 +00002606
Chris Lattner897cd902009-03-17 23:03:47 +00002607 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002608 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002609 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002610 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002611 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002612 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002613 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002614 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002615 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002616 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2617 if (!BD->isVariadic()) {
2618 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2619 return;
2620 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002621 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002622 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002623 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002624 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002625 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002626 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002627 int m = Ty->isFunctionPointerType() ? 0 : 1;
2628 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002629 return;
2630 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002631 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002633 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002634 return;
2635 }
Anders Carlsson77091822008-10-05 18:05:59 +00002636 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002638 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002639 return;
2640 }
Michael Han51d8c522013-01-24 16:46:58 +00002641 D->addAttr(::new (S.Context)
2642 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2643 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002644}
2645
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002646static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002647 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2648 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2649 else
2650 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2651}
2652
Chandler Carruth1b03c872011-07-02 00:01:44 +00002653static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002654 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002656 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002657 return;
2658 }
Mike Stumpbf916502009-07-24 19:02:52 +00002659
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002660 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2661 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2662 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002663 return;
2664 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002665 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2666 if (MD->getResultType()->isVoidType()) {
2667 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2668 << Attr.getName() << 1;
2669 return;
2670 }
2671
Michael Han51d8c522013-01-24 16:46:58 +00002672 D->addAttr(::new (S.Context)
2673 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2674 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002675}
2676
Chandler Carruth1b03c872011-07-02 00:01:44 +00002677static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002678 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002679 if (isa<CXXRecordDecl>(D)) {
2680 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2681 return;
2682 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002683 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2684 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002685 return;
2686 }
2687
Chandler Carruth87c44602011-07-01 23:49:12 +00002688 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002689
Michael Han51d8c522013-01-24 16:46:58 +00002690 nd->addAttr(::new (S.Context)
2691 WeakAttr(Attr.getRange(), S.Context,
2692 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002693}
2694
Chandler Carruth1b03c872011-07-02 00:01:44 +00002695static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002696 // weak_import only applies to variable & function declarations.
2697 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002698 if (!D->canBeWeakImported(isDef)) {
2699 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002700 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2701 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002702 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002703 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002704 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002705 // Nothing to warn about here.
2706 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002707 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002708 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002709
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002710 return;
2711 }
2712
Michael Han51d8c522013-01-24 16:46:58 +00002713 D->addAttr(::new (S.Context)
2714 WeakImportAttr(Attr.getRange(), S.Context,
2715 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002716}
2717
Tanya Lattner0df579e2012-07-09 22:06:01 +00002718// Handles reqd_work_group_size and work_group_size_hint.
2719static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002720 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002721 unsigned WGSize[3];
2722 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002723 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002724 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002725 if (E->isTypeDependent() || E->isValueDependent() ||
2726 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002727 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2728 << Attr.getName() << AANT_ArgumentIntegerConstant
2729 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002730 return;
2731 }
2732 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2733 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002734
2735 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2736 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2737 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2738 if (!(A->getXDim() == WGSize[0] &&
2739 A->getYDim() == WGSize[1] &&
2740 A->getZDim() == WGSize[2])) {
2741 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2742 Attr.getName();
2743 }
2744 }
2745
2746 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2747 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2748 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2749 if (!(A->getXDim() == WGSize[0] &&
2750 A->getYDim() == WGSize[1] &&
2751 A->getZDim() == WGSize[2])) {
2752 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2753 Attr.getName();
2754 }
2755 }
2756
2757 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2758 D->addAttr(::new (S.Context)
2759 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002760 WGSize[0], WGSize[1], WGSize[2],
2761 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002762 else
2763 D->addAttr(::new (S.Context)
2764 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002765 WGSize[0], WGSize[1], WGSize[2],
2766 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002767}
2768
Joey Gouly37453b92013-03-08 09:42:32 +00002769static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2770 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2771
Aaron Ballman624421f2013-08-31 01:11:41 +00002772 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002773 return;
2774
Aaron Ballman624421f2013-08-31 01:11:41 +00002775 if (!Attr.hasParsedType()) {
2776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2777 << Attr.getName() << 1;
2778 return;
2779 }
2780
Joey Gouly37453b92013-03-08 09:42:32 +00002781 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2782
2783 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2784 (ParmType->isBooleanType() ||
2785 !ParmType->isIntegralType(S.getASTContext()))) {
2786 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2787 << ParmType;
2788 return;
2789 }
2790
2791 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2792 D->hasAttr<VecTypeHintAttr>()) {
2793 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2794 if (A->getTypeHint() != ParmType) {
2795 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2796 return;
2797 }
2798 }
2799
2800 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2801 ParmType, Attr.getLoc()));
2802}
2803
Rafael Espindola599f1b72012-05-13 03:25:18 +00002804SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002805 StringRef Name,
2806 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002807 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2808 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002809 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002810 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2811 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002812 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002813 }
Michael Han51d8c522013-01-24 16:46:58 +00002814 return ::new (Context) SectionAttr(Range, Context, Name,
2815 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002816}
2817
Chandler Carruth1b03c872011-07-02 00:01:44 +00002818static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002819 // Make sure that there is a string literal as the sections's single
2820 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002821 StringRef Str;
2822 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002823 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002824 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Chris Lattner797c3c42009-08-10 19:03:04 +00002826 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002827 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002828 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002829 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002830 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002831 return;
2832 }
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002834 // This attribute cannot be applied to local variables.
2835 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002836 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002837 return;
2838 }
Michael Han51d8c522013-01-24 16:46:58 +00002839
2840 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002841 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002842 if (NewAttr)
2843 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002844}
2845
Chris Lattner6b6b5372008-06-26 18:38:35 +00002846
Chandler Carruth1b03c872011-07-02 00:01:44 +00002847static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002848 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002849 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002850 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002851 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002852 D->addAttr(::new (S.Context)
2853 NoThrowAttr(Attr.getRange(), S.Context,
2854 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002855 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002856}
2857
Chandler Carruth1b03c872011-07-02 00:01:44 +00002858static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002859 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002860 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002861 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002862 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002863 D->addAttr(::new (S.Context)
2864 ConstAttr(Attr.getRange(), S.Context,
2865 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002866 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002867}
2868
Chandler Carruth1b03c872011-07-02 00:01:44 +00002869static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002870 D->addAttr(::new (S.Context)
2871 PureAttr(Attr.getRange(), S.Context,
2872 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002873}
2874
Chandler Carruth1b03c872011-07-02 00:01:44 +00002875static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002876 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002877 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002878 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002879 return;
2880 }
Mike Stumpbf916502009-07-24 19:02:52 +00002881
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002882 Expr *E = Attr.getArgAsExpr(0);
2883 SourceLocation Loc = E->getExprLoc();
2884 FunctionDecl *FD = 0;
2885 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002886
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002887 // gcc only allows for simple identifiers. Since we support more than gcc, we
2888 // will warn the user.
2889 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2890 if (DRE->hasQualifier())
2891 S.Diag(Loc, diag::warn_cleanup_ext);
2892 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2893 NI = DRE->getNameInfo();
2894 if (!FD) {
2895 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2896 << NI.getName();
2897 return;
2898 }
2899 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2900 if (ULE->hasExplicitTemplateArgs())
2901 S.Diag(Loc, diag::warn_cleanup_ext);
Mike Stumpbf916502009-07-24 19:02:52 +00002902
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002903 // This will diagnose the case where the function cannot be found.
2904 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2905 NI = ULE->getNameInfo();
2906 } else {
2907 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002908 return;
2909 }
2910
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002911 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002912 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2913 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002914 return;
2915 }
Mike Stumpbf916502009-07-24 19:02:52 +00002916
Anders Carlsson89941c12009-02-07 23:16:50 +00002917 // We're currently more strict than GCC about what function types we accept.
2918 // If this ever proves to be a problem it should be easy to fix.
2919 QualType Ty = S.Context.getPointerType(VD->getType());
2920 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002921 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2922 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002923 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2924 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002925 return;
2926 }
Mike Stumpbf916502009-07-24 19:02:52 +00002927
Michael Han51d8c522013-01-24 16:46:58 +00002928 D->addAttr(::new (S.Context)
2929 CleanupAttr(Attr.getRange(), S.Context, FD,
2930 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002931}
2932
Mike Stumpbf916502009-07-24 19:02:52 +00002933/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002934/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002935static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002936 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002937 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002938 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002939 return;
2940 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002941
Aaron Ballman624421f2013-08-31 01:11:41 +00002942 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002943 uint64_t ArgIdx;
2944 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2945 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002946 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002947
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002948 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002949 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002950
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002951 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2952 if (not_nsstring_type &&
2953 !isCFStringType(Ty, S.Context) &&
2954 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002955 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002956 // FIXME: Should highlight the actual expression that has the wrong type.
2957 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002958 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002959 << IdxExpr->getSourceRange();
2960 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002961 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002962 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002963 if (!isNSStringType(Ty, S.Context) &&
2964 !isCFStringType(Ty, S.Context) &&
2965 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002966 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002967 // FIXME: Should highlight the actual expression that has the wrong type.
2968 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002969 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002970 << IdxExpr->getSourceRange();
2971 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002972 }
2973
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002974 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2975 // because that has corrected for the implicit this parameter, and is zero-
2976 // based. The attribute expects what the user wrote explicitly.
2977 llvm::APSInt Val;
2978 IdxExpr->EvaluateAsInt(Val, S.Context);
2979
Michael Han51d8c522013-01-24 16:46:58 +00002980 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002981 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002982 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002983}
2984
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002985enum FormatAttrKind {
2986 CFStringFormat,
2987 NSStringFormat,
2988 StrftimeFormat,
2989 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002990 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002991 InvalidFormat
2992};
2993
2994/// getFormatAttrKind - Map from format attribute names to supported format
2995/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002996static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002997 return llvm::StringSwitch<FormatAttrKind>(Format)
2998 // Check for formats that get handled specially.
2999 .Case("NSString", NSStringFormat)
3000 .Case("CFString", CFStringFormat)
3001 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003002
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003003 // Otherwise, check for supported formats.
3004 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3005 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3006 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003007
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003008 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3009 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003010}
3011
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003012/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003013/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003014static void handleInitPriorityAttr(Sema &S, Decl *D,
3015 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003016 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003017 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3018 return;
3019 }
3020
Chandler Carruth87c44602011-07-01 23:49:12 +00003021 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003022 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3023 Attr.setInvalid();
3024 return;
3025 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003026 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003027 if (S.Context.getAsArrayType(T))
3028 T = S.Context.getBaseElementType(T);
3029 if (!T->getAs<RecordType>()) {
3030 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3031 Attr.setInvalid();
3032 return;
3033 }
3034
Aaron Ballman624421f2013-08-31 01:11:41 +00003035 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003036
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003037 llvm::APSInt priority(32);
3038 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3039 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003040 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3041 << Attr.getName() << AANT_ArgumentIntegerConstant
3042 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003043 Attr.setInvalid();
3044 return;
3045 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003046 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003047 if (prioritynum < 101 || prioritynum > 65535) {
3048 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3049 << priorityExpr->getSourceRange();
3050 Attr.setInvalid();
3051 return;
3052 }
Michael Han51d8c522013-01-24 16:46:58 +00003053 D->addAttr(::new (S.Context)
3054 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3055 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003056}
3057
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003058FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3059 IdentifierInfo *Format, int FormatIdx,
3060 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003061 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003062 // Check whether we already have an equivalent format attribute.
3063 for (specific_attr_iterator<FormatAttr>
3064 i = D->specific_attr_begin<FormatAttr>(),
3065 e = D->specific_attr_end<FormatAttr>();
3066 i != e ; ++i) {
3067 FormatAttr *f = *i;
3068 if (f->getType() == Format &&
3069 f->getFormatIdx() == FormatIdx &&
3070 f->getFirstArg() == FirstArg) {
3071 // If we don't have a valid location for this attribute, adopt the
3072 // location.
3073 if (f->getLocation().isInvalid())
3074 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003075 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003076 }
3077 }
3078
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003079 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3080 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003081}
3082
Mike Stumpbf916502009-07-24 19:02:52 +00003083/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003084/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003085static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003086 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003088 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003089 return;
3090 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003091
Chandler Carruth87c44602011-07-01 23:49:12 +00003092 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003093 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003094 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003095 return;
3096 }
3097
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003098 // In C++ the implicit 'this' function parameter also counts, and they are
3099 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003100 bool HasImplicitThisParam = isInstanceMethod(D);
3101 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003102 unsigned FirstIdx = 1;
3103
Aaron Ballman624421f2013-08-31 01:11:41 +00003104 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3105 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003106
3107 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003108 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003109 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003110 // If we've modified the string name, we need a new identifier for it.
3111 II = &S.Context.Idents.get(Format);
3112 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003113
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003114 // Check for supported formats.
3115 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003116
3117 if (Kind == IgnoredFormat)
3118 return;
3119
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003120 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003121 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003122 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003123 return;
3124 }
3125
3126 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003127 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003128 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003129 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3130 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003131 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003132 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003133 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003134 return;
3135 }
3136
3137 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003138 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003139 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003140 return;
3141 }
3142
3143 // FIXME: Do we need to bounds check?
3144 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003145
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003146 if (HasImplicitThisParam) {
3147 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003148 S.Diag(Attr.getLoc(),
3149 diag::err_format_attribute_implicit_this_format_string)
3150 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003151 return;
3152 }
3153 ArgIdx--;
3154 }
Mike Stump1eb44332009-09-09 15:08:12 +00003155
Chris Lattner6b6b5372008-06-26 18:38:35 +00003156 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003157 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003158
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003159 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003160 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003161 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3162 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003163 return;
3164 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003165 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003166 // FIXME: do we need to check if the type is NSString*? What are the
3167 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003168 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003169 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003170 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3171 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003172 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003173 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003174 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003175 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003176 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003177 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3178 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003179 return;
3180 }
3181
3182 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003183 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003184 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003185 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3186 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003187 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003188 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003189 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003190 return;
3191 }
3192
3193 // check if the function is variadic if the 3rd argument non-zero
3194 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003195 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003196 ++NumArgs; // +1 for ...
3197 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003198 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003199 return;
3200 }
3201 }
3202
Chris Lattner3c73c412008-11-19 08:23:25 +00003203 // strftime requires FirstArg to be 0 because it doesn't read from any
3204 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003205 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003206 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003207 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3208 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003209 return;
3210 }
3211 // if 0 it disables parameter checking (to use with e.g. va_list)
3212 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003213 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003214 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003215 return;
3216 }
3217
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003218 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003219 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003220 FirstArg.getZExtValue(),
3221 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003222 if (NewAttr)
3223 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003224}
3225
Chandler Carruth1b03c872011-07-02 00:01:44 +00003226static void handleTransparentUnionAttr(Sema &S, Decl *D,
3227 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003228 // Try to find the underlying union declaration.
3229 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003230 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003231 if (TD && TD->getUnderlyingType()->isUnionType())
3232 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3233 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003234 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003235
3236 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003237 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003238 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003239 return;
3240 }
3241
John McCall5e1cdac2011-10-07 06:10:15 +00003242 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003243 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003244 diag::warn_transparent_union_attribute_not_definition);
3245 return;
3246 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003247
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003248 RecordDecl::field_iterator Field = RD->field_begin(),
3249 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003250 if (Field == FieldEnd) {
3251 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3252 return;
3253 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003254
David Blaikie581deb32012-06-06 20:45:41 +00003255 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003256 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003257 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003258 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003259 diag::warn_transparent_union_attribute_floating)
3260 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003261 return;
3262 }
3263
3264 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3265 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3266 for (; Field != FieldEnd; ++Field) {
3267 QualType FieldType = Field->getType();
3268 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3269 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3270 // Warn if we drop the attribute.
3271 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003272 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003273 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003274 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003275 diag::warn_transparent_union_attribute_field_size_align)
3276 << isSize << Field->getDeclName() << FieldBits;
3277 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003278 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003279 diag::note_transparent_union_first_field_size_align)
3280 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003281 return;
3282 }
3283 }
3284
Michael Han51d8c522013-01-24 16:46:58 +00003285 RD->addAttr(::new (S.Context)
3286 TransparentUnionAttr(Attr.getRange(), S.Context,
3287 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003288}
3289
Chandler Carruth1b03c872011-07-02 00:01:44 +00003290static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003291 // Make sure that there is a string literal as the annotation's single
3292 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003293 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003294 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003295 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003296
3297 // Don't duplicate annotations that are already set.
3298 for (specific_attr_iterator<AnnotateAttr>
3299 i = D->specific_attr_begin<AnnotateAttr>(),
3300 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003301 if ((*i)->getAnnotation() == Str)
3302 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003303 }
Michael Han51d8c522013-01-24 16:46:58 +00003304
3305 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003306 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003307 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003308}
3309
Chandler Carruth1b03c872011-07-02 00:01:44 +00003310static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003311 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003312 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3314 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003315 return;
3316 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003317
Richard Smithbe507b62013-02-01 08:12:08 +00003318 if (Attr.getNumArgs() == 0) {
3319 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3320 true, 0, Attr.getAttributeSpellingListIndex()));
3321 return;
3322 }
3323
Aaron Ballman624421f2013-08-31 01:11:41 +00003324 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003325 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3326 S.Diag(Attr.getEllipsisLoc(),
3327 diag::err_pack_expansion_without_parameter_packs);
3328 return;
3329 }
3330
3331 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3332 return;
3333
3334 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3335 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003336}
3337
3338void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003339 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003340 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3341 SourceLocation AttrLoc = AttrRange.getBegin();
3342
Richard Smith4cd81c52013-01-29 09:02:09 +00003343 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003344 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003345 // C++11 [dcl.align]p1:
3346 // An alignment-specifier may be applied to a variable or to a class
3347 // data member, but it shall not be applied to a bit-field, a function
3348 // parameter, the formal parameter of a catch clause, or a variable
3349 // declared with the register storage class specifier. An
3350 // alignment-specifier may also be applied to the declaration of a class
3351 // or enumeration type.
3352 // C11 6.7.5/2:
3353 // An alignment attribute shall not be specified in a declaration of
3354 // a typedef, or a bit-field, or a function, or a parameter, or an
3355 // object declared with the register storage-class specifier.
3356 int DiagKind = -1;
3357 if (isa<ParmVarDecl>(D)) {
3358 DiagKind = 0;
3359 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3360 if (VD->getStorageClass() == SC_Register)
3361 DiagKind = 1;
3362 if (VD->isExceptionVariable())
3363 DiagKind = 2;
3364 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3365 if (FD->isBitField())
3366 DiagKind = 3;
3367 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003368 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3369 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003370 << (TmpAttr.isC11() ? ExpectedVariableOrField
3371 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003372 return;
3373 }
3374 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003375 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003376 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003377 return;
3378 }
3379 }
3380
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003381 if (E->isTypeDependent() || E->isValueDependent()) {
3382 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003383 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3384 AA->setPackExpansion(IsPackExpansion);
3385 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003386 return;
3387 }
Michael Hana31f65b2013-02-01 01:19:17 +00003388
Sean Huntcf807c42010-08-18 23:23:40 +00003389 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003390 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003391 ExprResult ICE
3392 = VerifyIntegerConstantExpression(E, &Alignment,
3393 diag::err_aligned_attribute_argument_not_int,
3394 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003395 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003396 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003397
3398 // C++11 [dcl.align]p2:
3399 // -- if the constant expression evaluates to zero, the alignment
3400 // specifier shall have no effect
3401 // C11 6.7.5p6:
3402 // An alignment specification of zero has no effect.
3403 if (!(TmpAttr.isAlignas() && !Alignment) &&
3404 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003405 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3406 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003407 return;
3408 }
Michael Hana31f65b2013-02-01 01:19:17 +00003409
Richard Smithbe507b62013-02-01 08:12:08 +00003410 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003411 // We've already verified it's a power of 2, now let's make sure it's
3412 // 8192 or less.
3413 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003414 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003415 << E->getSourceRange();
3416 return;
3417 }
3418 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003419
Richard Smithf6565a92013-02-22 08:32:16 +00003420 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3421 ICE.take(), SpellingListIndex);
3422 AA->setPackExpansion(IsPackExpansion);
3423 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003424}
3425
Michael Hana31f65b2013-02-01 01:19:17 +00003426void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003427 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003428 // FIXME: Cache the number on the Attr object if non-dependent?
3429 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003430 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3431 SpellingListIndex);
3432 AA->setPackExpansion(IsPackExpansion);
3433 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003434}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003435
Richard Smithbe507b62013-02-01 08:12:08 +00003436void Sema::CheckAlignasUnderalignment(Decl *D) {
3437 assert(D->hasAttrs() && "no attributes on decl");
3438
3439 QualType Ty;
3440 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3441 Ty = VD->getType();
3442 else
3443 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003444 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003445 return;
3446
3447 // C++11 [dcl.align]p5, C11 6.7.5/4:
3448 // The combined effect of all alignment attributes in a declaration shall
3449 // not specify an alignment that is less strict than the alignment that
3450 // would otherwise be required for the entity being declared.
3451 AlignedAttr *AlignasAttr = 0;
3452 unsigned Align = 0;
3453 for (specific_attr_iterator<AlignedAttr>
3454 I = D->specific_attr_begin<AlignedAttr>(),
3455 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3456 if (I->isAlignmentDependent())
3457 return;
3458 if (I->isAlignas())
3459 AlignasAttr = *I;
3460 Align = std::max(Align, I->getAlignment(Context));
3461 }
3462
3463 if (AlignasAttr && Align) {
3464 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3465 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3466 if (NaturalAlign > RequestedAlign)
3467 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3468 << Ty << (unsigned)NaturalAlign.getQuantity();
3469 }
3470}
3471
Chandler Carruthd309c812011-07-01 23:49:16 +00003472/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003473/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003474///
Mike Stumpbf916502009-07-24 19:02:52 +00003475/// Despite what would be logical, the mode attribute is a decl attribute, not a
3476/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3477/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003478static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003479 // This attribute isn't documented, but glibc uses it. It changes
3480 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003481 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003482 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3483 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003484 return;
3485 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003486
Aaron Ballman624421f2013-08-31 01:11:41 +00003487 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3488 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003489
3490 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003491 if (Str.startswith("__") && Str.endswith("__"))
3492 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003493
3494 unsigned DestWidth = 0;
3495 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003496 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003497 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003498 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003499 switch (Str[0]) {
3500 case 'Q': DestWidth = 8; break;
3501 case 'H': DestWidth = 16; break;
3502 case 'S': DestWidth = 32; break;
3503 case 'D': DestWidth = 64; break;
3504 case 'X': DestWidth = 96; break;
3505 case 'T': DestWidth = 128; break;
3506 }
3507 if (Str[1] == 'F') {
3508 IntegerMode = false;
3509 } else if (Str[1] == 'C') {
3510 IntegerMode = false;
3511 ComplexMode = true;
3512 } else if (Str[1] != 'I') {
3513 DestWidth = 0;
3514 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003515 break;
3516 case 4:
3517 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3518 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003519 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003520 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003521 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003522 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003523 break;
3524 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003525 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003526 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003527 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003528 case 11:
3529 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003530 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003531 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003532 }
3533
3534 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003535 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003536 OldTy = TD->getUnderlyingType();
3537 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3538 OldTy = VD->getType();
3539 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003540 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003541 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003542 return;
3543 }
Eli Friedman73397492009-03-03 06:41:03 +00003544
John McCall183700f2009-09-21 23:43:11 +00003545 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003546 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3547 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003548 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003549 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3550 } else if (ComplexMode) {
3551 if (!OldTy->isComplexType())
3552 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3553 } else {
3554 if (!OldTy->isFloatingType())
3555 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3556 }
3557
Mike Stump390b4cc2009-05-16 07:39:55 +00003558 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3559 // and friends, at least with glibc.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003560 // FIXME: Make sure floating-point mappings are accurate
3561 // FIXME: Support XF and TF types
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003562 if (!DestWidth) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003563 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003564 return;
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003565 }
3566
3567 QualType NewTy;
3568
3569 if (IntegerMode)
3570 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3571 OldTy->isSignedIntegerType());
3572 else
3573 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3574
3575 if (NewTy.isNull()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003576 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003577 return;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003578 }
3579
Eli Friedman73397492009-03-03 06:41:03 +00003580 if (ComplexMode) {
3581 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003582 }
3583
3584 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003585 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3586 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3587 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003588 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003589
3590 D->addAttr(::new (S.Context)
3591 ModeAttr(Attr.getRange(), S.Context, Name,
3592 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003593}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003594
Chandler Carruth1b03c872011-07-02 00:01:44 +00003595static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003596 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3597 if (!VD->hasGlobalStorage())
3598 S.Diag(Attr.getLoc(),
3599 diag::warn_attribute_requires_functions_or_static_globals)
3600 << Attr.getName();
3601 } else if (!isFunctionOrMethod(D)) {
3602 S.Diag(Attr.getLoc(),
3603 diag::warn_attribute_requires_functions_or_static_globals)
3604 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003605 return;
3606 }
Mike Stumpbf916502009-07-24 19:02:52 +00003607
Michael Han51d8c522013-01-24 16:46:58 +00003608 D->addAttr(::new (S.Context)
3609 NoDebugAttr(Attr.getRange(), S.Context,
3610 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003611}
3612
Chandler Carruth1b03c872011-07-02 00:01:44 +00003613static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003614 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003616 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003617 return;
3618 }
Mike Stumpbf916502009-07-24 19:02:52 +00003619
Michael Han51d8c522013-01-24 16:46:58 +00003620 D->addAttr(::new (S.Context)
3621 NoInlineAttr(Attr.getRange(), S.Context,
3622 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003623}
3624
Chandler Carruth1b03c872011-07-02 00:01:44 +00003625static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3626 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003627 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003629 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003630 return;
3631 }
3632
Michael Han51d8c522013-01-24 16:46:58 +00003633 D->addAttr(::new (S.Context)
3634 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3635 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003636}
3637
Chandler Carruth1b03c872011-07-02 00:01:44 +00003638static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003639 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003640 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003642 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003643 return;
3644 }
3645
Michael Han51d8c522013-01-24 16:46:58 +00003646 D->addAttr(::new (S.Context)
3647 CUDAConstantAttr(Attr.getRange(), S.Context,
3648 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003649 } else {
3650 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3651 }
3652}
3653
Chandler Carruth1b03c872011-07-02 00:01:44 +00003654static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003655 if (S.LangOpts.CUDA) {
3656 // check the attribute arguments.
3657 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003658 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3659 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003660 return;
3661 }
3662
Chandler Carruth87c44602011-07-01 23:49:12 +00003663 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003664 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003665 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003666 return;
3667 }
3668
Michael Han51d8c522013-01-24 16:46:58 +00003669 D->addAttr(::new (S.Context)
3670 CUDADeviceAttr(Attr.getRange(), S.Context,
3671 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003672 } else {
3673 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3674 }
3675}
3676
Chandler Carruth1b03c872011-07-02 00:01:44 +00003677static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003678 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003679 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003680 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003681 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003682 return;
3683 }
3684
Chandler Carruth87c44602011-07-01 23:49:12 +00003685 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003686 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003687 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003688 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003689 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3690 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003691 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003692 "void");
3693 } else {
3694 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3695 << FD->getType();
3696 }
3697 return;
3698 }
3699
Michael Han51d8c522013-01-24 16:46:58 +00003700 D->addAttr(::new (S.Context)
3701 CUDAGlobalAttr(Attr.getRange(), S.Context,
3702 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003703 } else {
3704 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3705 }
3706}
3707
Chandler Carruth1b03c872011-07-02 00:01:44 +00003708static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003709 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003710 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003711 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003712 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003713 return;
3714 }
3715
Michael Han51d8c522013-01-24 16:46:58 +00003716 D->addAttr(::new (S.Context)
3717 CUDAHostAttr(Attr.getRange(), S.Context,
3718 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003719 } else {
3720 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3721 }
3722}
3723
Chandler Carruth1b03c872011-07-02 00:01:44 +00003724static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003725 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003726 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003727 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003728 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003729 return;
3730 }
3731
Michael Han51d8c522013-01-24 16:46:58 +00003732 D->addAttr(::new (S.Context)
3733 CUDASharedAttr(Attr.getRange(), S.Context,
3734 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003735 } else {
3736 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3737 }
3738}
3739
Chandler Carruth1b03c872011-07-02 00:01:44 +00003740static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003741 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003742 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003743 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003744 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003745 return;
3746 }
Mike Stumpbf916502009-07-24 19:02:52 +00003747
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003748 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003749 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003750 return;
3751 }
Mike Stumpbf916502009-07-24 19:02:52 +00003752
Michael Han51d8c522013-01-24 16:46:58 +00003753 D->addAttr(::new (S.Context)
3754 GNUInlineAttr(Attr.getRange(), S.Context,
3755 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003756}
3757
Chandler Carruth1b03c872011-07-02 00:01:44 +00003758static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003759 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003760
Aaron Ballmanfff32482012-12-09 17:45:41 +00003761 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003762 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003763 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3764 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003765 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003766 return;
3767
Chandler Carruth87c44602011-07-01 23:49:12 +00003768 if (!isa<ObjCMethodDecl>(D)) {
3769 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3770 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003771 return;
3772 }
3773
Chandler Carruth87c44602011-07-01 23:49:12 +00003774 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003775 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003776 D->addAttr(::new (S.Context)
3777 FastCallAttr(Attr.getRange(), S.Context,
3778 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003779 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003780 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003781 D->addAttr(::new (S.Context)
3782 StdCallAttr(Attr.getRange(), S.Context,
3783 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003784 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003785 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003786 D->addAttr(::new (S.Context)
3787 ThisCallAttr(Attr.getRange(), S.Context,
3788 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003789 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003790 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003791 D->addAttr(::new (S.Context)
3792 CDeclAttr(Attr.getRange(), S.Context,
3793 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003794 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003795 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003796 D->addAttr(::new (S.Context)
3797 PascalAttr(Attr.getRange(), S.Context,
3798 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003799 return;
Charles Davise8519c32013-08-30 04:39:01 +00003800 case AttributeList::AT_MSABI:
3801 D->addAttr(::new (S.Context)
3802 MSABIAttr(Attr.getRange(), S.Context,
3803 Attr.getAttributeSpellingListIndex()));
3804 return;
3805 case AttributeList::AT_SysVABI:
3806 D->addAttr(::new (S.Context)
3807 SysVABIAttr(Attr.getRange(), S.Context,
3808 Attr.getAttributeSpellingListIndex()));
3809 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003810 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003811 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003812 switch (CC) {
3813 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003814 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003815 break;
3816 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003817 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003818 break;
3819 default:
3820 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003821 }
3822
Michael Han51d8c522013-01-24 16:46:58 +00003823 D->addAttr(::new (S.Context)
3824 PcsAttr(Attr.getRange(), S.Context, PCS,
3825 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003826 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003827 }
Derek Schuff263366f2012-10-16 22:30:41 +00003828 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003829 D->addAttr(::new (S.Context)
3830 PnaclCallAttr(Attr.getRange(), S.Context,
3831 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003832 return;
Guy Benyei38980082012-12-25 08:53:55 +00003833 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003834 D->addAttr(::new (S.Context)
3835 IntelOclBiccAttr(Attr.getRange(), S.Context,
3836 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003837 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003838
Abramo Bagnarae215f722010-04-30 13:10:51 +00003839 default:
3840 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003841 }
3842}
3843
Chandler Carruth1b03c872011-07-02 00:01:44 +00003844static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003845 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003846}
3847
Guy Benyei1db70402013-03-24 13:58:12 +00003848static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003849 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003850 llvm::APSInt ArgNum(32);
3851 if (E->isTypeDependent() || E->isValueDependent() ||
3852 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3854 << Attr.getName() << AANT_ArgumentIntegerConstant
3855 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003856 return;
3857 }
3858
3859 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3860 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3861}
3862
Aaron Ballmanfff32482012-12-09 17:45:41 +00003863bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3864 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003865 if (attr.isInvalid())
3866 return true;
3867
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003868 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003869 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003870 attr.setInvalid();
3871 return true;
3872 }
3873
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003874 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3875 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003876 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003877 case AttributeList::AT_CDecl: CC = CC_C; break;
3878 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3879 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3880 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3881 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003882 case AttributeList::AT_MSABI:
3883 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3884 CC_X86_64Win64;
3885 break;
3886 case AttributeList::AT_SysVABI:
3887 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3888 CC_C;
3889 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003890 case AttributeList::AT_Pcs: {
Aaron Ballmanfbf6f5c2013-09-13 17:48:25 +00003891 StringRef StrRef;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003892 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003893 attr.setInvalid();
3894 return true;
3895 }
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003896 if (StrRef == "aapcs") {
3897 CC = CC_AAPCS;
3898 break;
3899 } else if (StrRef == "aapcs-vfp") {
3900 CC = CC_AAPCS_VFP;
3901 break;
3902 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003903
3904 attr.setInvalid();
3905 Diag(attr.getLoc(), diag::err_invalid_pcs);
3906 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003907 }
Derek Schuff263366f2012-10-16 22:30:41 +00003908 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003909 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003910 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003911 }
3912
Aaron Ballman82bfa192012-10-02 14:26:08 +00003913 const TargetInfo &TI = Context.getTargetInfo();
3914 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3915 if (A == TargetInfo::CCCR_Warning) {
3916 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003917
3918 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3919 if (FD)
3920 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3921 TargetInfo::CCMT_NonMember;
3922 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003923 }
3924
John McCall711c52b2011-01-05 12:14:39 +00003925 return false;
3926}
3927
Chandler Carruth1b03c872011-07-02 00:01:44 +00003928static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003929 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003930
3931 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003932 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003933 return;
3934
Chandler Carruth87c44602011-07-01 23:49:12 +00003935 if (!isa<ObjCMethodDecl>(D)) {
3936 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3937 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003938 return;
3939 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003940
Michael Han51d8c522013-01-24 16:46:58 +00003941 D->addAttr(::new (S.Context)
3942 RegparmAttr(Attr.getRange(), S.Context, numParams,
3943 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003944}
3945
3946/// Checks a regparm attribute, returning true if it is ill-formed and
3947/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003948bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3949 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003950 return true;
3951
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003952 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003953 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003954 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003955 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003956
Aaron Ballman624421f2013-08-31 01:11:41 +00003957 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003958 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003959 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003960 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003961 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3962 << Attr.getName() << AANT_ArgumentIntegerConstant
3963 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003964 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003965 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003966 }
3967
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003968 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003969 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003970 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003971 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003972 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003973 }
3974
John McCall711c52b2011-01-05 12:14:39 +00003975 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003976 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003977 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003978 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003979 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003980 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003981 }
3982
John McCall711c52b2011-01-05 12:14:39 +00003983 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003984}
3985
Chandler Carruth1b03c872011-07-02 00:01:44 +00003986static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003987 if (S.LangOpts.CUDA) {
3988 // check the attribute arguments.
3989 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003990 // FIXME: 0 is not okay.
3991 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003992 return;
3993 }
3994
Chandler Carruth87c44602011-07-01 23:49:12 +00003995 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003996 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003997 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003998 return;
3999 }
4000
Aaron Ballman624421f2013-08-31 01:11:41 +00004001 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004002 llvm::APSInt MaxThreads(32);
4003 if (MaxThreadsExpr->isTypeDependent() ||
4004 MaxThreadsExpr->isValueDependent() ||
4005 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004007 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004008 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004009 return;
4010 }
4011
4012 llvm::APSInt MinBlocks(32);
4013 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004014 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004015 if (MinBlocksExpr->isTypeDependent() ||
4016 MinBlocksExpr->isValueDependent() ||
4017 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004018 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004019 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004020 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004021 return;
4022 }
4023 }
4024
Michael Han51d8c522013-01-24 16:46:58 +00004025 D->addAttr(::new (S.Context)
4026 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4027 MaxThreads.getZExtValue(),
4028 MinBlocks.getZExtValue(),
4029 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004030 } else {
4031 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4032 }
4033}
4034
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004035static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4036 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004037 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004038 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004039 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004040 return;
4041 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004042
4043 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004044 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004045
Aaron Ballman624421f2013-08-31 01:11:41 +00004046 StringRef AttrName = Attr.getName()->getName();
4047 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004048
4049 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4050 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4051 << Attr.getName() << ExpectedFunctionOrMethod;
4052 return;
4053 }
4054
4055 uint64_t ArgumentIdx;
4056 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4057 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004058 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004059 return;
4060
4061 uint64_t TypeTagIdx;
4062 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4063 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004064 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004065 return;
4066
4067 bool IsPointer = (AttrName == "pointer_with_type_tag");
4068 if (IsPointer) {
4069 // Ensure that buffer has a pointer type.
4070 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4071 if (!BufferTy->isPointerType()) {
4072 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004073 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004074 }
4075 }
4076
Michael Han51d8c522013-01-24 16:46:58 +00004077 D->addAttr(::new (S.Context)
4078 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4079 ArgumentIdx, TypeTagIdx, IsPointer,
4080 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004081}
4082
4083static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4084 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004085 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004087 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004088 return;
4089 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004090
4091 if (!checkAttributeNumArgs(S, Attr, 1))
4092 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004093
Aaron Ballman624421f2013-08-31 01:11:41 +00004094 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004095 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4096
Michael Han51d8c522013-01-24 16:46:58 +00004097 D->addAttr(::new (S.Context)
4098 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4099 MatchingCType,
4100 Attr.getLayoutCompatible(),
4101 Attr.getMustBeNull(),
4102 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004103}
4104
Chris Lattner0744e5f2008-06-29 00:23:49 +00004105//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004106// Checker-specific attribute handlers.
4107//===----------------------------------------------------------------------===//
4108
John McCallc7ad3812011-01-25 03:31:58 +00004109static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004110 return type->isDependentType() ||
4111 type->isObjCObjectPointerType() ||
4112 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004113}
4114static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004115 return type->isDependentType() ||
4116 type->isPointerType() ||
4117 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004118}
4119
Chandler Carruth1b03c872011-07-02 00:01:44 +00004120static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004121 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004122 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004123 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004124 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004125 return;
4126 }
4127
4128 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004129 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004130 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4131 cf = false;
4132 } else {
4133 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4134 cf = true;
4135 }
4136
4137 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004138 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004139 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004140 return;
4141 }
4142
4143 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004144 param->addAttr(::new (S.Context)
4145 CFConsumedAttr(Attr.getRange(), S.Context,
4146 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004147 else
Michael Han51d8c522013-01-24 16:46:58 +00004148 param->addAttr(::new (S.Context)
4149 NSConsumedAttr(Attr.getRange(), S.Context,
4150 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004151}
4152
Chandler Carruth1b03c872011-07-02 00:01:44 +00004153static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4154 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004155 if (!isa<ObjCMethodDecl>(D)) {
4156 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004157 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004158 return;
4159 }
4160
Michael Han51d8c522013-01-24 16:46:58 +00004161 D->addAttr(::new (S.Context)
4162 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4163 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004164}
4165
Chandler Carruth1b03c872011-07-02 00:01:44 +00004166static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4167 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004168
John McCallc7ad3812011-01-25 03:31:58 +00004169 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004170
Chandler Carruth87c44602011-07-01 23:49:12 +00004171 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004172 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004173 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004174 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004175 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004176 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4177 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004178 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004179 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004180 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004181 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004182 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004183 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004184 return;
4185 }
Mike Stumpbf916502009-07-24 19:02:52 +00004186
John McCallc7ad3812011-01-25 03:31:58 +00004187 bool typeOK;
4188 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004189 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004190 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004191 case AttributeList::AT_NSReturnsAutoreleased:
4192 case AttributeList::AT_NSReturnsRetained:
4193 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004194 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4195 cf = false;
4196 break;
4197
Sean Hunt8e083e72012-06-19 23:57:03 +00004198 case AttributeList::AT_CFReturnsRetained:
4199 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004200 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4201 cf = true;
4202 break;
4203 }
4204
4205 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004206 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004207 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004208 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004209 }
Mike Stumpbf916502009-07-24 19:02:52 +00004210
Chandler Carruth87c44602011-07-01 23:49:12 +00004211 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004212 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004213 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004214 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004215 D->addAttr(::new (S.Context)
4216 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4217 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004218 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004219 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004220 D->addAttr(::new (S.Context)
4221 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4222 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004223 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004224 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004225 D->addAttr(::new (S.Context)
4226 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4227 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004228 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004229 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004230 D->addAttr(::new (S.Context)
4231 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4232 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004233 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004234 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004235 D->addAttr(::new (S.Context)
4236 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4237 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004238 return;
4239 };
4240}
4241
John McCalldc7c5ad2011-07-22 08:53:00 +00004242static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4243 const AttributeList &attr) {
Fariborz Jahanianaf1d28f2013-09-19 17:52:50 +00004244 const int EP_ObjCMethod = 1;
4245 const int EP_ObjCProperty = 2;
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004246
John McCalldc7c5ad2011-07-22 08:53:00 +00004247 SourceLocation loc = attr.getLoc();
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004248 QualType resultType;
4249
John McCalldc7c5ad2011-07-22 08:53:00 +00004250 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4251
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004252 if (!method) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004253 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4254 if (!property) {
4255 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4256 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4257 return;
4258 }
4259 resultType = property->getType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004260 }
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004261 else
4262 // Check that the method returns a normal pointer.
4263 resultType = method->getResultType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004264
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004265 if (!resultType->isReferenceType() &&
4266 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004267 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalldc7c5ad2011-07-22 08:53:00 +00004268 << SourceRange(loc)
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004269 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4270 << /*non-retainable pointer*/ 2;
John McCalldc7c5ad2011-07-22 08:53:00 +00004271
4272 // Drop the attribute.
4273 return;
4274 }
4275
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004276 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004277 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4278 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004279}
4280
Fariborz Jahanian84101132012-09-07 23:46:23 +00004281static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4282 const AttributeList &attr) {
4283 SourceLocation loc = attr.getLoc();
4284 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4285
4286 if (!method) {
4287 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4288 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4289 return;
4290 }
4291 DeclContext *DC = method->getDeclContext();
4292 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4293 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4294 << attr.getName() << 0;
4295 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4296 return;
4297 }
4298 if (method->getMethodFamily() == OMF_dealloc) {
4299 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4300 << attr.getName() << 1;
4301 return;
4302 }
4303
Michael Han51d8c522013-01-24 16:46:58 +00004304 method->addAttr(::new (S.Context)
4305 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4306 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004307}
4308
John McCall8dfac0b2011-09-30 05:12:12 +00004309/// Handle cf_audited_transfer and cf_unknown_transfer.
4310static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4311 if (!isa<FunctionDecl>(D)) {
4312 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004313 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004314 return;
4315 }
4316
Sean Hunt8e083e72012-06-19 23:57:03 +00004317 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004318
4319 // Check whether there's a conflicting attribute already present.
4320 Attr *Existing;
4321 if (IsAudited) {
4322 Existing = D->getAttr<CFUnknownTransferAttr>();
4323 } else {
4324 Existing = D->getAttr<CFAuditedTransferAttr>();
4325 }
4326 if (Existing) {
4327 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4328 << A.getName()
4329 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4330 << A.getRange() << Existing->getRange();
4331 return;
4332 }
4333
4334 // All clear; add the attribute.
4335 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004336 D->addAttr(::new (S.Context)
4337 CFAuditedTransferAttr(A.getRange(), S.Context,
4338 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004339 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004340 D->addAttr(::new (S.Context)
4341 CFUnknownTransferAttr(A.getRange(), S.Context,
4342 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004343 }
4344}
4345
John McCallfe98da02011-09-29 07:17:38 +00004346static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4347 const AttributeList &Attr) {
4348 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4349 if (!RD || RD->isUnion()) {
4350 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004351 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004352 }
4353
Aaron Ballman624421f2013-08-31 01:11:41 +00004354 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004355
4356 // In Objective-C, verify that the type names an Objective-C type.
4357 // We don't want to check this outside of ObjC because people sometimes
4358 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004359 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004360 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004361 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004362 Sema::LookupOrdinaryName);
4363 if (S.LookupName(R, Sc)) {
4364 NamedDecl *Target = R.getFoundDecl();
4365 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4366 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4367 S.Diag(Target->getLocStart(), diag::note_declared_at);
4368 }
4369 }
4370 }
4371
Michael Han51d8c522013-01-24 16:46:58 +00004372 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004373 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004374 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004375}
4376
Chandler Carruth1b03c872011-07-02 00:01:44 +00004377static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4378 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004379 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004380
Chandler Carruth87c44602011-07-01 23:49:12 +00004381 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004382 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004383}
4384
Chandler Carruth1b03c872011-07-02 00:01:44 +00004385static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4386 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004387 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004388 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004389 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004390 return;
4391 }
4392
Chandler Carruth87c44602011-07-01 23:49:12 +00004393 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004394 QualType type = vd->getType();
4395
4396 if (!type->isDependentType() &&
4397 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004398 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004399 << type;
4400 return;
4401 }
4402
4403 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4404
4405 // If we have no lifetime yet, check the lifetime we're presumably
4406 // going to infer.
4407 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4408 lifetime = type->getObjCARCImplicitLifetime();
4409
4410 switch (lifetime) {
4411 case Qualifiers::OCL_None:
4412 assert(type->isDependentType() &&
4413 "didn't infer lifetime for non-dependent type?");
4414 break;
4415
4416 case Qualifiers::OCL_Weak: // meaningful
4417 case Qualifiers::OCL_Strong: // meaningful
4418 break;
4419
4420 case Qualifiers::OCL_ExplicitNone:
4421 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004422 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004423 << (lifetime == Qualifiers::OCL_Autoreleasing);
4424 break;
4425 }
4426
Chandler Carruth87c44602011-07-01 23:49:12 +00004427 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004428 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4429 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004430}
4431
Francois Pichet11542142010-12-19 06:50:37 +00004432//===----------------------------------------------------------------------===//
4433// Microsoft specific attribute handlers.
4434//===----------------------------------------------------------------------===//
4435
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004436// Check if MS extensions or some other language extensions are enabled. If
4437// not, issue a diagnostic that the given attribute is unused.
4438static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4439 bool OtherExtension = false) {
4440 if (S.LangOpts.MicrosoftExt || OtherExtension)
4441 return true;
4442 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4443 return false;
4444}
4445
Chandler Carruth1b03c872011-07-02 00:01:44 +00004446static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004447 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4448 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004449
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004450 StringRef StrRef;
4451 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00004452 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004453 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004454
David Majnemerbb0ed292013-08-09 08:56:20 +00004455 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4456 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004457 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4458 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004459
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004460 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004461 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004462 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004463 return;
4464 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004465
David Majnemerbb0ed292013-08-09 08:56:20 +00004466 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004467 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004468 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004469 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004470 return;
4471 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004472 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004473 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004474 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004475 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004476 }
Francois Pichet11542142010-12-19 06:50:37 +00004477
David Majnemerbb0ed292013-08-09 08:56:20 +00004478 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4479 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004480}
4481
John McCallc052dbb2012-05-22 21:28:12 +00004482static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004483 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004484 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004485
4486 AttributeList::Kind Kind = Attr.getKind();
4487 if (Kind == AttributeList::AT_SingleInheritance)
4488 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004489 ::new (S.Context)
4490 SingleInheritanceAttr(Attr.getRange(), S.Context,
4491 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004492 else if (Kind == AttributeList::AT_MultipleInheritance)
4493 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004494 ::new (S.Context)
4495 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4496 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004497 else if (Kind == AttributeList::AT_VirtualInheritance)
4498 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004499 ::new (S.Context)
4500 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4501 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004502}
4503
4504static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004505 if (!checkMicrosoftExt(S, Attr))
4506 return;
4507
4508 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004509 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004510 D->addAttr(
4511 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4512 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004513}
4514
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004515static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004516 if (!checkMicrosoftExt(S, Attr))
4517 return;
4518 D->addAttr(::new (S.Context)
4519 ForceInlineAttr(Attr.getRange(), S.Context,
4520 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004521}
4522
Reid Klecknera7225342013-05-20 14:02:37 +00004523static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4524 if (!checkMicrosoftExt(S, Attr))
4525 return;
4526 // Check linkage after possibly merging declaratinos. See
4527 // checkAttributesAfterMerging().
4528 D->addAttr(::new (S.Context)
4529 SelectAnyAttr(Attr.getRange(), S.Context,
4530 Attr.getAttributeSpellingListIndex()));
4531}
4532
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004533/// Handles semantic checking for features that are common to all attributes,
4534/// such as checking whether a parameter was properly specified, or the correct
4535/// number of arguments were passed, etc.
4536static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4537 const AttributeList &Attr) {
4538 // Several attributes carry different semantics than the parsing requires, so
4539 // those are opted out of the common handling.
4540 //
4541 // We also bail on unknown and ignored attributes because those are handled
4542 // as part of the target-specific handling logic.
4543 if (Attr.hasCustomParsing() ||
4544 Attr.getKind() == AttributeList::UnknownAttribute ||
4545 Attr.getKind() == AttributeList::IgnoredAttribute)
4546 return false;
4547
4548 // If there are no optional arguments, then checking for the argument count
4549 // is trivial.
4550 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4551 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4552 return true;
4553 return false;
4554}
4555
Ted Kremenekb71368d2009-05-09 02:44:38 +00004556//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004557// Top Level Sema Entry Points
4558//===----------------------------------------------------------------------===//
4559
Richard Smith4a97b8e2013-08-29 00:47:48 +00004560/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4561/// the attribute applies to decls. If the attribute is a type attribute, just
4562/// silently ignore it if a GNU attribute.
4563static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4564 const AttributeList &Attr,
4565 bool IncludeCXX11Attributes) {
4566 if (Attr.isInvalid())
4567 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004568
Richard Smith4a97b8e2013-08-29 00:47:48 +00004569 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4570 // instead.
4571 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4572 return;
4573
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004574 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4575 return;
4576
Chris Lattner803d0802008-06-29 00:43:07 +00004577 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004578 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4579 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4580 case AttributeList::AT_IBOutletCollection:
4581 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004582 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004583 case AttributeList::AT_ObjCGC:
4584 case AttributeList::AT_VectorSize:
4585 case AttributeList::AT_NeonVectorType:
4586 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004587 case AttributeList::AT_Ptr32:
4588 case AttributeList::AT_Ptr64:
4589 case AttributeList::AT_SPtr:
4590 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004591 // Ignore these, these are type attributes, handled by
4592 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004593 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004594 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4595 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4596 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4597 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004598 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004599 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004600 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004601 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004602 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4603 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4604 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004605 handleDependencyAttr(S, scope, D, Attr);
4606 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004607 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4608 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4609 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004610 case AttributeList::AT_CXX11NoReturn:
4611 handleCXX11NoReturnAttr(S, D, Attr);
4612 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004613 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004614 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004615 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004616 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4617 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004618 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004619 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004620 case AttributeList::AT_MinSize:
4621 handleMinSizeAttr(S, D, Attr);
4622 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004623 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4624 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4625 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004626 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4627 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004628 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4629 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004630 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004631 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004632 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4633 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004634 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004635 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4636 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004637 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004638 case AttributeList::AT_ownership_returns:
4639 case AttributeList::AT_ownership_takes:
4640 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004641 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004642 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4643 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4644 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4645 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4646 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4647 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4648 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004649
Sean Hunt8e083e72012-06-19 23:57:03 +00004650 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004651 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004652 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004653 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004654
Sean Hunt8e083e72012-06-19 23:57:03 +00004655 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004656 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4657
Fariborz Jahanian84101132012-09-07 23:46:23 +00004658 case AttributeList::AT_ObjCRequiresSuper:
4659 handleObjCRequiresSuperAttr(S, D, Attr); break;
4660
Sean Hunt8e083e72012-06-19 23:57:03 +00004661 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004662 handleNSBridgedAttr(S, scope, D, Attr); break;
4663
Sean Hunt8e083e72012-06-19 23:57:03 +00004664 case AttributeList::AT_CFAuditedTransfer:
4665 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004666 handleCFTransferAttr(S, D, Attr); break;
4667
Ted Kremenekb71368d2009-05-09 02:44:38 +00004668 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004669 case AttributeList::AT_CFConsumed:
4670 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4671 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004672 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004673
Sean Hunt8e083e72012-06-19 23:57:03 +00004674 case AttributeList::AT_NSReturnsAutoreleased:
4675 case AttributeList::AT_NSReturnsNotRetained:
4676 case AttributeList::AT_CFReturnsNotRetained:
4677 case AttributeList::AT_NSReturnsRetained:
4678 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004679 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004680
Tanya Lattner0df579e2012-07-09 22:06:01 +00004681 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004682 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004683 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004684
Joey Gouly37453b92013-03-08 09:42:32 +00004685 case AttributeList::AT_VecTypeHint:
4686 handleVecTypeHint(S, D, Attr); break;
4687
Sean Hunt8e083e72012-06-19 23:57:03 +00004688 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004689 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004690
Sean Hunt8e083e72012-06-19 23:57:03 +00004691 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4692 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4693 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004694 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004695 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004696 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004697 handleArcWeakrefUnavailableAttr (S, D, Attr);
4698 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004699 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004700 handleObjCRootClassAttr(S, D, Attr);
4701 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004702 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004703 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004704 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004705 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4706 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004707 handleReturnsTwiceAttr(S, D, Attr);
4708 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004709 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004710 case AttributeList::AT_Visibility:
4711 handleVisibilityAttr(S, D, Attr, false);
4712 break;
4713 case AttributeList::AT_TypeVisibility:
4714 handleVisibilityAttr(S, D, Attr, true);
4715 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004716 case AttributeList::AT_WarnUnused:
4717 handleWarnUnusedAttr(S, D, Attr);
4718 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004719 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004720 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004721 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4722 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4723 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4724 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004725 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004726 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004727 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004728 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004729 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004730 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004731 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004732 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004733 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4734 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4735 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4736 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4737 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4738 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4739 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4740 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4741 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004742 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004743 // Just ignore
4744 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004745 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004746 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004747 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004748 case AttributeList::AT_StdCall:
4749 case AttributeList::AT_CDecl:
4750 case AttributeList::AT_FastCall:
4751 case AttributeList::AT_ThisCall:
4752 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004753 case AttributeList::AT_MSABI:
4754 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004755 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004756 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004757 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004758 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004759 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004761 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004762 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004763 case AttributeList::AT_OpenCLImageAccess:
4764 handleOpenCLImageAccessAttr(S, D, Attr);
4765 break;
John McCallc052dbb2012-05-22 21:28:12 +00004766
4767 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004768 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004769 handleMsStructAttr(S, D, Attr);
4770 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004771 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004772 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004773 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004774 case AttributeList::AT_SingleInheritance:
4775 case AttributeList::AT_MultipleInheritance:
4776 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004777 handleInheritanceAttr(S, D, Attr);
4778 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004779 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004780 handlePortabilityAttr(S, D, Attr);
4781 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004782 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004783 handleForceInlineAttr(S, D, Attr);
4784 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004785 case AttributeList::AT_SelectAny:
4786 handleSelectAnyAttr(S, D, Attr);
4787 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004788
4789 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004790 case AttributeList::AT_AssertExclusiveLock:
4791 handleAssertExclusiveLockAttr(S, D, Attr);
4792 break;
4793 case AttributeList::AT_AssertSharedLock:
4794 handleAssertSharedLockAttr(S, D, Attr);
4795 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004796 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004797 handleGuardedVarAttr(S, D, Attr);
4798 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004799 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004800 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004801 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004803 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004804 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004805 case AttributeList::AT_NoSanitizeAddress:
4806 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004807 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004808 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004809 handleNoThreadSafetyAnalysis(S, D, Attr);
4810 break;
4811 case AttributeList::AT_NoSanitizeThread:
4812 handleNoSanitizeThread(S, D, Attr);
4813 break;
4814 case AttributeList::AT_NoSanitizeMemory:
4815 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004816 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004818 handleLockableAttr(S, D, Attr);
4819 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004820 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004821 handleGuardedByAttr(S, D, Attr);
4822 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004823 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004824 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004825 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004827 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004828 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004829 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004830 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004831 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004832 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004833 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004834 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004835 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004836 handleLockReturnedAttr(S, D, Attr);
4837 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004838 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004839 handleLocksExcludedAttr(S, D, Attr);
4840 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004841 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004842 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004843 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004845 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004846 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004848 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004849 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004851 handleUnlockFunAttr(S, D, Attr);
4852 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004853 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004854 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004855 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004857 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004858 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004859
DeLesley Hutchins1bf63432013-10-11 22:30:48 +00004860 // Consumed analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004861 case AttributeList::AT_Consumable:
4862 handleConsumableAttr(S, D, Attr);
4863 break;
DeLesley Hutchins66540852013-10-04 21:28:06 +00004864 case AttributeList::AT_CallableWhen:
4865 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004866 break;
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00004867 case AttributeList::AT_ParamTypestate:
4868 handleParamTypestateAttr(S, D, Attr);
4869 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004870 case AttributeList::AT_ReturnTypestate:
4871 handleReturnTypestateAttr(S, D, Attr);
4872 break;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004873 case AttributeList::AT_SetTypestate:
4874 handleSetTypestateAttr(S, D, Attr);
4875 break;
4876 case AttributeList::AT_TestsTypestate:
4877 handleTestsTypestateAttr(S, D, Attr);
4878 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004879
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004880 // Type safety attributes.
4881 case AttributeList::AT_ArgumentWithTypeTag:
4882 handleArgumentWithTypeTagAttr(S, D, Attr);
4883 break;
4884 case AttributeList::AT_TypeTagForDatatype:
4885 handleTypeTagForDatatypeAttr(S, D, Attr);
4886 break;
4887
Chris Lattner803d0802008-06-29 00:43:07 +00004888 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004889 // Ask target about the attribute.
4890 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4891 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004892 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4893 diag::warn_unhandled_ms_attribute_ignored :
4894 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004895 break;
4896 }
4897}
4898
4899/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4900/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004901void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004902 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004903 bool IncludeCXX11Attributes) {
4904 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004905 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004906
4907 // GCC accepts
4908 // static int a9 __attribute__((weakref));
4909 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004910 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004911 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004912 cast<NamedDecl>(D)->getNameAsString();
4913 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004914 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004915 }
4916}
4917
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004918// Annotation attributes are the only attributes allowed after an access
4919// specifier.
4920bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4921 const AttributeList *AttrList) {
4922 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004923 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004924 handleAnnotateAttr(*this, ASDecl, *l);
4925 } else {
4926 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4927 return true;
4928 }
4929 }
4930
4931 return false;
4932}
4933
John McCalle82247a2011-10-01 05:17:03 +00004934/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4935/// contains any decl attributes that we should warn about.
4936static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4937 for ( ; A; A = A->getNext()) {
4938 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004939 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004940 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4941
4942 if (A->getKind() == AttributeList::UnknownAttribute) {
4943 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4944 << A->getName() << A->getRange();
4945 } else {
4946 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4947 << A->getName() << A->getRange();
4948 }
4949 }
4950}
4951
4952/// checkUnusedDeclAttributes - Given a declarator which is not being
4953/// used to build a declaration, complain about any decl attributes
4954/// which might be lying around on it.
4955void Sema::checkUnusedDeclAttributes(Declarator &D) {
4956 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4957 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4958 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4959 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4960}
4961
Ryan Flynne25ff832009-07-30 03:15:39 +00004962/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004963/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004964NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4965 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004966 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004967 NamedDecl *NewD = 0;
4968 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004969 FunctionDecl *NewFD;
4970 // FIXME: Missing call to CheckFunctionDeclaration().
4971 // FIXME: Mangling?
4972 // FIXME: Is the qualifier info correct?
4973 // FIXME: Is the DeclContext correct?
4974 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4975 Loc, Loc, DeclarationName(II),
4976 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004977 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004978 FD->hasPrototype(),
4979 false/*isConstexprSpecified*/);
4980 NewD = NewFD;
4981
4982 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004983 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004984
4985 // Fake up parameter variables; they are declared as if this were
4986 // a typedef.
4987 QualType FDTy = FD->getType();
4988 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4989 SmallVector<ParmVarDecl*, 16> Params;
4990 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4991 AE = FT->arg_type_end(); AI != AE; ++AI) {
4992 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4993 Param->setScopeInfo(0, Params.size());
4994 Params.push_back(Param);
4995 }
David Blaikie4278c652011-09-21 18:16:56 +00004996 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004997 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004998 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4999 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005000 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005001 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005002 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005003 if (VD->getQualifier()) {
5004 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005005 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005006 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005007 }
5008 return NewD;
5009}
5010
James Dennett1dfbd922012-06-14 21:40:34 +00005011/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005012/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005013void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005014 if (W.getUsed()) return; // only do this once
5015 W.setUsed(true);
5016 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5017 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005018 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005019 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5020 NDId->getName()));
5021 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005022 WeakTopLevelDecl.push_back(NewD);
5023 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5024 // to insert Decl at TU scope, sorry.
5025 DeclContext *SavedContext = CurContext;
5026 CurContext = Context.getTranslationUnitDecl();
5027 PushOnScopeChains(NewD, S);
5028 CurContext = SavedContext;
5029 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005030 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005031 }
5032}
5033
Rafael Espindola65611bf2013-03-02 21:41:48 +00005034void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5035 // It's valid to "forward-declare" #pragma weak, in which case we
5036 // have to do this.
5037 LoadExternalWeakUndeclaredIdentifiers();
5038 if (!WeakUndeclaredIdentifiers.empty()) {
5039 NamedDecl *ND = NULL;
5040 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5041 if (VD->isExternC())
5042 ND = VD;
5043 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5044 if (FD->isExternC())
5045 ND = FD;
5046 if (ND) {
5047 if (IdentifierInfo *Id = ND->getIdentifier()) {
5048 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5049 = WeakUndeclaredIdentifiers.find(Id);
5050 if (I != WeakUndeclaredIdentifiers.end()) {
5051 WeakInfo W = I->second;
5052 DeclApplyPragmaWeak(S, ND, W);
5053 WeakUndeclaredIdentifiers[Id] = W;
5054 }
5055 }
5056 }
5057 }
5058}
5059
Chris Lattner0744e5f2008-06-29 00:23:49 +00005060/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5061/// it, apply them to D. This is a bit tricky because PD can have attributes
5062/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005063void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005064 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005065 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005066 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005067
Chris Lattner0744e5f2008-06-29 00:23:49 +00005068 // Walk the declarator structure, applying decl attributes that were in a type
5069 // position to the decl itself. This handles cases like:
5070 // int *__attr__(x)** D;
5071 // when X is a decl attribute.
5072 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5073 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005074 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005075
Chris Lattner0744e5f2008-06-29 00:23:49 +00005076 // Finally, apply any attributes on the decl itself.
5077 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005078 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005079}
John McCall54abf7d2009-11-04 02:18:39 +00005080
John McCallf85e1932011-06-15 23:02:42 +00005081/// Is the given declaration allowed to use a forbidden type?
5082static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5083 // Private ivars are always okay. Unfortunately, people don't
5084 // always properly make their ivars private, even in system headers.
5085 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005086 // Function declarations in sys headers will be marked unavailable.
5087 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5088 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005089 return false;
5090
5091 // Require it to be declared in a system header.
5092 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5093}
5094
5095/// Handle a delayed forbidden-type diagnostic.
5096static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5097 Decl *decl) {
5098 if (decl && isForbiddenTypeAllowed(S, decl)) {
5099 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5100 "this system declaration uses an unsupported type"));
5101 return;
5102 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005103 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005104 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005105 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005106 // kind of forbidden type messages on unavailable functions.
5107 if (FD->hasAttr<UnavailableAttr>() &&
5108 diag.getForbiddenTypeDiagnostic() ==
5109 diag::err_arc_array_param_no_ownership) {
5110 diag.Triggered = true;
5111 return;
5112 }
5113 }
John McCallf85e1932011-06-15 23:02:42 +00005114
5115 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5116 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5117 diag.Triggered = true;
5118}
5119
John McCall92576642012-05-07 06:16:41 +00005120void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5121 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005122 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005123 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005124
John McCall92576642012-05-07 06:16:41 +00005125 // When delaying diagnostics to run in the context of a parsed
5126 // declaration, we only want to actually emit anything if parsing
5127 // succeeds.
5128 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005129
John McCall92576642012-05-07 06:16:41 +00005130 // We emit all the active diagnostics in this pool or any of its
5131 // parents. In general, we'll get one pool for the decl spec
5132 // and a child pool for each declarator; in a decl group like:
5133 // deprecated_typedef foo, *bar, baz();
5134 // only the declarator pops will be passed decls. This is correct;
5135 // we really do need to consider delayed diagnostics from the decl spec
5136 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005137 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005138 do {
John McCall13489672012-05-07 06:16:58 +00005139 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005140 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5141 // This const_cast is a bit lame. Really, Triggered should be mutable.
5142 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005143 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005144 continue;
5145
John McCalleee1d542011-02-14 07:13:47 +00005146 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005147 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005148 // Don't bother giving deprecation diagnostics if the decl is invalid.
5149 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005150 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005151 break;
5152
5153 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005154 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005155 break;
John McCallf85e1932011-06-15 23:02:42 +00005156
5157 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005158 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005159 break;
John McCall2f514482010-01-27 03:50:35 +00005160 }
5161 }
John McCall92576642012-05-07 06:16:41 +00005162 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005163}
5164
John McCall13489672012-05-07 06:16:58 +00005165/// Given a set of delayed diagnostics, re-emit them as if they had
5166/// been delayed in the current context instead of in the given pool.
5167/// Essentially, this just moves them to the current pool.
5168void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5169 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5170 assert(curPool && "re-emitting in undelayed context not supported");
5171 curPool->steal(pool);
5172}
5173
John McCall54abf7d2009-11-04 02:18:39 +00005174static bool isDeclDeprecated(Decl *D) {
5175 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005176 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005177 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005178 // A category implicitly has the availability of the interface.
5179 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5180 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005181 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5182 return false;
5183}
5184
Eli Friedmanc3b23082012-08-08 21:52:41 +00005185static void
5186DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5187 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005188 const ObjCInterfaceDecl *UnknownObjCClass,
5189 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005190 DeclarationName Name = D->getDeclName();
5191 if (!Message.empty()) {
5192 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5193 S.Diag(D->getLocation(),
5194 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5195 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005196 if (ObjCPropery)
5197 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5198 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005199 } else if (!UnknownObjCClass) {
5200 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5201 S.Diag(D->getLocation(),
5202 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5203 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005204 if (ObjCPropery)
5205 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5206 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005207 } else {
5208 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5209 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5210 }
5211}
5212
John McCall9c3087b2010-08-26 02:13:20 +00005213void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005214 Decl *Ctx) {
5215 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005216 return;
5217
John McCall2f514482010-01-27 03:50:35 +00005218 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005219 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5220 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005221 DD.getUnknownObjCClass(),
5222 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005223}
5224
Chris Lattner5f9e2722011-07-23 10:55:15 +00005225void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005226 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005227 const ObjCInterfaceDecl *UnknownObjCClass,
5228 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005229 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005230 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005231 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5232 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005233 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005234 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005235 return;
5236 }
5237
5238 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005239 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005240 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005241 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005242}