blob: 46a85c59b521819a0c87786d9eeb398556d9f18e [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 Jahanian933a8942013-11-13 23:59:17 +0000210static inline bool isCFRefType(TypedefNameDecl *TD, ASTContext &Ctx) {
211 StringRef TDName = TD->getIdentifier()->getName();
212 return ((TDName.startswith("CF") || TDName.startswith("CG")) &&
213 (TDName.rfind("Ref") != StringRef::npos));
214}
215
Richard Smithddc2a532013-10-31 21:23:20 +0000216static unsigned getNumAttributeArgs(const AttributeList &Attr) {
217 // FIXME: Include the type in the argument list.
218 return Attr.getNumArgs() + Attr.hasParsedType();
219}
220
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000221/// \brief Check if the attribute has exactly as many args as Num. May
222/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000223static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithddc2a532013-10-31 21:23:20 +0000224 unsigned Num) {
225 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000226 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
227 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000228 return false;
229 }
230
231 return true;
232}
233
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000234
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000235/// \brief Check if the attribute has at least as many args as Num. May
236/// output an error.
237static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithddc2a532013-10-31 21:23:20 +0000238 unsigned Num) {
239 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
241 return false;
242 }
243
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000244 return true;
245}
246
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000247/// \brief Check if IdxExpr is a valid argument index for a function or
248/// instance method D. May output an error.
249///
250/// \returns true if IdxExpr is a valid index.
251static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
252 StringRef AttrName,
253 SourceLocation AttrLoc,
254 unsigned AttrArgNum,
255 const Expr *IdxExpr,
256 uint64_t &Idx)
257{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000258 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000259
260 // In C++ the implicit 'this' function parameter also counts.
261 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000262 bool HP = hasFunctionProto(D);
263 bool HasImplicitThisParam = isInstanceMethod(D);
264 bool IV = HP && isFunctionOrMethodVariadic(D);
265 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
266 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000267
268 llvm::APSInt IdxInt;
269 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
270 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000271 std::string Name = std::string("'") + AttrName.str() + std::string("'");
272 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000273 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000274 return false;
275 }
276
277 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000278 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000279 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
280 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
281 return false;
282 }
283 Idx--; // Convert to zero-based.
284 if (HasImplicitThisParam) {
285 if (Idx == 0) {
286 S.Diag(AttrLoc,
287 diag::err_attribute_invalid_implicit_this_argument)
288 << AttrName << IdxExpr->getSourceRange();
289 return false;
290 }
291 --Idx;
292 }
293
294 return true;
295}
296
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000297/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
298/// If not emit an error and return false. If the argument is an identifier it
299/// will emit an error with a fixit hint and treat it as if it was a string
300/// literal.
Tim Northoverf8aebef2013-10-01 14:34:18 +0000301bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
302 unsigned ArgNum, StringRef &Str,
Tim Northover64eac852013-10-01 14:34:25 +0000303 SourceLocation *ArgLocation) {
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000304 // Look for identifiers. If we have one emit a hint to fix it to a literal.
305 if (Attr.isArgIdent(ArgNum)) {
306 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northoverf8aebef2013-10-01 14:34:18 +0000307 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000308 << Attr.getName() << AANT_ArgumentString
309 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northoverf8aebef2013-10-01 14:34:18 +0000310 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000311 Str = Loc->Ident->getName();
312 if (ArgLocation)
313 *ArgLocation = Loc->Loc;
314 return true;
315 }
316
317 // Now check for an actual string literal.
318 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
319 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
320 if (ArgLocation)
321 *ArgLocation = ArgExpr->getLocStart();
322
323 if (!Literal || !Literal->isAscii()) {
Tim Northoverf8aebef2013-10-01 14:34:18 +0000324 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman422ac9e2013-09-13 19:35:18 +0000325 << Attr.getName() << AANT_ArgumentString;
326 return false;
327 }
328
329 Str = Literal->getString();
330 return true;
331}
332
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000333///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000334/// \brief Check if passed in Decl is a field or potentially shared global var
335/// \return true if the Decl is a field or potentially shared global variable
336///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000337static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000338 if (isa<FieldDecl>(D))
339 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000340 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000341 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000342
343 return false;
344}
345
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000346/// \brief Check if the passed-in expression is of type int or bool.
347static bool isIntOrBool(Expr *Exp) {
348 QualType QT = Exp->getType();
349 return QT->isBooleanType() || QT->isIntegerType();
350}
351
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000352
353// Check to see if the type is a smart pointer of some kind. We assume
354// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000355static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
356 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
357 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000358 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000359 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000360
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000361 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
362 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000363 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000364 return false;
365
366 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000367}
368
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000369/// \brief Check if passed in Decl is a pointer type.
370/// Note that this function may produce an error message.
371/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000372static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
373 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000374 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000375 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000376 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000377 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000378
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000379 if (const RecordType *RT = QT->getAs<RecordType>()) {
380 // If it's an incomplete type, it could be a smart pointer; skip it.
381 // (We don't want to force template instantiation if we can avoid it,
382 // since that would alter the order in which templates are instantiated.)
383 if (RT->isIncompleteType())
384 return true;
385
386 if (threadSafetyCheckIsSmartPointer(S, RT))
387 return true;
388 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000389
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000390 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000391 << Attr.getName()->getName() << QT;
392 } else {
393 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
394 << Attr.getName();
395 }
396 return false;
397}
398
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000399/// \brief Checks that the passed in QualType either is of RecordType or points
400/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000401static const RecordType *getRecordType(QualType QT) {
402 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000403 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000404
405 // Now check if we point to record type.
406 if (const PointerType *PT = QT->getAs<PointerType>())
407 return PT->getPointeeType()->getAs<RecordType>();
408
409 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000410}
411
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000412
Jordy Rosefad5de92012-05-08 03:27:22 +0000413static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
414 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000415 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
416 if (RT->getDecl()->getAttr<LockableAttr>())
417 return true;
418 return false;
419}
420
421
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000422/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000423/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000424static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
425 QualType Ty) {
426 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000427
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000428 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000429 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000430 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000431 << Attr.getName() << Ty.getAsString();
432 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000433 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000434
Michael Hanf1aae3b2012-08-03 17:40:43 +0000435 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000436 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000437 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000438
439 // Allow smart pointers to be used as lockable objects.
440 // FIXME -- Check the type that the smart pointer points to.
441 if (threadSafetyCheckIsSmartPointer(S, RT))
442 return;
443
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000444 // Check if the type is lockable.
445 RecordDecl *RD = RT->getDecl();
446 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000447 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000448
449 // Else check if any base classes are lockable.
450 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
451 CXXBasePaths BPaths(false, false);
452 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
453 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000454 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000455
456 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
457 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000458}
459
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000460/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000461/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000462/// \param Sidx The attribute argument index to start checking with.
463/// \param ParamIdxOk Whether an argument can be indexing into a function
464/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000465static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000466 const AttributeList &Attr,
467 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468 int Sidx = 0,
469 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000470 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000471 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000472
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000473 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000474 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000475 Args.push_back(ArgExp);
476 continue;
477 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000478
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000479 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000480 if (StrLit->getLength() == 0 ||
Benjamin Kramerf37e4f22013-09-13 16:30:12 +0000481 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000482 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000483 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000484 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000485 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000486 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000487
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000488 // We allow constant strings to be used as a placeholder for expressions
489 // that are not valid C++ syntax, but warn that they are ignored.
490 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
491 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000492 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000493 continue;
494 }
495
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000496 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000497
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000498 // A pointer to member expression of the form &MyClass::mu is treated
499 // specially -- we need to look at the type of the member.
500 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
501 if (UOp->getOpcode() == UO_AddrOf)
502 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
503 if (DRE->getDecl()->isCXXInstanceMember())
504 ArgTy = DRE->getDecl()->getType();
505
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000506 // First see if we can just cast to record type, or point to record type.
507 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000508
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000509 // Now check if we index into a record type function param.
510 if(!RT && ParamIdxOk) {
511 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000512 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
513 if(FD && IL) {
514 unsigned int NumParams = FD->getNumParams();
515 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000516 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
517 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
518 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000519 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
520 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000521 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000522 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000523 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000524 }
525 }
526
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000527 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000528
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000529 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000530 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000531}
532
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000533//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000534// Attribute Implementations
535//===----------------------------------------------------------------------===//
536
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000537// FIXME: All this manual attribute parsing code is gross. At the
538// least add some helper functions to check most argument patterns (#
539// and types of args).
540
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000541enum ThreadAttributeDeclKind {
542 ThreadExpectedFieldOrGlobalVar,
543 ThreadExpectedFunctionOrMethod,
544 ThreadExpectedClassOrStruct
545};
546
Michael Hanf1aae3b2012-08-03 17:40:43 +0000547static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000548 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000549 // D must be either a member field or global (potentially shared) variable.
550 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000551 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
552 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000553 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000554 }
555
Michael Handc691572012-07-23 18:48:41 +0000556 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000557}
558
Michael Handc691572012-07-23 18:48:41 +0000559static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
560 if (!checkGuardedVarAttrCommon(S, D, Attr))
561 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000562
Michael Han51d8c522013-01-24 16:46:58 +0000563 D->addAttr(::new (S.Context)
564 GuardedVarAttr(Attr.getRange(), S.Context,
565 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000566}
567
Michael Hanf1aae3b2012-08-03 17:40:43 +0000568static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000569 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000570 if (!checkGuardedVarAttrCommon(S, D, Attr))
571 return;
572
573 if (!threadSafetyCheckIsPointer(S, D, Attr))
574 return;
575
Michael Han51d8c522013-01-24 16:46:58 +0000576 D->addAttr(::new (S.Context)
577 PtGuardedVarAttr(Attr.getRange(), S.Context,
578 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000579}
580
Michael Hanf1aae3b2012-08-03 17:40:43 +0000581static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
582 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000583 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000584 // D must be either a member field or global (potentially shared) variable.
585 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000586 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
587 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000588 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000589 }
590
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000591 SmallVector<Expr*, 1> Args;
592 // check that all arguments are lockable objects
593 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
594 unsigned Size = Args.size();
595 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000596 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000597
Michael Handc691572012-07-23 18:48:41 +0000598 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000599
Michael Handc691572012-07-23 18:48:41 +0000600 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000601}
602
Michael Handc691572012-07-23 18:48:41 +0000603static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
604 Expr *Arg = 0;
605 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
606 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000607
Michael Handc691572012-07-23 18:48:41 +0000608 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
609}
610
Michael Hanf1aae3b2012-08-03 17:40:43 +0000611static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000612 const AttributeList &Attr) {
613 Expr *Arg = 0;
614 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
615 return;
616
617 if (!threadSafetyCheckIsPointer(S, D, Attr))
618 return;
619
620 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
621 S.Context, Arg));
622}
623
Michael Hanf1aae3b2012-08-03 17:40:43 +0000624static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000625 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000626 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000627 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000628 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
629 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000630 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000631 }
632
Michael Handc691572012-07-23 18:48:41 +0000633 return true;
634}
635
636static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
637 if (!checkLockableAttrCommon(S, D, Attr))
638 return;
639
640 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
641}
642
Michael Hanf1aae3b2012-08-03 17:40:43 +0000643static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000644 const AttributeList &Attr) {
645 if (!checkLockableAttrCommon(S, D, Attr))
646 return;
647
Michael Han51d8c522013-01-24 16:46:58 +0000648 D->addAttr(::new (S.Context)
649 ScopedLockableAttr(Attr.getRange(), S.Context,
650 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000651}
652
Kostya Serebryany85aee962013-02-26 06:58:27 +0000653static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
654 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000655 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000656 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
657 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000658 return;
659 }
660
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000661 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000662 S.Context));
663}
664
Kostya Serebryany85aee962013-02-26 06:58:27 +0000665static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000666 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000667 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000669 << Attr.getName() << ExpectedFunctionOrMethod;
670 return;
671 }
672
Michael Han51d8c522013-01-24 16:46:58 +0000673 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000674 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
675 Attr.getAttributeSpellingListIndex()));
676}
677
678static void handleNoSanitizeMemory(Sema &S, Decl *D,
679 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000680 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
682 << Attr.getName() << ExpectedFunctionOrMethod;
683 return;
684 }
685
686 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
687 S.Context));
688}
689
690static void handleNoSanitizeThread(Sema &S, Decl *D,
691 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000692 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
693 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
694 << Attr.getName() << ExpectedFunctionOrMethod;
695 return;
696 }
697
698 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
699 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000700}
701
Michael Hanf1aae3b2012-08-03 17:40:43 +0000702static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
703 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000704 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000706 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000707
708 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000709 ValueDecl *VD = dyn_cast<ValueDecl>(D);
710 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000711 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
712 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000713 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000714 }
715
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000716 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000717 QualType QT = VD->getType();
718 if (!QT->isDependentType()) {
719 const RecordType *RT = getRecordType(QT);
720 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000721 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000722 << Attr.getName();
723 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000724 }
725 }
726
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000727 // Check that all arguments are lockable objects.
728 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000729 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000730 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000731
Michael Handc691572012-07-23 18:48:41 +0000732 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000733}
734
Michael Hanf1aae3b2012-08-03 17:40:43 +0000735static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000736 const AttributeList &Attr) {
737 SmallVector<Expr*, 1> Args;
738 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
739 return;
740
741 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000742 D->addAttr(::new (S.Context)
743 AcquiredAfterAttr(Attr.getRange(), S.Context,
744 StartArg, Args.size(),
745 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000746}
747
Michael Hanf1aae3b2012-08-03 17:40:43 +0000748static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000749 const AttributeList &Attr) {
750 SmallVector<Expr*, 1> Args;
751 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
752 return;
753
754 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000755 D->addAttr(::new (S.Context)
756 AcquiredBeforeAttr(Attr.getRange(), S.Context,
757 StartArg, Args.size(),
758 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000759}
760
Michael Hanf1aae3b2012-08-03 17:40:43 +0000761static bool checkLockFunAttrCommon(Sema &S, Decl *D,
762 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000763 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000764 // zero or more arguments ok
765
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000766 // check that the attribute is applied to a function
767 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000768 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
769 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000770 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000771 }
772
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000773 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000774 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000775
Michael Handc691572012-07-23 18:48:41 +0000776 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000777}
778
Michael Hanf1aae3b2012-08-03 17:40:43 +0000779static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000780 const AttributeList &Attr) {
781 SmallVector<Expr*, 1> Args;
782 if (!checkLockFunAttrCommon(S, D, Attr, Args))
783 return;
784
785 unsigned Size = Args.size();
786 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000787 D->addAttr(::new (S.Context)
788 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
789 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000790}
791
Michael Hanf1aae3b2012-08-03 17:40:43 +0000792static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000793 const AttributeList &Attr) {
794 SmallVector<Expr*, 1> Args;
795 if (!checkLockFunAttrCommon(S, D, Attr, Args))
796 return;
797
798 unsigned Size = Args.size();
799 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000800 D->addAttr(::new (S.Context)
801 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
802 StartArg, Size,
803 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000804}
805
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000806static void handleAssertSharedLockAttr(Sema &S, Decl *D,
807 const AttributeList &Attr) {
808 SmallVector<Expr*, 1> Args;
809 if (!checkLockFunAttrCommon(S, D, Attr, Args))
810 return;
811
812 unsigned Size = Args.size();
813 Expr **StartArg = Size == 0 ? 0 : &Args[0];
814 D->addAttr(::new (S.Context)
815 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
816 Attr.getAttributeSpellingListIndex()));
817}
818
819static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
820 const AttributeList &Attr) {
821 SmallVector<Expr*, 1> Args;
822 if (!checkLockFunAttrCommon(S, D, Attr, Args))
823 return;
824
825 unsigned Size = Args.size();
826 Expr **StartArg = Size == 0 ? 0 : &Args[0];
827 D->addAttr(::new (S.Context)
828 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
829 StartArg, Size,
830 Attr.getAttributeSpellingListIndex()));
831}
832
833
Michael Hanf1aae3b2012-08-03 17:40:43 +0000834static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
835 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000836 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000837 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000838 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000841 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
842 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000843 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000844 }
845
Aaron Ballman624421f2013-08-31 01:11:41 +0000846 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000847 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000848 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000849 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000850 }
851
852 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000853 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000854
Michael Handc691572012-07-23 18:48:41 +0000855 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000856}
857
Michael Hanf1aae3b2012-08-03 17:40:43 +0000858static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000859 const AttributeList &Attr) {
860 SmallVector<Expr*, 2> Args;
861 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
862 return;
863
Michael Han51d8c522013-01-24 16:46:58 +0000864 D->addAttr(::new (S.Context)
865 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000866 Attr.getArgAsExpr(0),
867 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000868 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000869}
870
Michael Hanf1aae3b2012-08-03 17:40:43 +0000871static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000872 const AttributeList &Attr) {
873 SmallVector<Expr*, 2> Args;
874 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
875 return;
876
Michael Han51d8c522013-01-24 16:46:58 +0000877 D->addAttr(::new (S.Context)
878 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000879 Attr.getArgAsExpr(0),
880 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000881 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000882}
883
Michael Hanf1aae3b2012-08-03 17:40:43 +0000884static bool checkLocksRequiredCommon(Sema &S, Decl *D,
885 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000886 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000887 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000888 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000889
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000890 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000891 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
892 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000893 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000894 }
895
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000896 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000897 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000898 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000899 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000900
Michael Handc691572012-07-23 18:48:41 +0000901 return true;
902}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000903
Michael Hanf1aae3b2012-08-03 17:40:43 +0000904static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000905 const AttributeList &Attr) {
906 SmallVector<Expr*, 1> Args;
907 if (!checkLocksRequiredCommon(S, D, Attr, Args))
908 return;
909
910 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000911 D->addAttr(::new (S.Context)
912 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
913 StartArg, Args.size(),
914 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000915}
916
Michael Hanf1aae3b2012-08-03 17:40:43 +0000917static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000918 const AttributeList &Attr) {
919 SmallVector<Expr*, 1> Args;
920 if (!checkLocksRequiredCommon(S, D, Attr, Args))
921 return;
922
923 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000924 D->addAttr(::new (S.Context)
925 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
926 StartArg, Args.size(),
927 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000928}
929
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000930static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000931 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000932 // zero or more arguments ok
933
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000934 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000935 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
936 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000937 return;
938 }
939
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000940 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000941 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000942 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000943 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000944 Expr **StartArg = Size == 0 ? 0 : &Args[0];
945
Michael Han51d8c522013-01-24 16:46:58 +0000946 D->addAttr(::new (S.Context)
947 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
948 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000949}
950
951static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000952 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000953 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000954 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
955 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000956 return;
957 }
958
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000959 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000960 SmallVector<Expr*, 1> Args;
961 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
962 unsigned Size = Args.size();
963 if (Size == 0)
964 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000965
Michael Han51d8c522013-01-24 16:46:58 +0000966 D->addAttr(::new (S.Context)
967 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
968 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000969}
970
971static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000972 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000973 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000974 return;
975
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000976 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000977 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
978 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000979 return;
980 }
981
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000982 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000983 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000984 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000985 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000986 if (Size == 0)
987 return;
988 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000989
Michael Han51d8c522013-01-24 16:46:58 +0000990 D->addAttr(::new (S.Context)
991 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
992 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000993}
994
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000995static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000996 ConsumableAttr::ConsumedState DefaultState;
997
998 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000999 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1000 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1001 DefaultState)) {
1002 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1003 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +00001004 return;
1005 }
David Blaikiea33ab602013-09-06 01:28:43 +00001006 } else {
1007 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1008 << Attr.getName() << AANT_ArgumentIdentifier;
1009 return;
1010 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001011
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001012 if (!isa<CXXRecordDecl>(D)) {
1013 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1014 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001015 return;
1016 }
1017
1018 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +00001019 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001020 Attr.getAttributeSpellingListIndex()));
1021}
1022
1023static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1024 const AttributeList &Attr) {
1025 ASTContext &CurrContext = S.getASTContext();
1026 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1027
1028 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1029 if (!RD->hasAttr<ConsumableAttr>()) {
1030 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1031 RD->getNameAsString();
1032
1033 return false;
1034 }
1035 }
1036
1037 return true;
1038}
1039
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001040
DeLesley Hutchins66540852013-10-04 21:28:06 +00001041static void handleCallableWhenAttr(Sema &S, Decl *D,
1042 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001043 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1044 return;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001045
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001046 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001047 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1048 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001049 return;
1050 }
1051
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001052 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1053 return;
1054
DeLesley Hutchins66540852013-10-04 21:28:06 +00001055 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1056 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1057 CallableWhenAttr::ConsumedState CallableState;
1058
Aaron Ballman9025a182013-10-05 22:45:34 +00001059 StringRef StateString;
1060 SourceLocation Loc;
1061 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1062 return;
1063
1064 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00001065 CallableState)) {
Aaron Ballman9025a182013-10-05 22:45:34 +00001066 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1067 << Attr.getName() << StateString;
DeLesley Hutchins66540852013-10-04 21:28:06 +00001068 return;
1069 }
Aaron Ballman9025a182013-10-05 22:45:34 +00001070
1071 States.push_back(CallableState);
DeLesley Hutchins66540852013-10-04 21:28:06 +00001072 }
1073
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001074 D->addAttr(::new (S.Context)
DeLesley Hutchins66540852013-10-04 21:28:06 +00001075 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1076 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001077}
1078
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001079
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00001080static void handleParamTypestateAttr(Sema &S, Decl *D,
1081 const AttributeList &Attr) {
1082 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1083
1084 if (!isa<ParmVarDecl>(D)) {
1085 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1086 Attr.getName() << ExpectedParameter;
1087 return;
1088 }
1089
1090 ParamTypestateAttr::ConsumedState ParamState;
1091
1092 if (Attr.isArgIdent(0)) {
1093 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1094 StringRef StateString = Ident->Ident->getName();
1095
1096 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1097 ParamState)) {
1098 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1099 << Attr.getName() << StateString;
1100 return;
1101 }
1102 } else {
1103 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1104 Attr.getName() << AANT_ArgumentIdentifier;
1105 return;
1106 }
1107
1108 // FIXME: This check is currently being done in the analysis. It can be
1109 // enabled here only after the parser propagates attributes at
1110 // template specialization definition, not declaration.
1111 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1112 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1113 //
1114 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1115 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1116 // ReturnType.getAsString();
1117 // return;
1118 //}
1119
1120 D->addAttr(::new (S.Context)
1121 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1122 Attr.getAttributeSpellingListIndex()));
1123}
1124
1125
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001126static void handleReturnTypestateAttr(Sema &S, Decl *D,
1127 const AttributeList &Attr) {
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001128 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1129
1130 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1131 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1132 Attr.getName() << ExpectedFunctionMethodOrParameter;
1133 return;
1134 }
1135
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001136 ReturnTypestateAttr::ConsumedState ReturnState;
1137
1138 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001139 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1140 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1141 ReturnState)) {
1142 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1143 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001144 return;
1145 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001146 } else {
1147 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1148 Attr.getName() << AANT_ArgumentIdentifier;
1149 return;
1150 }
1151
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001152 // FIXME: This check is currently being done in the analysis. It can be
1153 // enabled here only after the parser propagates attributes at
1154 // template specialization definition, not declaration.
1155 //QualType ReturnType;
1156 //
DeLesley Hutchinscd0f6d72013-10-17 22:53:04 +00001157 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1158 // ReturnType = Param->getType();
1159 //
1160 //} else if (const CXXConstructorDecl *Constructor =
1161 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001162 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1163 //
1164 //} else {
1165 //
1166 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1167 //}
1168 //
1169 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1170 //
1171 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1172 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1173 // ReturnType.getAsString();
1174 // return;
1175 //}
1176
1177 D->addAttr(::new (S.Context)
1178 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1179 Attr.getAttributeSpellingListIndex()));
1180}
1181
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001182
1183static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001184 if (!checkAttributeNumArgs(S, Attr, 1))
1185 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001186
1187 if (!isa<CXXMethodDecl>(D)) {
1188 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1189 Attr.getName() << ExpectedMethod;
1190 return;
1191 }
1192
1193 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1194 return;
1195
1196 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001197 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001198 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1199 StringRef Param = Ident->Ident->getName();
1200 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1201 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1202 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001203 return;
1204 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001205 } else {
1206 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1207 Attr.getName() << AANT_ArgumentIdentifier;
1208 return;
1209 }
1210
1211 D->addAttr(::new (S.Context)
1212 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1213 Attr.getAttributeSpellingListIndex()));
1214}
1215
Chris Wailes0e429f12013-10-29 20:28:41 +00001216static void handleTestTypestateAttr(Sema &S, Decl *D,
1217 const AttributeList &Attr) {
Aaron Ballman9aa92612013-10-14 23:26:04 +00001218 if (!checkAttributeNumArgs(S, Attr, 1))
1219 return;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001220
1221 if (!isa<CXXMethodDecl>(D)) {
1222 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1223 Attr.getName() << ExpectedMethod;
1224 return;
1225 }
1226
1227 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1228 return;
1229
Chris Wailes0e429f12013-10-29 20:28:41 +00001230 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001231 if (Attr.isArgIdent(0)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001232 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1233 StringRef Param = Ident->Ident->getName();
Chris Wailes0e429f12013-10-29 20:28:41 +00001234 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman2b1599c2013-10-14 23:22:37 +00001235 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1236 << Attr.getName() << Param;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001237 return;
1238 }
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001239 } else {
1240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1241 Attr.getName() << AANT_ArgumentIdentifier;
1242 return;
1243 }
1244
1245 D->addAttr(::new (S.Context)
Chris Wailes0e429f12013-10-29 20:28:41 +00001246 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00001247 Attr.getAttributeSpellingListIndex()));
1248}
1249
Chandler Carruth1b03c872011-07-02 00:01:44 +00001250static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1251 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001252 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1253 if (TD == 0) {
1254 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1255 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001256 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001257 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001258 }
Mike Stumpbf916502009-07-24 19:02:52 +00001259
Richard Smitha4fa9002013-01-13 02:11:23 +00001260 // Remember this typedef decl, we will need it later for diagnostics.
1261 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001262}
1263
Chandler Carruth1b03c872011-07-02 00:01:44 +00001264static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001265 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001266 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001267 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001268 // If the alignment is less than or equal to 8 bits, the packed attribute
1269 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001270 if (!FD->getType()->isDependentType() &&
1271 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001272 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001273 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001274 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001275 else
Michael Han51d8c522013-01-24 16:46:58 +00001276 FD->addAttr(::new (S.Context)
1277 PackedAttr(Attr.getRange(), S.Context,
1278 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001279 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001280 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001281}
1282
Chandler Carruth1b03c872011-07-02 00:01:44 +00001283static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001284 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001285 RD->addAttr(::new (S.Context)
1286 MsStructAttr(Attr.getRange(), S.Context,
1287 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001288 else
1289 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1290}
1291
Chandler Carruth1b03c872011-07-02 00:01:44 +00001292static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001293 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001294 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001295 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001296 D->addAttr(::new (S.Context)
1297 IBActionAttr(Attr.getRange(), S.Context,
1298 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001299 return;
1300 }
1301
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001302 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001303}
1304
Ted Kremenek2f041d02011-09-29 07:02:25 +00001305static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1306 // The IBOutlet/IBOutletCollection attributes only apply to instance
1307 // variables or properties of Objective-C classes. The outlet must also
1308 // have an object reference type.
1309 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1310 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001311 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001312 << Attr.getName() << VD->getType() << 0;
1313 return false;
1314 }
1315 }
1316 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1317 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001318 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001319 << Attr.getName() << PD->getType() << 1;
1320 return false;
1321 }
1322 }
1323 else {
1324 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1325 return false;
1326 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001327
Ted Kremenek2f041d02011-09-29 07:02:25 +00001328 return true;
1329}
1330
Chandler Carruth1b03c872011-07-02 00:01:44 +00001331static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001332 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001333 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001334
Michael Han51d8c522013-01-24 16:46:58 +00001335 D->addAttr(::new (S.Context)
1336 IBOutletAttr(Attr.getRange(), S.Context,
1337 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001338}
1339
Chandler Carruth1b03c872011-07-02 00:01:44 +00001340static void handleIBOutletCollection(Sema &S, Decl *D,
1341 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001342
1343 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001344 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001345 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1346 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001347 return;
1348 }
1349
Ted Kremenek2f041d02011-09-29 07:02:25 +00001350 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001351 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001352
Richard Smithd386fef2013-10-31 01:56:18 +00001353 ParsedType PT;
1354
1355 if (Attr.hasParsedType())
1356 PT = Attr.getTypeArg();
1357 else {
1358 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1359 S.getScopeForContext(D->getDeclContext()->getParent()));
1360 if (!PT) {
1361 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1362 return;
1363 }
Aaron Ballman624421f2013-08-31 01:11:41 +00001364 }
Richard Smithd386fef2013-10-31 01:56:18 +00001365
Richard Smithddc2a532013-10-31 21:23:20 +00001366 TypeSourceInfo *QTLoc = 0;
1367 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1368 if (!QTLoc)
1369 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithd386fef2013-10-31 01:56:18 +00001370
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001371 // Diagnose use of non-object type in iboutletcollection attribute.
1372 // FIXME. Gnu attribute extension ignores use of builtin types in
1373 // attributes. So, __attribute__((iboutletcollection(char))) will be
1374 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001375 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithd386fef2013-10-31 01:56:18 +00001376 S.Diag(Attr.getLoc(),
1377 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1378 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001379 return;
1380 }
Richard Smithd386fef2013-10-31 01:56:18 +00001381
Michael Han51d8c522013-01-24 16:46:58 +00001382 D->addAttr(::new (S.Context)
Richard Smithddc2a532013-10-31 21:23:20 +00001383 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han51d8c522013-01-24 16:46:58 +00001384 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001385}
1386
Chandler Carruthd309c812011-07-01 23:49:16 +00001387static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001388 if (const RecordType *UT = T->getAsUnionType())
1389 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1390 RecordDecl *UD = UT->getDecl();
1391 for (RecordDecl::field_iterator it = UD->field_begin(),
1392 itend = UD->field_end(); it != itend; ++it) {
1393 QualType QT = it->getType();
1394 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1395 T = QT;
1396 return;
1397 }
1398 }
1399 }
1400}
1401
Nuno Lopes587de5b2012-05-24 00:22:00 +00001402static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001403 if (!isFunctionOrMethod(D)) {
1404 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001405 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001406 return;
1407 }
1408
Nuno Lopes587de5b2012-05-24 00:22:00 +00001409 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1410 return;
1411
Nuno Lopes587de5b2012-05-24 00:22:00 +00001412 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001413 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001414 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001415 uint64_t Idx;
1416 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1417 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001418 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001419
1420 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001421 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001422 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001423 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1424 << Attr.getName() << AANT_ArgumentIntegerConstant
1425 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001426 return;
1427 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001428 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001429 }
1430
1431 // check if the function returns a pointer
1432 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1433 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001434 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001435 }
1436
Michael Han51d8c522013-01-24 16:46:58 +00001437 D->addAttr(::new (S.Context)
1438 AllocSizeAttr(Attr.getRange(), S.Context,
1439 SizeArgs.data(), SizeArgs.size(),
1440 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001441}
1442
Chandler Carruth1b03c872011-07-02 00:01:44 +00001443static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001444 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1445 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001446 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001447 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001448 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001449 return;
1450 }
Mike Stumpbf916502009-07-24 19:02:52 +00001451
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001452 SmallVector<unsigned, 8> NonNullArgs;
1453 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001454 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001455 uint64_t Idx;
1456 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1457 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001458 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001459
1460 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001461 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001462 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001463
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001464 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001465 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001466 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001467 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001468 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001469 }
Mike Stumpbf916502009-07-24 19:02:52 +00001470
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001471 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001472 }
Mike Stumpbf916502009-07-24 19:02:52 +00001473
1474 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1475 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001476 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001477 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1478 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001479 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001480 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001481 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001482 }
Mike Stumpbf916502009-07-24 19:02:52 +00001483
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001484 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001485 if (NonNullArgs.empty()) {
1486 // Warn the trivial case only if attribute is not coming from a
1487 // macro instantiation.
1488 if (Attr.getLoc().isFileID())
1489 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001490 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001491 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001492 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001493
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001494 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001495 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001496 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001497 D->addAttr(::new (S.Context)
1498 NonNullAttr(Attr.getRange(), S.Context, start, size,
1499 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001500}
1501
Aaron Ballmane1668a32013-09-16 18:11:41 +00001502static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1503 switch (K) {
1504 case OwnershipAttr::Holds: return "'ownership_holds'";
1505 case OwnershipAttr::Takes: return "'ownership_takes'";
1506 case OwnershipAttr::Returns: return "'ownership_returns'";
1507 }
1508 llvm_unreachable("unknown ownership");
1509}
1510
Chandler Carruth1b03c872011-07-02 00:01:44 +00001511static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001512 // This attribute must be applied to a function declaration. The first
1513 // argument to the attribute must be an identifier, the name of the resource,
1514 // for example: malloc. The following arguments must be argument indexes, the
1515 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001516 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballmane1668a32013-09-16 18:11:41 +00001517 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose2a479922010-08-12 08:54:03 +00001518 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001519
Aaron Ballman624421f2013-08-31 01:11:41 +00001520 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001521 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmane1668a32013-09-16 18:11:41 +00001522 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001523 return;
1524 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001525
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001526 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001527 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001528 switch (AL.getKind()) {
1529 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001530 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001531 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001532 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001533 return;
1534 }
Jordy Rose2a479922010-08-12 08:54:03 +00001535 break;
1536 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001537 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001538 if (AL.getNumArgs() < 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001539 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001540 return;
1541 }
Jordy Rose2a479922010-08-12 08:54:03 +00001542 break;
1543 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001544 K = OwnershipAttr::Returns;
Aaron Ballmane1668a32013-09-16 18:11:41 +00001545
Aaron Ballman624421f2013-08-31 01:11:41 +00001546 if (AL.getNumArgs() > 2) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001547 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001548 return;
1549 }
Jordy Rose2a479922010-08-12 08:54:03 +00001550 break;
1551 default:
1552 // This should never happen given how we are called.
1553 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001554 }
1555
Chandler Carruth87c44602011-07-01 23:49:12 +00001556 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001557 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1558 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001559 return;
1560 }
1561
Aaron Ballman624421f2013-08-31 01:11:41 +00001562 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001563
1564 // Normalize the argument, __foo__ becomes foo.
1565 if (Module.startswith("__") && Module.endswith("__"))
1566 Module = Module.substr(2, Module.size() - 4);
1567
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001568 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001569 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1570 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001571 uint64_t Idx;
1572 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001573 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001574 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001575
Aaron Ballmane1668a32013-09-16 18:11:41 +00001576 // Is the function argument a pointer type?
1577 QualType T = getFunctionOrMethodArgType(D, Idx);
1578 int Err = -1; // No error
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001579 switch (K) {
Aaron Ballmane1668a32013-09-16 18:11:41 +00001580 case OwnershipAttr::Takes:
1581 case OwnershipAttr::Holds:
1582 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1583 Err = 0;
1584 break;
1585 case OwnershipAttr::Returns:
1586 if (!T->isIntegerType())
1587 Err = 1;
1588 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001589 }
Aaron Ballmane1668a32013-09-16 18:11:41 +00001590 if (-1 != Err) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001591 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballmane1668a32013-09-16 18:11:41 +00001592 << Ex->getSourceRange();
1593 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001594 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001595
1596 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001597 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballmane1668a32013-09-16 18:11:41 +00001598 i = D->specific_attr_begin<OwnershipAttr>(),
1599 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1600 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1601 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1602 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1603 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1604 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001605 }
1606 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001607 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001608 }
1609
1610 unsigned* start = OwnershipArgs.data();
1611 unsigned size = OwnershipArgs.size();
1612 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001613
Michael Han51d8c522013-01-24 16:46:58 +00001614 D->addAttr(::new (S.Context)
1615 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1616 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001617}
1618
Chandler Carruth1b03c872011-07-02 00:01:44 +00001619static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001620 // Check the attribute arguments.
1621 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001622 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1623 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001624 return;
1625 }
1626
Chandler Carruth87c44602011-07-01 23:49:12 +00001627 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001628 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001629 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001630 return;
1631 }
1632
Chandler Carruth87c44602011-07-01 23:49:12 +00001633 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001634
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001635 // gcc rejects
1636 // class c {
1637 // static int a __attribute__((weakref ("v2")));
1638 // static int b() __attribute__((weakref ("f3")));
1639 // };
1640 // and ignores the attributes of
1641 // void f(void) {
1642 // static int a __attribute__((weakref ("v2")));
1643 // }
1644 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001645 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001646 if (!Ctx->isFileContext()) {
1647 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001648 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001649 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001650 }
1651
1652 // The GCC manual says
1653 //
1654 // At present, a declaration to which `weakref' is attached can only
1655 // be `static'.
1656 //
1657 // It also says
1658 //
1659 // Without a TARGET,
1660 // given as an argument to `weakref' or to `alias', `weakref' is
1661 // equivalent to `weak'.
1662 //
1663 // gcc 4.4.1 will accept
1664 // int a7 __attribute__((weakref));
1665 // as
1666 // int a7 __attribute__((weak));
1667 // This looks like a bug in gcc. We reject that for now. We should revisit
1668 // it if this behaviour is actually used.
1669
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001670 // GCC rejects
1671 // static ((alias ("y"), weakref)).
1672 // Should we? How to check that weakref is before or after alias?
1673
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001674 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1675 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1676 // StringRef parameter it was given anyway.
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001677 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001678 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001679 // GCC will accept anything as the argument of weakref. Should we
1680 // check for an existing decl?
Aaron Ballman422ac9e2013-09-13 19:35:18 +00001681 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1682 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001683
Michael Han51d8c522013-01-24 16:46:58 +00001684 D->addAttr(::new (S.Context)
1685 WeakRefAttr(Attr.getRange(), S.Context,
1686 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001687}
1688
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001689static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1690 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00001691 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001692 return;
1693
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001694 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001695 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1696 return;
1697 }
1698
Chris Lattner6b6b5372008-06-26 18:38:35 +00001699 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001700
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001701 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00001702 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001703}
1704
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001705static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001706 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1707 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1708 << Attr.getName() << ExpectedFunctionOrMethod;
1709 return;
1710 }
1711
Michael Han51d8c522013-01-24 16:46:58 +00001712 D->addAttr(::new (S.Context)
1713 MinSizeAttr(Attr.getRange(), S.Context,
1714 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001715}
1716
Benjamin Krameree409a92012-05-12 21:10:52 +00001717static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001718 if (!isa<FunctionDecl>(D)) {
1719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1720 << Attr.getName() << ExpectedFunction;
1721 return;
1722 }
1723
1724 if (D->hasAttr<HotAttr>()) {
1725 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1726 << Attr.getName() << "hot";
1727 return;
1728 }
1729
Michael Han51d8c522013-01-24 16:46:58 +00001730 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1731 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001732}
1733
1734static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001735 if (!isa<FunctionDecl>(D)) {
1736 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1737 << Attr.getName() << ExpectedFunction;
1738 return;
1739 }
1740
1741 if (D->hasAttr<ColdAttr>()) {
1742 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1743 << Attr.getName() << "cold";
1744 return;
1745 }
1746
Michael Han51d8c522013-01-24 16:46:58 +00001747 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1748 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001749}
1750
Chandler Carruth1b03c872011-07-02 00:01:44 +00001751static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001752 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001753 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001754 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001755 return;
1756 }
1757
Michael Han51d8c522013-01-24 16:46:58 +00001758 D->addAttr(::new (S.Context)
1759 NakedAttr(Attr.getRange(), S.Context,
1760 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001761}
1762
Chandler Carruth1b03c872011-07-02 00:01:44 +00001763static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1764 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001765 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001766 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001767 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001768 return;
1769 }
Mike Stumpbf916502009-07-24 19:02:52 +00001770
Michael Han51d8c522013-01-24 16:46:58 +00001771 D->addAttr(::new (S.Context)
1772 AlwaysInlineAttr(Attr.getRange(), S.Context,
1773 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001774}
1775
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001776static void handleTLSModelAttr(Sema &S, Decl *D,
1777 const AttributeList &Attr) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001778 StringRef Model;
1779 SourceLocation LiteralLoc;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001780 // Check that it is a string.
Tim Northoverf8aebef2013-10-01 14:34:18 +00001781 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001782 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001783
Richard Smith38afbc72013-04-13 02:43:54 +00001784 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001785 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1786 << Attr.getName() << ExpectedTLSVar;
1787 return;
1788 }
1789
1790 // Check that the value.
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001791 if (Model != "global-dynamic" && Model != "local-dynamic"
1792 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00001793 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001794 return;
1795 }
1796
Michael Han51d8c522013-01-24 16:46:58 +00001797 D->addAttr(::new (S.Context)
1798 TLSModelAttr(Attr.getRange(), S.Context, Model,
1799 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001800}
1801
Chandler Carruth1b03c872011-07-02 00:01:44 +00001802static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001803 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001804 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001805 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001806 D->addAttr(::new (S.Context)
1807 MallocAttr(Attr.getRange(), S.Context,
1808 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001809 return;
1810 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001811 }
1812
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001813 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001814}
1815
Chandler Carruth1b03c872011-07-02 00:01:44 +00001816static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001817 D->addAttr(::new (S.Context)
1818 MayAliasAttr(Attr.getRange(), S.Context,
1819 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001820}
1821
Chandler Carruth1b03c872011-07-02 00:01:44 +00001822static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001823 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001824 D->addAttr(::new (S.Context)
1825 NoCommonAttr(Attr.getRange(), S.Context,
1826 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001827 else
1828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001829 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001830}
1831
Chandler Carruth1b03c872011-07-02 00:01:44 +00001832static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001833 if (S.LangOpts.CPlusPlus) {
1834 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1835 return;
1836 }
1837
Chandler Carruth87c44602011-07-01 23:49:12 +00001838 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001839 D->addAttr(::new (S.Context)
1840 CommonAttr(Attr.getRange(), S.Context,
1841 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001842 else
1843 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001844 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001845}
1846
Chandler Carruth1b03c872011-07-02 00:01:44 +00001847static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001848 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001849
1850 if (S.CheckNoReturnAttr(attr)) return;
1851
Chandler Carruth87c44602011-07-01 23:49:12 +00001852 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001853 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001854 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001855 return;
1856 }
1857
Michael Han51d8c522013-01-24 16:46:58 +00001858 D->addAttr(::new (S.Context)
1859 NoReturnAttr(attr.getRange(), S.Context,
1860 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001861}
1862
1863bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001864 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001865 attr.setInvalid();
1866 return true;
1867 }
1868
1869 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001870}
1871
Chandler Carruth1b03c872011-07-02 00:01:44 +00001872static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1873 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001874
1875 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1876 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001877 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1878 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001879 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1880 && !VD->getType()->isFunctionPointerType())) {
1881 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001882 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001883 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001884 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001885 return;
1886 }
1887 }
1888
Michael Han51d8c522013-01-24 16:46:58 +00001889 D->addAttr(::new (S.Context)
1890 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1891 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001892}
1893
Richard Smithcd8ab512013-01-17 01:30:42 +00001894static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1895 const AttributeList &Attr) {
1896 // C++11 [dcl.attr.noreturn]p1:
1897 // The attribute may be applied to the declarator-id in a function
1898 // declaration.
1899 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1900 if (!FD) {
1901 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1902 << Attr.getName() << ExpectedFunctionOrMethod;
1903 return;
1904 }
1905
Michael Han51d8c522013-01-24 16:46:58 +00001906 D->addAttr(::new (S.Context)
1907 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1908 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001909}
1910
John Thompson35cc9622010-08-09 21:53:52 +00001911// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001912static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001913/*
1914 Returning a Vector Class in Registers
1915
Eric Christopherf48f3672010-12-01 22:13:54 +00001916 According to the PPU ABI specifications, a class with a single member of
1917 vector type is returned in memory when used as the return value of a function.
1918 This results in inefficient code when implementing vector classes. To return
1919 the value in a single vector register, add the vecreturn attribute to the
1920 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001921
1922 Example:
1923
1924 struct Vector
1925 {
1926 __vector float xyzw;
1927 } __attribute__((vecreturn));
1928
1929 Vector Add(Vector lhs, Vector rhs)
1930 {
1931 Vector result;
1932 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1933 return result; // This will be returned in a register
1934 }
1935*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001936 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001937 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001938 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001939 return;
1940 }
1941
Chandler Carruth87c44602011-07-01 23:49:12 +00001942 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001943 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1944 return;
1945 }
1946
Chandler Carruth87c44602011-07-01 23:49:12 +00001947 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001948 int count = 0;
1949
1950 if (!isa<CXXRecordDecl>(record)) {
1951 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1952 return;
1953 }
1954
1955 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1956 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1957 return;
1958 }
1959
Eric Christopherf48f3672010-12-01 22:13:54 +00001960 for (RecordDecl::field_iterator iter = record->field_begin();
1961 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001962 if ((count == 1) || !iter->getType()->isVectorType()) {
1963 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1964 return;
1965 }
1966 count++;
1967 }
1968
Michael Han51d8c522013-01-24 16:46:58 +00001969 D->addAttr(::new (S.Context)
1970 VecReturnAttr(Attr.getRange(), S.Context,
1971 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001972}
1973
Richard Smith3a2b7a12013-01-28 22:42:45 +00001974static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1975 const AttributeList &Attr) {
1976 if (isa<ParmVarDecl>(D)) {
1977 // [[carries_dependency]] can only be applied to a parameter if it is a
1978 // parameter of a function declaration or lambda.
1979 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1980 S.Diag(Attr.getLoc(),
1981 diag::err_carries_dependency_param_not_function_decl);
1982 return;
1983 }
1984 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001985 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001986 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001987 return;
1988 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001989
1990 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1991 Attr.getRange(), S.Context,
1992 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001993}
1994
Chandler Carruth1b03c872011-07-02 00:01:44 +00001995static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001996 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001997 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001998 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001999 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00002000 return;
2001 }
Mike Stumpbf916502009-07-24 19:02:52 +00002002
Michael Han51d8c522013-01-24 16:46:58 +00002003 D->addAttr(::new (S.Context)
2004 UnusedAttr(Attr.getRange(), S.Context,
2005 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00002006}
2007
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002008static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2009 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002010 if (!isa<FunctionDecl>(D)) {
2011 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2012 << Attr.getName() << ExpectedFunction;
2013 return;
2014 }
2015
Michael Han51d8c522013-01-24 16:46:58 +00002016 D->addAttr(::new (S.Context)
2017 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2018 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002019}
2020
Chandler Carruth1b03c872011-07-02 00:01:44 +00002021static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002022 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00002023 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002024 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2025 return;
2026 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002027 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002028 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002029 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002030 return;
2031 }
Mike Stumpbf916502009-07-24 19:02:52 +00002032
Michael Han51d8c522013-01-24 16:46:58 +00002033 D->addAttr(::new (S.Context)
2034 UsedAttr(Attr.getRange(), S.Context,
2035 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002036}
2037
Chandler Carruth1b03c872011-07-02 00:01:44 +00002038static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002039 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002040 if (Attr.getNumArgs() > 1) {
2041 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002042 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002043 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002044
2045 int priority = 65535; // FIXME: Do not hardcode such constants.
2046 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002047 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002048 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002049 if (E->isTypeDependent() || E->isValueDependent() ||
2050 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002051 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002052 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002053 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002054 return;
2055 }
2056 priority = Idx.getZExtValue();
2057 }
Mike Stumpbf916502009-07-24 19:02:52 +00002058
Chandler Carruth87c44602011-07-01 23:49:12 +00002059 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002060 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002061 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002062 return;
2063 }
2064
Michael Han51d8c522013-01-24 16:46:58 +00002065 D->addAttr(::new (S.Context)
2066 ConstructorAttr(Attr.getRange(), S.Context, priority,
2067 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002068}
2069
Chandler Carruth1b03c872011-07-02 00:01:44 +00002070static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002071 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002072 if (Attr.getNumArgs() > 1) {
2073 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002074 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002075 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002076
2077 int priority = 65535; // FIXME: Do not hardcode such constants.
2078 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002079 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002080 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002081 if (E->isTypeDependent() || E->isValueDependent() ||
2082 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002083 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002084 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002085 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002086 return;
2087 }
2088 priority = Idx.getZExtValue();
2089 }
Mike Stumpbf916502009-07-24 19:02:52 +00002090
Chandler Carruth87c44602011-07-01 23:49:12 +00002091 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002092 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002093 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002094 return;
2095 }
2096
Michael Han51d8c522013-01-24 16:46:58 +00002097 D->addAttr(::new (S.Context)
2098 DestructorAttr(Attr.getRange(), S.Context, priority,
2099 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002100}
2101
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002102template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002103static void handleAttrWithMessage(Sema &S, Decl *D,
2104 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002105 unsigned NumArgs = Attr.getNumArgs();
2106 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002107 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002108 return;
2109 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002110
2111 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002112 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002113 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002114 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002115
Michael Han51d8c522013-01-24 16:46:58 +00002116 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2117 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002118}
2119
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002120static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2121 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context)
2123 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2124 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002125}
2126
Patrick Beardb2f68202012-04-06 18:12:22 +00002127static void handleObjCRootClassAttr(Sema &S, Decl *D,
2128 const AttributeList &Attr) {
2129 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002130 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2131 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002132 return;
2133 }
2134
Michael Han51d8c522013-01-24 16:46:58 +00002135 D->addAttr(::new (S.Context)
2136 ObjCRootClassAttr(Attr.getRange(), S.Context,
2137 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002138}
2139
Michael Han51d8c522013-01-24 16:46:58 +00002140static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2141 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002142 if (!isa<ObjCInterfaceDecl>(D)) {
2143 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2144 return;
2145 }
2146
Michael Han51d8c522013-01-24 16:46:58 +00002147 D->addAttr(::new (S.Context)
2148 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2149 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002150}
2151
Jordy Rosefad5de92012-05-08 03:27:22 +00002152static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2153 IdentifierInfo *Platform,
2154 VersionTuple Introduced,
2155 VersionTuple Deprecated,
2156 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002157 StringRef PlatformName
2158 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2159 if (PlatformName.empty())
2160 PlatformName = Platform->getName();
2161
2162 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2163 // of these steps are needed).
2164 if (!Introduced.empty() && !Deprecated.empty() &&
2165 !(Introduced <= Deprecated)) {
2166 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2167 << 1 << PlatformName << Deprecated.getAsString()
2168 << 0 << Introduced.getAsString();
2169 return true;
2170 }
2171
2172 if (!Introduced.empty() && !Obsoleted.empty() &&
2173 !(Introduced <= Obsoleted)) {
2174 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2175 << 2 << PlatformName << Obsoleted.getAsString()
2176 << 0 << Introduced.getAsString();
2177 return true;
2178 }
2179
2180 if (!Deprecated.empty() && !Obsoleted.empty() &&
2181 !(Deprecated <= Obsoleted)) {
2182 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2183 << 2 << PlatformName << Obsoleted.getAsString()
2184 << 1 << Deprecated.getAsString();
2185 return true;
2186 }
2187
2188 return false;
2189}
2190
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002191/// \brief Check whether the two versions match.
2192///
2193/// If either version tuple is empty, then they are assumed to match. If
2194/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2195static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2196 bool BeforeIsOkay) {
2197 if (X.empty() || Y.empty())
2198 return true;
2199
2200 if (X == Y)
2201 return true;
2202
2203 if (BeforeIsOkay && X < Y)
2204 return true;
2205
2206 return false;
2207}
2208
Rafael Espindola51be6e32013-01-08 22:04:34 +00002209AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002210 IdentifierInfo *Platform,
2211 VersionTuple Introduced,
2212 VersionTuple Deprecated,
2213 VersionTuple Obsoleted,
2214 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002215 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002216 bool Override,
2217 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002218 VersionTuple MergedIntroduced = Introduced;
2219 VersionTuple MergedDeprecated = Deprecated;
2220 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002221 bool FoundAny = false;
2222
Rafael Espindola98ae8342012-05-10 02:50:16 +00002223 if (D->hasAttrs()) {
2224 AttrVec &Attrs = D->getAttrs();
2225 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2226 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2227 if (!OldAA) {
2228 ++i;
2229 continue;
2230 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002231
Rafael Espindola98ae8342012-05-10 02:50:16 +00002232 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2233 if (OldPlatform != Platform) {
2234 ++i;
2235 continue;
2236 }
2237
2238 FoundAny = true;
2239 VersionTuple OldIntroduced = OldAA->getIntroduced();
2240 VersionTuple OldDeprecated = OldAA->getDeprecated();
2241 VersionTuple OldObsoleted = OldAA->getObsoleted();
2242 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002243
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002244 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2245 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2246 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2247 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002248 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002249 if (Override) {
2250 int Which = -1;
2251 VersionTuple FirstVersion;
2252 VersionTuple SecondVersion;
2253 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2254 Which = 0;
2255 FirstVersion = OldIntroduced;
2256 SecondVersion = Introduced;
2257 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2258 Which = 1;
2259 FirstVersion = Deprecated;
2260 SecondVersion = OldDeprecated;
2261 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2262 Which = 2;
2263 FirstVersion = Obsoleted;
2264 SecondVersion = OldObsoleted;
2265 }
2266
2267 if (Which == -1) {
2268 Diag(OldAA->getLocation(),
2269 diag::warn_mismatched_availability_override_unavail)
2270 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2271 } else {
2272 Diag(OldAA->getLocation(),
2273 diag::warn_mismatched_availability_override)
2274 << Which
2275 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2276 << FirstVersion.getAsString() << SecondVersion.getAsString();
2277 }
2278 Diag(Range.getBegin(), diag::note_overridden_method);
2279 } else {
2280 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2281 Diag(Range.getBegin(), diag::note_previous_attribute);
2282 }
2283
Rafael Espindola98ae8342012-05-10 02:50:16 +00002284 Attrs.erase(Attrs.begin() + i);
2285 --e;
2286 continue;
2287 }
2288
2289 VersionTuple MergedIntroduced2 = MergedIntroduced;
2290 VersionTuple MergedDeprecated2 = MergedDeprecated;
2291 VersionTuple MergedObsoleted2 = MergedObsoleted;
2292
2293 if (MergedIntroduced2.empty())
2294 MergedIntroduced2 = OldIntroduced;
2295 if (MergedDeprecated2.empty())
2296 MergedDeprecated2 = OldDeprecated;
2297 if (MergedObsoleted2.empty())
2298 MergedObsoleted2 = OldObsoleted;
2299
2300 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2301 MergedIntroduced2, MergedDeprecated2,
2302 MergedObsoleted2)) {
2303 Attrs.erase(Attrs.begin() + i);
2304 --e;
2305 continue;
2306 }
2307
2308 MergedIntroduced = MergedIntroduced2;
2309 MergedDeprecated = MergedDeprecated2;
2310 MergedObsoleted = MergedObsoleted2;
2311 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002312 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002313 }
2314
2315 if (FoundAny &&
2316 MergedIntroduced == Introduced &&
2317 MergedDeprecated == Deprecated &&
2318 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002319 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002320
Ted Kremenekcb344392013-04-06 00:34:27 +00002321 // Only create a new attribute if !Override, but we want to do
2322 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002323 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002324 MergedDeprecated, MergedObsoleted) &&
2325 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002326 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2327 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002328 Obsoleted, IsUnavailable, Message,
2329 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002330 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002331 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002332}
2333
Chandler Carruth1b03c872011-07-02 00:01:44 +00002334static void handleAvailabilityAttr(Sema &S, Decl *D,
2335 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002336 if (!checkAttributeNumArgs(S, Attr, 1))
2337 return;
2338 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002339 unsigned Index = Attr.getAttributeSpellingListIndex();
2340
Aaron Ballman624421f2013-08-31 01:11:41 +00002341 IdentifierInfo *II = Platform->Ident;
2342 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2343 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2344 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002345
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002346 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2347 if (!ND) {
2348 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2349 return;
2350 }
2351
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002352 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2353 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2354 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002355 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002356 StringRef Str;
Benjamin Kramerc5617142013-09-13 17:31:48 +00002357 if (const StringLiteral *SE =
2358 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002359 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002360
Aaron Ballman624421f2013-08-31 01:11:41 +00002361 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002362 Introduced.Version,
2363 Deprecated.Version,
2364 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002365 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002366 /*Override=*/false,
2367 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002368 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002369 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002370}
2371
John McCalld4c3d662013-02-20 01:54:26 +00002372template <class T>
2373static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2374 typename T::VisibilityType value,
2375 unsigned attrSpellingListIndex) {
2376 T *existingAttr = D->getAttr<T>();
2377 if (existingAttr) {
2378 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2379 if (existingValue == value)
2380 return NULL;
2381 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2382 S.Diag(range.getBegin(), diag::note_previous_attribute);
2383 D->dropAttr<T>();
2384 }
2385 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2386}
2387
Rafael Espindola599f1b72012-05-13 03:25:18 +00002388VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002389 VisibilityAttr::VisibilityType Vis,
2390 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002391 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2392 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002393}
2394
John McCalld4c3d662013-02-20 01:54:26 +00002395TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2396 TypeVisibilityAttr::VisibilityType Vis,
2397 unsigned AttrSpellingListIndex) {
2398 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2399 AttrSpellingListIndex);
2400}
2401
2402static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2403 bool isTypeVisibility) {
2404 // Visibility attributes don't mean anything on a typedef.
2405 if (isa<TypedefNameDecl>(D)) {
2406 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2407 << Attr.getName();
2408 return;
2409 }
2410
2411 // 'type_visibility' can only go on a type or namespace.
2412 if (isTypeVisibility &&
2413 !(isa<TagDecl>(D) ||
2414 isa<ObjCInterfaceDecl>(D) ||
2415 isa<NamespaceDecl>(D))) {
2416 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2417 << Attr.getName() << ExpectedTypeOrNamespace;
2418 return;
2419 }
2420
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002421 // Check that the argument is a string literal.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002422 StringRef TypeStr;
2423 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002424 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002425 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002426
Sean Huntcf807c42010-08-18 23:23:40 +00002427 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002428 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002429 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmand0686072013-09-11 19:47:58 +00002430 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002431 return;
2432 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002433
2434 // Complain about attempts to use protected visibility on targets
2435 // (like Darwin) that don't support it.
2436 if (type == VisibilityAttr::Protected &&
2437 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2438 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2439 type = VisibilityAttr::Default;
2440 }
Mike Stumpbf916502009-07-24 19:02:52 +00002441
Michael Han51d8c522013-01-24 16:46:58 +00002442 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002443 clang::Attr *newAttr;
2444 if (isTypeVisibility) {
2445 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2446 (TypeVisibilityAttr::VisibilityType) type,
2447 Index);
2448 } else {
2449 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2450 }
2451 if (newAttr)
2452 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002453}
2454
Chandler Carruth1b03c872011-07-02 00:01:44 +00002455static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2456 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002457 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2458 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002460 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002461 return;
2462 }
2463
Aaron Ballman624421f2013-08-31 01:11:41 +00002464 if (!Attr.isArgIdent(0)) {
2465 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2466 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002467 return;
2468 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002469
Aaron Ballmand0686072013-09-11 19:47:58 +00002470 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2471 ObjCMethodFamilyAttr::FamilyKind F;
2472 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2473 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2474 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002475 return;
2476 }
2477
Aaron Ballmand0686072013-09-11 19:47:58 +00002478 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002479 !method->getResultType()->isObjCObjectPointerType()) {
2480 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2481 << method->getResultType();
2482 // Ignore the attribute.
2483 return;
2484 }
2485
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002486 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002487 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002488}
2489
Chandler Carruth1b03c872011-07-02 00:01:44 +00002490static void handleObjCExceptionAttr(Sema &S, Decl *D,
2491 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002492 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2493 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2495 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002496 return;
2497 }
Mike Stumpbf916502009-07-24 19:02:52 +00002498
Michael Han51d8c522013-01-24 16:46:58 +00002499 D->addAttr(::new (S.Context)
2500 ObjCExceptionAttr(Attr.getRange(), S.Context,
2501 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002502}
2503
Chandler Carruth1b03c872011-07-02 00:01:44 +00002504static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002505 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002506 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002507 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002508 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2509 return;
2510 }
2511 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002512 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2513 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002514 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002515 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2516 return;
2517 }
2518 }
2519 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002520 // It is okay to include this attribute on properties, e.g.:
2521 //
2522 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2523 //
2524 // In this case it follows tradition and suppresses an error in the above
2525 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002526 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002527 }
Michael Han51d8c522013-01-24 16:46:58 +00002528 D->addAttr(::new (S.Context)
2529 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2530 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002531}
2532
Mike Stumpbf916502009-07-24 19:02:52 +00002533static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002534handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002535 if (!isa<FunctionDecl>(D)) {
2536 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2537 return;
2538 }
2539
Michael Han51d8c522013-01-24 16:46:58 +00002540 D->addAttr(::new (S.Context)
2541 OverloadableAttr(Attr.getRange(), S.Context,
2542 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002543}
2544
Chandler Carruth1b03c872011-07-02 00:01:44 +00002545static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002546 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002547 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002548 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002549 return;
2550 }
Mike Stumpbf916502009-07-24 19:02:52 +00002551
Aaron Ballman624421f2013-08-31 01:11:41 +00002552 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002553 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002554 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2555 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2556 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002557 return;
2558 }
Mike Stumpbf916502009-07-24 19:02:52 +00002559
Michael Han51d8c522013-01-24 16:46:58 +00002560 D->addAttr(::new (S.Context)
2561 BlocksAttr(Attr.getRange(), S.Context, type,
2562 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002563}
2564
Chandler Carruth1b03c872011-07-02 00:01:44 +00002565static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002566 // check the attribute arguments.
2567 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002568 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002569 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002570 }
2571
John McCall3323fad2011-09-09 07:56:05 +00002572 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002573 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002574 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002575 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002576 if (E->isTypeDependent() || E->isValueDependent() ||
2577 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002578 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002579 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002580 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002581 return;
2582 }
Mike Stumpbf916502009-07-24 19:02:52 +00002583
John McCall3323fad2011-09-09 07:56:05 +00002584 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002585 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2586 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002587 return;
2588 }
John McCall3323fad2011-09-09 07:56:05 +00002589
2590 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002591 }
2592
John McCall3323fad2011-09-09 07:56:05 +00002593 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002594 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002595 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002596 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002597 if (E->isTypeDependent() || E->isValueDependent() ||
2598 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002599 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002600 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002601 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002602 return;
2603 }
2604 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002605
John McCall3323fad2011-09-09 07:56:05 +00002606 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002607 // FIXME: This error message could be improved, it would be nice
2608 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002609 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2610 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002611 return;
2612 }
2613 }
2614
Chandler Carruth87c44602011-07-01 23:49:12 +00002615 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002616 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002617 if (isa<FunctionNoProtoType>(FT)) {
2618 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2619 return;
2620 }
Mike Stumpbf916502009-07-24 19:02:52 +00002621
Chris Lattner897cd902009-03-17 23:03:47 +00002622 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002623 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002624 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002625 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002626 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002627 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002628 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002629 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002630 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002631 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2632 if (!BD->isVariadic()) {
2633 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2634 return;
2635 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002636 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002637 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002638 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002639 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002640 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002641 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002642 int m = Ty->isFunctionPointerType() ? 0 : 1;
2643 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002644 return;
2645 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002646 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002648 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002649 return;
2650 }
Anders Carlsson77091822008-10-05 18:05:59 +00002651 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002653 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002654 return;
2655 }
Michael Han51d8c522013-01-24 16:46:58 +00002656 D->addAttr(::new (S.Context)
2657 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2658 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002659}
2660
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002661static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002662 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2663 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2664 else
2665 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2666}
2667
Chandler Carruth1b03c872011-07-02 00:01:44 +00002668static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002669 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002671 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002672 return;
2673 }
Mike Stumpbf916502009-07-24 19:02:52 +00002674
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002675 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2676 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2677 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002678 return;
2679 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002680 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2681 if (MD->getResultType()->isVoidType()) {
2682 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2683 << Attr.getName() << 1;
2684 return;
2685 }
2686
Michael Han51d8c522013-01-24 16:46:58 +00002687 D->addAttr(::new (S.Context)
2688 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2689 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002690}
2691
Chandler Carruth1b03c872011-07-02 00:01:44 +00002692static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002693 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002694 if (isa<CXXRecordDecl>(D)) {
2695 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2696 return;
2697 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002698 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2699 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002700 return;
2701 }
2702
Chandler Carruth87c44602011-07-01 23:49:12 +00002703 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002704
Michael Han51d8c522013-01-24 16:46:58 +00002705 nd->addAttr(::new (S.Context)
2706 WeakAttr(Attr.getRange(), S.Context,
2707 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002708}
2709
Chandler Carruth1b03c872011-07-02 00:01:44 +00002710static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002711 // weak_import only applies to variable & function declarations.
2712 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002713 if (!D->canBeWeakImported(isDef)) {
2714 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002715 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2716 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002717 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002718 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002719 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002720 // Nothing to warn about here.
2721 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002722 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002723 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002724
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002725 return;
2726 }
2727
Michael Han51d8c522013-01-24 16:46:58 +00002728 D->addAttr(::new (S.Context)
2729 WeakImportAttr(Attr.getRange(), S.Context,
2730 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002731}
2732
Tanya Lattner0df579e2012-07-09 22:06:01 +00002733// Handles reqd_work_group_size and work_group_size_hint.
2734static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002735 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002736 unsigned WGSize[3];
2737 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002738 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002739 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002740 if (E->isTypeDependent() || E->isValueDependent() ||
2741 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002742 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2743 << Attr.getName() << AANT_ArgumentIntegerConstant
2744 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002745 return;
2746 }
2747 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2748 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002749
2750 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2751 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2752 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2753 if (!(A->getXDim() == WGSize[0] &&
2754 A->getYDim() == WGSize[1] &&
2755 A->getZDim() == WGSize[2])) {
2756 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2757 Attr.getName();
2758 }
2759 }
2760
2761 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2762 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2763 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2764 if (!(A->getXDim() == WGSize[0] &&
2765 A->getYDim() == WGSize[1] &&
2766 A->getZDim() == WGSize[2])) {
2767 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2768 Attr.getName();
2769 }
2770 }
2771
2772 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2773 D->addAttr(::new (S.Context)
2774 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002775 WGSize[0], WGSize[1], WGSize[2],
2776 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002777 else
2778 D->addAttr(::new (S.Context)
2779 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002780 WGSize[0], WGSize[1], WGSize[2],
2781 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002782}
2783
Joey Gouly37453b92013-03-08 09:42:32 +00002784static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2785 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2786
Aaron Ballman624421f2013-08-31 01:11:41 +00002787 if (!Attr.hasParsedType()) {
2788 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2789 << Attr.getName() << 1;
2790 return;
2791 }
2792
Richard Smithddc2a532013-10-31 21:23:20 +00002793 TypeSourceInfo *ParmTSI = 0;
2794 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2795 assert(ParmTSI && "no type source info for attribute argument");
Joey Gouly37453b92013-03-08 09:42:32 +00002796
2797 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2798 (ParmType->isBooleanType() ||
2799 !ParmType->isIntegralType(S.getASTContext()))) {
2800 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2801 << ParmType;
2802 return;
2803 }
2804
2805 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2806 D->hasAttr<VecTypeHintAttr>()) {
2807 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithddc2a532013-10-31 21:23:20 +00002808 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Gouly37453b92013-03-08 09:42:32 +00002809 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2810 return;
2811 }
2812 }
2813
2814 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithddc2a532013-10-31 21:23:20 +00002815 ParmTSI));
Joey Gouly37453b92013-03-08 09:42:32 +00002816}
2817
Rafael Espindola599f1b72012-05-13 03:25:18 +00002818SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002819 StringRef Name,
2820 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002821 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2822 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002823 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002824 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2825 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002826 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002827 }
Michael Han51d8c522013-01-24 16:46:58 +00002828 return ::new (Context) SectionAttr(Range, Context, Name,
2829 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002830}
2831
Chandler Carruth1b03c872011-07-02 00:01:44 +00002832static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002833 // Make sure that there is a string literal as the sections's single
2834 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002835 StringRef Str;
2836 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00002837 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002838 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002839
Chris Lattner797c3c42009-08-10 19:03:04 +00002840 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002841 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002842 if (!Error.empty()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002843 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002844 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002845 return;
2846 }
Mike Stump1eb44332009-09-09 15:08:12 +00002847
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002848 // This attribute cannot be applied to local variables.
2849 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002850 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002851 return;
2852 }
Michael Han51d8c522013-01-24 16:46:58 +00002853
2854 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer1a343e22013-09-13 15:35:43 +00002855 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002856 if (NewAttr)
2857 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002858}
2859
Chris Lattner6b6b5372008-06-26 18:38:35 +00002860
Chandler Carruth1b03c872011-07-02 00:01:44 +00002861static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002862 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002863 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002864 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002865 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002866 D->addAttr(::new (S.Context)
2867 NoThrowAttr(Attr.getRange(), S.Context,
2868 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002869 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002870}
2871
Chandler Carruth1b03c872011-07-02 00:01:44 +00002872static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002873 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002874 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002875 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002876 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002877 D->addAttr(::new (S.Context)
2878 ConstAttr(Attr.getRange(), S.Context,
2879 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002880 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002881}
2882
Chandler Carruth1b03c872011-07-02 00:01:44 +00002883static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002884 D->addAttr(::new (S.Context)
2885 PureAttr(Attr.getRange(), S.Context,
2886 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002887}
2888
Chandler Carruth1b03c872011-07-02 00:01:44 +00002889static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002890 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002891 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002892 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002893 return;
2894 }
Mike Stumpbf916502009-07-24 19:02:52 +00002895
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002896 Expr *E = Attr.getArgAsExpr(0);
2897 SourceLocation Loc = E->getExprLoc();
2898 FunctionDecl *FD = 0;
2899 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002900
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002901 // gcc only allows for simple identifiers. Since we support more than gcc, we
2902 // will warn the user.
2903 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2904 if (DRE->hasQualifier())
2905 S.Diag(Loc, diag::warn_cleanup_ext);
2906 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2907 NI = DRE->getNameInfo();
2908 if (!FD) {
2909 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2910 << NI.getName();
2911 return;
2912 }
2913 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2914 if (ULE->hasExplicitTemplateArgs())
2915 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002916 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2917 NI = ULE->getNameInfo();
Alp Toker8b407722013-10-20 18:48:56 +00002918 if (!FD) {
2919 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2920 << NI.getName();
2921 if (ULE->getType() == S.Context.OverloadTy)
2922 S.NoteAllOverloadCandidates(ULE);
2923 return;
2924 }
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002925 } else {
2926 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002927 return;
2928 }
2929
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002930 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002931 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2932 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002933 return;
2934 }
Mike Stumpbf916502009-07-24 19:02:52 +00002935
Anders Carlsson89941c12009-02-07 23:16:50 +00002936 // We're currently more strict than GCC about what function types we accept.
2937 // If this ever proves to be a problem it should be easy to fix.
2938 QualType Ty = S.Context.getPointerType(VD->getType());
2939 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002940 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2941 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002942 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2943 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002944 return;
2945 }
Mike Stumpbf916502009-07-24 19:02:52 +00002946
Michael Han51d8c522013-01-24 16:46:58 +00002947 D->addAttr(::new (S.Context)
2948 CleanupAttr(Attr.getRange(), S.Context, FD,
2949 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002950}
2951
Mike Stumpbf916502009-07-24 19:02:52 +00002952/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002953/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002954static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002955 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002956 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002957 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002958 return;
2959 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002960
Aaron Ballman624421f2013-08-31 01:11:41 +00002961 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002962 uint64_t ArgIdx;
2963 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2964 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002965 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002966
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002967 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002968 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002969
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002970 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2971 if (not_nsstring_type &&
2972 !isCFStringType(Ty, S.Context) &&
2973 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002974 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002975 // FIXME: Should highlight the actual expression that has the wrong type.
2976 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002977 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002978 << IdxExpr->getSourceRange();
2979 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002980 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002981 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002982 if (!isNSStringType(Ty, S.Context) &&
2983 !isCFStringType(Ty, S.Context) &&
2984 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002985 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002986 // FIXME: Should highlight the actual expression that has the wrong type.
2987 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002988 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002989 << IdxExpr->getSourceRange();
2990 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002991 }
2992
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002993 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2994 // because that has corrected for the implicit this parameter, and is zero-
2995 // based. The attribute expects what the user wrote explicitly.
2996 llvm::APSInt Val;
2997 IdxExpr->EvaluateAsInt(Val, S.Context);
2998
Michael Han51d8c522013-01-24 16:46:58 +00002999 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003000 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003001 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003002}
3003
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003004enum FormatAttrKind {
3005 CFStringFormat,
3006 NSStringFormat,
3007 StrftimeFormat,
3008 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003009 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003010 InvalidFormat
3011};
3012
3013/// getFormatAttrKind - Map from format attribute names to supported format
3014/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003015static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003016 return llvm::StringSwitch<FormatAttrKind>(Format)
3017 // Check for formats that get handled specially.
3018 .Case("NSString", NSStringFormat)
3019 .Case("CFString", CFStringFormat)
3020 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003021
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003022 // Otherwise, check for supported formats.
3023 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3024 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3025 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003026
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003027 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3028 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003029}
3030
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003031/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003032/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003033static void handleInitPriorityAttr(Sema &S, Decl *D,
3034 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003035 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003036 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3037 return;
3038 }
3039
Chandler Carruth87c44602011-07-01 23:49:12 +00003040 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003041 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3042 Attr.setInvalid();
3043 return;
3044 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003045 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003046 if (S.Context.getAsArrayType(T))
3047 T = S.Context.getBaseElementType(T);
3048 if (!T->getAs<RecordType>()) {
3049 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3050 Attr.setInvalid();
3051 return;
3052 }
3053
Aaron Ballman624421f2013-08-31 01:11:41 +00003054 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003055
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003056 llvm::APSInt priority(32);
3057 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3058 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003059 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3060 << Attr.getName() << AANT_ArgumentIntegerConstant
3061 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003062 Attr.setInvalid();
3063 return;
3064 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003065 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003066 if (prioritynum < 101 || prioritynum > 65535) {
3067 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3068 << priorityExpr->getSourceRange();
3069 Attr.setInvalid();
3070 return;
3071 }
Michael Han51d8c522013-01-24 16:46:58 +00003072 D->addAttr(::new (S.Context)
3073 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3074 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003075}
3076
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003077FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3078 IdentifierInfo *Format, int FormatIdx,
3079 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003080 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003081 // Check whether we already have an equivalent format attribute.
3082 for (specific_attr_iterator<FormatAttr>
3083 i = D->specific_attr_begin<FormatAttr>(),
3084 e = D->specific_attr_end<FormatAttr>();
3085 i != e ; ++i) {
3086 FormatAttr *f = *i;
3087 if (f->getType() == Format &&
3088 f->getFormatIdx() == FormatIdx &&
3089 f->getFirstArg() == FirstArg) {
3090 // If we don't have a valid location for this attribute, adopt the
3091 // location.
3092 if (f->getLocation().isInvalid())
3093 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003094 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003095 }
3096 }
3097
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003098 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3099 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003100}
3101
Mike Stumpbf916502009-07-24 19:02:52 +00003102/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003103/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003104static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003105 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003107 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003108 return;
3109 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003110
Chandler Carruth87c44602011-07-01 23:49:12 +00003111 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003112 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003113 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003114 return;
3115 }
3116
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003117 // In C++ the implicit 'this' function parameter also counts, and they are
3118 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003119 bool HasImplicitThisParam = isInstanceMethod(D);
3120 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003121 unsigned FirstIdx = 1;
3122
Aaron Ballman624421f2013-08-31 01:11:41 +00003123 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3124 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003125
3126 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003127 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003128 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003129 // If we've modified the string name, we need a new identifier for it.
3130 II = &S.Context.Idents.get(Format);
3131 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003132
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003133 // Check for supported formats.
3134 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003135
3136 if (Kind == IgnoredFormat)
3137 return;
3138
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003139 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003140 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003141 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003142 return;
3143 }
3144
3145 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003146 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003147 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003148 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3149 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003151 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003152 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003153 return;
3154 }
3155
3156 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003157 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003158 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003159 return;
3160 }
3161
3162 // FIXME: Do we need to bounds check?
3163 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003164
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003165 if (HasImplicitThisParam) {
3166 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003167 S.Diag(Attr.getLoc(),
3168 diag::err_format_attribute_implicit_this_format_string)
3169 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003170 return;
3171 }
3172 ArgIdx--;
3173 }
Mike Stump1eb44332009-09-09 15:08:12 +00003174
Chris Lattner6b6b5372008-06-26 18:38:35 +00003175 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003176 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003177
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003178 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003179 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003180 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3181 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003182 return;
3183 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003184 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003185 // FIXME: do we need to check if the type is NSString*? What are the
3186 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003187 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003188 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003189 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3190 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003191 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003192 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003193 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003194 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003195 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003196 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3197 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003198 return;
3199 }
3200
3201 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003202 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003203 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003204 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3205 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003206 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003207 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003208 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003209 return;
3210 }
3211
3212 // check if the function is variadic if the 3rd argument non-zero
3213 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003214 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003215 ++NumArgs; // +1 for ...
3216 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003217 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003218 return;
3219 }
3220 }
3221
Chris Lattner3c73c412008-11-19 08:23:25 +00003222 // strftime requires FirstArg to be 0 because it doesn't read from any
3223 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003224 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003225 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003226 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3227 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003228 return;
3229 }
3230 // if 0 it disables parameter checking (to use with e.g. va_list)
3231 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003232 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003233 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003234 return;
3235 }
3236
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003237 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003238 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003239 FirstArg.getZExtValue(),
3240 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003241 if (NewAttr)
3242 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003243}
3244
Chandler Carruth1b03c872011-07-02 00:01:44 +00003245static void handleTransparentUnionAttr(Sema &S, Decl *D,
3246 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003247 // Try to find the underlying union declaration.
3248 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003249 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003250 if (TD && TD->getUnderlyingType()->isUnionType())
3251 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3252 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003253 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003254
3255 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003256 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003257 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003258 return;
3259 }
3260
John McCall5e1cdac2011-10-07 06:10:15 +00003261 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003262 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003263 diag::warn_transparent_union_attribute_not_definition);
3264 return;
3265 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003266
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003267 RecordDecl::field_iterator Field = RD->field_begin(),
3268 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003269 if (Field == FieldEnd) {
3270 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3271 return;
3272 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003273
David Blaikie581deb32012-06-06 20:45:41 +00003274 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003275 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003276 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003277 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003278 diag::warn_transparent_union_attribute_floating)
3279 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003280 return;
3281 }
3282
3283 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3284 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3285 for (; Field != FieldEnd; ++Field) {
3286 QualType FieldType = Field->getType();
3287 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3288 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3289 // Warn if we drop the attribute.
3290 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003291 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003292 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003293 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003294 diag::warn_transparent_union_attribute_field_size_align)
3295 << isSize << Field->getDeclName() << FieldBits;
3296 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003297 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003298 diag::note_transparent_union_first_field_size_align)
3299 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003300 return;
3301 }
3302 }
3303
Michael Han51d8c522013-01-24 16:46:58 +00003304 RD->addAttr(::new (S.Context)
3305 TransparentUnionAttr(Attr.getRange(), S.Context,
3306 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003307}
3308
Chandler Carruth1b03c872011-07-02 00:01:44 +00003309static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003310 // Make sure that there is a string literal as the annotation's single
3311 // argument.
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003312 StringRef Str;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003313 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003314 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003315
3316 // Don't duplicate annotations that are already set.
3317 for (specific_attr_iterator<AnnotateAttr>
3318 i = D->specific_attr_begin<AnnotateAttr>(),
3319 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003320 if ((*i)->getAnnotation() == Str)
3321 return;
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003322 }
Michael Han51d8c522013-01-24 16:46:58 +00003323
3324 D->addAttr(::new (S.Context)
Benjamin Kramer1a343e22013-09-13 15:35:43 +00003325 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han51d8c522013-01-24 16:46:58 +00003326 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003327}
3328
Chandler Carruth1b03c872011-07-02 00:01:44 +00003329static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003330 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003331 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003332 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3333 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003334 return;
3335 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003336
Richard Smithbe507b62013-02-01 08:12:08 +00003337 if (Attr.getNumArgs() == 0) {
3338 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3339 true, 0, Attr.getAttributeSpellingListIndex()));
3340 return;
3341 }
3342
Aaron Ballman624421f2013-08-31 01:11:41 +00003343 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003344 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3345 S.Diag(Attr.getEllipsisLoc(),
3346 diag::err_pack_expansion_without_parameter_packs);
3347 return;
3348 }
3349
3350 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3351 return;
3352
3353 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3354 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003355}
3356
3357void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003358 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003359 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3360 SourceLocation AttrLoc = AttrRange.getBegin();
3361
Richard Smith4cd81c52013-01-29 09:02:09 +00003362 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003363 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003364 // C++11 [dcl.align]p1:
3365 // An alignment-specifier may be applied to a variable or to a class
3366 // data member, but it shall not be applied to a bit-field, a function
3367 // parameter, the formal parameter of a catch clause, or a variable
3368 // declared with the register storage class specifier. An
3369 // alignment-specifier may also be applied to the declaration of a class
3370 // or enumeration type.
3371 // C11 6.7.5/2:
3372 // An alignment attribute shall not be specified in a declaration of
3373 // a typedef, or a bit-field, or a function, or a parameter, or an
3374 // object declared with the register storage-class specifier.
3375 int DiagKind = -1;
3376 if (isa<ParmVarDecl>(D)) {
3377 DiagKind = 0;
3378 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3379 if (VD->getStorageClass() == SC_Register)
3380 DiagKind = 1;
3381 if (VD->isExceptionVariable())
3382 DiagKind = 2;
3383 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3384 if (FD->isBitField())
3385 DiagKind = 3;
3386 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003387 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3388 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003389 << (TmpAttr.isC11() ? ExpectedVariableOrField
3390 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003391 return;
3392 }
3393 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003394 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003395 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003396 return;
3397 }
3398 }
3399
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003400 if (E->isTypeDependent() || E->isValueDependent()) {
3401 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003402 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3403 AA->setPackExpansion(IsPackExpansion);
3404 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003405 return;
3406 }
Michael Hana31f65b2013-02-01 01:19:17 +00003407
Sean Huntcf807c42010-08-18 23:23:40 +00003408 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003409 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003410 ExprResult ICE
3411 = VerifyIntegerConstantExpression(E, &Alignment,
3412 diag::err_aligned_attribute_argument_not_int,
3413 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003414 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003415 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003416
3417 // C++11 [dcl.align]p2:
3418 // -- if the constant expression evaluates to zero, the alignment
3419 // specifier shall have no effect
3420 // C11 6.7.5p6:
3421 // An alignment specification of zero has no effect.
3422 if (!(TmpAttr.isAlignas() && !Alignment) &&
3423 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003424 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3425 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003426 return;
3427 }
Michael Hana31f65b2013-02-01 01:19:17 +00003428
Richard Smithbe507b62013-02-01 08:12:08 +00003429 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003430 // We've already verified it's a power of 2, now let's make sure it's
3431 // 8192 or less.
3432 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003433 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003434 << E->getSourceRange();
3435 return;
3436 }
3437 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003438
Richard Smithf6565a92013-02-22 08:32:16 +00003439 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3440 ICE.take(), SpellingListIndex);
3441 AA->setPackExpansion(IsPackExpansion);
3442 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003443}
3444
Michael Hana31f65b2013-02-01 01:19:17 +00003445void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003446 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003447 // FIXME: Cache the number on the Attr object if non-dependent?
3448 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003449 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3450 SpellingListIndex);
3451 AA->setPackExpansion(IsPackExpansion);
3452 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003453}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003454
Richard Smithbe507b62013-02-01 08:12:08 +00003455void Sema::CheckAlignasUnderalignment(Decl *D) {
3456 assert(D->hasAttrs() && "no attributes on decl");
3457
3458 QualType Ty;
3459 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3460 Ty = VD->getType();
3461 else
3462 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003463 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003464 return;
3465
3466 // C++11 [dcl.align]p5, C11 6.7.5/4:
3467 // The combined effect of all alignment attributes in a declaration shall
3468 // not specify an alignment that is less strict than the alignment that
3469 // would otherwise be required for the entity being declared.
3470 AlignedAttr *AlignasAttr = 0;
3471 unsigned Align = 0;
3472 for (specific_attr_iterator<AlignedAttr>
3473 I = D->specific_attr_begin<AlignedAttr>(),
3474 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3475 if (I->isAlignmentDependent())
3476 return;
3477 if (I->isAlignas())
3478 AlignasAttr = *I;
3479 Align = std::max(Align, I->getAlignment(Context));
3480 }
3481
3482 if (AlignasAttr && Align) {
3483 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3484 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3485 if (NaturalAlign > RequestedAlign)
3486 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3487 << Ty << (unsigned)NaturalAlign.getQuantity();
3488 }
3489}
3490
Chandler Carruthd309c812011-07-01 23:49:16 +00003491/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003492/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003493///
Mike Stumpbf916502009-07-24 19:02:52 +00003494/// Despite what would be logical, the mode attribute is a decl attribute, not a
3495/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3496/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003497static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003498 // This attribute isn't documented, but glibc uses it. It changes
3499 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003500 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003501 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3502 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003503 return;
3504 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003505
Aaron Ballman624421f2013-08-31 01:11:41 +00003506 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3507 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003508
3509 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003510 if (Str.startswith("__") && Str.endswith("__"))
3511 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003512
3513 unsigned DestWidth = 0;
3514 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003515 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003516 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003517 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003518 switch (Str[0]) {
3519 case 'Q': DestWidth = 8; break;
3520 case 'H': DestWidth = 16; break;
3521 case 'S': DestWidth = 32; break;
3522 case 'D': DestWidth = 64; break;
3523 case 'X': DestWidth = 96; break;
3524 case 'T': DestWidth = 128; break;
3525 }
3526 if (Str[1] == 'F') {
3527 IntegerMode = false;
3528 } else if (Str[1] == 'C') {
3529 IntegerMode = false;
3530 ComplexMode = true;
3531 } else if (Str[1] != 'I') {
3532 DestWidth = 0;
3533 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003534 break;
3535 case 4:
3536 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3537 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003538 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003539 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003540 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003541 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003542 break;
3543 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003544 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003545 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003546 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003547 case 11:
3548 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003549 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003550 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003551 }
3552
3553 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003554 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003555 OldTy = TD->getUnderlyingType();
3556 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3557 OldTy = VD->getType();
3558 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003559 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003560 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003561 return;
3562 }
Eli Friedman73397492009-03-03 06:41:03 +00003563
John McCall183700f2009-09-21 23:43:11 +00003564 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003565 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3566 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003567 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003568 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3569 } else if (ComplexMode) {
3570 if (!OldTy->isComplexType())
3571 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3572 } else {
3573 if (!OldTy->isFloatingType())
3574 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3575 }
3576
Mike Stump390b4cc2009-05-16 07:39:55 +00003577 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3578 // and friends, at least with glibc.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003579 // FIXME: Make sure floating-point mappings are accurate
3580 // FIXME: Support XF and TF types
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003581 if (!DestWidth) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003582 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003583 return;
Stepan Dyatkovskiyd5692952013-09-18 09:08:52 +00003584 }
3585
3586 QualType NewTy;
3587
3588 if (IntegerMode)
3589 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3590 OldTy->isSignedIntegerType());
3591 else
3592 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3593
3594 if (NewTy.isNull()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003595 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003596 return;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003597 }
3598
Eli Friedman73397492009-03-03 06:41:03 +00003599 if (ComplexMode) {
3600 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003601 }
3602
3603 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003604 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3605 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3606 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003607 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003608
3609 D->addAttr(::new (S.Context)
3610 ModeAttr(Attr.getRange(), S.Context, Name,
3611 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003612}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003613
Chandler Carruth1b03c872011-07-02 00:01:44 +00003614static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003615 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3616 if (!VD->hasGlobalStorage())
3617 S.Diag(Attr.getLoc(),
3618 diag::warn_attribute_requires_functions_or_static_globals)
3619 << Attr.getName();
3620 } else if (!isFunctionOrMethod(D)) {
3621 S.Diag(Attr.getLoc(),
3622 diag::warn_attribute_requires_functions_or_static_globals)
3623 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003624 return;
3625 }
Mike Stumpbf916502009-07-24 19:02:52 +00003626
Michael Han51d8c522013-01-24 16:46:58 +00003627 D->addAttr(::new (S.Context)
3628 NoDebugAttr(Attr.getRange(), S.Context,
3629 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003630}
3631
Chandler Carruth1b03c872011-07-02 00:01:44 +00003632static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003633 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003634 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003635 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003636 return;
3637 }
Mike Stumpbf916502009-07-24 19:02:52 +00003638
Michael Han51d8c522013-01-24 16:46:58 +00003639 D->addAttr(::new (S.Context)
3640 NoInlineAttr(Attr.getRange(), S.Context,
3641 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003642}
3643
Chandler Carruth1b03c872011-07-02 00:01:44 +00003644static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3645 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003646 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003647 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003648 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003649 return;
3650 }
3651
Michael Han51d8c522013-01-24 16:46:58 +00003652 D->addAttr(::new (S.Context)
3653 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3654 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003655}
3656
Chandler Carruth1b03c872011-07-02 00:01:44 +00003657static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003658 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003659 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003661 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003662 return;
3663 }
3664
Michael Han51d8c522013-01-24 16:46:58 +00003665 D->addAttr(::new (S.Context)
3666 CUDAConstantAttr(Attr.getRange(), S.Context,
3667 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003668 } else {
3669 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3670 }
3671}
3672
Chandler Carruth1b03c872011-07-02 00:01:44 +00003673static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003674 if (S.LangOpts.CUDA) {
3675 // check the attribute arguments.
3676 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3678 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003679 return;
3680 }
3681
Chandler Carruth87c44602011-07-01 23:49:12 +00003682 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003683 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003684 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003685 return;
3686 }
3687
Michael Han51d8c522013-01-24 16:46:58 +00003688 D->addAttr(::new (S.Context)
3689 CUDADeviceAttr(Attr.getRange(), S.Context,
3690 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003691 } else {
3692 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3693 }
3694}
3695
Chandler Carruth1b03c872011-07-02 00:01:44 +00003696static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003697 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003698 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003699 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003700 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003701 return;
3702 }
3703
Chandler Carruth87c44602011-07-01 23:49:12 +00003704 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003705 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003706 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003707 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003708 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3709 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003710 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003711 "void");
3712 } else {
3713 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3714 << FD->getType();
3715 }
3716 return;
3717 }
3718
Michael Han51d8c522013-01-24 16:46:58 +00003719 D->addAttr(::new (S.Context)
3720 CUDAGlobalAttr(Attr.getRange(), S.Context,
3721 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003722 } else {
3723 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3724 }
3725}
3726
Chandler Carruth1b03c872011-07-02 00:01:44 +00003727static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003728 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003729 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003730 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003731 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003732 return;
3733 }
3734
Michael Han51d8c522013-01-24 16:46:58 +00003735 D->addAttr(::new (S.Context)
3736 CUDAHostAttr(Attr.getRange(), S.Context,
3737 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003738 } else {
3739 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3740 }
3741}
3742
Chandler Carruth1b03c872011-07-02 00:01:44 +00003743static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003744 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003745 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003746 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003747 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003748 return;
3749 }
3750
Michael Han51d8c522013-01-24 16:46:58 +00003751 D->addAttr(::new (S.Context)
3752 CUDASharedAttr(Attr.getRange(), S.Context,
3753 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003754 } else {
3755 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3756 }
3757}
3758
Chandler Carruth1b03c872011-07-02 00:01:44 +00003759static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003760 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003761 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003763 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003764 return;
3765 }
Mike Stumpbf916502009-07-24 19:02:52 +00003766
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003767 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003768 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003769 return;
3770 }
Mike Stumpbf916502009-07-24 19:02:52 +00003771
Michael Han51d8c522013-01-24 16:46:58 +00003772 D->addAttr(::new (S.Context)
3773 GNUInlineAttr(Attr.getRange(), S.Context,
3774 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003775}
3776
Chandler Carruth1b03c872011-07-02 00:01:44 +00003777static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003778 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003779
Aaron Ballmanfff32482012-12-09 17:45:41 +00003780 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003781 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003782 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3783 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003784 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003785 return;
3786
Chandler Carruth87c44602011-07-01 23:49:12 +00003787 if (!isa<ObjCMethodDecl>(D)) {
3788 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3789 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003790 return;
3791 }
3792
Chandler Carruth87c44602011-07-01 23:49:12 +00003793 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003794 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003795 D->addAttr(::new (S.Context)
3796 FastCallAttr(Attr.getRange(), S.Context,
3797 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003798 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003799 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003800 D->addAttr(::new (S.Context)
3801 StdCallAttr(Attr.getRange(), S.Context,
3802 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003803 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003804 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003805 D->addAttr(::new (S.Context)
3806 ThisCallAttr(Attr.getRange(), S.Context,
3807 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003808 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003809 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003810 D->addAttr(::new (S.Context)
3811 CDeclAttr(Attr.getRange(), S.Context,
3812 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003813 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003814 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003815 D->addAttr(::new (S.Context)
3816 PascalAttr(Attr.getRange(), S.Context,
3817 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003818 return;
Charles Davise8519c32013-08-30 04:39:01 +00003819 case AttributeList::AT_MSABI:
3820 D->addAttr(::new (S.Context)
3821 MSABIAttr(Attr.getRange(), S.Context,
3822 Attr.getAttributeSpellingListIndex()));
3823 return;
3824 case AttributeList::AT_SysVABI:
3825 D->addAttr(::new (S.Context)
3826 SysVABIAttr(Attr.getRange(), S.Context,
3827 Attr.getAttributeSpellingListIndex()));
3828 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003829 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003830 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003831 switch (CC) {
3832 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003833 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003834 break;
3835 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003836 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003837 break;
3838 default:
3839 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003840 }
3841
Michael Han51d8c522013-01-24 16:46:58 +00003842 D->addAttr(::new (S.Context)
3843 PcsAttr(Attr.getRange(), S.Context, PCS,
3844 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003845 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003846 }
Derek Schuff263366f2012-10-16 22:30:41 +00003847 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003848 D->addAttr(::new (S.Context)
3849 PnaclCallAttr(Attr.getRange(), S.Context,
3850 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003851 return;
Guy Benyei38980082012-12-25 08:53:55 +00003852 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003853 D->addAttr(::new (S.Context)
3854 IntelOclBiccAttr(Attr.getRange(), S.Context,
3855 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003856 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003857
Abramo Bagnarae215f722010-04-30 13:10:51 +00003858 default:
3859 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003860 }
3861}
3862
Chandler Carruth1b03c872011-07-02 00:01:44 +00003863static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003864 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003865}
3866
Guy Benyei1db70402013-03-24 13:58:12 +00003867static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003868 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003869 llvm::APSInt ArgNum(32);
3870 if (E->isTypeDependent() || E->isValueDependent() ||
3871 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003872 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3873 << Attr.getName() << AANT_ArgumentIntegerConstant
3874 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003875 return;
3876 }
3877
3878 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3879 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3880}
3881
Aaron Ballmanfff32482012-12-09 17:45:41 +00003882bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3883 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003884 if (attr.isInvalid())
3885 return true;
3886
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003887 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003888 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003889 attr.setInvalid();
3890 return true;
3891 }
3892
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003893 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3894 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003895 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003896 case AttributeList::AT_CDecl: CC = CC_C; break;
3897 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3898 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3899 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3900 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003901 case AttributeList::AT_MSABI:
3902 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3903 CC_X86_64Win64;
3904 break;
3905 case AttributeList::AT_SysVABI:
3906 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3907 CC_C;
3908 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003909 case AttributeList::AT_Pcs: {
Aaron Ballmanfbf6f5c2013-09-13 17:48:25 +00003910 StringRef StrRef;
Tim Northoverf8aebef2013-10-01 14:34:18 +00003911 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003912 attr.setInvalid();
3913 return true;
3914 }
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003915 if (StrRef == "aapcs") {
3916 CC = CC_AAPCS;
3917 break;
3918 } else if (StrRef == "aapcs-vfp") {
3919 CC = CC_AAPCS_VFP;
3920 break;
3921 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003922
3923 attr.setInvalid();
3924 Diag(attr.getLoc(), diag::err_invalid_pcs);
3925 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003926 }
Derek Schuff263366f2012-10-16 22:30:41 +00003927 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003928 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003929 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003930 }
3931
Aaron Ballman82bfa192012-10-02 14:26:08 +00003932 const TargetInfo &TI = Context.getTargetInfo();
3933 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3934 if (A == TargetInfo::CCCR_Warning) {
3935 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003936
3937 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3938 if (FD)
3939 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3940 TargetInfo::CCMT_NonMember;
3941 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003942 }
3943
John McCall711c52b2011-01-05 12:14:39 +00003944 return false;
3945}
3946
Chandler Carruth1b03c872011-07-02 00:01:44 +00003947static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003948 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003949
3950 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003951 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003952 return;
3953
Chandler Carruth87c44602011-07-01 23:49:12 +00003954 if (!isa<ObjCMethodDecl>(D)) {
3955 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3956 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003957 return;
3958 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003959
Michael Han51d8c522013-01-24 16:46:58 +00003960 D->addAttr(::new (S.Context)
3961 RegparmAttr(Attr.getRange(), S.Context, numParams,
3962 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003963}
3964
3965/// Checks a regparm attribute, returning true if it is ill-formed and
3966/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003967bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3968 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003969 return true;
3970
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003971 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003972 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003973 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003974 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003975
Aaron Ballman624421f2013-08-31 01:11:41 +00003976 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003977 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003978 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003979 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003980 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3981 << Attr.getName() << AANT_ArgumentIntegerConstant
3982 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003983 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003984 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003985 }
3986
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003987 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003988 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003989 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003990 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003991 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003992 }
3993
John McCall711c52b2011-01-05 12:14:39 +00003994 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003995 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003996 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003997 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003998 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003999 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004000 }
4001
John McCall711c52b2011-01-05 12:14:39 +00004002 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004003}
4004
Chandler Carruth1b03c872011-07-02 00:01:44 +00004005static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004006 if (S.LangOpts.CUDA) {
4007 // check the attribute arguments.
4008 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004009 // FIXME: 0 is not okay.
4010 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004011 return;
4012 }
4013
Chandler Carruth87c44602011-07-01 23:49:12 +00004014 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004016 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004017 return;
4018 }
4019
Aaron Ballman624421f2013-08-31 01:11:41 +00004020 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004021 llvm::APSInt MaxThreads(32);
4022 if (MaxThreadsExpr->isTypeDependent() ||
4023 MaxThreadsExpr->isValueDependent() ||
4024 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004025 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004026 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004027 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004028 return;
4029 }
4030
4031 llvm::APSInt MinBlocks(32);
4032 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004033 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004034 if (MinBlocksExpr->isTypeDependent() ||
4035 MinBlocksExpr->isValueDependent() ||
4036 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004037 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004038 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004039 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004040 return;
4041 }
4042 }
4043
Michael Han51d8c522013-01-24 16:46:58 +00004044 D->addAttr(::new (S.Context)
4045 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4046 MaxThreads.getZExtValue(),
4047 MinBlocks.getZExtValue(),
4048 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004049 } else {
4050 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4051 }
4052}
4053
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004054static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4055 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004056 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004057 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004058 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004059 return;
4060 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004061
4062 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004063 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004064
Aaron Ballman624421f2013-08-31 01:11:41 +00004065 StringRef AttrName = Attr.getName()->getName();
4066 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004067
4068 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4069 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4070 << Attr.getName() << ExpectedFunctionOrMethod;
4071 return;
4072 }
4073
4074 uint64_t ArgumentIdx;
4075 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4076 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004077 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004078 return;
4079
4080 uint64_t TypeTagIdx;
4081 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4082 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004083 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004084 return;
4085
4086 bool IsPointer = (AttrName == "pointer_with_type_tag");
4087 if (IsPointer) {
4088 // Ensure that buffer has a pointer type.
4089 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4090 if (!BufferTy->isPointerType()) {
4091 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004092 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004093 }
4094 }
4095
Michael Han51d8c522013-01-24 16:46:58 +00004096 D->addAttr(::new (S.Context)
4097 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4098 ArgumentIdx, TypeTagIdx, IsPointer,
4099 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004100}
4101
4102static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4103 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004104 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004105 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004106 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004107 return;
4108 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004109
4110 if (!checkAttributeNumArgs(S, Attr, 1))
4111 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004112
Aaron Ballman624421f2013-08-31 01:11:41 +00004113 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithddc2a532013-10-31 21:23:20 +00004114 TypeSourceInfo *MatchingCTypeLoc = 0;
4115 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4116 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004117
Michael Han51d8c522013-01-24 16:46:58 +00004118 D->addAttr(::new (S.Context)
4119 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithddc2a532013-10-31 21:23:20 +00004120 MatchingCTypeLoc,
Michael Han51d8c522013-01-24 16:46:58 +00004121 Attr.getLayoutCompatible(),
4122 Attr.getMustBeNull(),
4123 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004124}
4125
Chris Lattner0744e5f2008-06-29 00:23:49 +00004126//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004127// Checker-specific attribute handlers.
4128//===----------------------------------------------------------------------===//
4129
John McCallc7ad3812011-01-25 03:31:58 +00004130static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004131 return type->isDependentType() ||
4132 type->isObjCObjectPointerType() ||
4133 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004134}
4135static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004136 return type->isDependentType() ||
4137 type->isPointerType() ||
4138 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004139}
4140
Chandler Carruth1b03c872011-07-02 00:01:44 +00004141static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004142 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004143 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004144 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004145 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004146 return;
4147 }
4148
4149 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004150 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004151 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4152 cf = false;
4153 } else {
4154 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4155 cf = true;
4156 }
4157
4158 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004159 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004160 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004161 return;
4162 }
4163
4164 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004165 param->addAttr(::new (S.Context)
4166 CFConsumedAttr(Attr.getRange(), S.Context,
4167 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004168 else
Michael Han51d8c522013-01-24 16:46:58 +00004169 param->addAttr(::new (S.Context)
4170 NSConsumedAttr(Attr.getRange(), S.Context,
4171 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004172}
4173
Chandler Carruth1b03c872011-07-02 00:01:44 +00004174static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4175 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004176 if (!isa<ObjCMethodDecl>(D)) {
4177 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004178 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004179 return;
4180 }
4181
Michael Han51d8c522013-01-24 16:46:58 +00004182 D->addAttr(::new (S.Context)
4183 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4184 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004185}
4186
Chandler Carruth1b03c872011-07-02 00:01:44 +00004187static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4188 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004189
John McCallc7ad3812011-01-25 03:31:58 +00004190 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004191
Chandler Carruth87c44602011-07-01 23:49:12 +00004192 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004193 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004194 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004195 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004196 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004197 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4198 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004199 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004200 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004201 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004202 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004203 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004204 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004205 return;
4206 }
Mike Stumpbf916502009-07-24 19:02:52 +00004207
John McCallc7ad3812011-01-25 03:31:58 +00004208 bool typeOK;
4209 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004210 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004211 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004212 case AttributeList::AT_NSReturnsAutoreleased:
4213 case AttributeList::AT_NSReturnsRetained:
4214 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004215 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4216 cf = false;
4217 break;
4218
Sean Hunt8e083e72012-06-19 23:57:03 +00004219 case AttributeList::AT_CFReturnsRetained:
4220 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004221 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4222 cf = true;
4223 break;
4224 }
4225
4226 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004227 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004228 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004229 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004230 }
Mike Stumpbf916502009-07-24 19:02:52 +00004231
Chandler Carruth87c44602011-07-01 23:49:12 +00004232 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004233 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004234 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004235 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004236 D->addAttr(::new (S.Context)
4237 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4238 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004239 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004240 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004241 D->addAttr(::new (S.Context)
4242 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4243 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004244 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004245 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004246 D->addAttr(::new (S.Context)
4247 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4248 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004249 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004250 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004251 D->addAttr(::new (S.Context)
4252 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4253 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004254 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004255 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004256 D->addAttr(::new (S.Context)
4257 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4258 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004259 return;
4260 };
4261}
4262
John McCalldc7c5ad2011-07-22 08:53:00 +00004263static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4264 const AttributeList &attr) {
Fariborz Jahanianaf1d28f2013-09-19 17:52:50 +00004265 const int EP_ObjCMethod = 1;
4266 const int EP_ObjCProperty = 2;
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004267
John McCalldc7c5ad2011-07-22 08:53:00 +00004268 SourceLocation loc = attr.getLoc();
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004269 QualType resultType;
4270
John McCalldc7c5ad2011-07-22 08:53:00 +00004271 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4272
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004273 if (!method) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004274 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4275 if (!property) {
4276 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4277 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4278 return;
4279 }
4280 resultType = property->getType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004281 }
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004282 else
4283 // Check that the method returns a normal pointer.
4284 resultType = method->getResultType();
John McCalldc7c5ad2011-07-22 08:53:00 +00004285
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004286 if (!resultType->isReferenceType() &&
4287 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004288 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalldc7c5ad2011-07-22 08:53:00 +00004289 << SourceRange(loc)
Fariborz Jahanianf35a3562013-09-19 17:18:55 +00004290 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4291 << /*non-retainable pointer*/ 2;
John McCalldc7c5ad2011-07-22 08:53:00 +00004292
4293 // Drop the attribute.
4294 return;
4295 }
4296
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00004297 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004298 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4299 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004300}
4301
Fariborz Jahanian84101132012-09-07 23:46:23 +00004302static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4303 const AttributeList &attr) {
4304 SourceLocation loc = attr.getLoc();
4305 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4306
4307 if (!method) {
4308 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4309 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4310 return;
4311 }
4312 DeclContext *DC = method->getDeclContext();
4313 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4314 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4315 << attr.getName() << 0;
4316 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4317 return;
4318 }
4319 if (method->getMethodFamily() == OMF_dealloc) {
4320 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4321 << attr.getName() << 1;
4322 return;
4323 }
4324
Michael Han51d8c522013-01-24 16:46:58 +00004325 method->addAttr(::new (S.Context)
4326 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4327 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004328}
4329
John McCall8dfac0b2011-09-30 05:12:12 +00004330/// Handle cf_audited_transfer and cf_unknown_transfer.
4331static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4332 if (!isa<FunctionDecl>(D)) {
4333 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004334 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004335 return;
4336 }
4337
Sean Hunt8e083e72012-06-19 23:57:03 +00004338 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004339
4340 // Check whether there's a conflicting attribute already present.
4341 Attr *Existing;
4342 if (IsAudited) {
4343 Existing = D->getAttr<CFUnknownTransferAttr>();
4344 } else {
4345 Existing = D->getAttr<CFAuditedTransferAttr>();
4346 }
4347 if (Existing) {
4348 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4349 << A.getName()
4350 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4351 << A.getRange() << Existing->getRange();
4352 return;
4353 }
4354
4355 // All clear; add the attribute.
4356 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004357 D->addAttr(::new (S.Context)
4358 CFAuditedTransferAttr(A.getRange(), S.Context,
4359 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004360 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004361 D->addAttr(::new (S.Context)
4362 CFUnknownTransferAttr(A.getRange(), S.Context,
4363 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004364 }
4365}
4366
John McCallfe98da02011-09-29 07:17:38 +00004367static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4368 const AttributeList &Attr) {
4369 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4370 if (!RD || RD->isUnion()) {
4371 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004372 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004373 }
4374
Aaron Ballman624421f2013-08-31 01:11:41 +00004375 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004376
4377 // In Objective-C, verify that the type names an Objective-C type.
4378 // We don't want to check this outside of ObjC because people sometimes
4379 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004380 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004381 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004382 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004383 Sema::LookupOrdinaryName);
4384 if (S.LookupName(R, Sc)) {
4385 NamedDecl *Target = R.getFoundDecl();
4386 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4387 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4388 S.Diag(Target->getLocStart(), diag::note_declared_at);
4389 }
4390 }
4391 }
4392
Michael Han51d8c522013-01-24 16:46:58 +00004393 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004394 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004395 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004396}
4397
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004398static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4399 const AttributeList &Attr) {
4400 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
4401 QualType T = TD->getUnderlyingType();
4402 if (T->isPointerType()) {
4403 T = T->getPointeeType();
4404 if (T->isRecordType()) {
4405 RecordDecl *RD = T->getAs<RecordType>()->getDecl();
4406 if (!RD || RD->isUnion()) {
4407 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4408 << Attr.getRange() << Attr.getName() << ExpectedStruct;
4409 return;
4410 }
4411 }
4412 } else {
4413 S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_pointertype);
4414 return;
4415 }
4416 // Check for T being a CFType goes here.
4417 if (!isCFRefType(TD, S.Context)) {
4418 S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_cftype);
4419 return;
4420 }
4421 }
4422 else {
4423 S.Diag(D->getLocStart(), diag::err_objc_bridge_attribute);
4424 return;
4425 }
4426
4427 if (Attr.getNumArgs() != 1) {
4428 S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
4429 return;
4430 }
4431 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
4432 if (!Parm) {
4433 S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
4434 return;
4435 }
4436
4437 D->addAttr(::new (S.Context)
4438 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
4439 Attr.getAttributeSpellingListIndex()));
4440}
4441
Chandler Carruth1b03c872011-07-02 00:01:44 +00004442static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4443 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004444 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004445
Chandler Carruth87c44602011-07-01 23:49:12 +00004446 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004447 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004448}
4449
Chandler Carruth1b03c872011-07-02 00:01:44 +00004450static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4451 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004452 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004453 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004454 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004455 return;
4456 }
4457
Chandler Carruth87c44602011-07-01 23:49:12 +00004458 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004459 QualType type = vd->getType();
4460
4461 if (!type->isDependentType() &&
4462 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004463 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004464 << type;
4465 return;
4466 }
4467
4468 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4469
4470 // If we have no lifetime yet, check the lifetime we're presumably
4471 // going to infer.
4472 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4473 lifetime = type->getObjCARCImplicitLifetime();
4474
4475 switch (lifetime) {
4476 case Qualifiers::OCL_None:
4477 assert(type->isDependentType() &&
4478 "didn't infer lifetime for non-dependent type?");
4479 break;
4480
4481 case Qualifiers::OCL_Weak: // meaningful
4482 case Qualifiers::OCL_Strong: // meaningful
4483 break;
4484
4485 case Qualifiers::OCL_ExplicitNone:
4486 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004487 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004488 << (lifetime == Qualifiers::OCL_Autoreleasing);
4489 break;
4490 }
4491
Chandler Carruth87c44602011-07-01 23:49:12 +00004492 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004493 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4494 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004495}
4496
Francois Pichet11542142010-12-19 06:50:37 +00004497//===----------------------------------------------------------------------===//
4498// Microsoft specific attribute handlers.
4499//===----------------------------------------------------------------------===//
4500
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004501// Check if MS extensions or some other language extensions are enabled. If
4502// not, issue a diagnostic that the given attribute is unused.
4503static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4504 bool OtherExtension = false) {
4505 if (S.LangOpts.MicrosoftExt || OtherExtension)
4506 return true;
4507 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4508 return false;
4509}
4510
Chandler Carruth1b03c872011-07-02 00:01:44 +00004511static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004512 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4513 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004514
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004515 StringRef StrRef;
4516 SourceLocation LiteralLoc;
Tim Northoverf8aebef2013-10-01 14:34:18 +00004517 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004518 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004519
David Majnemerbb0ed292013-08-09 08:56:20 +00004520 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4521 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemerbb0ed292013-08-09 08:56:20 +00004522 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4523 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004524
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004525 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004526 if (StrRef.size() != 36) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004527 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004528 return;
4529 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004530
David Majnemerbb0ed292013-08-09 08:56:20 +00004531 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004532 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004533 if (StrRef[i] != '-') {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004534 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichetd3d3be92010-12-20 01:41:49 +00004535 return;
4536 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004537 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer1a343e22013-09-13 15:35:43 +00004538 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004539 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004540 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004541 }
Francois Pichet11542142010-12-19 06:50:37 +00004542
David Majnemerbb0ed292013-08-09 08:56:20 +00004543 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4544 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004545}
4546
John McCallc052dbb2012-05-22 21:28:12 +00004547static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004548 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004549 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004550
4551 AttributeList::Kind Kind = Attr.getKind();
4552 if (Kind == AttributeList::AT_SingleInheritance)
4553 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004554 ::new (S.Context)
4555 SingleInheritanceAttr(Attr.getRange(), S.Context,
4556 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004557 else if (Kind == AttributeList::AT_MultipleInheritance)
4558 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004559 ::new (S.Context)
4560 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4561 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004562 else if (Kind == AttributeList::AT_VirtualInheritance)
4563 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004564 ::new (S.Context)
4565 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4566 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004567}
4568
4569static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004570 if (!checkMicrosoftExt(S, Attr))
4571 return;
4572
4573 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004574 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004575 D->addAttr(
4576 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4577 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004578}
4579
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004580static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004581 if (!checkMicrosoftExt(S, Attr))
4582 return;
4583 D->addAttr(::new (S.Context)
4584 ForceInlineAttr(Attr.getRange(), S.Context,
4585 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004586}
4587
Reid Klecknera7225342013-05-20 14:02:37 +00004588static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4589 if (!checkMicrosoftExt(S, Attr))
4590 return;
4591 // Check linkage after possibly merging declaratinos. See
4592 // checkAttributesAfterMerging().
4593 D->addAttr(::new (S.Context)
4594 SelectAnyAttr(Attr.getRange(), S.Context,
4595 Attr.getAttributeSpellingListIndex()));
4596}
4597
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004598/// Handles semantic checking for features that are common to all attributes,
4599/// such as checking whether a parameter was properly specified, or the correct
4600/// number of arguments were passed, etc.
4601static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4602 const AttributeList &Attr) {
4603 // Several attributes carry different semantics than the parsing requires, so
4604 // those are opted out of the common handling.
4605 //
4606 // We also bail on unknown and ignored attributes because those are handled
4607 // as part of the target-specific handling logic.
4608 if (Attr.hasCustomParsing() ||
4609 Attr.getKind() == AttributeList::UnknownAttribute ||
4610 Attr.getKind() == AttributeList::IgnoredAttribute)
4611 return false;
4612
4613 // If there are no optional arguments, then checking for the argument count
4614 // is trivial.
4615 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4616 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4617 return true;
4618 return false;
4619}
4620
Ted Kremenekb71368d2009-05-09 02:44:38 +00004621//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004622// Top Level Sema Entry Points
4623//===----------------------------------------------------------------------===//
4624
Richard Smith4a97b8e2013-08-29 00:47:48 +00004625/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4626/// the attribute applies to decls. If the attribute is a type attribute, just
4627/// silently ignore it if a GNU attribute.
4628static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4629 const AttributeList &Attr,
4630 bool IncludeCXX11Attributes) {
4631 if (Attr.isInvalid())
4632 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004633
Richard Smith4a97b8e2013-08-29 00:47:48 +00004634 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4635 // instead.
4636 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4637 return;
4638
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004639 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4640 return;
4641
Chris Lattner803d0802008-06-29 00:43:07 +00004642 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004643 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4644 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4645 case AttributeList::AT_IBOutletCollection:
4646 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004647 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004648 case AttributeList::AT_ObjCGC:
4649 case AttributeList::AT_VectorSize:
4650 case AttributeList::AT_NeonVectorType:
4651 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004652 case AttributeList::AT_Ptr32:
4653 case AttributeList::AT_Ptr64:
4654 case AttributeList::AT_SPtr:
4655 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004656 // Ignore these, these are type attributes, handled by
4657 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004658 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004659 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4660 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4661 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4662 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004663 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004664 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004665 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004666 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004667 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4668 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4669 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004670 handleDependencyAttr(S, scope, D, Attr);
4671 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004672 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4673 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4674 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004675 case AttributeList::AT_CXX11NoReturn:
4676 handleCXX11NoReturnAttr(S, D, Attr);
4677 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004678 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004679 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004680 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004681 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4682 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004683 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004684 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004685 case AttributeList::AT_MinSize:
4686 handleMinSizeAttr(S, D, Attr);
4687 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004688 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4689 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4690 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004691 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4692 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004693 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4694 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004695 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004696 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004697 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4698 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004699 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004700 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4701 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004702 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004703 case AttributeList::AT_ownership_returns:
4704 case AttributeList::AT_ownership_takes:
4705 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004706 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004707 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4708 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4709 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4710 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4711 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4712 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4713 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004714
Sean Hunt8e083e72012-06-19 23:57:03 +00004715 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004716 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004717 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004718 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004719
Sean Hunt8e083e72012-06-19 23:57:03 +00004720 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004721 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4722
Fariborz Jahanian84101132012-09-07 23:46:23 +00004723 case AttributeList::AT_ObjCRequiresSuper:
4724 handleObjCRequiresSuperAttr(S, D, Attr); break;
4725
Sean Hunt8e083e72012-06-19 23:57:03 +00004726 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004727 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian933a8942013-11-13 23:59:17 +00004728
4729 case AttributeList::AT_ObjCBridge:
4730 handleObjCBridgeAttr(S, scope, D, Attr); break;
John McCallfe98da02011-09-29 07:17:38 +00004731
Sean Hunt8e083e72012-06-19 23:57:03 +00004732 case AttributeList::AT_CFAuditedTransfer:
4733 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004734 handleCFTransferAttr(S, D, Attr); break;
4735
Ted Kremenekb71368d2009-05-09 02:44:38 +00004736 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004737 case AttributeList::AT_CFConsumed:
4738 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4739 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004740 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004741
Sean Hunt8e083e72012-06-19 23:57:03 +00004742 case AttributeList::AT_NSReturnsAutoreleased:
4743 case AttributeList::AT_NSReturnsNotRetained:
4744 case AttributeList::AT_CFReturnsNotRetained:
4745 case AttributeList::AT_NSReturnsRetained:
4746 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004747 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004748
Tanya Lattner0df579e2012-07-09 22:06:01 +00004749 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004750 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004751 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004752
Joey Gouly37453b92013-03-08 09:42:32 +00004753 case AttributeList::AT_VecTypeHint:
4754 handleVecTypeHint(S, D, Attr); break;
4755
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004757 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004758
Sean Hunt8e083e72012-06-19 23:57:03 +00004759 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4760 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4761 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004762 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004763 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004764 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004765 handleArcWeakrefUnavailableAttr (S, D, Attr);
4766 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004767 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004768 handleObjCRootClassAttr(S, D, Attr);
4769 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004770 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004771 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004772 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004773 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4774 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004775 handleReturnsTwiceAttr(S, D, Attr);
4776 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004777 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004778 case AttributeList::AT_Visibility:
4779 handleVisibilityAttr(S, D, Attr, false);
4780 break;
4781 case AttributeList::AT_TypeVisibility:
4782 handleVisibilityAttr(S, D, Attr, true);
4783 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004784 case AttributeList::AT_WarnUnused:
4785 handleWarnUnusedAttr(S, D, Attr);
4786 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004787 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004788 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004789 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4790 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4791 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4792 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004793 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004794 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004795 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004796 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004797 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004798 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004799 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004800 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004801 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4802 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4803 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4804 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4805 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4806 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4807 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4808 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4809 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004810 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004811 // Just ignore
4812 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004813 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004814 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004815 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004816 case AttributeList::AT_StdCall:
4817 case AttributeList::AT_CDecl:
4818 case AttributeList::AT_FastCall:
4819 case AttributeList::AT_ThisCall:
4820 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004821 case AttributeList::AT_MSABI:
4822 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004823 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004824 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004825 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004826 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004827 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004828 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004829 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004830 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004831 case AttributeList::AT_OpenCLImageAccess:
4832 handleOpenCLImageAccessAttr(S, D, Attr);
4833 break;
John McCallc052dbb2012-05-22 21:28:12 +00004834
4835 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004836 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004837 handleMsStructAttr(S, D, Attr);
4838 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004839 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004840 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004841 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_SingleInheritance:
4843 case AttributeList::AT_MultipleInheritance:
4844 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004845 handleInheritanceAttr(S, D, Attr);
4846 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004848 handlePortabilityAttr(S, D, Attr);
4849 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004851 handleForceInlineAttr(S, D, Attr);
4852 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004853 case AttributeList::AT_SelectAny:
4854 handleSelectAnyAttr(S, D, Attr);
4855 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004856
4857 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004858 case AttributeList::AT_AssertExclusiveLock:
4859 handleAssertExclusiveLockAttr(S, D, Attr);
4860 break;
4861 case AttributeList::AT_AssertSharedLock:
4862 handleAssertSharedLockAttr(S, D, Attr);
4863 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004864 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004865 handleGuardedVarAttr(S, D, Attr);
4866 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004867 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004868 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004869 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004870 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004871 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004872 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004873 case AttributeList::AT_NoSanitizeAddress:
4874 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004875 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004876 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004877 handleNoThreadSafetyAnalysis(S, D, Attr);
4878 break;
4879 case AttributeList::AT_NoSanitizeThread:
4880 handleNoSanitizeThread(S, D, Attr);
4881 break;
4882 case AttributeList::AT_NoSanitizeMemory:
4883 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004884 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004885 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004886 handleLockableAttr(S, D, Attr);
4887 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004888 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004889 handleGuardedByAttr(S, D, Attr);
4890 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004891 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004892 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004893 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004894 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004895 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004896 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004897 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004898 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004899 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004900 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004901 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004902 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004903 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004904 handleLockReturnedAttr(S, D, Attr);
4905 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004906 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004907 handleLocksExcludedAttr(S, D, Attr);
4908 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004909 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004910 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004911 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004912 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004913 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004914 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004915 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004916 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004917 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004918 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004919 handleUnlockFunAttr(S, D, Attr);
4920 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004921 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004922 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004923 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004924 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004925 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004926 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004927
DeLesley Hutchins1bf63432013-10-11 22:30:48 +00004928 // Consumed analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004929 case AttributeList::AT_Consumable:
4930 handleConsumableAttr(S, D, Attr);
4931 break;
DeLesley Hutchins66540852013-10-04 21:28:06 +00004932 case AttributeList::AT_CallableWhen:
4933 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004934 break;
DeLesley Hutchinsd4f0e192013-10-17 23:23:53 +00004935 case AttributeList::AT_ParamTypestate:
4936 handleParamTypestateAttr(S, D, Attr);
4937 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004938 case AttributeList::AT_ReturnTypestate:
4939 handleReturnTypestateAttr(S, D, Attr);
4940 break;
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004941 case AttributeList::AT_SetTypestate:
4942 handleSetTypestateAttr(S, D, Attr);
4943 break;
Chris Wailes0e429f12013-10-29 20:28:41 +00004944 case AttributeList::AT_TestTypestate:
4945 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004946 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004947
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004948 // Type safety attributes.
4949 case AttributeList::AT_ArgumentWithTypeTag:
4950 handleArgumentWithTypeTagAttr(S, D, Attr);
4951 break;
4952 case AttributeList::AT_TypeTagForDatatype:
4953 handleTypeTagForDatatypeAttr(S, D, Attr);
4954 break;
4955
Chris Lattner803d0802008-06-29 00:43:07 +00004956 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004957 // Ask target about the attribute.
4958 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4959 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004960 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4961 diag::warn_unhandled_ms_attribute_ignored :
4962 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004963 break;
4964 }
4965}
4966
4967/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4968/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004969void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004970 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004971 bool IncludeCXX11Attributes) {
4972 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004973 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004974
4975 // GCC accepts
4976 // static int a9 __attribute__((weakref));
4977 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004978 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004979 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004980 cast<NamedDecl>(D)->getNameAsString();
4981 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004982 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004983 }
4984}
4985
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004986// Annotation attributes are the only attributes allowed after an access
4987// specifier.
4988bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4989 const AttributeList *AttrList) {
4990 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004991 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004992 handleAnnotateAttr(*this, ASDecl, *l);
4993 } else {
4994 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4995 return true;
4996 }
4997 }
4998
4999 return false;
5000}
5001
John McCalle82247a2011-10-01 05:17:03 +00005002/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5003/// contains any decl attributes that we should warn about.
5004static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5005 for ( ; A; A = A->getNext()) {
5006 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005007 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005008 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5009
5010 if (A->getKind() == AttributeList::UnknownAttribute) {
5011 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5012 << A->getName() << A->getRange();
5013 } else {
5014 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5015 << A->getName() << A->getRange();
5016 }
5017 }
5018}
5019
5020/// checkUnusedDeclAttributes - Given a declarator which is not being
5021/// used to build a declaration, complain about any decl attributes
5022/// which might be lying around on it.
5023void Sema::checkUnusedDeclAttributes(Declarator &D) {
5024 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5025 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5026 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5027 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5028}
5029
Ryan Flynne25ff832009-07-30 03:15:39 +00005030/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005031/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005032NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5033 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005034 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005035 NamedDecl *NewD = 0;
5036 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005037 FunctionDecl *NewFD;
5038 // FIXME: Missing call to CheckFunctionDeclaration().
5039 // FIXME: Mangling?
5040 // FIXME: Is the qualifier info correct?
5041 // FIXME: Is the DeclContext correct?
5042 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5043 Loc, Loc, DeclarationName(II),
5044 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005045 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005046 FD->hasPrototype(),
5047 false/*isConstexprSpecified*/);
5048 NewD = NewFD;
5049
5050 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005051 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005052
5053 // Fake up parameter variables; they are declared as if this were
5054 // a typedef.
5055 QualType FDTy = FD->getType();
5056 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5057 SmallVector<ParmVarDecl*, 16> Params;
5058 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5059 AE = FT->arg_type_end(); AI != AE; ++AI) {
5060 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5061 Param->setScopeInfo(0, Params.size());
5062 Params.push_back(Param);
5063 }
David Blaikie4278c652011-09-21 18:16:56 +00005064 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005065 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005066 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5067 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005068 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005069 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005070 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005071 if (VD->getQualifier()) {
5072 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005073 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005074 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005075 }
5076 return NewD;
5077}
5078
James Dennett1dfbd922012-06-14 21:40:34 +00005079/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005080/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005081void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005082 if (W.getUsed()) return; // only do this once
5083 W.setUsed(true);
5084 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5085 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005086 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005087 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5088 NDId->getName()));
5089 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005090 WeakTopLevelDecl.push_back(NewD);
5091 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5092 // to insert Decl at TU scope, sorry.
5093 DeclContext *SavedContext = CurContext;
5094 CurContext = Context.getTranslationUnitDecl();
5095 PushOnScopeChains(NewD, S);
5096 CurContext = SavedContext;
5097 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005098 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005099 }
5100}
5101
Rafael Espindola65611bf2013-03-02 21:41:48 +00005102void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5103 // It's valid to "forward-declare" #pragma weak, in which case we
5104 // have to do this.
5105 LoadExternalWeakUndeclaredIdentifiers();
5106 if (!WeakUndeclaredIdentifiers.empty()) {
5107 NamedDecl *ND = NULL;
5108 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5109 if (VD->isExternC())
5110 ND = VD;
5111 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5112 if (FD->isExternC())
5113 ND = FD;
5114 if (ND) {
5115 if (IdentifierInfo *Id = ND->getIdentifier()) {
5116 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5117 = WeakUndeclaredIdentifiers.find(Id);
5118 if (I != WeakUndeclaredIdentifiers.end()) {
5119 WeakInfo W = I->second;
5120 DeclApplyPragmaWeak(S, ND, W);
5121 WeakUndeclaredIdentifiers[Id] = W;
5122 }
5123 }
5124 }
5125 }
5126}
5127
Chris Lattner0744e5f2008-06-29 00:23:49 +00005128/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5129/// it, apply them to D. This is a bit tricky because PD can have attributes
5130/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005131void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005132 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005133 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005134 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005135
Chris Lattner0744e5f2008-06-29 00:23:49 +00005136 // Walk the declarator structure, applying decl attributes that were in a type
5137 // position to the decl itself. This handles cases like:
5138 // int *__attr__(x)** D;
5139 // when X is a decl attribute.
5140 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5141 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005142 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005143
Chris Lattner0744e5f2008-06-29 00:23:49 +00005144 // Finally, apply any attributes on the decl itself.
5145 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005146 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005147}
John McCall54abf7d2009-11-04 02:18:39 +00005148
John McCallf85e1932011-06-15 23:02:42 +00005149/// Is the given declaration allowed to use a forbidden type?
5150static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5151 // Private ivars are always okay. Unfortunately, people don't
5152 // always properly make their ivars private, even in system headers.
5153 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005154 // Function declarations in sys headers will be marked unavailable.
5155 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5156 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005157 return false;
5158
5159 // Require it to be declared in a system header.
5160 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5161}
5162
5163/// Handle a delayed forbidden-type diagnostic.
5164static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5165 Decl *decl) {
5166 if (decl && isForbiddenTypeAllowed(S, decl)) {
5167 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5168 "this system declaration uses an unsupported type"));
5169 return;
5170 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005171 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005172 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005173 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005174 // kind of forbidden type messages on unavailable functions.
5175 if (FD->hasAttr<UnavailableAttr>() &&
5176 diag.getForbiddenTypeDiagnostic() ==
5177 diag::err_arc_array_param_no_ownership) {
5178 diag.Triggered = true;
5179 return;
5180 }
5181 }
John McCallf85e1932011-06-15 23:02:42 +00005182
5183 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5184 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5185 diag.Triggered = true;
5186}
5187
John McCall92576642012-05-07 06:16:41 +00005188void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5189 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005190 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005191 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005192
John McCall92576642012-05-07 06:16:41 +00005193 // When delaying diagnostics to run in the context of a parsed
5194 // declaration, we only want to actually emit anything if parsing
5195 // succeeds.
5196 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005197
John McCall92576642012-05-07 06:16:41 +00005198 // We emit all the active diagnostics in this pool or any of its
5199 // parents. In general, we'll get one pool for the decl spec
5200 // and a child pool for each declarator; in a decl group like:
5201 // deprecated_typedef foo, *bar, baz();
5202 // only the declarator pops will be passed decls. This is correct;
5203 // we really do need to consider delayed diagnostics from the decl spec
5204 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005205 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005206 do {
John McCall13489672012-05-07 06:16:58 +00005207 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005208 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5209 // This const_cast is a bit lame. Really, Triggered should be mutable.
5210 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005211 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005212 continue;
5213
John McCalleee1d542011-02-14 07:13:47 +00005214 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005215 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005216 // Don't bother giving deprecation diagnostics if the decl is invalid.
5217 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005218 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005219 break;
5220
5221 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005222 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005223 break;
John McCallf85e1932011-06-15 23:02:42 +00005224
5225 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005226 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005227 break;
John McCall2f514482010-01-27 03:50:35 +00005228 }
5229 }
John McCall92576642012-05-07 06:16:41 +00005230 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005231}
5232
John McCall13489672012-05-07 06:16:58 +00005233/// Given a set of delayed diagnostics, re-emit them as if they had
5234/// been delayed in the current context instead of in the given pool.
5235/// Essentially, this just moves them to the current pool.
5236void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5237 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5238 assert(curPool && "re-emitting in undelayed context not supported");
5239 curPool->steal(pool);
5240}
5241
John McCall54abf7d2009-11-04 02:18:39 +00005242static bool isDeclDeprecated(Decl *D) {
5243 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005244 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005245 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005246 // A category implicitly has the availability of the interface.
5247 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5248 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005249 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5250 return false;
5251}
5252
Eli Friedmanc3b23082012-08-08 21:52:41 +00005253static void
5254DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5255 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005256 const ObjCInterfaceDecl *UnknownObjCClass,
5257 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005258 DeclarationName Name = D->getDeclName();
5259 if (!Message.empty()) {
5260 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5261 S.Diag(D->getLocation(),
5262 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5263 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005264 if (ObjCPropery)
5265 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5266 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005267 } else if (!UnknownObjCClass) {
5268 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5269 S.Diag(D->getLocation(),
5270 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5271 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005272 if (ObjCPropery)
5273 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5274 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005275 } else {
5276 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5277 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5278 }
5279}
5280
John McCall9c3087b2010-08-26 02:13:20 +00005281void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005282 Decl *Ctx) {
5283 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005284 return;
5285
John McCall2f514482010-01-27 03:50:35 +00005286 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005287 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5288 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005289 DD.getUnknownObjCClass(),
5290 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005291}
5292
Chris Lattner5f9e2722011-07-23 10:55:15 +00005293void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005294 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005295 const ObjCInterfaceDecl *UnknownObjCClass,
5296 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005297 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005298 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005299 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5300 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005301 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005302 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005303 return;
5304 }
5305
5306 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005307 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005308 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005309 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005310}