blob: 7987a18f6105ab1a0377ac57a87bcf09675eb908 [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
Fariborz Jahanian7f822972013-11-14 00:43:05 +0000210static inline bool isTollFreeBridgeCFRefType(TypedefNameDecl *TD, ASTContext &Ctx) {
Fariborz Jahanian933a8942013-11-13 23:59:17 +0000211 StringRef TDName = TD->getIdentifier()->getName();
Fariborz Jahanian7f822972013-11-14 00:43:05 +0000212 return (TDName.startswith("CF") && TDName.endswith("Ref"));
Fariborz Jahanian933a8942013-11-13 23:59:17 +0000213}
214
Richard Smithddc2a532013-10-31 21:23:20 +0000215static unsigned getNumAttributeArgs(const AttributeList &Attr) {
216 // FIXME: Include the type in the argument list.
217 return Attr.getNumArgs() + Attr.hasParsedType();
218}
219
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000220/// \brief Check if the attribute has exactly as many args as Num. May
221/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000222static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithddc2a532013-10-31 21:23:20 +0000223 unsigned Num) {
224 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
226 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000227 return false;
228 }
229
230 return true;
231}
232
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000233
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000234/// \brief Check if the attribute has at least as many args as Num. May
235/// output an error.
236static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithddc2a532013-10-31 21:23:20 +0000237 unsigned Num) {
238 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
240 return false;
241 }
242
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000243 return true;
244}
245
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000246/// \brief Check if IdxExpr is a valid argument index for a function or
247/// instance method D. May output an error.
248///
249/// \returns true if IdxExpr is a valid index.
250static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
251 StringRef AttrName,
252 SourceLocation AttrLoc,
253 unsigned AttrArgNum,
254 const Expr *IdxExpr,
255 uint64_t &Idx)
256{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000257 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000258
259 // In C++ the implicit 'this' function parameter also counts.
260 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000261 bool HP = hasFunctionProto(D);
262 bool HasImplicitThisParam = isInstanceMethod(D);
263 bool IV = HP && isFunctionOrMethodVariadic(D);
264 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
265 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000266
267 llvm::APSInt IdxInt;
268 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
269 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000270 std::string Name = std::string("'") + AttrName.str() + std::string("'");
271 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000272 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000273 return false;
274 }
275
276 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000277 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000278 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
279 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
280 return false;
281 }
282 Idx--; // Convert to zero-based.
283 if (HasImplicitThisParam) {
284 if (Idx == 0) {
285 S.Diag(AttrLoc,
286 diag::err_attribute_invalid_implicit_this_argument)
287 << AttrName << IdxExpr->getSourceRange();
288 return false;
289 }
290 --Idx;
291 }
292
293 return true;
294}
295
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000296/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
297/// If not emit an error and return false. If the argument is an identifier it
298/// will emit an error with a fixit hint and treat it as if it was a string
299/// literal.
Tim Northoverf8aebef2013-10-01 14:34:18 +0000300bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
301 unsigned ArgNum, StringRef &Str,
Tim Northover64eac852013-10-01 14:34:25 +0000302 SourceLocation *ArgLocation) {
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000303 // Look for identifiers. If we have one emit a hint to fix it to a literal.
304 if (Attr.isArgIdent(ArgNum)) {
305 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northoverf8aebef2013-10-01 14:34:18 +0000306 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000307 << Attr.getName() << AANT_ArgumentString
308 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northoverf8aebef2013-10-01 14:34:18 +0000309 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000310 Str = Loc->Ident->getName();
311 if (ArgLocation)
312 *ArgLocation = Loc->Loc;
313 return true;
314 }
315
316 // Now check for an actual string literal.
317 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
318 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
319 if (ArgLocation)
320 *ArgLocation = ArgExpr->getLocStart();
321
322 if (!Literal || !Literal->isAscii()) {
Tim Northoverf8aebef2013-10-01 14:34:18 +0000323 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000324 << Attr.getName() << AANT_ArgumentString;
325 return false;
326 }
327
328 Str = Literal->getString();
329 return true;
330}
331
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000332///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000333/// \brief Check if passed in Decl is a field or potentially shared global var
334/// \return true if the Decl is a field or potentially shared global variable
335///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000336static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000337 if (isa<FieldDecl>(D))
338 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000339 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000340 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000341
342 return false;
343}
344
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000345/// \brief Check if the passed-in expression is of type int or bool.
346static bool isIntOrBool(Expr *Exp) {
347 QualType QT = Exp->getType();
348 return QT->isBooleanType() || QT->isIntegerType();
349}
350
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000351
352// Check to see if the type is a smart pointer of some kind. We assume
353// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000354static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
355 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
356 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000357 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000358 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000359
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000360 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
361 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000362 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000363 return false;
364
365 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000366}
367
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000368/// \brief Check if passed in Decl is a pointer type.
369/// Note that this function may produce an error message.
370/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000371static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
372 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000373 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000374 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000375 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000376 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000377
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000378 if (const RecordType *RT = QT->getAs<RecordType>()) {
379 // If it's an incomplete type, it could be a smart pointer; skip it.
380 // (We don't want to force template instantiation if we can avoid it,
381 // since that would alter the order in which templates are instantiated.)
382 if (RT->isIncompleteType())
383 return true;
384
385 if (threadSafetyCheckIsSmartPointer(S, RT))
386 return true;
387 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000388
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000389 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000390 << Attr.getName()->getName() << QT;
391 } else {
392 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
393 << Attr.getName();
394 }
395 return false;
396}
397
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000398/// \brief Checks that the passed in QualType either is of RecordType or points
399/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000400static const RecordType *getRecordType(QualType QT) {
401 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000402 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000403
404 // Now check if we point to record type.
405 if (const PointerType *PT = QT->getAs<PointerType>())
406 return PT->getPointeeType()->getAs<RecordType>();
407
408 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000409}
410
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000411
Jordy Rosefad5de92012-05-08 03:27:22 +0000412static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
413 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000414 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
415 if (RT->getDecl()->getAttr<LockableAttr>())
416 return true;
417 return false;
418}
419
420
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000421/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000422/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000423static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
424 QualType Ty) {
425 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000426
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000427 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000428 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000429 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000430 << Attr.getName() << Ty.getAsString();
431 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000432 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000433
Michael Hanf1aae3b2012-08-03 17:40:43 +0000434 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000435 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000436 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000437
438 // Allow smart pointers to be used as lockable objects.
439 // FIXME -- Check the type that the smart pointer points to.
440 if (threadSafetyCheckIsSmartPointer(S, RT))
441 return;
442
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000443 // Check if the type is lockable.
444 RecordDecl *RD = RT->getDecl();
445 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000446 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000447
448 // Else check if any base classes are lockable.
449 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
450 CXXBasePaths BPaths(false, false);
451 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
452 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000453 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000454
455 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
456 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000457}
458
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000460/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000461/// \param Sidx The attribute argument index to start checking with.
462/// \param ParamIdxOk Whether an argument can be indexing into a function
463/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000464static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000465 const AttributeList &Attr,
466 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000467 int Sidx = 0,
468 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000469 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000470 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000471
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000472 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000473 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000474 Args.push_back(ArgExp);
475 continue;
476 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000477
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000478 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000479 if (StrLit->getLength() == 0 ||
Benjamin Kramerf37e4f22013-09-13 16:30:12 +0000480 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000481 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000482 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000483 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000484 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000485 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000486
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000487 // We allow constant strings to be used as a placeholder for expressions
488 // that are not valid C++ syntax, but warn that they are ignored.
489 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
490 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000491 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000492 continue;
493 }
494
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000495 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000496
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000497 // A pointer to member expression of the form &MyClass::mu is treated
498 // specially -- we need to look at the type of the member.
499 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
500 if (UOp->getOpcode() == UO_AddrOf)
501 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
502 if (DRE->getDecl()->isCXXInstanceMember())
503 ArgTy = DRE->getDecl()->getType();
504
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000505 // First see if we can just cast to record type, or point to record type.
506 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000507
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000508 // Now check if we index into a record type function param.
509 if(!RT && ParamIdxOk) {
510 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000511 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
512 if(FD && IL) {
513 unsigned int NumParams = FD->getNumParams();
514 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000515 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
516 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
517 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000518 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
519 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000520 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000521 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000522 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000523 }
524 }
525
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000526 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000527
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000528 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000529 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000530}
531
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000532//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000533// Attribute Implementations
534//===----------------------------------------------------------------------===//
535
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000536// FIXME: All this manual attribute parsing code is gross. At the
537// least add some helper functions to check most argument patterns (#
538// and types of args).
539
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000540enum ThreadAttributeDeclKind {
541 ThreadExpectedFieldOrGlobalVar,
542 ThreadExpectedFunctionOrMethod,
543 ThreadExpectedClassOrStruct
544};
545
Michael Hanf1aae3b2012-08-03 17:40:43 +0000546static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000547 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000548 // D must be either a member field or global (potentially shared) variable.
549 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000550 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
551 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000552 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000553 }
554
Michael Handc691572012-07-23 18:48:41 +0000555 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000556}
557
Michael Handc691572012-07-23 18:48:41 +0000558static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
559 if (!checkGuardedVarAttrCommon(S, D, Attr))
560 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000561
Michael Han51d8c522013-01-24 16:46:58 +0000562 D->addAttr(::new (S.Context)
563 GuardedVarAttr(Attr.getRange(), S.Context,
564 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000565}
566
Michael Hanf1aae3b2012-08-03 17:40:43 +0000567static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000568 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000569 if (!checkGuardedVarAttrCommon(S, D, Attr))
570 return;
571
572 if (!threadSafetyCheckIsPointer(S, D, Attr))
573 return;
574
Michael Han51d8c522013-01-24 16:46:58 +0000575 D->addAttr(::new (S.Context)
576 PtGuardedVarAttr(Attr.getRange(), S.Context,
577 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000578}
579
Michael Hanf1aae3b2012-08-03 17:40:43 +0000580static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
581 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000582 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000583 // D must be either a member field or global (potentially shared) variable.
584 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000585 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
586 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000587 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000588 }
589
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000590 SmallVector<Expr*, 1> Args;
591 // check that all arguments are lockable objects
592 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
593 unsigned Size = Args.size();
594 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000595 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000596
Michael Handc691572012-07-23 18:48:41 +0000597 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000598
Michael Handc691572012-07-23 18:48:41 +0000599 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000600}
601
Michael Handc691572012-07-23 18:48:41 +0000602static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
603 Expr *Arg = 0;
604 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
605 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000606
Michael Handc691572012-07-23 18:48:41 +0000607 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
608}
609
Michael Hanf1aae3b2012-08-03 17:40:43 +0000610static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000611 const AttributeList &Attr) {
612 Expr *Arg = 0;
613 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
614 return;
615
616 if (!threadSafetyCheckIsPointer(S, D, Attr))
617 return;
618
619 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
620 S.Context, Arg));
621}
622
Michael Hanf1aae3b2012-08-03 17:40:43 +0000623static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000624 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000625 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000626 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000627 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
628 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000629 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000630 }
631
Michael Handc691572012-07-23 18:48:41 +0000632 return true;
633}
634
635static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
636 if (!checkLockableAttrCommon(S, D, Attr))
637 return;
638
639 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
640}
641
Michael Hanf1aae3b2012-08-03 17:40:43 +0000642static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000643 const AttributeList &Attr) {
644 if (!checkLockableAttrCommon(S, D, Attr))
645 return;
646
Michael Han51d8c522013-01-24 16:46:58 +0000647 D->addAttr(::new (S.Context)
648 ScopedLockableAttr(Attr.getRange(), S.Context,
649 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000650}
651
Kostya Serebryany85aee962013-02-26 06:58:27 +0000652static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
653 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000654 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000655 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
656 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000657 return;
658 }
659
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000660 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000661 S.Context));
662}
663
Kostya Serebryany85aee962013-02-26 06:58:27 +0000664static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000665 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000666 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000667 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000668 << Attr.getName() << ExpectedFunctionOrMethod;
669 return;
670 }
671
Michael Han51d8c522013-01-24 16:46:58 +0000672 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000673 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
674 Attr.getAttributeSpellingListIndex()));
675}
676
677static void handleNoSanitizeMemory(Sema &S, Decl *D,
678 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000679 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
680 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
681 << Attr.getName() << ExpectedFunctionOrMethod;
682 return;
683 }
684
685 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
686 S.Context));
687}
688
689static void handleNoSanitizeThread(Sema &S, Decl *D,
690 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000691 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
693 << Attr.getName() << ExpectedFunctionOrMethod;
694 return;
695 }
696
697 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
698 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000699}
700
Michael Hanf1aae3b2012-08-03 17:40:43 +0000701static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
702 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000703 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000704 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000705 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000706
707 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000708 ValueDecl *VD = dyn_cast<ValueDecl>(D);
709 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000710 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
711 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000712 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000713 }
714
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000715 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000716 QualType QT = VD->getType();
717 if (!QT->isDependentType()) {
718 const RecordType *RT = getRecordType(QT);
719 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000720 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000721 << Attr.getName();
722 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000723 }
724 }
725
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000726 // Check that all arguments are lockable objects.
727 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000728 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000729 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000730
Michael Handc691572012-07-23 18:48:41 +0000731 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000732}
733
Michael Hanf1aae3b2012-08-03 17:40:43 +0000734static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000735 const AttributeList &Attr) {
736 SmallVector<Expr*, 1> Args;
737 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
738 return;
739
740 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000741 D->addAttr(::new (S.Context)
742 AcquiredAfterAttr(Attr.getRange(), S.Context,
743 StartArg, Args.size(),
744 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000745}
746
Michael Hanf1aae3b2012-08-03 17:40:43 +0000747static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000748 const AttributeList &Attr) {
749 SmallVector<Expr*, 1> Args;
750 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
751 return;
752
753 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000754 D->addAttr(::new (S.Context)
755 AcquiredBeforeAttr(Attr.getRange(), S.Context,
756 StartArg, Args.size(),
757 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000758}
759
Michael Hanf1aae3b2012-08-03 17:40:43 +0000760static bool checkLockFunAttrCommon(Sema &S, Decl *D,
761 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000762 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000763 // zero or more arguments ok
764
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000765 // check that the attribute is applied to a function
766 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000767 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
768 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000769 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000770 }
771
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000772 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000773 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000774
Michael Handc691572012-07-23 18:48:41 +0000775 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000776}
777
Michael Hanf1aae3b2012-08-03 17:40:43 +0000778static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000779 const AttributeList &Attr) {
780 SmallVector<Expr*, 1> Args;
781 if (!checkLockFunAttrCommon(S, D, Attr, Args))
782 return;
783
784 unsigned Size = Args.size();
785 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000786 D->addAttr(::new (S.Context)
787 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
788 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000789}
790
Michael Hanf1aae3b2012-08-03 17:40:43 +0000791static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000792 const AttributeList &Attr) {
793 SmallVector<Expr*, 1> Args;
794 if (!checkLockFunAttrCommon(S, D, Attr, Args))
795 return;
796
797 unsigned Size = Args.size();
798 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000799 D->addAttr(::new (S.Context)
800 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
801 StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000803}
804
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000805static void handleAssertSharedLockAttr(Sema &S, Decl *D,
806 const AttributeList &Attr) {
807 SmallVector<Expr*, 1> Args;
808 if (!checkLockFunAttrCommon(S, D, Attr, Args))
809 return;
810
811 unsigned Size = Args.size();
812 Expr **StartArg = Size == 0 ? 0 : &Args[0];
813 D->addAttr(::new (S.Context)
814 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
815 Attr.getAttributeSpellingListIndex()));
816}
817
818static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
819 const AttributeList &Attr) {
820 SmallVector<Expr*, 1> Args;
821 if (!checkLockFunAttrCommon(S, D, Attr, Args))
822 return;
823
824 unsigned Size = Args.size();
825 Expr **StartArg = Size == 0 ? 0 : &Args[0];
826 D->addAttr(::new (S.Context)
827 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
828 StartArg, Size,
829 Attr.getAttributeSpellingListIndex()));
830}
831
832
Michael Hanf1aae3b2012-08-03 17:40:43 +0000833static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
834 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000835 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000836 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000837 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000838
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000839 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000840 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
841 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000842 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000843 }
844
Aaron Ballman624421f2013-08-31 01:11:41 +0000845 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000846 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000847 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000848 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000849 }
850
851 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000852 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000853
Michael Handc691572012-07-23 18:48:41 +0000854 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000855}
856
Michael Hanf1aae3b2012-08-03 17:40:43 +0000857static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000858 const AttributeList &Attr) {
859 SmallVector<Expr*, 2> Args;
860 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
861 return;
862
Michael Han51d8c522013-01-24 16:46:58 +0000863 D->addAttr(::new (S.Context)
864 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000865 Attr.getArgAsExpr(0),
866 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000867 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000868}
869
Michael Hanf1aae3b2012-08-03 17:40:43 +0000870static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000871 const AttributeList &Attr) {
872 SmallVector<Expr*, 2> Args;
873 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
874 return;
875
Michael Han51d8c522013-01-24 16:46:58 +0000876 D->addAttr(::new (S.Context)
877 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000878 Attr.getArgAsExpr(0),
879 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000880 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000881}
882
Michael Hanf1aae3b2012-08-03 17:40:43 +0000883static bool checkLocksRequiredCommon(Sema &S, Decl *D,
884 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000885 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000887 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000888
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000889 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000890 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
891 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000892 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000893 }
894
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000895 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000896 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000897 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000898 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000899
Michael Handc691572012-07-23 18:48:41 +0000900 return true;
901}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000902
Michael Hanf1aae3b2012-08-03 17:40:43 +0000903static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000904 const AttributeList &Attr) {
905 SmallVector<Expr*, 1> Args;
906 if (!checkLocksRequiredCommon(S, D, Attr, Args))
907 return;
908
909 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000910 D->addAttr(::new (S.Context)
911 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
912 StartArg, Args.size(),
913 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000914}
915
Michael Hanf1aae3b2012-08-03 17:40:43 +0000916static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000917 const AttributeList &Attr) {
918 SmallVector<Expr*, 1> Args;
919 if (!checkLocksRequiredCommon(S, D, Attr, Args))
920 return;
921
922 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000923 D->addAttr(::new (S.Context)
924 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
925 StartArg, Args.size(),
926 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000927}
928
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000929static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000930 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000931 // zero or more arguments ok
932
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000933 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000934 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
935 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000936 return;
937 }
938
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000939 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000940 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000941 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000942 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000943 Expr **StartArg = Size == 0 ? 0 : &Args[0];
944
Michael Han51d8c522013-01-24 16:46:58 +0000945 D->addAttr(::new (S.Context)
946 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
947 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000948}
949
950static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000951 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000952 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000953 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
954 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000955 return;
956 }
957
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000958 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000959 SmallVector<Expr*, 1> Args;
960 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
961 unsigned Size = Args.size();
962 if (Size == 0)
963 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000964
Michael Han51d8c522013-01-24 16:46:58 +0000965 D->addAttr(::new (S.Context)
966 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
967 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000968}
969
970static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000971 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000972 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000973 return;
974
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000975 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000976 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
977 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000978 return;
979 }
980
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000981 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000982 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000983 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000984 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000985 if (Size == 0)
986 return;
987 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000988
Michael Han51d8c522013-01-24 16:46:58 +0000989 D->addAttr(::new (S.Context)
990 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
991 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000992}
993
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000994static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000995 ConsumableAttr::ConsumedState DefaultState;
996
997 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000998 IdentifierLoc *IL = Attr.getArgAsIdent(0);
999 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1000 DefaultState)) {
1001 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1002 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +00001003 return;
1004 }
David Blaikiea33ab602013-09-06 01:28:43 +00001005 } else {
1006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1007 << Attr.getName() << AANT_ArgumentIdentifier;
1008 return;
1009 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001010
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001011 if (!isa<CXXRecordDecl>(D)) {
1012 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1013 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001014 return;
1015 }
1016
1017 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +00001018 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001019 Attr.getAttributeSpellingListIndex()));
1020}
1021
1022static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1023 const AttributeList &Attr) {
1024 ASTContext &CurrContext = S.getASTContext();
1025 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1026
1027 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1028 if (!RD->hasAttr<ConsumableAttr>()) {
1029 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1030 RD->getNameAsString();
1031
1032 return false;
1033 }
1034 }
1035
1036 return true;
1037}
1038
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001039
DeLesley Hutchins66540852013-10-04 21:28:06 +00001040static void handleCallableWhenAttr(Sema &S, Decl *D,
1041 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001042 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1043 return;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001044
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001045 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1047 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001048 return;
1049 }
1050
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001051 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1052 return;
1053
DeLesley Hutchins66540852013-10-04 21:28:06 +00001054 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1055 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1056 CallableWhenAttr::ConsumedState CallableState;
1057
Aaron Ballman9025a182013-10-05 22:45:34 +00001058 StringRef StateString;
1059 SourceLocation Loc;
1060 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1061 return;
1062
1063 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00001064 CallableState)) {
Aaron Ballman9025a182013-10-05 22:45:34 +00001065 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1066 << Attr.getName() << StateString;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001067 return;
1068 }
Aaron Ballman9025a182013-10-05 22:45:34 +00001069
1070 States.push_back(CallableState);
DeLesley Hutchins66540852013-10-04 21:28:06 +00001071 }
1072
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001073 D->addAttr(::new (S.Context)
DeLesley Hutchins66540852013-10-04 21:28:06 +00001074 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1075 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001076}
1077
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001078
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00001079static void handleParamTypestateAttr(Sema &S, Decl *D,
1080 const AttributeList &Attr) {
1081 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1082
1083 if (!isa<ParmVarDecl>(D)) {
1084 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1085 Attr.getName() << ExpectedParameter;
1086 return;
1087 }
1088
1089 ParamTypestateAttr::ConsumedState ParamState;
1090
1091 if (Attr.isArgIdent(0)) {
1092 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1093 StringRef StateString = Ident->Ident->getName();
1094
1095 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1096 ParamState)) {
1097 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1098 << Attr.getName() << StateString;
1099 return;
1100 }
1101 } else {
1102 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1103 Attr.getName() << AANT_ArgumentIdentifier;
1104 return;
1105 }
1106
1107 // FIXME: This check is currently being done in the analysis. It can be
1108 // enabled here only after the parser propagates attributes at
1109 // template specialization definition, not declaration.
1110 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1111 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1112 //
1113 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1114 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1115 // ReturnType.getAsString();
1116 // return;
1117 //}
1118
1119 D->addAttr(::new (S.Context)
1120 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1121 Attr.getAttributeSpellingListIndex()));
1122}
1123
1124
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001125static void handleReturnTypestateAttr(Sema &S, Decl *D,
1126 const AttributeList &Attr) {
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001127 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1128
1129 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1130 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1131 Attr.getName() << ExpectedFunctionMethodOrParameter;
1132 return;
1133 }
1134
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001135 ReturnTypestateAttr::ConsumedState ReturnState;
1136
1137 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001138 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1139 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1140 ReturnState)) {
1141 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1142 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001143 return;
1144 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001145 } else {
1146 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1147 Attr.getName() << AANT_ArgumentIdentifier;
1148 return;
1149 }
1150
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001151 // FIXME: This check is currently being done in the analysis. It can be
1152 // enabled here only after the parser propagates attributes at
1153 // template specialization definition, not declaration.
1154 //QualType ReturnType;
1155 //
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001156 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1157 // ReturnType = Param->getType();
1158 //
1159 //} else if (const CXXConstructorDecl *Constructor =
1160 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001161 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1162 //
1163 //} else {
1164 //
1165 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1166 //}
1167 //
1168 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1169 //
1170 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1171 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1172 // ReturnType.getAsString();
1173 // return;
1174 //}
1175
1176 D->addAttr(::new (S.Context)
1177 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1178 Attr.getAttributeSpellingListIndex()));
1179}
1180
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001181
1182static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001183 if (!checkAttributeNumArgs(S, Attr, 1))
1184 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001185
1186 if (!isa<CXXMethodDecl>(D)) {
1187 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1188 Attr.getName() << ExpectedMethod;
1189 return;
1190 }
1191
1192 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1193 return;
1194
1195 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001196 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001197 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1198 StringRef Param = Ident->Ident->getName();
1199 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1200 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1201 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001202 return;
1203 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001204 } else {
1205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1206 Attr.getName() << AANT_ArgumentIdentifier;
1207 return;
1208 }
1209
1210 D->addAttr(::new (S.Context)
1211 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1212 Attr.getAttributeSpellingListIndex()));
1213}
1214
Chris Wailes0e429f12013-10-29 20:28:41 +00001215static void handleTestTypestateAttr(Sema &S, Decl *D,
1216 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001217 if (!checkAttributeNumArgs(S, Attr, 1))
1218 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001219
1220 if (!isa<CXXMethodDecl>(D)) {
1221 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1222 Attr.getName() << ExpectedMethod;
1223 return;
1224 }
1225
1226 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1227 return;
1228
Chris Wailes0e429f12013-10-29 20:28:41 +00001229 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001230 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001231 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1232 StringRef Param = Ident->Ident->getName();
Chris Wailes0e429f12013-10-29 20:28:41 +00001233 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001234 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1235 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001236 return;
1237 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001238 } else {
1239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1240 Attr.getName() << AANT_ArgumentIdentifier;
1241 return;
1242 }
1243
1244 D->addAttr(::new (S.Context)
Chris Wailes0e429f12013-10-29 20:28:41 +00001245 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001246 Attr.getAttributeSpellingListIndex()));
1247}
1248
Chandler Carruth1b03c872011-07-02 00:01:44 +00001249static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1250 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001251 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1252 if (TD == 0) {
1253 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1254 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001255 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001256 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001257 }
Mike Stumpbf916502009-07-24 19:02:52 +00001258
Richard Smitha4fa9002013-01-13 02:11:23 +00001259 // Remember this typedef decl, we will need it later for diagnostics.
1260 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001261}
1262
Chandler Carruth1b03c872011-07-02 00:01:44 +00001263static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001264 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001265 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001266 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001267 // If the alignment is less than or equal to 8 bits, the packed attribute
1268 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001269 if (!FD->getType()->isDependentType() &&
1270 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001271 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001272 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001273 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001274 else
Michael Han51d8c522013-01-24 16:46:58 +00001275 FD->addAttr(::new (S.Context)
1276 PackedAttr(Attr.getRange(), S.Context,
1277 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001278 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001279 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001280}
1281
Chandler Carruth1b03c872011-07-02 00:01:44 +00001282static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001283 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001284 RD->addAttr(::new (S.Context)
1285 MsStructAttr(Attr.getRange(), S.Context,
1286 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001287 else
1288 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1289}
1290
Chandler Carruth1b03c872011-07-02 00:01:44 +00001291static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001292 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001293 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001294 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001295 D->addAttr(::new (S.Context)
1296 IBActionAttr(Attr.getRange(), S.Context,
1297 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001298 return;
1299 }
1300
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001301 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001302}
1303
Ted Kremenek2f041d02011-09-29 07:02:25 +00001304static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1305 // The IBOutlet/IBOutletCollection attributes only apply to instance
1306 // variables or properties of Objective-C classes. The outlet must also
1307 // have an object reference type.
1308 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1309 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001310 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001311 << Attr.getName() << VD->getType() << 0;
1312 return false;
1313 }
1314 }
1315 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1316 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001317 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001318 << Attr.getName() << PD->getType() << 1;
1319 return false;
1320 }
1321 }
1322 else {
1323 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1324 return false;
1325 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001326
Ted Kremenek2f041d02011-09-29 07:02:25 +00001327 return true;
1328}
1329
Chandler Carruth1b03c872011-07-02 00:01:44 +00001330static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001331 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001332 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001333
Michael Han51d8c522013-01-24 16:46:58 +00001334 D->addAttr(::new (S.Context)
1335 IBOutletAttr(Attr.getRange(), S.Context,
1336 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001337}
1338
Chandler Carruth1b03c872011-07-02 00:01:44 +00001339static void handleIBOutletCollection(Sema &S, Decl *D,
1340 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001341
1342 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001343 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001344 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1345 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001346 return;
1347 }
1348
Ted Kremenek2f041d02011-09-29 07:02:25 +00001349 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001350 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001351
Richard Smithd386fef2013-10-31 01:56:18 +00001352 ParsedType PT;
1353
1354 if (Attr.hasParsedType())
1355 PT = Attr.getTypeArg();
1356 else {
1357 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1358 S.getScopeForContext(D->getDeclContext()->getParent()));
1359 if (!PT) {
1360 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1361 return;
1362 }
Aaron Ballman624421f2013-08-31 01:11:41 +00001363 }
Richard Smithd386fef2013-10-31 01:56:18 +00001364
Richard Smithddc2a532013-10-31 21:23:20 +00001365 TypeSourceInfo *QTLoc = 0;
1366 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1367 if (!QTLoc)
1368 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithd386fef2013-10-31 01:56:18 +00001369
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001370 // Diagnose use of non-object type in iboutletcollection attribute.
1371 // FIXME. Gnu attribute extension ignores use of builtin types in
1372 // attributes. So, __attribute__((iboutletcollection(char))) will be
1373 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001374 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithd386fef2013-10-31 01:56:18 +00001375 S.Diag(Attr.getLoc(),
1376 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1377 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001378 return;
1379 }
Richard Smithd386fef2013-10-31 01:56:18 +00001380
Michael Han51d8c522013-01-24 16:46:58 +00001381 D->addAttr(::new (S.Context)
Richard Smithddc2a532013-10-31 21:23:20 +00001382 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han51d8c522013-01-24 16:46:58 +00001383 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001384}
1385
Chandler Carruthd309c812011-07-01 23:49:16 +00001386static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001387 if (const RecordType *UT = T->getAsUnionType())
1388 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1389 RecordDecl *UD = UT->getDecl();
1390 for (RecordDecl::field_iterator it = UD->field_begin(),
1391 itend = UD->field_end(); it != itend; ++it) {
1392 QualType QT = it->getType();
1393 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1394 T = QT;
1395 return;
1396 }
1397 }
1398 }
1399}
1400
Nuno Lopes587de5b2012-05-24 00:22:00 +00001401static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001402 if (!isFunctionOrMethod(D)) {
1403 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001404 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001405 return;
1406 }
1407
Nuno Lopes587de5b2012-05-24 00:22:00 +00001408 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1409 return;
1410
Nuno Lopes587de5b2012-05-24 00:22:00 +00001411 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001412 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001413 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001414 uint64_t Idx;
1415 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1416 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001417 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001418
1419 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001420 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001421 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001422 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1423 << Attr.getName() << AANT_ArgumentIntegerConstant
1424 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001425 return;
1426 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001427 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001428 }
1429
1430 // check if the function returns a pointer
1431 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1432 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001433 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001434 }
1435
Michael Han51d8c522013-01-24 16:46:58 +00001436 D->addAttr(::new (S.Context)
1437 AllocSizeAttr(Attr.getRange(), S.Context,
1438 SizeArgs.data(), SizeArgs.size(),
1439 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001440}
1441
Chandler Carruth1b03c872011-07-02 00:01:44 +00001442static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001443 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1444 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001445 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001446 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001447 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001448 return;
1449 }
Mike Stumpbf916502009-07-24 19:02:52 +00001450
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001451 SmallVector<unsigned, 8> NonNullArgs;
1452 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001453 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001454 uint64_t Idx;
1455 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1456 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001457 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001458
1459 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001460 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001461 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001462
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001463 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001464 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001465 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001466 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001467 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001468 }
Mike Stumpbf916502009-07-24 19:02:52 +00001469
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001470 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001471 }
Mike Stumpbf916502009-07-24 19:02:52 +00001472
1473 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1474 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001475 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001476 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1477 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001478 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001479 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001480 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001481 }
Mike Stumpbf916502009-07-24 19:02:52 +00001482
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001483 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001484 if (NonNullArgs.empty()) {
1485 // Warn the trivial case only if attribute is not coming from a
1486 // macro instantiation.
1487 if (Attr.getLoc().isFileID())
1488 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001489 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001490 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001491 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001492
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001493 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001494 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001495 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001496 D->addAttr(::new (S.Context)
1497 NonNullAttr(Attr.getRange(), S.Context, start, size,
1498 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001499}
1500
Aaron Ballmane1668a32013-09-16 18:11:41 +00001501static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1502 switch (K) {
1503 case OwnershipAttr::Holds: return "'ownership_holds'";
1504 case OwnershipAttr::Takes: return "'ownership_takes'";
1505 case OwnershipAttr::Returns: return "'ownership_returns'";
1506 }
1507 llvm_unreachable("unknown ownership");
1508}
1509
Chandler Carruth1b03c872011-07-02 00:01:44 +00001510static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001511 // This attribute must be applied to a function declaration. The first
1512 // argument to the attribute must be an identifier, the name of the resource,
1513 // for example: malloc. The following arguments must be argument indexes, the
1514 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001515 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballmane1668a32013-09-16 18:11:41 +00001516 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose2a479922010-08-12 08:54:03 +00001517 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001518
Aaron Ballman624421f2013-08-31 01:11:41 +00001519 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001520 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmane1668a32013-09-16 18:11:41 +00001521 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001522 return;
1523 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001524
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001525 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001526 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001527 switch (AL.getKind()) {
1528 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001529 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001530 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001531 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001532 return;
1533 }
Jordy Rose2a479922010-08-12 08:54:03 +00001534 break;
1535 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001536 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001537 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001538 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001539 return;
1540 }
Jordy Rose2a479922010-08-12 08:54:03 +00001541 break;
1542 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001543 K = OwnershipAttr::Returns;
Aaron Ballmane1668a32013-09-16 18:11:41 +00001544
Aaron Ballman624421f2013-08-31 01:11:41 +00001545 if (AL.getNumArgs() > 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001546 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001547 return;
1548 }
Jordy Rose2a479922010-08-12 08:54:03 +00001549 break;
1550 default:
1551 // This should never happen given how we are called.
1552 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001553 }
1554
Chandler Carruth87c44602011-07-01 23:49:12 +00001555 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001556 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1557 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001558 return;
1559 }
1560
Aaron Ballman624421f2013-08-31 01:11:41 +00001561 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001562
1563 // Normalize the argument, __foo__ becomes foo.
1564 if (Module.startswith("__") && Module.endswith("__"))
1565 Module = Module.substr(2, Module.size() - 4);
1566
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001567 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001568 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1569 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001570 uint64_t Idx;
1571 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001572 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001573 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001574
Aaron Ballmane1668a32013-09-16 18:11:41 +00001575 // Is the function argument a pointer type?
1576 QualType T = getFunctionOrMethodArgType(D, Idx);
1577 int Err = -1; // No error
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001578 switch (K) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001579 case OwnershipAttr::Takes:
1580 case OwnershipAttr::Holds:
1581 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1582 Err = 0;
1583 break;
1584 case OwnershipAttr::Returns:
1585 if (!T->isIntegerType())
1586 Err = 1;
1587 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001588 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001589 if (-1 != Err) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001590 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballmane1668a32013-09-16 18:11:41 +00001591 << Ex->getSourceRange();
1592 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001593 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001594
1595 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001596 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballmane1668a32013-09-16 18:11:41 +00001597 i = D->specific_attr_begin<OwnershipAttr>(),
1598 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1599 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1600 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1601 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1602 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1603 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001604 }
1605 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001606 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001607 }
1608
1609 unsigned* start = OwnershipArgs.data();
1610 unsigned size = OwnershipArgs.size();
1611 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001612
Michael Han51d8c522013-01-24 16:46:58 +00001613 D->addAttr(::new (S.Context)
1614 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1615 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001616}
1617
Chandler Carruth1b03c872011-07-02 00:01:44 +00001618static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001619 // Check the attribute arguments.
1620 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1622 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001623 return;
1624 }
1625
Chandler Carruth87c44602011-07-01 23:49:12 +00001626 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001628 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001629 return;
1630 }
1631
Chandler Carruth87c44602011-07-01 23:49:12 +00001632 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001633
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001634 // gcc rejects
1635 // class c {
1636 // static int a __attribute__((weakref ("v2")));
1637 // static int b() __attribute__((weakref ("f3")));
1638 // };
1639 // and ignores the attributes of
1640 // void f(void) {
1641 // static int a __attribute__((weakref ("v2")));
1642 // }
1643 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001644 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001645 if (!Ctx->isFileContext()) {
1646 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001647 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001648 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001649 }
1650
1651 // The GCC manual says
1652 //
1653 // At present, a declaration to which `weakref' is attached can only
1654 // be `static'.
1655 //
1656 // It also says
1657 //
1658 // Without a TARGET,
1659 // given as an argument to `weakref' or to `alias', `weakref' is
1660 // equivalent to `weak'.
1661 //
1662 // gcc 4.4.1 will accept
1663 // int a7 __attribute__((weakref));
1664 // as
1665 // int a7 __attribute__((weak));
1666 // This looks like a bug in gcc. We reject that for now. We should revisit
1667 // it if this behaviour is actually used.
1668
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001669 // GCC rejects
1670 // static ((alias ("y"), weakref)).
1671 // Should we? How to check that weakref is before or after alias?
1672
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001673 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1674 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1675 // StringRef parameter it was given anyway.
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001676 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001677 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001678 // GCC will accept anything as the argument of weakref. Should we
1679 // check for an existing decl?
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001680 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1681 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001682
Michael Han51d8c522013-01-24 16:46:58 +00001683 D->addAttr(::new (S.Context)
1684 WeakRefAttr(Attr.getRange(), S.Context,
1685 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001686}
1687
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001688static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1689 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001690 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001691 return;
1692
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001693 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001694 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1695 return;
1696 }
1697
Chris Lattner6b6b5372008-06-26 18:38:35 +00001698 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001699
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001700 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001701 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001702}
1703
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001704static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001705 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1707 << Attr.getName() << ExpectedFunctionOrMethod;
1708 return;
1709 }
1710
Michael Han51d8c522013-01-24 16:46:58 +00001711 D->addAttr(::new (S.Context)
1712 MinSizeAttr(Attr.getRange(), S.Context,
1713 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001714}
1715
Benjamin Krameree409a92012-05-12 21:10:52 +00001716static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001717 if (!isa<FunctionDecl>(D)) {
1718 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1719 << Attr.getName() << ExpectedFunction;
1720 return;
1721 }
1722
1723 if (D->hasAttr<HotAttr>()) {
1724 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1725 << Attr.getName() << "hot";
1726 return;
1727 }
1728
Michael Han51d8c522013-01-24 16:46:58 +00001729 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1730 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001731}
1732
1733static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001734 if (!isa<FunctionDecl>(D)) {
1735 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1736 << Attr.getName() << ExpectedFunction;
1737 return;
1738 }
1739
1740 if (D->hasAttr<ColdAttr>()) {
1741 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1742 << Attr.getName() << "cold";
1743 return;
1744 }
1745
Michael Han51d8c522013-01-24 16:46:58 +00001746 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1747 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001748}
1749
Chandler Carruth1b03c872011-07-02 00:01:44 +00001750static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001751 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001752 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001753 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001754 return;
1755 }
1756
Michael Han51d8c522013-01-24 16:46:58 +00001757 D->addAttr(::new (S.Context)
1758 NakedAttr(Attr.getRange(), S.Context,
1759 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001760}
1761
Chandler Carruth1b03c872011-07-02 00:01:44 +00001762static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1763 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001764 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001765 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001766 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001767 return;
1768 }
Mike Stumpbf916502009-07-24 19:02:52 +00001769
Michael Han51d8c522013-01-24 16:46:58 +00001770 D->addAttr(::new (S.Context)
1771 AlwaysInlineAttr(Attr.getRange(), S.Context,
1772 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001773}
1774
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001775static void handleTLSModelAttr(Sema &S, Decl *D,
1776 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001777 StringRef Model;
1778 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001779 // Check that it is a string.
Tim Northoverf8aebef2013-10-01 14:34:18 +00001780 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001781 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001782
Richard Smith38afbc72013-04-13 02:43:54 +00001783 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001784 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1785 << Attr.getName() << ExpectedTLSVar;
1786 return;
1787 }
1788
1789 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001790 if (Model != "global-dynamic" && Model != "local-dynamic"
1791 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001792 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001793 return;
1794 }
1795
Michael Han51d8c522013-01-24 16:46:58 +00001796 D->addAttr(::new (S.Context)
1797 TLSModelAttr(Attr.getRange(), S.Context, Model,
1798 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001799}
1800
Chandler Carruth1b03c872011-07-02 00:01:44 +00001801static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001802 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001803 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001804 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001805 D->addAttr(::new (S.Context)
1806 MallocAttr(Attr.getRange(), S.Context,
1807 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001808 return;
1809 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001810 }
1811
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001812 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001813}
1814
Chandler Carruth1b03c872011-07-02 00:01:44 +00001815static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001816 D->addAttr(::new (S.Context)
1817 MayAliasAttr(Attr.getRange(), S.Context,
1818 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001819}
1820
Chandler Carruth1b03c872011-07-02 00:01:44 +00001821static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001822 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001823 D->addAttr(::new (S.Context)
1824 NoCommonAttr(Attr.getRange(), S.Context,
1825 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001826 else
1827 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001828 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001829}
1830
Chandler Carruth1b03c872011-07-02 00:01:44 +00001831static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001832 if (S.LangOpts.CPlusPlus) {
1833 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1834 return;
1835 }
1836
Chandler Carruth87c44602011-07-01 23:49:12 +00001837 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001838 D->addAttr(::new (S.Context)
1839 CommonAttr(Attr.getRange(), S.Context,
1840 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001841 else
1842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001843 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001844}
1845
Chandler Carruth1b03c872011-07-02 00:01:44 +00001846static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001847 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001848
1849 if (S.CheckNoReturnAttr(attr)) return;
1850
Chandler Carruth87c44602011-07-01 23:49:12 +00001851 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001852 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001853 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001854 return;
1855 }
1856
Michael Han51d8c522013-01-24 16:46:58 +00001857 D->addAttr(::new (S.Context)
1858 NoReturnAttr(attr.getRange(), S.Context,
1859 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001860}
1861
1862bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001863 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001864 attr.setInvalid();
1865 return true;
1866 }
1867
1868 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001869}
1870
Chandler Carruth1b03c872011-07-02 00:01:44 +00001871static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1872 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001873
1874 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1875 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001876 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1877 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001878 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1879 && !VD->getType()->isFunctionPointerType())) {
1880 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001881 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001882 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001883 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001884 return;
1885 }
1886 }
1887
Michael Han51d8c522013-01-24 16:46:58 +00001888 D->addAttr(::new (S.Context)
1889 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1890 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001891}
1892
Richard Smithcd8ab512013-01-17 01:30:42 +00001893static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1894 const AttributeList &Attr) {
1895 // C++11 [dcl.attr.noreturn]p1:
1896 // The attribute may be applied to the declarator-id in a function
1897 // declaration.
1898 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1899 if (!FD) {
1900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1901 << Attr.getName() << ExpectedFunctionOrMethod;
1902 return;
1903 }
1904
Michael Han51d8c522013-01-24 16:46:58 +00001905 D->addAttr(::new (S.Context)
1906 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1907 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001908}
1909
John Thompson35cc9622010-08-09 21:53:52 +00001910// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001911static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001912/*
1913 Returning a Vector Class in Registers
1914
Eric Christopherf48f3672010-12-01 22:13:54 +00001915 According to the PPU ABI specifications, a class with a single member of
1916 vector type is returned in memory when used as the return value of a function.
1917 This results in inefficient code when implementing vector classes. To return
1918 the value in a single vector register, add the vecreturn attribute to the
1919 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001920
1921 Example:
1922
1923 struct Vector
1924 {
1925 __vector float xyzw;
1926 } __attribute__((vecreturn));
1927
1928 Vector Add(Vector lhs, Vector rhs)
1929 {
1930 Vector result;
1931 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1932 return result; // This will be returned in a register
1933 }
1934*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001935 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001936 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001937 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001938 return;
1939 }
1940
Chandler Carruth87c44602011-07-01 23:49:12 +00001941 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001942 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1943 return;
1944 }
1945
Chandler Carruth87c44602011-07-01 23:49:12 +00001946 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001947 int count = 0;
1948
1949 if (!isa<CXXRecordDecl>(record)) {
1950 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1951 return;
1952 }
1953
1954 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1955 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1956 return;
1957 }
1958
Eric Christopherf48f3672010-12-01 22:13:54 +00001959 for (RecordDecl::field_iterator iter = record->field_begin();
1960 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001961 if ((count == 1) || !iter->getType()->isVectorType()) {
1962 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1963 return;
1964 }
1965 count++;
1966 }
1967
Michael Han51d8c522013-01-24 16:46:58 +00001968 D->addAttr(::new (S.Context)
1969 VecReturnAttr(Attr.getRange(), S.Context,
1970 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001971}
1972
Richard Smith3a2b7a12013-01-28 22:42:45 +00001973static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1974 const AttributeList &Attr) {
1975 if (isa<ParmVarDecl>(D)) {
1976 // [[carries_dependency]] can only be applied to a parameter if it is a
1977 // parameter of a function declaration or lambda.
1978 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1979 S.Diag(Attr.getLoc(),
1980 diag::err_carries_dependency_param_not_function_decl);
1981 return;
1982 }
1983 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001984 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001985 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001986 return;
1987 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001988
1989 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1990 Attr.getRange(), S.Context,
1991 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001992}
1993
Chandler Carruth1b03c872011-07-02 00:01:44 +00001994static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001995 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001996 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001998 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001999 return;
2000 }
Mike Stumpbf916502009-07-24 19:02:52 +00002001
Michael Han51d8c522013-01-24 16:46:58 +00002002 D->addAttr(::new (S.Context)
2003 UnusedAttr(Attr.getRange(), S.Context,
2004 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00002005}
2006
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002007static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2008 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002009 if (!isa<FunctionDecl>(D)) {
2010 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2011 << Attr.getName() << ExpectedFunction;
2012 return;
2013 }
2014
Michael Han51d8c522013-01-24 16:46:58 +00002015 D->addAttr(::new (S.Context)
2016 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2017 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002018}
2019
Chandler Carruth1b03c872011-07-02 00:01:44 +00002020static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002021 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00002022 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002023 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2024 return;
2025 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002026 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002027 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002028 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002029 return;
2030 }
Mike Stumpbf916502009-07-24 19:02:52 +00002031
Michael Han51d8c522013-01-24 16:46:58 +00002032 D->addAttr(::new (S.Context)
2033 UsedAttr(Attr.getRange(), S.Context,
2034 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002035}
2036
Chandler Carruth1b03c872011-07-02 00:01:44 +00002037static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002038 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002039 if (Attr.getNumArgs() > 1) {
2040 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002041 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002042 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002043
2044 int priority = 65535; // FIXME: Do not hardcode such constants.
2045 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002046 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002047 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002048 if (E->isTypeDependent() || E->isValueDependent() ||
2049 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002051 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002052 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002053 return;
2054 }
2055 priority = Idx.getZExtValue();
2056 }
Mike Stumpbf916502009-07-24 19:02:52 +00002057
Chandler Carruth87c44602011-07-01 23:49:12 +00002058 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002059 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002060 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002061 return;
2062 }
2063
Michael Han51d8c522013-01-24 16:46:58 +00002064 D->addAttr(::new (S.Context)
2065 ConstructorAttr(Attr.getRange(), S.Context, priority,
2066 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002067}
2068
Chandler Carruth1b03c872011-07-02 00:01:44 +00002069static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002070 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002071 if (Attr.getNumArgs() > 1) {
2072 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002073 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002074 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002075
2076 int priority = 65535; // FIXME: Do not hardcode such constants.
2077 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002078 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002079 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002080 if (E->isTypeDependent() || E->isValueDependent() ||
2081 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002082 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002083 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002084 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002085 return;
2086 }
2087 priority = Idx.getZExtValue();
2088 }
Mike Stumpbf916502009-07-24 19:02:52 +00002089
Chandler Carruth87c44602011-07-01 23:49:12 +00002090 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002091 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002092 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002093 return;
2094 }
2095
Michael Han51d8c522013-01-24 16:46:58 +00002096 D->addAttr(::new (S.Context)
2097 DestructorAttr(Attr.getRange(), S.Context, priority,
2098 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002099}
2100
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002101template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002102static void handleAttrWithMessage(Sema &S, Decl *D,
2103 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002104 unsigned NumArgs = Attr.getNumArgs();
2105 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002106 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002107 return;
2108 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002109
2110 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002111 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002112 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002113 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002114
Michael Han51d8c522013-01-24 16:46:58 +00002115 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2116 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002117}
2118
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002119static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2120 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002121 D->addAttr(::new (S.Context)
2122 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2123 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002124}
2125
Patrick Beardb2f68202012-04-06 18:12:22 +00002126static void handleObjCRootClassAttr(Sema &S, Decl *D,
2127 const AttributeList &Attr) {
2128 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002129 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2130 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002131 return;
2132 }
2133
Michael Han51d8c522013-01-24 16:46:58 +00002134 D->addAttr(::new (S.Context)
2135 ObjCRootClassAttr(Attr.getRange(), S.Context,
2136 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002137}
2138
Michael Han51d8c522013-01-24 16:46:58 +00002139static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2140 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002141 if (!isa<ObjCInterfaceDecl>(D)) {
2142 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2143 return;
2144 }
2145
Michael Han51d8c522013-01-24 16:46:58 +00002146 D->addAttr(::new (S.Context)
2147 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2148 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002149}
2150
Jordy Rosefad5de92012-05-08 03:27:22 +00002151static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2152 IdentifierInfo *Platform,
2153 VersionTuple Introduced,
2154 VersionTuple Deprecated,
2155 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002156 StringRef PlatformName
2157 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2158 if (PlatformName.empty())
2159 PlatformName = Platform->getName();
2160
2161 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2162 // of these steps are needed).
2163 if (!Introduced.empty() && !Deprecated.empty() &&
2164 !(Introduced <= Deprecated)) {
2165 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2166 << 1 << PlatformName << Deprecated.getAsString()
2167 << 0 << Introduced.getAsString();
2168 return true;
2169 }
2170
2171 if (!Introduced.empty() && !Obsoleted.empty() &&
2172 !(Introduced <= Obsoleted)) {
2173 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2174 << 2 << PlatformName << Obsoleted.getAsString()
2175 << 0 << Introduced.getAsString();
2176 return true;
2177 }
2178
2179 if (!Deprecated.empty() && !Obsoleted.empty() &&
2180 !(Deprecated <= Obsoleted)) {
2181 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2182 << 2 << PlatformName << Obsoleted.getAsString()
2183 << 1 << Deprecated.getAsString();
2184 return true;
2185 }
2186
2187 return false;
2188}
2189
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002190/// \brief Check whether the two versions match.
2191///
2192/// If either version tuple is empty, then they are assumed to match. If
2193/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2194static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2195 bool BeforeIsOkay) {
2196 if (X.empty() || Y.empty())
2197 return true;
2198
2199 if (X == Y)
2200 return true;
2201
2202 if (BeforeIsOkay && X < Y)
2203 return true;
2204
2205 return false;
2206}
2207
Rafael Espindola51be6e32013-01-08 22:04:34 +00002208AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002209 IdentifierInfo *Platform,
2210 VersionTuple Introduced,
2211 VersionTuple Deprecated,
2212 VersionTuple Obsoleted,
2213 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002214 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002215 bool Override,
2216 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002217 VersionTuple MergedIntroduced = Introduced;
2218 VersionTuple MergedDeprecated = Deprecated;
2219 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002220 bool FoundAny = false;
2221
Rafael Espindola98ae8342012-05-10 02:50:16 +00002222 if (D->hasAttrs()) {
2223 AttrVec &Attrs = D->getAttrs();
2224 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2225 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2226 if (!OldAA) {
2227 ++i;
2228 continue;
2229 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002230
Rafael Espindola98ae8342012-05-10 02:50:16 +00002231 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2232 if (OldPlatform != Platform) {
2233 ++i;
2234 continue;
2235 }
2236
2237 FoundAny = true;
2238 VersionTuple OldIntroduced = OldAA->getIntroduced();
2239 VersionTuple OldDeprecated = OldAA->getDeprecated();
2240 VersionTuple OldObsoleted = OldAA->getObsoleted();
2241 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002242
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002243 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2244 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2245 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2246 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002247 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002248 if (Override) {
2249 int Which = -1;
2250 VersionTuple FirstVersion;
2251 VersionTuple SecondVersion;
2252 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2253 Which = 0;
2254 FirstVersion = OldIntroduced;
2255 SecondVersion = Introduced;
2256 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2257 Which = 1;
2258 FirstVersion = Deprecated;
2259 SecondVersion = OldDeprecated;
2260 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2261 Which = 2;
2262 FirstVersion = Obsoleted;
2263 SecondVersion = OldObsoleted;
2264 }
2265
2266 if (Which == -1) {
2267 Diag(OldAA->getLocation(),
2268 diag::warn_mismatched_availability_override_unavail)
2269 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2270 } else {
2271 Diag(OldAA->getLocation(),
2272 diag::warn_mismatched_availability_override)
2273 << Which
2274 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2275 << FirstVersion.getAsString() << SecondVersion.getAsString();
2276 }
2277 Diag(Range.getBegin(), diag::note_overridden_method);
2278 } else {
2279 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2280 Diag(Range.getBegin(), diag::note_previous_attribute);
2281 }
2282
Rafael Espindola98ae8342012-05-10 02:50:16 +00002283 Attrs.erase(Attrs.begin() + i);
2284 --e;
2285 continue;
2286 }
2287
2288 VersionTuple MergedIntroduced2 = MergedIntroduced;
2289 VersionTuple MergedDeprecated2 = MergedDeprecated;
2290 VersionTuple MergedObsoleted2 = MergedObsoleted;
2291
2292 if (MergedIntroduced2.empty())
2293 MergedIntroduced2 = OldIntroduced;
2294 if (MergedDeprecated2.empty())
2295 MergedDeprecated2 = OldDeprecated;
2296 if (MergedObsoleted2.empty())
2297 MergedObsoleted2 = OldObsoleted;
2298
2299 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2300 MergedIntroduced2, MergedDeprecated2,
2301 MergedObsoleted2)) {
2302 Attrs.erase(Attrs.begin() + i);
2303 --e;
2304 continue;
2305 }
2306
2307 MergedIntroduced = MergedIntroduced2;
2308 MergedDeprecated = MergedDeprecated2;
2309 MergedObsoleted = MergedObsoleted2;
2310 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002311 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002312 }
2313
2314 if (FoundAny &&
2315 MergedIntroduced == Introduced &&
2316 MergedDeprecated == Deprecated &&
2317 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002318 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002319
Ted Kremenekcb344392013-04-06 00:34:27 +00002320 // Only create a new attribute if !Override, but we want to do
2321 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002322 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002323 MergedDeprecated, MergedObsoleted) &&
2324 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002325 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2326 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002327 Obsoleted, IsUnavailable, Message,
2328 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002329 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002330 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002331}
2332
Chandler Carruth1b03c872011-07-02 00:01:44 +00002333static void handleAvailabilityAttr(Sema &S, Decl *D,
2334 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002335 if (!checkAttributeNumArgs(S, Attr, 1))
2336 return;
2337 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002338 unsigned Index = Attr.getAttributeSpellingListIndex();
2339
Aaron Ballman624421f2013-08-31 01:11:41 +00002340 IdentifierInfo *II = Platform->Ident;
2341 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2342 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2343 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002344
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002345 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2346 if (!ND) {
2347 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2348 return;
2349 }
2350
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002351 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2352 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2353 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002354 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002355 StringRef Str;
Benjamin Kramerc5617142013-09-13 17:31:48 +00002356 if (const StringLiteral *SE =
2357 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002358 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002359
Aaron Ballman624421f2013-08-31 01:11:41 +00002360 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002361 Introduced.Version,
2362 Deprecated.Version,
2363 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002364 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002365 /*Override=*/false,
2366 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002367 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002368 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002369}
2370
John McCalld4c3d662013-02-20 01:54:26 +00002371template <class T>
2372static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2373 typename T::VisibilityType value,
2374 unsigned attrSpellingListIndex) {
2375 T *existingAttr = D->getAttr<T>();
2376 if (existingAttr) {
2377 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2378 if (existingValue == value)
2379 return NULL;
2380 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2381 S.Diag(range.getBegin(), diag::note_previous_attribute);
2382 D->dropAttr<T>();
2383 }
2384 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2385}
2386
Rafael Espindola599f1b72012-05-13 03:25:18 +00002387VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002388 VisibilityAttr::VisibilityType Vis,
2389 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002390 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2391 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002392}
2393
John McCalld4c3d662013-02-20 01:54:26 +00002394TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2395 TypeVisibilityAttr::VisibilityType Vis,
2396 unsigned AttrSpellingListIndex) {
2397 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2398 AttrSpellingListIndex);
2399}
2400
2401static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2402 bool isTypeVisibility) {
2403 // Visibility attributes don't mean anything on a typedef.
2404 if (isa<TypedefNameDecl>(D)) {
2405 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2406 << Attr.getName();
2407 return;
2408 }
2409
2410 // 'type_visibility' can only go on a type or namespace.
2411 if (isTypeVisibility &&
2412 !(isa<TagDecl>(D) ||
2413 isa<ObjCInterfaceDecl>(D) ||
2414 isa<NamespaceDecl>(D))) {
2415 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2416 << Attr.getName() << ExpectedTypeOrNamespace;
2417 return;
2418 }
2419
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002420 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002421 StringRef TypeStr;
2422 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002423 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002424 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002425
Sean Huntcf807c42010-08-18 23:23:40 +00002426 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002427 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002428 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002429 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002430 return;
2431 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002432
2433 // Complain about attempts to use protected visibility on targets
2434 // (like Darwin) that don't support it.
2435 if (type == VisibilityAttr::Protected &&
2436 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2437 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2438 type = VisibilityAttr::Default;
2439 }
Mike Stumpbf916502009-07-24 19:02:52 +00002440
Michael Han51d8c522013-01-24 16:46:58 +00002441 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002442 clang::Attr *newAttr;
2443 if (isTypeVisibility) {
2444 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2445 (TypeVisibilityAttr::VisibilityType) type,
2446 Index);
2447 } else {
2448 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2449 }
2450 if (newAttr)
2451 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002452}
2453
Chandler Carruth1b03c872011-07-02 00:01:44 +00002454static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2455 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002456 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2457 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002459 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002460 return;
2461 }
2462
Aaron Ballman624421f2013-08-31 01:11:41 +00002463 if (!Attr.isArgIdent(0)) {
2464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2465 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002466 return;
2467 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002468
Aaron Ballmand0686072013-09-11 19:47:58 +00002469 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2470 ObjCMethodFamilyAttr::FamilyKind F;
2471 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2472 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2473 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002474 return;
2475 }
2476
Aaron Ballmand0686072013-09-11 19:47:58 +00002477 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002478 !method->getResultType()->isObjCObjectPointerType()) {
2479 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2480 << method->getResultType();
2481 // Ignore the attribute.
2482 return;
2483 }
2484
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002485 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002486 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002487}
2488
Chandler Carruth1b03c872011-07-02 00:01:44 +00002489static void handleObjCExceptionAttr(Sema &S, Decl *D,
2490 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002491 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2492 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2494 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002495 return;
2496 }
Mike Stumpbf916502009-07-24 19:02:52 +00002497
Michael Han51d8c522013-01-24 16:46:58 +00002498 D->addAttr(::new (S.Context)
2499 ObjCExceptionAttr(Attr.getRange(), S.Context,
2500 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002501}
2502
Chandler Carruth1b03c872011-07-02 00:01:44 +00002503static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002504 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002505 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002506 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002507 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2508 return;
2509 }
2510 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002511 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2512 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002513 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002514 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2515 return;
2516 }
2517 }
2518 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002519 // It is okay to include this attribute on properties, e.g.:
2520 //
2521 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2522 //
2523 // In this case it follows tradition and suppresses an error in the above
2524 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002525 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002526 }
Michael Han51d8c522013-01-24 16:46:58 +00002527 D->addAttr(::new (S.Context)
2528 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2529 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002530}
2531
Mike Stumpbf916502009-07-24 19:02:52 +00002532static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002533handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002534 if (!isa<FunctionDecl>(D)) {
2535 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2536 return;
2537 }
2538
Michael Han51d8c522013-01-24 16:46:58 +00002539 D->addAttr(::new (S.Context)
2540 OverloadableAttr(Attr.getRange(), S.Context,
2541 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002542}
2543
Chandler Carruth1b03c872011-07-02 00:01:44 +00002544static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002545 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002547 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002548 return;
2549 }
Mike Stumpbf916502009-07-24 19:02:52 +00002550
Aaron Ballman624421f2013-08-31 01:11:41 +00002551 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002552 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002553 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2554 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2555 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002556 return;
2557 }
Mike Stumpbf916502009-07-24 19:02:52 +00002558
Michael Han51d8c522013-01-24 16:46:58 +00002559 D->addAttr(::new (S.Context)
2560 BlocksAttr(Attr.getRange(), S.Context, type,
2561 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002562}
2563
Chandler Carruth1b03c872011-07-02 00:01:44 +00002564static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002565 // check the attribute arguments.
2566 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002567 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002568 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002569 }
2570
John McCall3323fad2011-09-09 07:56:05 +00002571 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002572 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002573 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002574 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002575 if (E->isTypeDependent() || E->isValueDependent() ||
2576 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002577 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002578 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002579 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002580 return;
2581 }
Mike Stumpbf916502009-07-24 19:02:52 +00002582
John McCall3323fad2011-09-09 07:56:05 +00002583 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002584 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2585 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002586 return;
2587 }
John McCall3323fad2011-09-09 07:56:05 +00002588
2589 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002590 }
2591
John McCall3323fad2011-09-09 07:56:05 +00002592 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002593 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002594 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002595 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002596 if (E->isTypeDependent() || E->isValueDependent() ||
2597 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002598 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002599 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002600 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002601 return;
2602 }
2603 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002604
John McCall3323fad2011-09-09 07:56:05 +00002605 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002606 // FIXME: This error message could be improved, it would be nice
2607 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002608 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2609 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002610 return;
2611 }
2612 }
2613
Chandler Carruth87c44602011-07-01 23:49:12 +00002614 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002615 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002616 if (isa<FunctionNoProtoType>(FT)) {
2617 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2618 return;
2619 }
Mike Stumpbf916502009-07-24 19:02:52 +00002620
Chris Lattner897cd902009-03-17 23:03:47 +00002621 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002622 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002623 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002624 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002625 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002626 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002627 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002628 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002629 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002630 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2631 if (!BD->isVariadic()) {
2632 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2633 return;
2634 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002635 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002636 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002637 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002638 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002639 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002640 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002641 int m = Ty->isFunctionPointerType() ? 0 : 1;
2642 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002643 return;
2644 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002645 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002646 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002647 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002648 return;
2649 }
Anders Carlsson77091822008-10-05 18:05:59 +00002650 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002651 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002652 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002653 return;
2654 }
Michael Han51d8c522013-01-24 16:46:58 +00002655 D->addAttr(::new (S.Context)
2656 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2657 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002658}
2659
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002660static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002661 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2662 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2663 else
2664 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2665}
2666
Chandler Carruth1b03c872011-07-02 00:01:44 +00002667static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002668 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002670 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002671 return;
2672 }
Mike Stumpbf916502009-07-24 19:02:52 +00002673
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002674 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2675 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2676 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002677 return;
2678 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002679 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2680 if (MD->getResultType()->isVoidType()) {
2681 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2682 << Attr.getName() << 1;
2683 return;
2684 }
2685
Michael Han51d8c522013-01-24 16:46:58 +00002686 D->addAttr(::new (S.Context)
2687 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2688 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002689}
2690
Chandler Carruth1b03c872011-07-02 00:01:44 +00002691static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002692 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002693 if (isa<CXXRecordDecl>(D)) {
2694 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2695 return;
2696 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2698 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002699 return;
2700 }
2701
Chandler Carruth87c44602011-07-01 23:49:12 +00002702 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002703
Michael Han51d8c522013-01-24 16:46:58 +00002704 nd->addAttr(::new (S.Context)
2705 WeakAttr(Attr.getRange(), S.Context,
2706 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002707}
2708
Chandler Carruth1b03c872011-07-02 00:01:44 +00002709static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002710 // weak_import only applies to variable & function declarations.
2711 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002712 if (!D->canBeWeakImported(isDef)) {
2713 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002714 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2715 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002716 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002717 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002718 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002719 // Nothing to warn about here.
2720 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002721 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002722 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002723
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002724 return;
2725 }
2726
Michael Han51d8c522013-01-24 16:46:58 +00002727 D->addAttr(::new (S.Context)
2728 WeakImportAttr(Attr.getRange(), S.Context,
2729 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002730}
2731
Tanya Lattner0df579e2012-07-09 22:06:01 +00002732// Handles reqd_work_group_size and work_group_size_hint.
2733static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002734 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002735 unsigned WGSize[3];
2736 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002737 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002738 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002739 if (E->isTypeDependent() || E->isValueDependent() ||
2740 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002741 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2742 << Attr.getName() << AANT_ArgumentIntegerConstant
2743 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002744 return;
2745 }
2746 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2747 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002748
2749 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2750 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2751 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2752 if (!(A->getXDim() == WGSize[0] &&
2753 A->getYDim() == WGSize[1] &&
2754 A->getZDim() == WGSize[2])) {
2755 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2756 Attr.getName();
2757 }
2758 }
2759
2760 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2761 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2762 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2763 if (!(A->getXDim() == WGSize[0] &&
2764 A->getYDim() == WGSize[1] &&
2765 A->getZDim() == WGSize[2])) {
2766 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2767 Attr.getName();
2768 }
2769 }
2770
2771 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2772 D->addAttr(::new (S.Context)
2773 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002774 WGSize[0], WGSize[1], WGSize[2],
2775 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002776 else
2777 D->addAttr(::new (S.Context)
2778 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002779 WGSize[0], WGSize[1], WGSize[2],
2780 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002781}
2782
Joey Gouly37453b92013-03-08 09:42:32 +00002783static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2784 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2785
Aaron Ballman624421f2013-08-31 01:11:41 +00002786 if (!Attr.hasParsedType()) {
2787 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2788 << Attr.getName() << 1;
2789 return;
2790 }
2791
Richard Smithddc2a532013-10-31 21:23:20 +00002792 TypeSourceInfo *ParmTSI = 0;
2793 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2794 assert(ParmTSI && "no type source info for attribute argument");
Joey Gouly37453b92013-03-08 09:42:32 +00002795
2796 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2797 (ParmType->isBooleanType() ||
2798 !ParmType->isIntegralType(S.getASTContext()))) {
2799 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2800 << ParmType;
2801 return;
2802 }
2803
2804 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2805 D->hasAttr<VecTypeHintAttr>()) {
2806 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithddc2a532013-10-31 21:23:20 +00002807 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Gouly37453b92013-03-08 09:42:32 +00002808 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2809 return;
2810 }
2811 }
2812
2813 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithddc2a532013-10-31 21:23:20 +00002814 ParmTSI));
Joey Gouly37453b92013-03-08 09:42:32 +00002815}
2816
Rafael Espindola599f1b72012-05-13 03:25:18 +00002817SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002818 StringRef Name,
2819 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002820 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2821 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002822 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002823 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2824 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002825 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002826 }
Michael Han51d8c522013-01-24 16:46:58 +00002827 return ::new (Context) SectionAttr(Range, Context, Name,
2828 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002829}
2830
Chandler Carruth1b03c872011-07-02 00:01:44 +00002831static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002832 // Make sure that there is a string literal as the sections's single
2833 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002834 StringRef Str;
2835 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002836 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002837 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Chris Lattner797c3c42009-08-10 19:03:04 +00002839 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002840 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002841 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002842 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002843 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002844 return;
2845 }
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002847 // This attribute cannot be applied to local variables.
2848 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002849 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002850 return;
2851 }
Michael Han51d8c522013-01-24 16:46:58 +00002852
2853 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002854 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002855 if (NewAttr)
2856 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002857}
2858
Chris Lattner6b6b5372008-06-26 18:38:35 +00002859
Chandler Carruth1b03c872011-07-02 00:01:44 +00002860static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002861 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002862 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002863 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002864 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002865 D->addAttr(::new (S.Context)
2866 NoThrowAttr(Attr.getRange(), S.Context,
2867 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002868 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002869}
2870
Chandler Carruth1b03c872011-07-02 00:01:44 +00002871static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002872 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002873 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002874 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002875 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002876 D->addAttr(::new (S.Context)
2877 ConstAttr(Attr.getRange(), S.Context,
2878 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002879 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002880}
2881
Chandler Carruth1b03c872011-07-02 00:01:44 +00002882static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002883 D->addAttr(::new (S.Context)
2884 PureAttr(Attr.getRange(), S.Context,
2885 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002886}
2887
Chandler Carruth1b03c872011-07-02 00:01:44 +00002888static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002889 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002890 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002891 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002892 return;
2893 }
Mike Stumpbf916502009-07-24 19:02:52 +00002894
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002895 Expr *E = Attr.getArgAsExpr(0);
2896 SourceLocation Loc = E->getExprLoc();
2897 FunctionDecl *FD = 0;
2898 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002899
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002900 // gcc only allows for simple identifiers. Since we support more than gcc, we
2901 // will warn the user.
2902 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2903 if (DRE->hasQualifier())
2904 S.Diag(Loc, diag::warn_cleanup_ext);
2905 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2906 NI = DRE->getNameInfo();
2907 if (!FD) {
2908 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2909 << NI.getName();
2910 return;
2911 }
2912 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2913 if (ULE->hasExplicitTemplateArgs())
2914 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002915 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2916 NI = ULE->getNameInfo();
Alp Toker8b407722013-10-20 18:48:56 +00002917 if (!FD) {
2918 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2919 << NI.getName();
2920 if (ULE->getType() == S.Context.OverloadTy)
2921 S.NoteAllOverloadCandidates(ULE);
2922 return;
2923 }
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002924 } else {
2925 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002926 return;
2927 }
2928
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002929 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002930 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2931 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002932 return;
2933 }
Mike Stumpbf916502009-07-24 19:02:52 +00002934
Anders Carlsson89941c12009-02-07 23:16:50 +00002935 // We're currently more strict than GCC about what function types we accept.
2936 // If this ever proves to be a problem it should be easy to fix.
2937 QualType Ty = S.Context.getPointerType(VD->getType());
2938 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002939 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2940 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002941 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2942 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002943 return;
2944 }
Mike Stumpbf916502009-07-24 19:02:52 +00002945
Michael Han51d8c522013-01-24 16:46:58 +00002946 D->addAttr(::new (S.Context)
2947 CleanupAttr(Attr.getRange(), S.Context, FD,
2948 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002949}
2950
Mike Stumpbf916502009-07-24 19:02:52 +00002951/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002952/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002953static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002954 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002955 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002956 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002957 return;
2958 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002959
Aaron Ballman624421f2013-08-31 01:11:41 +00002960 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002961 uint64_t ArgIdx;
2962 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2963 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002964 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002965
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002966 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002967 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002968
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002969 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2970 if (not_nsstring_type &&
2971 !isCFStringType(Ty, S.Context) &&
2972 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002973 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002974 // FIXME: Should highlight the actual expression that has the wrong type.
2975 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002976 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002977 << IdxExpr->getSourceRange();
2978 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002979 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002980 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002981 if (!isNSStringType(Ty, S.Context) &&
2982 !isCFStringType(Ty, S.Context) &&
2983 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002984 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002985 // FIXME: Should highlight the actual expression that has the wrong type.
2986 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002987 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002988 << IdxExpr->getSourceRange();
2989 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002990 }
2991
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002992 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2993 // because that has corrected for the implicit this parameter, and is zero-
2994 // based. The attribute expects what the user wrote explicitly.
2995 llvm::APSInt Val;
2996 IdxExpr->EvaluateAsInt(Val, S.Context);
2997
Michael Han51d8c522013-01-24 16:46:58 +00002998 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002999 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003000 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003001}
3002
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003003enum FormatAttrKind {
3004 CFStringFormat,
3005 NSStringFormat,
3006 StrftimeFormat,
3007 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003008 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003009 InvalidFormat
3010};
3011
3012/// getFormatAttrKind - Map from format attribute names to supported format
3013/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003014static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003015 return llvm::StringSwitch<FormatAttrKind>(Format)
3016 // Check for formats that get handled specially.
3017 .Case("NSString", NSStringFormat)
3018 .Case("CFString", CFStringFormat)
3019 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003020
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003021 // Otherwise, check for supported formats.
3022 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3023 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3024 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003025
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003026 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3027 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003028}
3029
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003030/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003031/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003032static void handleInitPriorityAttr(Sema &S, Decl *D,
3033 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003034 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003035 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3036 return;
3037 }
3038
Chandler Carruth87c44602011-07-01 23:49:12 +00003039 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003040 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3041 Attr.setInvalid();
3042 return;
3043 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003044 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003045 if (S.Context.getAsArrayType(T))
3046 T = S.Context.getBaseElementType(T);
3047 if (!T->getAs<RecordType>()) {
3048 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3049 Attr.setInvalid();
3050 return;
3051 }
3052
Aaron Ballman624421f2013-08-31 01:11:41 +00003053 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003054
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003055 llvm::APSInt priority(32);
3056 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3057 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003058 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3059 << Attr.getName() << AANT_ArgumentIntegerConstant
3060 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003061 Attr.setInvalid();
3062 return;
3063 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003064 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003065 if (prioritynum < 101 || prioritynum > 65535) {
3066 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3067 << priorityExpr->getSourceRange();
3068 Attr.setInvalid();
3069 return;
3070 }
Michael Han51d8c522013-01-24 16:46:58 +00003071 D->addAttr(::new (S.Context)
3072 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3073 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003074}
3075
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003076FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3077 IdentifierInfo *Format, int FormatIdx,
3078 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003079 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003080 // Check whether we already have an equivalent format attribute.
3081 for (specific_attr_iterator<FormatAttr>
3082 i = D->specific_attr_begin<FormatAttr>(),
3083 e = D->specific_attr_end<FormatAttr>();
3084 i != e ; ++i) {
3085 FormatAttr *f = *i;
3086 if (f->getType() == Format &&
3087 f->getFormatIdx() == FormatIdx &&
3088 f->getFirstArg() == FirstArg) {
3089 // If we don't have a valid location for this attribute, adopt the
3090 // location.
3091 if (f->getLocation().isInvalid())
3092 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003093 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003094 }
3095 }
3096
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003097 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3098 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003099}
3100
Mike Stumpbf916502009-07-24 19:02:52 +00003101/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003102/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003103static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003104 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003105 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003106 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003107 return;
3108 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003109
Chandler Carruth87c44602011-07-01 23:49:12 +00003110 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003111 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003112 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003113 return;
3114 }
3115
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003116 // In C++ the implicit 'this' function parameter also counts, and they are
3117 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003118 bool HasImplicitThisParam = isInstanceMethod(D);
3119 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003120 unsigned FirstIdx = 1;
3121
Aaron Ballman624421f2013-08-31 01:11:41 +00003122 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3123 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003124
3125 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003126 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003127 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003128 // If we've modified the string name, we need a new identifier for it.
3129 II = &S.Context.Idents.get(Format);
3130 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003132 // Check for supported formats.
3133 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003134
3135 if (Kind == IgnoredFormat)
3136 return;
3137
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003138 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003139 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003140 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003141 return;
3142 }
3143
3144 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003145 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003146 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003147 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3148 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003150 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003151 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003152 return;
3153 }
3154
3155 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003156 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003157 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003158 return;
3159 }
3160
3161 // FIXME: Do we need to bounds check?
3162 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003163
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003164 if (HasImplicitThisParam) {
3165 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003166 S.Diag(Attr.getLoc(),
3167 diag::err_format_attribute_implicit_this_format_string)
3168 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003169 return;
3170 }
3171 ArgIdx--;
3172 }
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Chris Lattner6b6b5372008-06-26 18:38:35 +00003174 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003175 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003176
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003177 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003178 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003179 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3180 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003181 return;
3182 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003183 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003184 // FIXME: do we need to check if the type is NSString*? What are the
3185 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003186 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003187 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003188 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3189 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003190 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003191 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003192 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003193 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003194 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003195 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3196 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003197 return;
3198 }
3199
3200 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003201 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003202 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003203 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3204 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003206 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003207 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003208 return;
3209 }
3210
3211 // check if the function is variadic if the 3rd argument non-zero
3212 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003213 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003214 ++NumArgs; // +1 for ...
3215 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003216 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003217 return;
3218 }
3219 }
3220
Chris Lattner3c73c412008-11-19 08:23:25 +00003221 // strftime requires FirstArg to be 0 because it doesn't read from any
3222 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003223 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003224 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003225 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3226 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003227 return;
3228 }
3229 // if 0 it disables parameter checking (to use with e.g. va_list)
3230 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003232 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003233 return;
3234 }
3235
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003236 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003237 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003238 FirstArg.getZExtValue(),
3239 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003240 if (NewAttr)
3241 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242}
3243
Chandler Carruth1b03c872011-07-02 00:01:44 +00003244static void handleTransparentUnionAttr(Sema &S, Decl *D,
3245 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003246 // Try to find the underlying union declaration.
3247 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003248 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003249 if (TD && TD->getUnderlyingType()->isUnionType())
3250 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3251 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003252 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003253
3254 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003255 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003256 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003257 return;
3258 }
3259
John McCall5e1cdac2011-10-07 06:10:15 +00003260 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003261 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003262 diag::warn_transparent_union_attribute_not_definition);
3263 return;
3264 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003265
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003266 RecordDecl::field_iterator Field = RD->field_begin(),
3267 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003268 if (Field == FieldEnd) {
3269 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3270 return;
3271 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003272
David Blaikie581deb32012-06-06 20:45:41 +00003273 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003274 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003275 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003276 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003277 diag::warn_transparent_union_attribute_floating)
3278 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003279 return;
3280 }
3281
3282 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3283 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3284 for (; Field != FieldEnd; ++Field) {
3285 QualType FieldType = Field->getType();
3286 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3287 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3288 // Warn if we drop the attribute.
3289 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003290 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003291 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003292 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003293 diag::warn_transparent_union_attribute_field_size_align)
3294 << isSize << Field->getDeclName() << FieldBits;
3295 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003296 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003297 diag::note_transparent_union_first_field_size_align)
3298 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003299 return;
3300 }
3301 }
3302
Michael Han51d8c522013-01-24 16:46:58 +00003303 RD->addAttr(::new (S.Context)
3304 TransparentUnionAttr(Attr.getRange(), S.Context,
3305 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003306}
3307
Chandler Carruth1b03c872011-07-02 00:01:44 +00003308static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003309 // Make sure that there is a string literal as the annotation's single
3310 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003311 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003312 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003313 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003314
3315 // Don't duplicate annotations that are already set.
3316 for (specific_attr_iterator<AnnotateAttr>
3317 i = D->specific_attr_begin<AnnotateAttr>(),
3318 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003319 if ((*i)->getAnnotation() == Str)
3320 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003321 }
Michael Han51d8c522013-01-24 16:46:58 +00003322
3323 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003324 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003325 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003326}
3327
Chandler Carruth1b03c872011-07-02 00:01:44 +00003328static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003329 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003330 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003331 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3332 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003333 return;
3334 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003335
Richard Smithbe507b62013-02-01 08:12:08 +00003336 if (Attr.getNumArgs() == 0) {
3337 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3338 true, 0, Attr.getAttributeSpellingListIndex()));
3339 return;
3340 }
3341
Aaron Ballman624421f2013-08-31 01:11:41 +00003342 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003343 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3344 S.Diag(Attr.getEllipsisLoc(),
3345 diag::err_pack_expansion_without_parameter_packs);
3346 return;
3347 }
3348
3349 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3350 return;
3351
3352 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3353 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003354}
3355
3356void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003357 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003358 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3359 SourceLocation AttrLoc = AttrRange.getBegin();
3360
Richard Smith4cd81c52013-01-29 09:02:09 +00003361 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003362 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003363 // C++11 [dcl.align]p1:
3364 // An alignment-specifier may be applied to a variable or to a class
3365 // data member, but it shall not be applied to a bit-field, a function
3366 // parameter, the formal parameter of a catch clause, or a variable
3367 // declared with the register storage class specifier. An
3368 // alignment-specifier may also be applied to the declaration of a class
3369 // or enumeration type.
3370 // C11 6.7.5/2:
3371 // An alignment attribute shall not be specified in a declaration of
3372 // a typedef, or a bit-field, or a function, or a parameter, or an
3373 // object declared with the register storage-class specifier.
3374 int DiagKind = -1;
3375 if (isa<ParmVarDecl>(D)) {
3376 DiagKind = 0;
3377 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3378 if (VD->getStorageClass() == SC_Register)
3379 DiagKind = 1;
3380 if (VD->isExceptionVariable())
3381 DiagKind = 2;
3382 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3383 if (FD->isBitField())
3384 DiagKind = 3;
3385 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003386 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3387 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003388 << (TmpAttr.isC11() ? ExpectedVariableOrField
3389 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003390 return;
3391 }
3392 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003393 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003394 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003395 return;
3396 }
3397 }
3398
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003399 if (E->isTypeDependent() || E->isValueDependent()) {
3400 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003401 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3402 AA->setPackExpansion(IsPackExpansion);
3403 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003404 return;
3405 }
Michael Hana31f65b2013-02-01 01:19:17 +00003406
Sean Huntcf807c42010-08-18 23:23:40 +00003407 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003408 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003409 ExprResult ICE
3410 = VerifyIntegerConstantExpression(E, &Alignment,
3411 diag::err_aligned_attribute_argument_not_int,
3412 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003413 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003414 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003415
3416 // C++11 [dcl.align]p2:
3417 // -- if the constant expression evaluates to zero, the alignment
3418 // specifier shall have no effect
3419 // C11 6.7.5p6:
3420 // An alignment specification of zero has no effect.
3421 if (!(TmpAttr.isAlignas() && !Alignment) &&
3422 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003423 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3424 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003425 return;
3426 }
Michael Hana31f65b2013-02-01 01:19:17 +00003427
Richard Smithbe507b62013-02-01 08:12:08 +00003428 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003429 // We've already verified it's a power of 2, now let's make sure it's
3430 // 8192 or less.
3431 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003432 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003433 << E->getSourceRange();
3434 return;
3435 }
3436 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003437
Richard Smithf6565a92013-02-22 08:32:16 +00003438 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3439 ICE.take(), SpellingListIndex);
3440 AA->setPackExpansion(IsPackExpansion);
3441 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003442}
3443
Michael Hana31f65b2013-02-01 01:19:17 +00003444void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003445 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003446 // FIXME: Cache the number on the Attr object if non-dependent?
3447 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003448 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3449 SpellingListIndex);
3450 AA->setPackExpansion(IsPackExpansion);
3451 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003452}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003453
Richard Smithbe507b62013-02-01 08:12:08 +00003454void Sema::CheckAlignasUnderalignment(Decl *D) {
3455 assert(D->hasAttrs() && "no attributes on decl");
3456
3457 QualType Ty;
3458 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3459 Ty = VD->getType();
3460 else
3461 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003462 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003463 return;
3464
3465 // C++11 [dcl.align]p5, C11 6.7.5/4:
3466 // The combined effect of all alignment attributes in a declaration shall
3467 // not specify an alignment that is less strict than the alignment that
3468 // would otherwise be required for the entity being declared.
3469 AlignedAttr *AlignasAttr = 0;
3470 unsigned Align = 0;
3471 for (specific_attr_iterator<AlignedAttr>
3472 I = D->specific_attr_begin<AlignedAttr>(),
3473 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3474 if (I->isAlignmentDependent())
3475 return;
3476 if (I->isAlignas())
3477 AlignasAttr = *I;
3478 Align = std::max(Align, I->getAlignment(Context));
3479 }
3480
3481 if (AlignasAttr && Align) {
3482 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3483 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3484 if (NaturalAlign > RequestedAlign)
3485 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3486 << Ty << (unsigned)NaturalAlign.getQuantity();
3487 }
3488}
3489
Chandler Carruthd309c812011-07-01 23:49:16 +00003490/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003491/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003492///
Mike Stumpbf916502009-07-24 19:02:52 +00003493/// Despite what would be logical, the mode attribute is a decl attribute, not a
3494/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3495/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003496static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003497 // This attribute isn't documented, but glibc uses it. It changes
3498 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003499 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3501 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003502 return;
3503 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003504
Aaron Ballman624421f2013-08-31 01:11:41 +00003505 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3506 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003507
3508 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003509 if (Str.startswith("__") && Str.endswith("__"))
3510 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003511
3512 unsigned DestWidth = 0;
3513 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003514 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003515 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003516 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003517 switch (Str[0]) {
3518 case 'Q': DestWidth = 8; break;
3519 case 'H': DestWidth = 16; break;
3520 case 'S': DestWidth = 32; break;
3521 case 'D': DestWidth = 64; break;
3522 case 'X': DestWidth = 96; break;
3523 case 'T': DestWidth = 128; break;
3524 }
3525 if (Str[1] == 'F') {
3526 IntegerMode = false;
3527 } else if (Str[1] == 'C') {
3528 IntegerMode = false;
3529 ComplexMode = true;
3530 } else if (Str[1] != 'I') {
3531 DestWidth = 0;
3532 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003533 break;
3534 case 4:
3535 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3536 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003537 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003538 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003539 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003540 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003541 break;
3542 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003543 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003544 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003545 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003546 case 11:
3547 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003548 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003549 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003550 }
3551
3552 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003553 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003554 OldTy = TD->getUnderlyingType();
3555 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3556 OldTy = VD->getType();
3557 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003558 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003559 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003560 return;
3561 }
Eli Friedman73397492009-03-03 06:41:03 +00003562
John McCall183700f2009-09-21 23:43:11 +00003563 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003564 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3565 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003566 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003567 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3568 } else if (ComplexMode) {
3569 if (!OldTy->isComplexType())
3570 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3571 } else {
3572 if (!OldTy->isFloatingType())
3573 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3574 }
3575
Mike Stump390b4cc2009-05-16 07:39:55 +00003576 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3577 // and friends, at least with glibc.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003578 // FIXME: Make sure floating-point mappings are accurate
3579 // FIXME: Support XF and TF types
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003580 if (!DestWidth) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003581 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003582 return;
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003583 }
3584
3585 QualType NewTy;
3586
3587 if (IntegerMode)
3588 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3589 OldTy->isSignedIntegerType());
3590 else
3591 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3592
3593 if (NewTy.isNull()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003594 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003595 return;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003596 }
3597
Eli Friedman73397492009-03-03 06:41:03 +00003598 if (ComplexMode) {
3599 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003600 }
3601
3602 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003603 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3604 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3605 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003606 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003607
3608 D->addAttr(::new (S.Context)
3609 ModeAttr(Attr.getRange(), S.Context, Name,
3610 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003611}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003612
Chandler Carruth1b03c872011-07-02 00:01:44 +00003613static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003614 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3615 if (!VD->hasGlobalStorage())
3616 S.Diag(Attr.getLoc(),
3617 diag::warn_attribute_requires_functions_or_static_globals)
3618 << Attr.getName();
3619 } else if (!isFunctionOrMethod(D)) {
3620 S.Diag(Attr.getLoc(),
3621 diag::warn_attribute_requires_functions_or_static_globals)
3622 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003623 return;
3624 }
Mike Stumpbf916502009-07-24 19:02:52 +00003625
Michael Han51d8c522013-01-24 16:46:58 +00003626 D->addAttr(::new (S.Context)
3627 NoDebugAttr(Attr.getRange(), S.Context,
3628 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003629}
3630
Chandler Carruth1b03c872011-07-02 00:01:44 +00003631static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003632 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003633 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003634 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003635 return;
3636 }
Mike Stumpbf916502009-07-24 19:02:52 +00003637
Michael Han51d8c522013-01-24 16:46:58 +00003638 D->addAttr(::new (S.Context)
3639 NoInlineAttr(Attr.getRange(), S.Context,
3640 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003641}
3642
Chandler Carruth1b03c872011-07-02 00:01:44 +00003643static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3644 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003645 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003646 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003647 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003648 return;
3649 }
3650
Michael Han51d8c522013-01-24 16:46:58 +00003651 D->addAttr(::new (S.Context)
3652 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3653 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003654}
3655
Chandler Carruth1b03c872011-07-02 00:01:44 +00003656static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003657 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003658 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003660 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003661 return;
3662 }
3663
Michael Han51d8c522013-01-24 16:46:58 +00003664 D->addAttr(::new (S.Context)
3665 CUDAConstantAttr(Attr.getRange(), S.Context,
3666 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003667 } else {
3668 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3669 }
3670}
3671
Chandler Carruth1b03c872011-07-02 00:01:44 +00003672static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003673 if (S.LangOpts.CUDA) {
3674 // check the attribute arguments.
3675 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003676 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3677 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003678 return;
3679 }
3680
Chandler Carruth87c44602011-07-01 23:49:12 +00003681 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003682 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003683 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003684 return;
3685 }
3686
Michael Han51d8c522013-01-24 16:46:58 +00003687 D->addAttr(::new (S.Context)
3688 CUDADeviceAttr(Attr.getRange(), S.Context,
3689 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003690 } else {
3691 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3692 }
3693}
3694
Chandler Carruth1b03c872011-07-02 00:01:44 +00003695static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003696 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003697 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003698 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003699 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003700 return;
3701 }
3702
Chandler Carruth87c44602011-07-01 23:49:12 +00003703 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003704 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003705 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003706 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003707 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3708 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003709 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003710 "void");
3711 } else {
3712 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3713 << FD->getType();
3714 }
3715 return;
3716 }
3717
Michael Han51d8c522013-01-24 16:46:58 +00003718 D->addAttr(::new (S.Context)
3719 CUDAGlobalAttr(Attr.getRange(), S.Context,
3720 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003721 } else {
3722 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3723 }
3724}
3725
Chandler Carruth1b03c872011-07-02 00:01:44 +00003726static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003727 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003728 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003730 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003731 return;
3732 }
3733
Michael Han51d8c522013-01-24 16:46:58 +00003734 D->addAttr(::new (S.Context)
3735 CUDAHostAttr(Attr.getRange(), S.Context,
3736 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003737 } else {
3738 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3739 }
3740}
3741
Chandler Carruth1b03c872011-07-02 00:01:44 +00003742static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003743 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003744 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003745 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003746 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003747 return;
3748 }
3749
Michael Han51d8c522013-01-24 16:46:58 +00003750 D->addAttr(::new (S.Context)
3751 CUDASharedAttr(Attr.getRange(), S.Context,
3752 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003753 } else {
3754 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3755 }
3756}
3757
Chandler Carruth1b03c872011-07-02 00:01:44 +00003758static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003759 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003760 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003761 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003762 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003763 return;
3764 }
Mike Stumpbf916502009-07-24 19:02:52 +00003765
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003766 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003767 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003768 return;
3769 }
Mike Stumpbf916502009-07-24 19:02:52 +00003770
Michael Han51d8c522013-01-24 16:46:58 +00003771 D->addAttr(::new (S.Context)
3772 GNUInlineAttr(Attr.getRange(), S.Context,
3773 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003774}
3775
Chandler Carruth1b03c872011-07-02 00:01:44 +00003776static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003777 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003778
Aaron Ballmanfff32482012-12-09 17:45:41 +00003779 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003780 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003781 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3782 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003783 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003784 return;
3785
Chandler Carruth87c44602011-07-01 23:49:12 +00003786 if (!isa<ObjCMethodDecl>(D)) {
3787 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3788 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003789 return;
3790 }
3791
Chandler Carruth87c44602011-07-01 23:49:12 +00003792 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003793 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003794 D->addAttr(::new (S.Context)
3795 FastCallAttr(Attr.getRange(), S.Context,
3796 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003797 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003798 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003799 D->addAttr(::new (S.Context)
3800 StdCallAttr(Attr.getRange(), S.Context,
3801 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003802 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003803 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003804 D->addAttr(::new (S.Context)
3805 ThisCallAttr(Attr.getRange(), S.Context,
3806 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003807 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003808 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003809 D->addAttr(::new (S.Context)
3810 CDeclAttr(Attr.getRange(), S.Context,
3811 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003812 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003813 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003814 D->addAttr(::new (S.Context)
3815 PascalAttr(Attr.getRange(), S.Context,
3816 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003817 return;
Charles Davise8519c32013-08-30 04:39:01 +00003818 case AttributeList::AT_MSABI:
3819 D->addAttr(::new (S.Context)
3820 MSABIAttr(Attr.getRange(), S.Context,
3821 Attr.getAttributeSpellingListIndex()));
3822 return;
3823 case AttributeList::AT_SysVABI:
3824 D->addAttr(::new (S.Context)
3825 SysVABIAttr(Attr.getRange(), S.Context,
3826 Attr.getAttributeSpellingListIndex()));
3827 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003828 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003829 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003830 switch (CC) {
3831 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003832 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003833 break;
3834 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003835 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003836 break;
3837 default:
3838 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003839 }
3840
Michael Han51d8c522013-01-24 16:46:58 +00003841 D->addAttr(::new (S.Context)
3842 PcsAttr(Attr.getRange(), S.Context, PCS,
3843 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003844 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003845 }
Derek Schuff263366f2012-10-16 22:30:41 +00003846 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003847 D->addAttr(::new (S.Context)
3848 PnaclCallAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003850 return;
Guy Benyei38980082012-12-25 08:53:55 +00003851 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003852 D->addAttr(::new (S.Context)
3853 IntelOclBiccAttr(Attr.getRange(), S.Context,
3854 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003855 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003856
Abramo Bagnarae215f722010-04-30 13:10:51 +00003857 default:
3858 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003859 }
3860}
3861
Chandler Carruth1b03c872011-07-02 00:01:44 +00003862static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003863 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003864}
3865
Guy Benyei1db70402013-03-24 13:58:12 +00003866static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003867 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003868 llvm::APSInt ArgNum(32);
3869 if (E->isTypeDependent() || E->isValueDependent() ||
3870 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003871 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3872 << Attr.getName() << AANT_ArgumentIntegerConstant
3873 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003874 return;
3875 }
3876
3877 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3878 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3879}
3880
Aaron Ballmanfff32482012-12-09 17:45:41 +00003881bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3882 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003883 if (attr.isInvalid())
3884 return true;
3885
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003886 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003887 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003888 attr.setInvalid();
3889 return true;
3890 }
3891
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003892 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3893 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003894 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003895 case AttributeList::AT_CDecl: CC = CC_C; break;
3896 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3897 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3898 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3899 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003900 case AttributeList::AT_MSABI:
3901 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3902 CC_X86_64Win64;
3903 break;
3904 case AttributeList::AT_SysVABI:
3905 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3906 CC_C;
3907 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003908 case AttributeList::AT_Pcs: {
Aaron Ballmanfbf6f5c2013-09-13 17:48:25 +00003909 StringRef StrRef;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003910 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003911 attr.setInvalid();
3912 return true;
3913 }
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003914 if (StrRef == "aapcs") {
3915 CC = CC_AAPCS;
3916 break;
3917 } else if (StrRef == "aapcs-vfp") {
3918 CC = CC_AAPCS_VFP;
3919 break;
3920 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003921
3922 attr.setInvalid();
3923 Diag(attr.getLoc(), diag::err_invalid_pcs);
3924 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003925 }
Derek Schuff263366f2012-10-16 22:30:41 +00003926 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003927 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003928 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003929 }
3930
Aaron Ballman82bfa192012-10-02 14:26:08 +00003931 const TargetInfo &TI = Context.getTargetInfo();
3932 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3933 if (A == TargetInfo::CCCR_Warning) {
3934 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003935
3936 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3937 if (FD)
3938 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3939 TargetInfo::CCMT_NonMember;
3940 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003941 }
3942
John McCall711c52b2011-01-05 12:14:39 +00003943 return false;
3944}
3945
Chandler Carruth1b03c872011-07-02 00:01:44 +00003946static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003947 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003948
3949 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003950 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003951 return;
3952
Chandler Carruth87c44602011-07-01 23:49:12 +00003953 if (!isa<ObjCMethodDecl>(D)) {
3954 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3955 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003956 return;
3957 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003958
Michael Han51d8c522013-01-24 16:46:58 +00003959 D->addAttr(::new (S.Context)
3960 RegparmAttr(Attr.getRange(), S.Context, numParams,
3961 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003962}
3963
3964/// Checks a regparm attribute, returning true if it is ill-formed and
3965/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003966bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3967 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003968 return true;
3969
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003970 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003971 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003972 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003973 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003974
Aaron Ballman624421f2013-08-31 01:11:41 +00003975 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003976 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003977 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003978 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003979 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3980 << Attr.getName() << AANT_ArgumentIntegerConstant
3981 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003982 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003983 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003984 }
3985
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003986 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003987 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003988 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003989 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003990 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003991 }
3992
John McCall711c52b2011-01-05 12:14:39 +00003993 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003994 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003995 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003996 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003997 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003998 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003999 }
4000
John McCall711c52b2011-01-05 12:14:39 +00004001 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004002}
4003
Chandler Carruth1b03c872011-07-02 00:01:44 +00004004static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004005 if (S.LangOpts.CUDA) {
4006 // check the attribute arguments.
4007 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004008 // FIXME: 0 is not okay.
4009 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004010 return;
4011 }
4012
Chandler Carruth87c44602011-07-01 23:49:12 +00004013 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004014 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004015 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004016 return;
4017 }
4018
Aaron Ballman624421f2013-08-31 01:11:41 +00004019 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004020 llvm::APSInt MaxThreads(32);
4021 if (MaxThreadsExpr->isTypeDependent() ||
4022 MaxThreadsExpr->isValueDependent() ||
4023 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004024 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004025 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004026 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004027 return;
4028 }
4029
4030 llvm::APSInt MinBlocks(32);
4031 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004032 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004033 if (MinBlocksExpr->isTypeDependent() ||
4034 MinBlocksExpr->isValueDependent() ||
4035 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004036 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004037 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004038 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004039 return;
4040 }
4041 }
4042
Michael Han51d8c522013-01-24 16:46:58 +00004043 D->addAttr(::new (S.Context)
4044 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4045 MaxThreads.getZExtValue(),
4046 MinBlocks.getZExtValue(),
4047 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004048 } else {
4049 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4050 }
4051}
4052
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004053static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4054 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004055 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004056 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004057 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004058 return;
4059 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004060
4061 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004062 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004063
Aaron Ballman624421f2013-08-31 01:11:41 +00004064 StringRef AttrName = Attr.getName()->getName();
4065 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004066
4067 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4068 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4069 << Attr.getName() << ExpectedFunctionOrMethod;
4070 return;
4071 }
4072
4073 uint64_t ArgumentIdx;
4074 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4075 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004076 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004077 return;
4078
4079 uint64_t TypeTagIdx;
4080 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4081 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004082 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004083 return;
4084
4085 bool IsPointer = (AttrName == "pointer_with_type_tag");
4086 if (IsPointer) {
4087 // Ensure that buffer has a pointer type.
4088 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4089 if (!BufferTy->isPointerType()) {
4090 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004091 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004092 }
4093 }
4094
Michael Han51d8c522013-01-24 16:46:58 +00004095 D->addAttr(::new (S.Context)
4096 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4097 ArgumentIdx, TypeTagIdx, IsPointer,
4098 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004099}
4100
4101static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4102 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004103 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004104 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004105 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004106 return;
4107 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004108
4109 if (!checkAttributeNumArgs(S, Attr, 1))
4110 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004111
Aaron Ballman624421f2013-08-31 01:11:41 +00004112 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithddc2a532013-10-31 21:23:20 +00004113 TypeSourceInfo *MatchingCTypeLoc = 0;
4114 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4115 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004116
Michael Han51d8c522013-01-24 16:46:58 +00004117 D->addAttr(::new (S.Context)
4118 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithddc2a532013-10-31 21:23:20 +00004119 MatchingCTypeLoc,
Michael Han51d8c522013-01-24 16:46:58 +00004120 Attr.getLayoutCompatible(),
4121 Attr.getMustBeNull(),
4122 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004123}
4124
Chris Lattner0744e5f2008-06-29 00:23:49 +00004125//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004126// Checker-specific attribute handlers.
4127//===----------------------------------------------------------------------===//
4128
John McCallc7ad3812011-01-25 03:31:58 +00004129static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004130 return type->isDependentType() ||
4131 type->isObjCObjectPointerType() ||
4132 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004133}
4134static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004135 return type->isDependentType() ||
4136 type->isPointerType() ||
4137 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004138}
4139
Chandler Carruth1b03c872011-07-02 00:01:44 +00004140static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004141 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004142 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004143 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004144 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004145 return;
4146 }
4147
4148 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004149 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004150 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4151 cf = false;
4152 } else {
4153 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4154 cf = true;
4155 }
4156
4157 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004158 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004159 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004160 return;
4161 }
4162
4163 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004164 param->addAttr(::new (S.Context)
4165 CFConsumedAttr(Attr.getRange(), S.Context,
4166 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004167 else
Michael Han51d8c522013-01-24 16:46:58 +00004168 param->addAttr(::new (S.Context)
4169 NSConsumedAttr(Attr.getRange(), S.Context,
4170 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004171}
4172
Chandler Carruth1b03c872011-07-02 00:01:44 +00004173static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4174 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004175 if (!isa<ObjCMethodDecl>(D)) {
4176 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004177 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004178 return;
4179 }
4180
Michael Han51d8c522013-01-24 16:46:58 +00004181 D->addAttr(::new (S.Context)
4182 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4183 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004184}
4185
Chandler Carruth1b03c872011-07-02 00:01:44 +00004186static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4187 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004188
John McCallc7ad3812011-01-25 03:31:58 +00004189 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004190
Chandler Carruth87c44602011-07-01 23:49:12 +00004191 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004192 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004193 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004194 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004195 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004196 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4197 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004198 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004199 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004200 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004201 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004202 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004203 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004204 return;
4205 }
Mike Stumpbf916502009-07-24 19:02:52 +00004206
John McCallc7ad3812011-01-25 03:31:58 +00004207 bool typeOK;
4208 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004209 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004210 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004211 case AttributeList::AT_NSReturnsAutoreleased:
4212 case AttributeList::AT_NSReturnsRetained:
4213 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004214 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4215 cf = false;
4216 break;
4217
Sean Hunt8e083e72012-06-19 23:57:03 +00004218 case AttributeList::AT_CFReturnsRetained:
4219 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004220 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4221 cf = true;
4222 break;
4223 }
4224
4225 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004226 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004227 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004228 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004229 }
Mike Stumpbf916502009-07-24 19:02:52 +00004230
Chandler Carruth87c44602011-07-01 23:49:12 +00004231 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004232 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004233 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004234 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004235 D->addAttr(::new (S.Context)
4236 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4237 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004238 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004239 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004240 D->addAttr(::new (S.Context)
4241 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4242 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004243 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004244 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004245 D->addAttr(::new (S.Context)
4246 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4247 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004248 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004249 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004250 D->addAttr(::new (S.Context)
4251 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4252 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004253 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004254 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004255 D->addAttr(::new (S.Context)
4256 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4257 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004258 return;
4259 };
4260}
4261
John McCalldc7c5ad2011-07-22 08:53:00 +00004262static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4263 const AttributeList &attr) {
Fariborz Jahanianaf1d28f2013-09-19 17:52:50 +00004264 const int EP_ObjCMethod = 1;
4265 const int EP_ObjCProperty = 2;
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004266
John McCalldc7c5ad2011-07-22 08:53:00 +00004267 SourceLocation loc = attr.getLoc();
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004268 QualType resultType;
4269
John McCalldc7c5ad2011-07-22 08:53:00 +00004270 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4271
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004272 if (!method) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004273 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4274 if (!property) {
4275 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4276 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4277 return;
4278 }
4279 resultType = property->getType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004280 }
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004281 else
4282 // Check that the method returns a normal pointer.
4283 resultType = method->getResultType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004284
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004285 if (!resultType->isReferenceType() &&
4286 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004287 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalldc7c5ad2011-07-22 08:53:00 +00004288 << SourceRange(loc)
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004289 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4290 << /*non-retainable pointer*/ 2;
John McCalldc7c5ad2011-07-22 08:53:00 +00004291
4292 // Drop the attribute.
4293 return;
4294 }
4295
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004296 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004297 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4298 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004299}
4300
Fariborz Jahanian84101132012-09-07 23:46:23 +00004301static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4302 const AttributeList &attr) {
4303 SourceLocation loc = attr.getLoc();
4304 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4305
4306 if (!method) {
4307 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4308 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4309 return;
4310 }
4311 DeclContext *DC = method->getDeclContext();
4312 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4313 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4314 << attr.getName() << 0;
4315 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4316 return;
4317 }
4318 if (method->getMethodFamily() == OMF_dealloc) {
4319 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4320 << attr.getName() << 1;
4321 return;
4322 }
4323
Michael Han51d8c522013-01-24 16:46:58 +00004324 method->addAttr(::new (S.Context)
4325 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4326 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004327}
4328
John McCall8dfac0b2011-09-30 05:12:12 +00004329/// Handle cf_audited_transfer and cf_unknown_transfer.
4330static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4331 if (!isa<FunctionDecl>(D)) {
4332 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004333 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004334 return;
4335 }
4336
Sean Hunt8e083e72012-06-19 23:57:03 +00004337 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004338
4339 // Check whether there's a conflicting attribute already present.
4340 Attr *Existing;
4341 if (IsAudited) {
4342 Existing = D->getAttr<CFUnknownTransferAttr>();
4343 } else {
4344 Existing = D->getAttr<CFAuditedTransferAttr>();
4345 }
4346 if (Existing) {
4347 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4348 << A.getName()
4349 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4350 << A.getRange() << Existing->getRange();
4351 return;
4352 }
4353
4354 // All clear; add the attribute.
4355 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004356 D->addAttr(::new (S.Context)
4357 CFAuditedTransferAttr(A.getRange(), S.Context,
4358 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004359 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004360 D->addAttr(::new (S.Context)
4361 CFUnknownTransferAttr(A.getRange(), S.Context,
4362 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004363 }
4364}
4365
John McCallfe98da02011-09-29 07:17:38 +00004366static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4367 const AttributeList &Attr) {
4368 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4369 if (!RD || RD->isUnion()) {
4370 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004371 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004372 }
4373
Aaron Ballman624421f2013-08-31 01:11:41 +00004374 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004375
4376 // In Objective-C, verify that the type names an Objective-C type.
4377 // We don't want to check this outside of ObjC because people sometimes
4378 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004379 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004380 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004381 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004382 Sema::LookupOrdinaryName);
4383 if (S.LookupName(R, Sc)) {
4384 NamedDecl *Target = R.getFoundDecl();
4385 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4386 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4387 S.Diag(Target->getLocStart(), diag::note_declared_at);
4388 }
4389 }
4390 }
4391
Michael Han51d8c522013-01-24 16:46:58 +00004392 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004393 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004394 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004395}
4396
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004397static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4398 const AttributeList &Attr) {
4399 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
4400 QualType T = TD->getUnderlyingType();
4401 if (T->isPointerType()) {
4402 T = T->getPointeeType();
4403 if (T->isRecordType()) {
4404 RecordDecl *RD = T->getAs<RecordType>()->getDecl();
4405 if (!RD || RD->isUnion()) {
Fariborz Jahanian4b777c02013-11-15 23:14:45 +00004406 S.Diag(D->getLocStart(), diag::err_objc_bridge_not_pointert_to_struct)
4407 << Attr.getRange();
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004408 return;
4409 }
Fariborz Jahanian4b777c02013-11-15 23:14:45 +00004410 } else {
4411 S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_pointert_to_struct)
4412 << Attr.getRange();
4413 return;
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004414 }
4415 } else {
Fariborz Jahanian4b777c02013-11-15 23:14:45 +00004416 S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_pointert_to_struct)
4417 << Attr.getRange();
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004418 return;
4419 }
4420 // Check for T being a CFType goes here.
Fariborz Jahanian7f822972013-11-14 00:43:05 +00004421 if (!isTollFreeBridgeCFRefType(TD, S.Context)) {
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004422 S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_cftype);
4423 return;
4424 }
4425 }
4426 else {
4427 S.Diag(D->getLocStart(), diag::err_objc_bridge_attribute);
4428 return;
4429 }
4430
4431 if (Attr.getNumArgs() != 1) {
4432 S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
4433 return;
4434 }
4435 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
4436 if (!Parm) {
4437 S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
4438 return;
4439 }
4440
4441 D->addAttr(::new (S.Context)
4442 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
4443 Attr.getAttributeSpellingListIndex()));
4444}
4445
Chandler Carruth1b03c872011-07-02 00:01:44 +00004446static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4447 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004448 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004449
Chandler Carruth87c44602011-07-01 23:49:12 +00004450 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004451 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004452}
4453
Chandler Carruth1b03c872011-07-02 00:01:44 +00004454static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4455 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004456 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004457 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004458 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004459 return;
4460 }
4461
Chandler Carruth87c44602011-07-01 23:49:12 +00004462 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004463 QualType type = vd->getType();
4464
4465 if (!type->isDependentType() &&
4466 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004467 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004468 << type;
4469 return;
4470 }
4471
4472 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4473
4474 // If we have no lifetime yet, check the lifetime we're presumably
4475 // going to infer.
4476 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4477 lifetime = type->getObjCARCImplicitLifetime();
4478
4479 switch (lifetime) {
4480 case Qualifiers::OCL_None:
4481 assert(type->isDependentType() &&
4482 "didn't infer lifetime for non-dependent type?");
4483 break;
4484
4485 case Qualifiers::OCL_Weak: // meaningful
4486 case Qualifiers::OCL_Strong: // meaningful
4487 break;
4488
4489 case Qualifiers::OCL_ExplicitNone:
4490 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004491 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004492 << (lifetime == Qualifiers::OCL_Autoreleasing);
4493 break;
4494 }
4495
Chandler Carruth87c44602011-07-01 23:49:12 +00004496 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004497 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4498 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004499}
4500
Francois Pichet11542142010-12-19 06:50:37 +00004501//===----------------------------------------------------------------------===//
4502// Microsoft specific attribute handlers.
4503//===----------------------------------------------------------------------===//
4504
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004505// Check if MS extensions or some other language extensions are enabled. If
4506// not, issue a diagnostic that the given attribute is unused.
4507static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4508 bool OtherExtension = false) {
4509 if (S.LangOpts.MicrosoftExt || OtherExtension)
4510 return true;
4511 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4512 return false;
4513}
4514
Chandler Carruth1b03c872011-07-02 00:01:44 +00004515static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004516 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4517 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004518
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004519 StringRef StrRef;
4520 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00004521 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004522 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004523
David Majnemerbb0ed292013-08-09 08:56:20 +00004524 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4525 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004526 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4527 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004528
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004529 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004530 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004531 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004532 return;
4533 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004534
David Majnemerbb0ed292013-08-09 08:56:20 +00004535 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004536 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004537 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004538 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004539 return;
4540 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004541 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004542 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004543 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004544 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004545 }
Francois Pichet11542142010-12-19 06:50:37 +00004546
David Majnemerbb0ed292013-08-09 08:56:20 +00004547 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4548 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004549}
4550
John McCallc052dbb2012-05-22 21:28:12 +00004551static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004552 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004553 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004554
4555 AttributeList::Kind Kind = Attr.getKind();
4556 if (Kind == AttributeList::AT_SingleInheritance)
4557 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004558 ::new (S.Context)
4559 SingleInheritanceAttr(Attr.getRange(), S.Context,
4560 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004561 else if (Kind == AttributeList::AT_MultipleInheritance)
4562 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004563 ::new (S.Context)
4564 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4565 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004566 else if (Kind == AttributeList::AT_VirtualInheritance)
4567 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004568 ::new (S.Context)
4569 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4570 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004571}
4572
4573static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004574 if (!checkMicrosoftExt(S, Attr))
4575 return;
4576
4577 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004578 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004579 D->addAttr(
4580 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4581 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004582}
4583
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004584static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004585 if (!checkMicrosoftExt(S, Attr))
4586 return;
4587 D->addAttr(::new (S.Context)
4588 ForceInlineAttr(Attr.getRange(), S.Context,
4589 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004590}
4591
Reid Klecknera7225342013-05-20 14:02:37 +00004592static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4593 if (!checkMicrosoftExt(S, Attr))
4594 return;
4595 // Check linkage after possibly merging declaratinos. See
4596 // checkAttributesAfterMerging().
4597 D->addAttr(::new (S.Context)
4598 SelectAnyAttr(Attr.getRange(), S.Context,
4599 Attr.getAttributeSpellingListIndex()));
4600}
4601
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004602/// Handles semantic checking for features that are common to all attributes,
4603/// such as checking whether a parameter was properly specified, or the correct
4604/// number of arguments were passed, etc.
4605static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4606 const AttributeList &Attr) {
4607 // Several attributes carry different semantics than the parsing requires, so
4608 // those are opted out of the common handling.
4609 //
4610 // We also bail on unknown and ignored attributes because those are handled
4611 // as part of the target-specific handling logic.
4612 if (Attr.hasCustomParsing() ||
4613 Attr.getKind() == AttributeList::UnknownAttribute ||
4614 Attr.getKind() == AttributeList::IgnoredAttribute)
4615 return false;
4616
4617 // If there are no optional arguments, then checking for the argument count
4618 // is trivial.
4619 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4620 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4621 return true;
4622 return false;
4623}
4624
Ted Kremenekb71368d2009-05-09 02:44:38 +00004625//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004626// Top Level Sema Entry Points
4627//===----------------------------------------------------------------------===//
4628
Richard Smith4a97b8e2013-08-29 00:47:48 +00004629/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4630/// the attribute applies to decls. If the attribute is a type attribute, just
4631/// silently ignore it if a GNU attribute.
4632static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4633 const AttributeList &Attr,
4634 bool IncludeCXX11Attributes) {
4635 if (Attr.isInvalid())
4636 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004637
Richard Smith4a97b8e2013-08-29 00:47:48 +00004638 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4639 // instead.
4640 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4641 return;
4642
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004643 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4644 return;
4645
Chris Lattner803d0802008-06-29 00:43:07 +00004646 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004647 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4648 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4649 case AttributeList::AT_IBOutletCollection:
4650 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004651 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004652 case AttributeList::AT_ObjCGC:
4653 case AttributeList::AT_VectorSize:
4654 case AttributeList::AT_NeonVectorType:
4655 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004656 case AttributeList::AT_Ptr32:
4657 case AttributeList::AT_Ptr64:
4658 case AttributeList::AT_SPtr:
4659 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004660 // Ignore these, these are type attributes, handled by
4661 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004662 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004663 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4664 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4665 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4666 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004667 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004668 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004669 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004670 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004671 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4672 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4673 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004674 handleDependencyAttr(S, scope, D, Attr);
4675 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004676 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4677 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4678 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004679 case AttributeList::AT_CXX11NoReturn:
4680 handleCXX11NoReturnAttr(S, D, Attr);
4681 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004682 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004683 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004684 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004685 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4686 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004687 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004688 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004689 case AttributeList::AT_MinSize:
4690 handleMinSizeAttr(S, D, Attr);
4691 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004692 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4693 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4694 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004695 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4696 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004697 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4698 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004699 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004700 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004701 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4702 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004703 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004704 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4705 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004706 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004707 case AttributeList::AT_ownership_returns:
4708 case AttributeList::AT_ownership_takes:
4709 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004710 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004711 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4712 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4713 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4714 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4715 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4716 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4717 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004718
Sean Hunt8e083e72012-06-19 23:57:03 +00004719 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004720 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004721 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004722 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004723
Sean Hunt8e083e72012-06-19 23:57:03 +00004724 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004725 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4726
Fariborz Jahanian84101132012-09-07 23:46:23 +00004727 case AttributeList::AT_ObjCRequiresSuper:
4728 handleObjCRequiresSuperAttr(S, D, Attr); break;
4729
Sean Hunt8e083e72012-06-19 23:57:03 +00004730 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004731 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004732
4733 case AttributeList::AT_ObjCBridge:
4734 handleObjCBridgeAttr(S, scope, D, Attr); break;
John McCallfe98da02011-09-29 07:17:38 +00004735
Sean Hunt8e083e72012-06-19 23:57:03 +00004736 case AttributeList::AT_CFAuditedTransfer:
4737 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004738 handleCFTransferAttr(S, D, Attr); break;
4739
Ted Kremenekb71368d2009-05-09 02:44:38 +00004740 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004741 case AttributeList::AT_CFConsumed:
4742 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4743 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004744 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004745
Sean Hunt8e083e72012-06-19 23:57:03 +00004746 case AttributeList::AT_NSReturnsAutoreleased:
4747 case AttributeList::AT_NSReturnsNotRetained:
4748 case AttributeList::AT_CFReturnsNotRetained:
4749 case AttributeList::AT_NSReturnsRetained:
4750 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004751 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004752
Tanya Lattner0df579e2012-07-09 22:06:01 +00004753 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004754 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004755 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004756
Joey Gouly37453b92013-03-08 09:42:32 +00004757 case AttributeList::AT_VecTypeHint:
4758 handleVecTypeHint(S, D, Attr); break;
4759
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004761 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004762
Sean Hunt8e083e72012-06-19 23:57:03 +00004763 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4764 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4765 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004766 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004767 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004768 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004769 handleArcWeakrefUnavailableAttr (S, D, Attr);
4770 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004771 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004772 handleObjCRootClassAttr(S, D, Attr);
4773 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004774 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004775 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004776 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004777 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4778 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004779 handleReturnsTwiceAttr(S, D, Attr);
4780 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004781 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004782 case AttributeList::AT_Visibility:
4783 handleVisibilityAttr(S, D, Attr, false);
4784 break;
4785 case AttributeList::AT_TypeVisibility:
4786 handleVisibilityAttr(S, D, Attr, true);
4787 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004788 case AttributeList::AT_WarnUnused:
4789 handleWarnUnusedAttr(S, D, Attr);
4790 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004791 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004792 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004793 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4794 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4795 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4796 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004797 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004798 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004799 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004800 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004801 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004803 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004804 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004805 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4806 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4807 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4808 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4809 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4810 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4811 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4812 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4813 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004814 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004815 // Just ignore
4816 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004818 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004819 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004820 case AttributeList::AT_StdCall:
4821 case AttributeList::AT_CDecl:
4822 case AttributeList::AT_FastCall:
4823 case AttributeList::AT_ThisCall:
4824 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004825 case AttributeList::AT_MSABI:
4826 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004827 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004828 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004829 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004830 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004831 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004832 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004833 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004834 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004835 case AttributeList::AT_OpenCLImageAccess:
4836 handleOpenCLImageAccessAttr(S, D, Attr);
4837 break;
John McCallc052dbb2012-05-22 21:28:12 +00004838
4839 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004840 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004841 handleMsStructAttr(S, D, Attr);
4842 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004843 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004844 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004845 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004846 case AttributeList::AT_SingleInheritance:
4847 case AttributeList::AT_MultipleInheritance:
4848 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004849 handleInheritanceAttr(S, D, Attr);
4850 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004851 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004852 handlePortabilityAttr(S, D, Attr);
4853 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004854 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004855 handleForceInlineAttr(S, D, Attr);
4856 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004857 case AttributeList::AT_SelectAny:
4858 handleSelectAnyAttr(S, D, Attr);
4859 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004860
4861 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004862 case AttributeList::AT_AssertExclusiveLock:
4863 handleAssertExclusiveLockAttr(S, D, Attr);
4864 break;
4865 case AttributeList::AT_AssertSharedLock:
4866 handleAssertSharedLockAttr(S, D, Attr);
4867 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004868 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004869 handleGuardedVarAttr(S, D, Attr);
4870 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004871 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004872 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004873 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004874 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004875 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004876 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004877 case AttributeList::AT_NoSanitizeAddress:
4878 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004879 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004880 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004881 handleNoThreadSafetyAnalysis(S, D, Attr);
4882 break;
4883 case AttributeList::AT_NoSanitizeThread:
4884 handleNoSanitizeThread(S, D, Attr);
4885 break;
4886 case AttributeList::AT_NoSanitizeMemory:
4887 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004888 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004889 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004890 handleLockableAttr(S, D, Attr);
4891 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004892 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004893 handleGuardedByAttr(S, D, Attr);
4894 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004895 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004896 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004897 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004898 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004899 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004900 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004901 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004902 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004903 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004904 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004905 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004906 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004907 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004908 handleLockReturnedAttr(S, D, Attr);
4909 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004910 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004911 handleLocksExcludedAttr(S, D, Attr);
4912 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004913 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004914 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004915 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004916 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004917 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004918 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004919 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004920 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004921 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004922 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004923 handleUnlockFunAttr(S, D, Attr);
4924 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004925 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004926 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004927 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004928 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004929 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004930 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004931
DeLesley Hutchins1bf63432013-10-11 22:30:48 +00004932 // Consumed analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004933 case AttributeList::AT_Consumable:
4934 handleConsumableAttr(S, D, Attr);
4935 break;
DeLesley Hutchins66540852013-10-04 21:28:06 +00004936 case AttributeList::AT_CallableWhen:
4937 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004938 break;
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00004939 case AttributeList::AT_ParamTypestate:
4940 handleParamTypestateAttr(S, D, Attr);
4941 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004942 case AttributeList::AT_ReturnTypestate:
4943 handleReturnTypestateAttr(S, D, Attr);
4944 break;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004945 case AttributeList::AT_SetTypestate:
4946 handleSetTypestateAttr(S, D, Attr);
4947 break;
Chris Wailes0e429f12013-10-29 20:28:41 +00004948 case AttributeList::AT_TestTypestate:
4949 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004950 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004951
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004952 // Type safety attributes.
4953 case AttributeList::AT_ArgumentWithTypeTag:
4954 handleArgumentWithTypeTagAttr(S, D, Attr);
4955 break;
4956 case AttributeList::AT_TypeTagForDatatype:
4957 handleTypeTagForDatatypeAttr(S, D, Attr);
4958 break;
4959
Chris Lattner803d0802008-06-29 00:43:07 +00004960 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004961 // Ask target about the attribute.
4962 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4963 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004964 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4965 diag::warn_unhandled_ms_attribute_ignored :
4966 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004967 break;
4968 }
4969}
4970
4971/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4972/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004973void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004974 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004975 bool IncludeCXX11Attributes) {
4976 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004977 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004978
4979 // GCC accepts
4980 // static int a9 __attribute__((weakref));
4981 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004982 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004983 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004984 cast<NamedDecl>(D)->getNameAsString();
4985 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004986 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004987 }
4988}
4989
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004990// Annotation attributes are the only attributes allowed after an access
4991// specifier.
4992bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4993 const AttributeList *AttrList) {
4994 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004995 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004996 handleAnnotateAttr(*this, ASDecl, *l);
4997 } else {
4998 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4999 return true;
5000 }
5001 }
5002
5003 return false;
5004}
5005
John McCalle82247a2011-10-01 05:17:03 +00005006/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5007/// contains any decl attributes that we should warn about.
5008static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5009 for ( ; A; A = A->getNext()) {
5010 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005011 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005012 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5013
5014 if (A->getKind() == AttributeList::UnknownAttribute) {
5015 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5016 << A->getName() << A->getRange();
5017 } else {
5018 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5019 << A->getName() << A->getRange();
5020 }
5021 }
5022}
5023
5024/// checkUnusedDeclAttributes - Given a declarator which is not being
5025/// used to build a declaration, complain about any decl attributes
5026/// which might be lying around on it.
5027void Sema::checkUnusedDeclAttributes(Declarator &D) {
5028 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5029 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5030 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5031 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5032}
5033
Ryan Flynne25ff832009-07-30 03:15:39 +00005034/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005035/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005036NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5037 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005038 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005039 NamedDecl *NewD = 0;
5040 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005041 FunctionDecl *NewFD;
5042 // FIXME: Missing call to CheckFunctionDeclaration().
5043 // FIXME: Mangling?
5044 // FIXME: Is the qualifier info correct?
5045 // FIXME: Is the DeclContext correct?
5046 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5047 Loc, Loc, DeclarationName(II),
5048 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005049 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005050 FD->hasPrototype(),
5051 false/*isConstexprSpecified*/);
5052 NewD = NewFD;
5053
5054 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005055 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005056
5057 // Fake up parameter variables; they are declared as if this were
5058 // a typedef.
5059 QualType FDTy = FD->getType();
5060 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5061 SmallVector<ParmVarDecl*, 16> Params;
5062 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5063 AE = FT->arg_type_end(); AI != AE; ++AI) {
5064 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5065 Param->setScopeInfo(0, Params.size());
5066 Params.push_back(Param);
5067 }
David Blaikie4278c652011-09-21 18:16:56 +00005068 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005069 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005070 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5071 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005072 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005073 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005074 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005075 if (VD->getQualifier()) {
5076 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005077 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005078 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005079 }
5080 return NewD;
5081}
5082
James Dennett1dfbd922012-06-14 21:40:34 +00005083/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005084/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005085void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005086 if (W.getUsed()) return; // only do this once
5087 W.setUsed(true);
5088 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5089 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005090 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005091 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5092 NDId->getName()));
5093 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005094 WeakTopLevelDecl.push_back(NewD);
5095 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5096 // to insert Decl at TU scope, sorry.
5097 DeclContext *SavedContext = CurContext;
5098 CurContext = Context.getTranslationUnitDecl();
5099 PushOnScopeChains(NewD, S);
5100 CurContext = SavedContext;
5101 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005102 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005103 }
5104}
5105
Rafael Espindola65611bf2013-03-02 21:41:48 +00005106void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5107 // It's valid to "forward-declare" #pragma weak, in which case we
5108 // have to do this.
5109 LoadExternalWeakUndeclaredIdentifiers();
5110 if (!WeakUndeclaredIdentifiers.empty()) {
5111 NamedDecl *ND = NULL;
5112 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5113 if (VD->isExternC())
5114 ND = VD;
5115 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5116 if (FD->isExternC())
5117 ND = FD;
5118 if (ND) {
5119 if (IdentifierInfo *Id = ND->getIdentifier()) {
5120 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5121 = WeakUndeclaredIdentifiers.find(Id);
5122 if (I != WeakUndeclaredIdentifiers.end()) {
5123 WeakInfo W = I->second;
5124 DeclApplyPragmaWeak(S, ND, W);
5125 WeakUndeclaredIdentifiers[Id] = W;
5126 }
5127 }
5128 }
5129 }
5130}
5131
Chris Lattner0744e5f2008-06-29 00:23:49 +00005132/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5133/// it, apply them to D. This is a bit tricky because PD can have attributes
5134/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005135void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005136 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005137 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005138 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005139
Chris Lattner0744e5f2008-06-29 00:23:49 +00005140 // Walk the declarator structure, applying decl attributes that were in a type
5141 // position to the decl itself. This handles cases like:
5142 // int *__attr__(x)** D;
5143 // when X is a decl attribute.
5144 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5145 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005146 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005147
Chris Lattner0744e5f2008-06-29 00:23:49 +00005148 // Finally, apply any attributes on the decl itself.
5149 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005150 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005151}
John McCall54abf7d2009-11-04 02:18:39 +00005152
John McCallf85e1932011-06-15 23:02:42 +00005153/// Is the given declaration allowed to use a forbidden type?
5154static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5155 // Private ivars are always okay. Unfortunately, people don't
5156 // always properly make their ivars private, even in system headers.
5157 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005158 // Function declarations in sys headers will be marked unavailable.
5159 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5160 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005161 return false;
5162
5163 // Require it to be declared in a system header.
5164 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5165}
5166
5167/// Handle a delayed forbidden-type diagnostic.
5168static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5169 Decl *decl) {
5170 if (decl && isForbiddenTypeAllowed(S, decl)) {
5171 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5172 "this system declaration uses an unsupported type"));
5173 return;
5174 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005175 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005176 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005177 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005178 // kind of forbidden type messages on unavailable functions.
5179 if (FD->hasAttr<UnavailableAttr>() &&
5180 diag.getForbiddenTypeDiagnostic() ==
5181 diag::err_arc_array_param_no_ownership) {
5182 diag.Triggered = true;
5183 return;
5184 }
5185 }
John McCallf85e1932011-06-15 23:02:42 +00005186
5187 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5188 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5189 diag.Triggered = true;
5190}
5191
John McCall92576642012-05-07 06:16:41 +00005192void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5193 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005194 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005195 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005196
John McCall92576642012-05-07 06:16:41 +00005197 // When delaying diagnostics to run in the context of a parsed
5198 // declaration, we only want to actually emit anything if parsing
5199 // succeeds.
5200 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005201
John McCall92576642012-05-07 06:16:41 +00005202 // We emit all the active diagnostics in this pool or any of its
5203 // parents. In general, we'll get one pool for the decl spec
5204 // and a child pool for each declarator; in a decl group like:
5205 // deprecated_typedef foo, *bar, baz();
5206 // only the declarator pops will be passed decls. This is correct;
5207 // we really do need to consider delayed diagnostics from the decl spec
5208 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005209 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005210 do {
John McCall13489672012-05-07 06:16:58 +00005211 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005212 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5213 // This const_cast is a bit lame. Really, Triggered should be mutable.
5214 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005215 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005216 continue;
5217
John McCalleee1d542011-02-14 07:13:47 +00005218 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005219 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005220 // Don't bother giving deprecation diagnostics if the decl is invalid.
5221 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005222 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005223 break;
5224
5225 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005226 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005227 break;
John McCallf85e1932011-06-15 23:02:42 +00005228
5229 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005230 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005231 break;
John McCall2f514482010-01-27 03:50:35 +00005232 }
5233 }
John McCall92576642012-05-07 06:16:41 +00005234 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005235}
5236
John McCall13489672012-05-07 06:16:58 +00005237/// Given a set of delayed diagnostics, re-emit them as if they had
5238/// been delayed in the current context instead of in the given pool.
5239/// Essentially, this just moves them to the current pool.
5240void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5241 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5242 assert(curPool && "re-emitting in undelayed context not supported");
5243 curPool->steal(pool);
5244}
5245
John McCall54abf7d2009-11-04 02:18:39 +00005246static bool isDeclDeprecated(Decl *D) {
5247 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005248 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005249 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005250 // A category implicitly has the availability of the interface.
5251 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5252 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005253 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5254 return false;
5255}
5256
Eli Friedmanc3b23082012-08-08 21:52:41 +00005257static void
5258DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5259 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005260 const ObjCInterfaceDecl *UnknownObjCClass,
5261 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005262 DeclarationName Name = D->getDeclName();
5263 if (!Message.empty()) {
5264 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5265 S.Diag(D->getLocation(),
5266 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5267 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005268 if (ObjCPropery)
5269 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5270 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005271 } else if (!UnknownObjCClass) {
5272 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5273 S.Diag(D->getLocation(),
5274 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5275 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005276 if (ObjCPropery)
5277 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5278 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005279 } else {
5280 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5281 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5282 }
5283}
5284
John McCall9c3087b2010-08-26 02:13:20 +00005285void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005286 Decl *Ctx) {
5287 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005288 return;
5289
John McCall2f514482010-01-27 03:50:35 +00005290 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005291 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5292 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005293 DD.getUnknownObjCClass(),
5294 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005295}
5296
Chris Lattner5f9e2722011-07-23 10:55:15 +00005297void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005298 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005299 const ObjCInterfaceDecl *UnknownObjCClass,
5300 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005301 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005302 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005303 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5304 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005305 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005306 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005307 return;
5308 }
5309
5310 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005311 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005312 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005313 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005314}