blob: 50d11175995c4ac7ed4829e3ff423d3cff483580 [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"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000028using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030
John McCall883cc2c2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
John McCall883cc2c2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedVariable,
43 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000044 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000045 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000046 ExpectedStruct,
47 ExpectedTLSVar
John McCall883cc2c2011-03-02 12:29:23 +000048};
49
Chris Lattnere5c5ee12008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Chandler Carruth87c44602011-07-01 23:49:12 +000054static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000057 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000061 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000062 Ty = decl->getUnderlyingType();
63 else
64 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000065
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000068 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000069 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070
John McCall183700f2009-09-21 23:43:11 +000071 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000072}
73
Daniel Dunbar35682492008-09-26 04:12:28 +000074// FIXME: We should provide an abstraction around a method or function
75// to provide the following bits of information.
76
Nuno Lopesd20254f2009-12-20 23:11:08 +000077/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000078/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000079static bool isFunction(const Decl *D) {
80 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081}
82
83/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000084/// type (function or function-typed variable) or an Objective-C
85/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000086static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000087 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000088}
89
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000090/// isFunctionOrMethodOrBlock - Return true if the given decl has function
91/// type (function or function-typed variable) or an Objective-C
92/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000093static bool isFunctionOrMethodOrBlock(const Decl *D) {
94 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095 return true;
96 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000097 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098 QualType Ty = V->getType();
99 return Ty->isBlockPointerType();
100 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000101 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000102}
103
John McCall711c52b2011-01-05 12:14:39 +0000104/// Return true if the given decl has a declarator that should have
105/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000107 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
109 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000110}
111
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000112/// hasFunctionProto - Return true if the given decl has a argument
113/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000114/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasFunctionProto(const Decl *D) {
116 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000118 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 return true;
121 }
122}
123
124/// getFunctionOrMethodNumArgs - Return number of function or method
125/// arguments. It is an error to call this on a K&R function (use
126/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000127static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
128 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000131 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000133}
134
Chandler Carruth87c44602011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000139 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000140
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodResultType(const Decl *D) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000148}
149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150static bool isFunctionOrMethodVariadic(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000152 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000153 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000154 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000155 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000156 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000158 }
159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isInstanceMethod(const Decl *D) {
162 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000163 return MethodDecl->isInstance();
164 return false;
165}
166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000168 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000169 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000171
John McCall506b57e2010-05-17 21:00:27 +0000172 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
173 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000175
John McCall506b57e2010-05-17 21:00:27 +0000176 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 // FIXME: Should we walk the chain of classes?
179 return ClsName == &Ctx.Idents.get("NSString") ||
180 ClsName == &Ctx.Idents.get("NSMutableString");
181}
182
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000184 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185 if (!PT)
186 return false;
187
Ted Kremenek6217b802009-07-29 21:53:49 +0000188 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000189 if (!RT)
190 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000193 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 return false;
195
196 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197}
198
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000199/// \brief Check if the attribute has exactly as many args as Num. May
200/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000201static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned int Num) {
203 if (Attr.getNumArgs() != Num) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
205 return false;
206 }
207
208 return true;
209}
210
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000211
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000212/// \brief Check if the attribute has at least as many args as Num. May
213/// output an error.
214static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
215 unsigned int Num) {
216 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000217 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
218 return false;
219 }
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221 return true;
222}
223
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000224/// \brief Check if IdxExpr is a valid argument index for a function or
225/// instance method D. May output an error.
226///
227/// \returns true if IdxExpr is a valid index.
228static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
229 StringRef AttrName,
230 SourceLocation AttrLoc,
231 unsigned AttrArgNum,
232 const Expr *IdxExpr,
233 uint64_t &Idx)
234{
235 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
236
237 // In C++ the implicit 'this' function parameter also counts.
238 // Parameters are counted from one.
239 const bool HasImplicitThisParam = isInstanceMethod(D);
240 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
241 const unsigned FirstIdx = 1;
242
243 llvm::APSInt IdxInt;
244 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
245 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
246 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
247 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
248 return false;
249 }
250
251 Idx = IdxInt.getLimitedValue();
252 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
253 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
254 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
255 return false;
256 }
257 Idx--; // Convert to zero-based.
258 if (HasImplicitThisParam) {
259 if (Idx == 0) {
260 S.Diag(AttrLoc,
261 diag::err_attribute_invalid_implicit_this_argument)
262 << AttrName << IdxExpr->getSourceRange();
263 return false;
264 }
265 --Idx;
266 }
267
268 return true;
269}
270
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000271///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000272/// \brief Check if passed in Decl is a field or potentially shared global var
273/// \return true if the Decl is a field or potentially shared global variable
274///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000275static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000276 if (isa<FieldDecl>(D))
277 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000278 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000279 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
280
281 return false;
282}
283
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000284/// \brief Check if the passed-in expression is of type int or bool.
285static bool isIntOrBool(Expr *Exp) {
286 QualType QT = Exp->getType();
287 return QT->isBooleanType() || QT->isIntegerType();
288}
289
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000290
291// Check to see if the type is a smart pointer of some kind. We assume
292// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000293static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
294 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
295 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
296 if (Res1.first == Res1.second)
297 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000298
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000299 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
300 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
301 if (Res2.first == Res2.second)
302 return false;
303
304 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000305}
306
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000307/// \brief Check if passed in Decl is a pointer type.
308/// Note that this function may produce an error message.
309/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000310static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
311 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000312 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000313 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000314 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000315 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000316
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000317 if (const RecordType *RT = QT->getAs<RecordType>()) {
318 // If it's an incomplete type, it could be a smart pointer; skip it.
319 // (We don't want to force template instantiation if we can avoid it,
320 // since that would alter the order in which templates are instantiated.)
321 if (RT->isIncompleteType())
322 return true;
323
324 if (threadSafetyCheckIsSmartPointer(S, RT))
325 return true;
326 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000327
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000328 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000329 << Attr.getName()->getName() << QT;
330 } else {
331 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
332 << Attr.getName();
333 }
334 return false;
335}
336
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000337/// \brief Checks that the passed in QualType either is of RecordType or points
338/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000339static const RecordType *getRecordType(QualType QT) {
340 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000341 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000342
343 // Now check if we point to record type.
344 if (const PointerType *PT = QT->getAs<PointerType>())
345 return PT->getPointeeType()->getAs<RecordType>();
346
347 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000348}
349
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000350
Jordy Rosefad5de92012-05-08 03:27:22 +0000351static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
352 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000353 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
354 if (RT->getDecl()->getAttr<LockableAttr>())
355 return true;
356 return false;
357}
358
359
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000360/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000361/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000362static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
363 QualType Ty) {
364 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000365
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000366 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000367 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000368 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000369 << Attr.getName() << Ty.getAsString();
370 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000371 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000372
Michael Hanf1aae3b2012-08-03 17:40:43 +0000373 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000374 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000375 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000376
377 // Allow smart pointers to be used as lockable objects.
378 // FIXME -- Check the type that the smart pointer points to.
379 if (threadSafetyCheckIsSmartPointer(S, RT))
380 return;
381
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000382 // Check if the type is lockable.
383 RecordDecl *RD = RT->getDecl();
384 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000385 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000386
387 // Else check if any base classes are lockable.
388 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
389 CXXBasePaths BPaths(false, false);
390 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
391 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000392 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000393
394 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
395 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000396}
397
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000398/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000399/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000400/// \param Sidx The attribute argument index to start checking with.
401/// \param ParamIdxOk Whether an argument can be indexing into a function
402/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000403static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000404 const AttributeList &Attr,
405 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000406 int Sidx = 0,
407 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000408 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000409 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000410
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000411 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000413 Args.push_back(ArgExp);
414 continue;
415 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000416
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000417 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000418 if (StrLit->getLength() == 0) {
419 // Pass empty strings to the analyzer without warnings.
420 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000421 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000422 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000423
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000424 // We allow constant strings to be used as a placeholder for expressions
425 // that are not valid C++ syntax, but warn that they are ignored.
426 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
427 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000428 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000429 continue;
430 }
431
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000432 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000433
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000434 // A pointer to member expression of the form &MyClass::mu is treated
435 // specially -- we need to look at the type of the member.
436 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
437 if (UOp->getOpcode() == UO_AddrOf)
438 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
439 if (DRE->getDecl()->isCXXInstanceMember())
440 ArgTy = DRE->getDecl()->getType();
441
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000442 // First see if we can just cast to record type, or point to record type.
443 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000444
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000445 // Now check if we index into a record type function param.
446 if(!RT && ParamIdxOk) {
447 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000448 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
449 if(FD && IL) {
450 unsigned int NumParams = FD->getNumParams();
451 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000452 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
453 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
454 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000455 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
456 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000457 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000458 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000459 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000460 }
461 }
462
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000463 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000464
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000465 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000466 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000467}
468
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000469//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000470// Attribute Implementations
471//===----------------------------------------------------------------------===//
472
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000473// FIXME: All this manual attribute parsing code is gross. At the
474// least add some helper functions to check most argument patterns (#
475// and types of args).
476
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000477enum ThreadAttributeDeclKind {
478 ThreadExpectedFieldOrGlobalVar,
479 ThreadExpectedFunctionOrMethod,
480 ThreadExpectedClassOrStruct
481};
482
Michael Hanf1aae3b2012-08-03 17:40:43 +0000483static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000484 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000485 assert(!Attr.isInvalid());
486
487 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000488 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000489
490 // D must be either a member field or global (potentially shared) variable.
491 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000492 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
493 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000494 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000495 }
496
Michael Handc691572012-07-23 18:48:41 +0000497 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000498}
499
Michael Handc691572012-07-23 18:48:41 +0000500static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
501 if (!checkGuardedVarAttrCommon(S, D, Attr))
502 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000503
Michael Handc691572012-07-23 18:48:41 +0000504 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
505}
506
Michael Hanf1aae3b2012-08-03 17:40:43 +0000507static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000508 const AttributeList &Attr) {
509 if (!checkGuardedVarAttrCommon(S, D, Attr))
510 return;
511
512 if (!threadSafetyCheckIsPointer(S, D, Attr))
513 return;
514
515 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
516}
517
Michael Hanf1aae3b2012-08-03 17:40:43 +0000518static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
519 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000520 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000521 assert(!Attr.isInvalid());
522
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000523 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000524 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000525
526 // D must be either a member field or global (potentially shared) variable.
527 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000528 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
529 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000530 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000531 }
532
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000533 SmallVector<Expr*, 1> Args;
534 // check that all arguments are lockable objects
535 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
536 unsigned Size = Args.size();
537 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000538 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000539
Michael Handc691572012-07-23 18:48:41 +0000540 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000541
Michael Handc691572012-07-23 18:48:41 +0000542 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000543}
544
Michael Handc691572012-07-23 18:48:41 +0000545static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
546 Expr *Arg = 0;
547 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
548 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000549
Michael Handc691572012-07-23 18:48:41 +0000550 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
551}
552
Michael Hanf1aae3b2012-08-03 17:40:43 +0000553static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000554 const AttributeList &Attr) {
555 Expr *Arg = 0;
556 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
557 return;
558
559 if (!threadSafetyCheckIsPointer(S, D, Attr))
560 return;
561
562 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
563 S.Context, Arg));
564}
565
Michael Hanf1aae3b2012-08-03 17:40:43 +0000566static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000567 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000568 assert(!Attr.isInvalid());
569
570 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000571 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000572
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000573 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000574 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000575 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
576 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000577 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000578 }
579
Michael Handc691572012-07-23 18:48:41 +0000580 return true;
581}
582
583static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
584 if (!checkLockableAttrCommon(S, D, Attr))
585 return;
586
587 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
588}
589
Michael Hanf1aae3b2012-08-03 17:40:43 +0000590static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000591 const AttributeList &Attr) {
592 if (!checkLockableAttrCommon(S, D, Attr))
593 return;
594
595 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000596}
597
598static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
599 const AttributeList &Attr) {
600 assert(!Attr.isInvalid());
601
602 if (!checkAttributeNumArgs(S, Attr, 0))
603 return;
604
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000605 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000606 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
607 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000608 return;
609 }
610
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000611 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000612 S.Context));
613}
614
Kostya Serebryany71efba02012-01-24 19:25:38 +0000615static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000616 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000617 assert(!Attr.isInvalid());
618
619 if (!checkAttributeNumArgs(S, Attr, 0))
620 return;
621
622 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
623 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
624 << Attr.getName() << ExpectedFunctionOrMethod;
625 return;
626 }
627
628 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000629 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000630}
631
Michael Hanf1aae3b2012-08-03 17:40:43 +0000632static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
633 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000634 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000635 assert(!Attr.isInvalid());
636
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000637 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000638 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000639
640 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000641 ValueDecl *VD = dyn_cast<ValueDecl>(D);
642 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000643 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
644 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000645 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000646 }
647
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000648 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000649 QualType QT = VD->getType();
650 if (!QT->isDependentType()) {
651 const RecordType *RT = getRecordType(QT);
652 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000653 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000654 << Attr.getName();
655 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000656 }
657 }
658
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000659 // Check that all arguments are lockable objects.
660 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000661 if (Args.size() == 0)
662 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000663
Michael Handc691572012-07-23 18:48:41 +0000664 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000665}
666
Michael Hanf1aae3b2012-08-03 17:40:43 +0000667static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000668 const AttributeList &Attr) {
669 SmallVector<Expr*, 1> Args;
670 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
671 return;
672
673 Expr **StartArg = &Args[0];
674 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000675 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000676}
677
Michael Hanf1aae3b2012-08-03 17:40:43 +0000678static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000679 const AttributeList &Attr) {
680 SmallVector<Expr*, 1> Args;
681 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
682 return;
683
684 Expr **StartArg = &Args[0];
685 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000686 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000687}
688
Michael Hanf1aae3b2012-08-03 17:40:43 +0000689static bool checkLockFunAttrCommon(Sema &S, Decl *D,
690 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000691 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000692 assert(!Attr.isInvalid());
693
694 // zero or more arguments ok
695
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000696 // check that the attribute is applied to a function
697 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000698 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
699 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000700 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000701 }
702
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000703 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000704 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000705
Michael Handc691572012-07-23 18:48:41 +0000706 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000707}
708
Michael Hanf1aae3b2012-08-03 17:40:43 +0000709static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000710 const AttributeList &Attr) {
711 SmallVector<Expr*, 1> Args;
712 if (!checkLockFunAttrCommon(S, D, Attr, Args))
713 return;
714
715 unsigned Size = Args.size();
716 Expr **StartArg = Size == 0 ? 0 : &Args[0];
717 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000718 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000719 StartArg, Size));
720}
721
Michael Hanf1aae3b2012-08-03 17:40:43 +0000722static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000723 const AttributeList &Attr) {
724 SmallVector<Expr*, 1> Args;
725 if (!checkLockFunAttrCommon(S, D, Attr, Args))
726 return;
727
728 unsigned Size = Args.size();
729 Expr **StartArg = Size == 0 ? 0 : &Args[0];
730 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000731 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000732 StartArg, Size));
733}
734
Michael Hanf1aae3b2012-08-03 17:40:43 +0000735static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
736 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000737 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000738 assert(!Attr.isInvalid());
739
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000740 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000741 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000742
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000743 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000744 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
745 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000746 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000747 }
748
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000749 if (!isIntOrBool(Attr.getArg(0))) {
750 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000751 << Attr.getName();
752 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000753 }
754
755 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000756 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000757
Michael Handc691572012-07-23 18:48:41 +0000758 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000759}
760
Michael Hanf1aae3b2012-08-03 17:40:43 +0000761static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000762 const AttributeList &Attr) {
763 SmallVector<Expr*, 2> Args;
764 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
765 return;
766
767 unsigned Size = Args.size();
768 Expr **StartArg = Size == 0 ? 0 : &Args[0];
769 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000770 S.Context,
771 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000772 StartArg, Size));
773}
774
Michael Hanf1aae3b2012-08-03 17:40:43 +0000775static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000776 const AttributeList &Attr) {
777 SmallVector<Expr*, 2> Args;
778 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
779 return;
780
781 unsigned Size = Args.size();
782 Expr **StartArg = Size == 0 ? 0 : &Args[0];
783 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000784 S.Context,
785 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000786 StartArg, Size));
787}
788
Michael Hanf1aae3b2012-08-03 17:40:43 +0000789static bool checkLocksRequiredCommon(Sema &S, Decl *D,
790 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000791 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000792 assert(!Attr.isInvalid());
793
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000794 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000795 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000796
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000797 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000798 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
799 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000800 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000801 }
802
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000803 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000804 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000805 if (Args.size() == 0)
806 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000807
Michael Handc691572012-07-23 18:48:41 +0000808 return true;
809}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000810
Michael Hanf1aae3b2012-08-03 17:40:43 +0000811static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000812 const AttributeList &Attr) {
813 SmallVector<Expr*, 1> Args;
814 if (!checkLocksRequiredCommon(S, D, Attr, Args))
815 return;
816
817 Expr **StartArg = &Args[0];
818 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000819 S.Context,
820 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000821 Args.size()));
822}
823
Michael Hanf1aae3b2012-08-03 17:40:43 +0000824static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000825 const AttributeList &Attr) {
826 SmallVector<Expr*, 1> Args;
827 if (!checkLocksRequiredCommon(S, D, Attr, Args))
828 return;
829
830 Expr **StartArg = &Args[0];
831 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000832 S.Context,
833 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000834 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000835}
836
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000837static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839 assert(!Attr.isInvalid());
840
841 // zero or more arguments ok
842
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000843 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000844 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
845 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000846 return;
847 }
848
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000849 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000850 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000851 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000852 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000853 Expr **StartArg = Size == 0 ? 0 : &Args[0];
854
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000855 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000856 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000857}
858
859static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000860 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000861 assert(!Attr.isInvalid());
862
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000863 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000864 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000865 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000866
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000867 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000868 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
869 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000870 return;
871 }
872
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000873 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000874 return;
875
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000876 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000877 SmallVector<Expr*, 1> Args;
878 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
879 unsigned Size = Args.size();
880 if (Size == 0)
881 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000882
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000883 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
884 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000885}
886
887static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000888 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000889 assert(!Attr.isInvalid());
890
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000891 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000892 return;
893
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000894 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000895 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
896 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000897 return;
898 }
899
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000900 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000901 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000902 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000903 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000904 if (Size == 0)
905 return;
906 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000907
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000908 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000909 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000910}
911
912
Chandler Carruth1b03c872011-07-02 00:01:44 +0000913static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
914 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000915 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000916 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000917 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000918 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000919 }
Mike Stumpbf916502009-07-24 19:02:52 +0000920
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000922
923 Expr *sizeExpr;
924
925 // Special case where the argument is a template id.
926 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000927 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000928 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000929 UnqualifiedId id;
930 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hanf1aae3b2012-08-03 17:40:43 +0000931
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000932 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
933 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000934 if (Size.isInvalid())
935 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000936
Douglas Gregor4ac01402011-06-15 16:02:29 +0000937 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000938 } else {
939 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000940 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000941 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000942
Peter Collingbourne7a730022010-11-23 20:45:58 +0000943 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000944 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000945
946 // Instantiate/Install the vector type, and let Sema build the type for us.
947 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000948 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000949 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000950 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000951 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000952
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000953 // Remember this typedef decl, we will need it later for diagnostics.
954 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000955 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000956}
957
Chandler Carruth1b03c872011-07-02 00:01:44 +0000958static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000959 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000960 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000961 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000962
Chandler Carruth87c44602011-07-01 23:49:12 +0000963 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000964 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000965 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000966 // If the alignment is less than or equal to 8 bits, the packed attribute
967 // has no effect.
968 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000969 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000970 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000971 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000972 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000973 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000974 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000975 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000976}
977
Chandler Carruth1b03c872011-07-02 00:01:44 +0000978static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000979 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000980 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000981 else
982 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
983}
984
Chandler Carruth1b03c872011-07-02 00:01:44 +0000985static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000986 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000987 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000988 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000989
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000990 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000991 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000992 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000993 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000994 return;
995 }
996
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000997 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000998}
999
Ted Kremenek2f041d02011-09-29 07:02:25 +00001000static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1001 // The IBOutlet/IBOutletCollection attributes only apply to instance
1002 // variables or properties of Objective-C classes. The outlet must also
1003 // have an object reference type.
1004 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1005 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001006 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001007 << Attr.getName() << VD->getType() << 0;
1008 return false;
1009 }
1010 }
1011 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1012 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001013 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001014 << Attr.getName() << PD->getType() << 1;
1015 return false;
1016 }
1017 }
1018 else {
1019 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1020 return false;
1021 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001022
Ted Kremenek2f041d02011-09-29 07:02:25 +00001023 return true;
1024}
1025
Chandler Carruth1b03c872011-07-02 00:01:44 +00001026static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001027 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001028 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001029 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001030
1031 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001032 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001033
Ted Kremenek2f041d02011-09-29 07:02:25 +00001034 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001035}
1036
Chandler Carruth1b03c872011-07-02 00:01:44 +00001037static void handleIBOutletCollection(Sema &S, Decl *D,
1038 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001039
1040 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001041 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001042 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1043 return;
1044 }
1045
Ted Kremenek2f041d02011-09-29 07:02:25 +00001046 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001047 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001048
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001049 IdentifierInfo *II = Attr.getParameterName();
1050 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001051 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001052
John McCallb3d87482010-08-24 05:47:05 +00001053 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001054 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001055 if (!TypeRep) {
1056 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1057 return;
1058 }
John McCallb3d87482010-08-24 05:47:05 +00001059 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001060 // Diagnose use of non-object type in iboutletcollection attribute.
1061 // FIXME. Gnu attribute extension ignores use of builtin types in
1062 // attributes. So, __attribute__((iboutletcollection(char))) will be
1063 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001064 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001065 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1066 return;
1067 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001068 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1069 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001070}
1071
Chandler Carruthd309c812011-07-01 23:49:16 +00001072static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001073 if (const RecordType *UT = T->getAsUnionType())
1074 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1075 RecordDecl *UD = UT->getDecl();
1076 for (RecordDecl::field_iterator it = UD->field_begin(),
1077 itend = UD->field_end(); it != itend; ++it) {
1078 QualType QT = it->getType();
1079 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1080 T = QT;
1081 return;
1082 }
1083 }
1084 }
1085}
1086
Nuno Lopes587de5b2012-05-24 00:22:00 +00001087static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001088 if (!isFunctionOrMethod(D)) {
1089 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1090 << "alloc_size" << ExpectedFunctionOrMethod;
1091 return;
1092 }
1093
Nuno Lopes587de5b2012-05-24 00:22:00 +00001094 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1095 return;
1096
1097 // In C++ the implicit 'this' function parameter also counts, and they are
1098 // counted from one.
1099 bool HasImplicitThisParam = isInstanceMethod(D);
1100 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1101
1102 SmallVector<unsigned, 8> SizeArgs;
1103
1104 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1105 E = Attr.arg_end(); I!=E; ++I) {
1106 // The argument must be an integer constant expression.
1107 Expr *Ex = *I;
1108 llvm::APSInt ArgNum;
1109 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1110 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1111 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1112 << "alloc_size" << Ex->getSourceRange();
1113 return;
1114 }
1115
1116 uint64_t x = ArgNum.getZExtValue();
1117
1118 if (x < 1 || x > NumArgs) {
1119 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1120 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1121 return;
1122 }
1123
1124 --x;
1125 if (HasImplicitThisParam) {
1126 if (x == 0) {
1127 S.Diag(Attr.getLoc(),
1128 diag::err_attribute_invalid_implicit_this_argument)
1129 << "alloc_size" << Ex->getSourceRange();
1130 return;
1131 }
1132 --x;
1133 }
1134
1135 // check if the function argument is of an integer type
1136 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1137 if (!T->isIntegerType()) {
1138 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1139 << "alloc_size" << Ex->getSourceRange();
1140 return;
1141 }
1142
Nuno Lopes587de5b2012-05-24 00:22:00 +00001143 SizeArgs.push_back(x);
1144 }
1145
1146 // check if the function returns a pointer
1147 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1148 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1149 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1150 }
1151
Nuno Lopes96c67d12012-06-18 16:27:56 +00001152 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1153 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001154}
1155
Chandler Carruth1b03c872011-07-02 00:01:44 +00001156static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001157 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1158 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001159 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001160 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001161 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001162 return;
1163 }
Mike Stumpbf916502009-07-24 19:02:52 +00001164
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001165 // In C++ the implicit 'this' function parameter also counts, and they are
1166 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001167 bool HasImplicitThisParam = isInstanceMethod(D);
1168 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001169
1170 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001171 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001172
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001173 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1174 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001175
1176
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001177 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001178 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001179 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001180 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1181 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001182 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1183 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001184 return;
1185 }
Mike Stumpbf916502009-07-24 19:02:52 +00001186
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001187 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001188
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001189 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001190 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001191 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001192 return;
1193 }
Mike Stumpbf916502009-07-24 19:02:52 +00001194
Ted Kremenek465172f2008-07-21 22:09:15 +00001195 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001196 if (HasImplicitThisParam) {
1197 if (x == 0) {
1198 S.Diag(Attr.getLoc(),
1199 diag::err_attribute_invalid_implicit_this_argument)
1200 << "nonnull" << Ex->getSourceRange();
1201 return;
1202 }
1203 --x;
1204 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001205
1206 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001207 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001208 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001209
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001210 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001211 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001212 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001213 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001214 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001215 }
Mike Stumpbf916502009-07-24 19:02:52 +00001216
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001217 NonNullArgs.push_back(x);
1218 }
Mike Stumpbf916502009-07-24 19:02:52 +00001219
1220 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1221 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001222 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001223 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1224 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001225 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001226 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001227 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001228 }
Mike Stumpbf916502009-07-24 19:02:52 +00001229
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001230 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001231 if (NonNullArgs.empty()) {
1232 // Warn the trivial case only if attribute is not coming from a
1233 // macro instantiation.
1234 if (Attr.getLoc().isFileID())
1235 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001236 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001237 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001238 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001239
1240 unsigned* start = &NonNullArgs[0];
1241 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001242 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001243 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001244 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001245}
1246
Chandler Carruth1b03c872011-07-02 00:01:44 +00001247static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001248 // This attribute must be applied to a function declaration.
1249 // The first argument to the attribute must be a string,
1250 // the name of the resource, for example "malloc".
1251 // The following arguments must be argument indexes, the arguments must be
1252 // of integer type for Returns, otherwise of pointer type.
1253 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001254 // after being held. free() should be __attribute((ownership_takes)), whereas
1255 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001256
1257 if (!AL.getParameterName()) {
1258 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1259 << AL.getName()->getName() << 1;
1260 return;
1261 }
1262 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001263 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001264 switch (AL.getKind()) {
1265 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001266 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001267 if (AL.getNumArgs() < 1) {
1268 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1269 return;
1270 }
Jordy Rose2a479922010-08-12 08:54:03 +00001271 break;
1272 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001273 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001274 if (AL.getNumArgs() < 1) {
1275 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1276 return;
1277 }
Jordy Rose2a479922010-08-12 08:54:03 +00001278 break;
1279 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001280 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001281 if (AL.getNumArgs() > 1) {
1282 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1283 << AL.getNumArgs() + 1;
1284 return;
1285 }
Jordy Rose2a479922010-08-12 08:54:03 +00001286 break;
1287 default:
1288 // This should never happen given how we are called.
1289 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001290 }
1291
Chandler Carruth87c44602011-07-01 23:49:12 +00001292 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001293 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1294 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001295 return;
1296 }
1297
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001298 // In C++ the implicit 'this' function parameter also counts, and they are
1299 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001300 bool HasImplicitThisParam = isInstanceMethod(D);
1301 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001302
Chris Lattner5f9e2722011-07-23 10:55:15 +00001303 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001304
1305 // Normalize the argument, __foo__ becomes foo.
1306 if (Module.startswith("__") && Module.endswith("__"))
1307 Module = Module.substr(2, Module.size() - 4);
1308
Chris Lattner5f9e2722011-07-23 10:55:15 +00001309 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001310
Jordy Rose2a479922010-08-12 08:54:03 +00001311 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1312 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001313
Peter Collingbourne7a730022010-11-23 20:45:58 +00001314 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001315 llvm::APSInt ArgNum(32);
1316 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1317 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1318 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1319 << AL.getName()->getName() << IdxExpr->getSourceRange();
1320 continue;
1321 }
1322
1323 unsigned x = (unsigned) ArgNum.getZExtValue();
1324
1325 if (x > NumArgs || x < 1) {
1326 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1327 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1328 continue;
1329 }
1330 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001331 if (HasImplicitThisParam) {
1332 if (x == 0) {
1333 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1334 << "ownership" << IdxExpr->getSourceRange();
1335 return;
1336 }
1337 --x;
1338 }
1339
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001340 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001341 case OwnershipAttr::Takes:
1342 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001343 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001344 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001345 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1346 // FIXME: Should also highlight argument in decl.
1347 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001348 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001349 << "pointer"
1350 << IdxExpr->getSourceRange();
1351 continue;
1352 }
1353 break;
1354 }
Sean Huntcf807c42010-08-18 23:23:40 +00001355 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001356 if (AL.getNumArgs() > 1) {
1357 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001358 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001359 llvm::APSInt ArgNum(32);
1360 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1361 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1362 S.Diag(AL.getLoc(), diag::err_ownership_type)
1363 << "ownership_returns" << "integer"
1364 << IdxExpr->getSourceRange();
1365 return;
1366 }
1367 }
1368 break;
1369 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001370 } // switch
1371
1372 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001373 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001374 i = D->specific_attr_begin<OwnershipAttr>(),
1375 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001376 i != e; ++i) {
1377 if ((*i)->getOwnKind() != K) {
1378 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1379 I!=E; ++I) {
1380 if (x == *I) {
1381 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1382 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001383 }
1384 }
1385 }
1386 }
1387 OwnershipArgs.push_back(x);
1388 }
1389
1390 unsigned* start = OwnershipArgs.data();
1391 unsigned size = OwnershipArgs.size();
1392 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001393
1394 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1395 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1396 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001397 }
Sean Huntcf807c42010-08-18 23:23:40 +00001398
Chandler Carruth87c44602011-07-01 23:49:12 +00001399 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001400 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001401}
1402
John McCall332bb2a2011-02-08 22:35:49 +00001403/// Whether this declaration has internal linkage for the purposes of
1404/// things that want to complain about things not have internal linkage.
1405static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1406 switch (D->getLinkage()) {
1407 case NoLinkage:
1408 case InternalLinkage:
1409 return true;
1410
1411 // Template instantiations that go from external to unique-external
1412 // shouldn't get diagnosed.
1413 case UniqueExternalLinkage:
1414 return true;
1415
1416 case ExternalLinkage:
1417 return false;
1418 }
1419 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001420}
1421
Chandler Carruth1b03c872011-07-02 00:01:44 +00001422static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001423 // Check the attribute arguments.
1424 if (Attr.getNumArgs() > 1) {
1425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1426 return;
1427 }
1428
Chandler Carruth87c44602011-07-01 23:49:12 +00001429 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001431 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001432 return;
1433 }
1434
Chandler Carruth87c44602011-07-01 23:49:12 +00001435 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001436
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001437 // gcc rejects
1438 // class c {
1439 // static int a __attribute__((weakref ("v2")));
1440 // static int b() __attribute__((weakref ("f3")));
1441 // };
1442 // and ignores the attributes of
1443 // void f(void) {
1444 // static int a __attribute__((weakref ("v2")));
1445 // }
1446 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001447 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001448 if (!Ctx->isFileContext()) {
1449 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001450 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001451 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001452 }
1453
1454 // The GCC manual says
1455 //
1456 // At present, a declaration to which `weakref' is attached can only
1457 // be `static'.
1458 //
1459 // It also says
1460 //
1461 // Without a TARGET,
1462 // given as an argument to `weakref' or to `alias', `weakref' is
1463 // equivalent to `weak'.
1464 //
1465 // gcc 4.4.1 will accept
1466 // int a7 __attribute__((weakref));
1467 // as
1468 // int a7 __attribute__((weak));
1469 // This looks like a bug in gcc. We reject that for now. We should revisit
1470 // it if this behaviour is actually used.
1471
John McCall332bb2a2011-02-08 22:35:49 +00001472 if (!hasEffectivelyInternalLinkage(nd)) {
1473 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001474 return;
1475 }
1476
1477 // GCC rejects
1478 // static ((alias ("y"), weakref)).
1479 // Should we? How to check that weakref is before or after alias?
1480
1481 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001482 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001483 Arg = Arg->IgnoreParenCasts();
1484 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1485
Douglas Gregor5cee1192011-07-27 05:40:30 +00001486 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001487 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1488 << "weakref" << 1;
1489 return;
1490 }
1491 // GCC will accept anything as the argument of weakref. Should we
1492 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001493 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001494 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001495 }
1496
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001497 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001498}
1499
Chandler Carruth1b03c872011-07-02 00:01:44 +00001500static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001501 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001502 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001503 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001504 return;
1505 }
Mike Stumpbf916502009-07-24 19:02:52 +00001506
Peter Collingbourne7a730022010-11-23 20:45:58 +00001507 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001508 Arg = Arg->IgnoreParenCasts();
1509 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001510
Douglas Gregor5cee1192011-07-27 05:40:30 +00001511 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001512 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001513 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001514 return;
1515 }
Mike Stumpbf916502009-07-24 19:02:52 +00001516
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001517 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001518 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1519 return;
1520 }
1521
Chris Lattner6b6b5372008-06-26 18:38:35 +00001522 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001523
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001524 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001525 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001526}
1527
Benjamin Krameree409a92012-05-12 21:10:52 +00001528static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1529 // Check the attribute arguments.
1530 if (!checkAttributeNumArgs(S, Attr, 0))
1531 return;
1532
1533 if (!isa<FunctionDecl>(D)) {
1534 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1535 << Attr.getName() << ExpectedFunction;
1536 return;
1537 }
1538
1539 if (D->hasAttr<HotAttr>()) {
1540 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1541 << Attr.getName() << "hot";
1542 return;
1543 }
1544
1545 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1546}
1547
1548static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1549 // Check the attribute arguments.
1550 if (!checkAttributeNumArgs(S, Attr, 0))
1551 return;
1552
1553 if (!isa<FunctionDecl>(D)) {
1554 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1555 << Attr.getName() << ExpectedFunction;
1556 return;
1557 }
1558
1559 if (D->hasAttr<ColdAttr>()) {
1560 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1561 << Attr.getName() << "cold";
1562 return;
1563 }
1564
1565 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1566}
1567
Chandler Carruth1b03c872011-07-02 00:01:44 +00001568static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001569 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001570 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001571 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001572
Chandler Carruth87c44602011-07-01 23:49:12 +00001573 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001574 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001575 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001576 return;
1577 }
1578
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001579 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001580}
1581
Chandler Carruth1b03c872011-07-02 00:01:44 +00001582static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1583 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001584 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001585 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1587 return;
1588 }
1589
Chandler Carruth87c44602011-07-01 23:49:12 +00001590 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001591 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001592 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001593 return;
1594 }
Mike Stumpbf916502009-07-24 19:02:52 +00001595
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001596 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001597}
1598
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001599static void handleTLSModelAttr(Sema &S, Decl *D,
1600 const AttributeList &Attr) {
1601 // Check the attribute arguments.
1602 if (Attr.getNumArgs() != 1) {
1603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1604 return;
1605 }
1606
1607 Expr *Arg = Attr.getArg(0);
1608 Arg = Arg->IgnoreParenCasts();
1609 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1610
1611 // Check that it is a string.
1612 if (!Str) {
1613 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1614 return;
1615 }
1616
1617 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1618 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1619 << Attr.getName() << ExpectedTLSVar;
1620 return;
1621 }
1622
1623 // Check that the value.
1624 StringRef Model = Str->getString();
1625 if (Model != "global-dynamic" && Model != "local-dynamic"
1626 && Model != "initial-exec" && Model != "local-exec") {
1627 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1628 return;
1629 }
1630
1631 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1632 Model));
1633}
1634
Chandler Carruth1b03c872011-07-02 00:01:44 +00001635static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001636 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001637 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1639 return;
1640 }
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Chandler Carruth87c44602011-07-01 23:49:12 +00001642 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001643 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001644 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001645 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001646 return;
1647 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001648 }
1649
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001650 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001651}
1652
Chandler Carruth1b03c872011-07-02 00:01:44 +00001653static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001654 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001655 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001656 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001657
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001658 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001659}
1660
Chandler Carruth1b03c872011-07-02 00:01:44 +00001661static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001662 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001663 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001664 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001665 else
1666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001667 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001668}
1669
Chandler Carruth1b03c872011-07-02 00:01:44 +00001670static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001671 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001672 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001673 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001674 else
1675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001676 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001677}
1678
Chandler Carruth1b03c872011-07-02 00:01:44 +00001679static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001680 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001681
1682 if (S.CheckNoReturnAttr(attr)) return;
1683
Chandler Carruth87c44602011-07-01 23:49:12 +00001684 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001685 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001686 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001687 return;
1688 }
1689
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001690 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001691}
1692
1693bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001694 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001695 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1696 attr.setInvalid();
1697 return true;
1698 }
1699
1700 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001701}
1702
Chandler Carruth1b03c872011-07-02 00:01:44 +00001703static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1704 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001705
1706 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1707 // because 'analyzer_noreturn' does not impact the type.
1708
Chandler Carruth1731e202011-07-11 23:30:35 +00001709 if(!checkAttributeNumArgs(S, Attr, 0))
1710 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001711
Chandler Carruth87c44602011-07-01 23:49:12 +00001712 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1713 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001714 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1715 && !VD->getType()->isFunctionPointerType())) {
1716 S.Diag(Attr.getLoc(),
1717 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1718 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001719 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001720 return;
1721 }
1722 }
1723
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001724 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001725}
1726
John Thompson35cc9622010-08-09 21:53:52 +00001727// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001728static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001729/*
1730 Returning a Vector Class in Registers
1731
Eric Christopherf48f3672010-12-01 22:13:54 +00001732 According to the PPU ABI specifications, a class with a single member of
1733 vector type is returned in memory when used as the return value of a function.
1734 This results in inefficient code when implementing vector classes. To return
1735 the value in a single vector register, add the vecreturn attribute to the
1736 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001737
1738 Example:
1739
1740 struct Vector
1741 {
1742 __vector float xyzw;
1743 } __attribute__((vecreturn));
1744
1745 Vector Add(Vector lhs, Vector rhs)
1746 {
1747 Vector result;
1748 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1749 return result; // This will be returned in a register
1750 }
1751*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001752 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001753 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001754 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001755 return;
1756 }
1757
Chandler Carruth87c44602011-07-01 23:49:12 +00001758 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001759 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1760 return;
1761 }
1762
Chandler Carruth87c44602011-07-01 23:49:12 +00001763 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001764 int count = 0;
1765
1766 if (!isa<CXXRecordDecl>(record)) {
1767 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1768 return;
1769 }
1770
1771 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1772 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1773 return;
1774 }
1775
Eric Christopherf48f3672010-12-01 22:13:54 +00001776 for (RecordDecl::field_iterator iter = record->field_begin();
1777 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001778 if ((count == 1) || !iter->getType()->isVectorType()) {
1779 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1780 return;
1781 }
1782 count++;
1783 }
1784
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001785 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001786}
1787
Chandler Carruth1b03c872011-07-02 00:01:44 +00001788static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001789 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001791 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001792 return;
1793 }
1794 // FIXME: Actually store the attribute on the declaration
1795}
1796
Chandler Carruth1b03c872011-07-02 00:01:44 +00001797static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001798 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001799 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001801 return;
1802 }
Mike Stumpbf916502009-07-24 19:02:52 +00001803
Chandler Carruth87c44602011-07-01 23:49:12 +00001804 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001805 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001806 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001807 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001808 return;
1809 }
Mike Stumpbf916502009-07-24 19:02:52 +00001810
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001811 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001812}
1813
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001814static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1815 const AttributeList &Attr) {
1816 // check the attribute arguments.
1817 if (Attr.hasParameterOrArguments()) {
1818 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1819 return;
1820 }
1821
1822 if (!isa<FunctionDecl>(D)) {
1823 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1824 << Attr.getName() << ExpectedFunction;
1825 return;
1826 }
1827
1828 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1829}
1830
Chandler Carruth1b03c872011-07-02 00:01:44 +00001831static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001832 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001833 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1835 return;
1836 }
Mike Stumpbf916502009-07-24 19:02:52 +00001837
Chandler Carruth87c44602011-07-01 23:49:12 +00001838 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001839 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001840 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1841 return;
1842 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001843 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001844 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001845 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001846 return;
1847 }
Mike Stumpbf916502009-07-24 19:02:52 +00001848
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001849 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001850}
1851
Chandler Carruth1b03c872011-07-02 00:01:44 +00001852static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001853 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001854 if (Attr.getNumArgs() > 1) {
1855 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001856 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001857 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001858
1859 int priority = 65535; // FIXME: Do not hardcode such constants.
1860 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001861 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001862 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001863 if (E->isTypeDependent() || E->isValueDependent() ||
1864 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001865 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001866 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001867 return;
1868 }
1869 priority = Idx.getZExtValue();
1870 }
Mike Stumpbf916502009-07-24 19:02:52 +00001871
Chandler Carruth87c44602011-07-01 23:49:12 +00001872 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001873 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001874 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001875 return;
1876 }
1877
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001878 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001879 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001880}
1881
Chandler Carruth1b03c872011-07-02 00:01:44 +00001882static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001883 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001884 if (Attr.getNumArgs() > 1) {
1885 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001886 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001887 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001888
1889 int priority = 65535; // FIXME: Do not hardcode such constants.
1890 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001891 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001892 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001893 if (E->isTypeDependent() || E->isValueDependent() ||
1894 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001895 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001896 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001897 return;
1898 }
1899 priority = Idx.getZExtValue();
1900 }
Mike Stumpbf916502009-07-24 19:02:52 +00001901
Chandler Carruth87c44602011-07-01 23:49:12 +00001902 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001903 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001904 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001905 return;
1906 }
1907
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001908 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001909 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001910}
1911
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001912template <typename AttrTy>
1913static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1914 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001915 unsigned NumArgs = Attr.getNumArgs();
1916 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001917 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001918 return;
1919 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001920
1921 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001922 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001923 if (NumArgs == 1) {
1924 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001925 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001926 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001927 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001928 return;
1929 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001930 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001931 }
Mike Stumpbf916502009-07-24 19:02:52 +00001932
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001933 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001934}
1935
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001936static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1937 const AttributeList &Attr) {
1938 unsigned NumArgs = Attr.getNumArgs();
1939 if (NumArgs > 0) {
1940 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1941 return;
1942 }
1943
1944 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001945 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001946}
1947
Patrick Beardb2f68202012-04-06 18:12:22 +00001948static void handleObjCRootClassAttr(Sema &S, Decl *D,
1949 const AttributeList &Attr) {
1950 if (!isa<ObjCInterfaceDecl>(D)) {
1951 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1952 return;
1953 }
1954
1955 unsigned NumArgs = Attr.getNumArgs();
1956 if (NumArgs > 0) {
1957 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1958 return;
1959 }
1960
1961 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1962}
1963
Ted Kremenek71207fc2012-01-05 22:47:47 +00001964static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001965 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001966 if (!isa<ObjCInterfaceDecl>(D)) {
1967 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1968 return;
1969 }
1970
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001971 unsigned NumArgs = Attr.getNumArgs();
1972 if (NumArgs > 0) {
1973 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1974 return;
1975 }
1976
Ted Kremenek71207fc2012-01-05 22:47:47 +00001977 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001978 Attr.getRange(), S.Context));
1979}
1980
Jordy Rosefad5de92012-05-08 03:27:22 +00001981static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1982 IdentifierInfo *Platform,
1983 VersionTuple Introduced,
1984 VersionTuple Deprecated,
1985 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001986 StringRef PlatformName
1987 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1988 if (PlatformName.empty())
1989 PlatformName = Platform->getName();
1990
1991 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1992 // of these steps are needed).
1993 if (!Introduced.empty() && !Deprecated.empty() &&
1994 !(Introduced <= Deprecated)) {
1995 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1996 << 1 << PlatformName << Deprecated.getAsString()
1997 << 0 << Introduced.getAsString();
1998 return true;
1999 }
2000
2001 if (!Introduced.empty() && !Obsoleted.empty() &&
2002 !(Introduced <= Obsoleted)) {
2003 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2004 << 2 << PlatformName << Obsoleted.getAsString()
2005 << 0 << Introduced.getAsString();
2006 return true;
2007 }
2008
2009 if (!Deprecated.empty() && !Obsoleted.empty() &&
2010 !(Deprecated <= Obsoleted)) {
2011 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2012 << 2 << PlatformName << Obsoleted.getAsString()
2013 << 1 << Deprecated.getAsString();
2014 return true;
2015 }
2016
2017 return false;
2018}
2019
Rafael Espindola599f1b72012-05-13 03:25:18 +00002020AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2021 IdentifierInfo *Platform,
2022 VersionTuple Introduced,
2023 VersionTuple Deprecated,
2024 VersionTuple Obsoleted,
2025 bool IsUnavailable,
2026 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002027 VersionTuple MergedIntroduced = Introduced;
2028 VersionTuple MergedDeprecated = Deprecated;
2029 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002030 bool FoundAny = false;
2031
Rafael Espindola98ae8342012-05-10 02:50:16 +00002032 if (D->hasAttrs()) {
2033 AttrVec &Attrs = D->getAttrs();
2034 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2035 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2036 if (!OldAA) {
2037 ++i;
2038 continue;
2039 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002040
Rafael Espindola98ae8342012-05-10 02:50:16 +00002041 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2042 if (OldPlatform != Platform) {
2043 ++i;
2044 continue;
2045 }
2046
2047 FoundAny = true;
2048 VersionTuple OldIntroduced = OldAA->getIntroduced();
2049 VersionTuple OldDeprecated = OldAA->getDeprecated();
2050 VersionTuple OldObsoleted = OldAA->getObsoleted();
2051 bool OldIsUnavailable = OldAA->getUnavailable();
2052 StringRef OldMessage = OldAA->getMessage();
2053
2054 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2055 OldIntroduced != Introduced) ||
2056 (!OldDeprecated.empty() && !Deprecated.empty() &&
2057 OldDeprecated != Deprecated) ||
2058 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2059 OldObsoleted != Obsoleted) ||
2060 (OldIsUnavailable != IsUnavailable) ||
2061 (OldMessage != Message)) {
2062 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2063 Diag(Range.getBegin(), diag::note_previous_attribute);
2064 Attrs.erase(Attrs.begin() + i);
2065 --e;
2066 continue;
2067 }
2068
2069 VersionTuple MergedIntroduced2 = MergedIntroduced;
2070 VersionTuple MergedDeprecated2 = MergedDeprecated;
2071 VersionTuple MergedObsoleted2 = MergedObsoleted;
2072
2073 if (MergedIntroduced2.empty())
2074 MergedIntroduced2 = OldIntroduced;
2075 if (MergedDeprecated2.empty())
2076 MergedDeprecated2 = OldDeprecated;
2077 if (MergedObsoleted2.empty())
2078 MergedObsoleted2 = OldObsoleted;
2079
2080 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2081 MergedIntroduced2, MergedDeprecated2,
2082 MergedObsoleted2)) {
2083 Attrs.erase(Attrs.begin() + i);
2084 --e;
2085 continue;
2086 }
2087
2088 MergedIntroduced = MergedIntroduced2;
2089 MergedDeprecated = MergedDeprecated2;
2090 MergedObsoleted = MergedObsoleted2;
2091 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002092 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002093 }
2094
2095 if (FoundAny &&
2096 MergedIntroduced == Introduced &&
2097 MergedDeprecated == Deprecated &&
2098 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002099 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002100
Rafael Espindola98ae8342012-05-10 02:50:16 +00002101 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002102 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002103 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2104 Introduced, Deprecated,
2105 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002106 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002107 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002108}
2109
Chandler Carruth1b03c872011-07-02 00:01:44 +00002110static void handleAvailabilityAttr(Sema &S, Decl *D,
2111 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002112 IdentifierInfo *Platform = Attr.getParameterName();
2113 SourceLocation PlatformLoc = Attr.getParameterLoc();
2114
Rafael Espindola3b294362012-05-06 19:56:25 +00002115 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002116 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2117 << Platform;
2118
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002119 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2120 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2121 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002122 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002123 StringRef Str;
2124 const StringLiteral *SE =
2125 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2126 if (SE)
2127 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002128
Rafael Espindola599f1b72012-05-13 03:25:18 +00002129 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2130 Platform,
2131 Introduced.Version,
2132 Deprecated.Version,
2133 Obsoleted.Version,
2134 IsUnavailable, Str);
2135 if (NewAttr)
2136 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002137}
2138
Rafael Espindola599f1b72012-05-13 03:25:18 +00002139VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2140 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002141 if (isa<TypedefNameDecl>(D)) {
2142 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002143 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002144 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002145 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2146 if (ExistingAttr) {
2147 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2148 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002149 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002150 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2151 Diag(Range.getBegin(), diag::note_previous_attribute);
2152 D->dropAttr<VisibilityAttr>();
2153 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002154 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002155}
2156
Chandler Carruth1b03c872011-07-02 00:01:44 +00002157static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002158 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002159 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002160 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002161
Peter Collingbourne7a730022010-11-23 20:45:58 +00002162 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002163 Arg = Arg->IgnoreParenCasts();
2164 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002165
Douglas Gregor5cee1192011-07-27 05:40:30 +00002166 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002167 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002168 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002169 return;
2170 }
Mike Stumpbf916502009-07-24 19:02:52 +00002171
Chris Lattner5f9e2722011-07-23 10:55:15 +00002172 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002173 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002174
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002175 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002176 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002177 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002178 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002179 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002180 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002181 else if (TypeStr == "protected") {
2182 // Complain about attempts to use protected visibility on targets
2183 // (like Darwin) that don't support it.
2184 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2185 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2186 type = VisibilityAttr::Default;
2187 } else {
2188 type = VisibilityAttr::Protected;
2189 }
2190 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002191 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002192 return;
2193 }
Mike Stumpbf916502009-07-24 19:02:52 +00002194
Rafael Espindola599f1b72012-05-13 03:25:18 +00002195 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2196 if (NewAttr)
2197 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002198}
2199
Chandler Carruth1b03c872011-07-02 00:01:44 +00002200static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2201 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002202 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2203 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002205 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002206 return;
2207 }
2208
Chandler Carruth87c44602011-07-01 23:49:12 +00002209 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2210 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2211 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002212 << "objc_method_family" << 1;
2213 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002215 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002216 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002217 return;
2218 }
2219
Chris Lattner5f9e2722011-07-23 10:55:15 +00002220 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002221 ObjCMethodFamilyAttr::FamilyKind family;
2222 if (param == "none")
2223 family = ObjCMethodFamilyAttr::OMF_None;
2224 else if (param == "alloc")
2225 family = ObjCMethodFamilyAttr::OMF_alloc;
2226 else if (param == "copy")
2227 family = ObjCMethodFamilyAttr::OMF_copy;
2228 else if (param == "init")
2229 family = ObjCMethodFamilyAttr::OMF_init;
2230 else if (param == "mutableCopy")
2231 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2232 else if (param == "new")
2233 family = ObjCMethodFamilyAttr::OMF_new;
2234 else {
2235 // Just warn and ignore it. This is future-proof against new
2236 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002237 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002238 return;
2239 }
2240
John McCallf85e1932011-06-15 23:02:42 +00002241 if (family == ObjCMethodFamilyAttr::OMF_init &&
2242 !method->getResultType()->isObjCObjectPointerType()) {
2243 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2244 << method->getResultType();
2245 // Ignore the attribute.
2246 return;
2247 }
2248
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002249 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002250 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002251}
2252
Chandler Carruth1b03c872011-07-02 00:01:44 +00002253static void handleObjCExceptionAttr(Sema &S, Decl *D,
2254 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002255 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002256 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002257
Chris Lattner0db29ec2009-02-14 08:09:34 +00002258 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2259 if (OCI == 0) {
2260 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2261 return;
2262 }
Mike Stumpbf916502009-07-24 19:02:52 +00002263
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002264 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002265}
2266
Chandler Carruth1b03c872011-07-02 00:01:44 +00002267static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002268 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002269 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002270 return;
2271 }
Richard Smith162e1c12011-04-15 14:24:37 +00002272 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002273 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002274 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002275 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2276 return;
2277 }
2278 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002279 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2280 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002281 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002282 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2283 return;
2284 }
2285 }
2286 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002287 // It is okay to include this attribute on properties, e.g.:
2288 //
2289 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2290 //
2291 // In this case it follows tradition and suppresses an error in the above
2292 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002293 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002294 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002295 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002296}
2297
Mike Stumpbf916502009-07-24 19:02:52 +00002298static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002299handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002300 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002301 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002302 return;
2303 }
2304
2305 if (!isa<FunctionDecl>(D)) {
2306 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2307 return;
2308 }
2309
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002310 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002311}
2312
Chandler Carruth1b03c872011-07-02 00:01:44 +00002313static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002314 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002315 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002316 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002317 return;
2318 }
Mike Stumpbf916502009-07-24 19:02:52 +00002319
Steve Naroff9eae5762008-09-18 16:44:58 +00002320 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002321 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002322 return;
2323 }
Mike Stumpbf916502009-07-24 19:02:52 +00002324
Sean Huntcf807c42010-08-18 23:23:40 +00002325 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002326 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002327 type = BlocksAttr::ByRef;
2328 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002329 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002330 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002331 return;
2332 }
Mike Stumpbf916502009-07-24 19:02:52 +00002333
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002334 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002335}
2336
Chandler Carruth1b03c872011-07-02 00:01:44 +00002337static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002338 // check the attribute arguments.
2339 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002340 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002341 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002342 }
2343
John McCall3323fad2011-09-09 07:56:05 +00002344 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002345 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002346 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002347 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002348 if (E->isTypeDependent() || E->isValueDependent() ||
2349 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002350 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002351 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002352 return;
2353 }
Mike Stumpbf916502009-07-24 19:02:52 +00002354
John McCall3323fad2011-09-09 07:56:05 +00002355 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002356 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2357 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002358 return;
2359 }
John McCall3323fad2011-09-09 07:56:05 +00002360
2361 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002362 }
2363
John McCall3323fad2011-09-09 07:56:05 +00002364 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002365 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002366 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002367 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002368 if (E->isTypeDependent() || E->isValueDependent() ||
2369 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002370 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002371 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002372 return;
2373 }
2374 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002375
John McCall3323fad2011-09-09 07:56:05 +00002376 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002377 // FIXME: This error message could be improved, it would be nice
2378 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002379 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2380 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002381 return;
2382 }
2383 }
2384
Chandler Carruth87c44602011-07-01 23:49:12 +00002385 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002386 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002387 if (isa<FunctionNoProtoType>(FT)) {
2388 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2389 return;
2390 }
Mike Stumpbf916502009-07-24 19:02:52 +00002391
Chris Lattner897cd902009-03-17 23:03:47 +00002392 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002393 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002394 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002395 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002396 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002397 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002398 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002399 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002400 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002401 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2402 if (!BD->isVariadic()) {
2403 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2404 return;
2405 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002406 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002407 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002408 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002409 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002410 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002411 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002412 int m = Ty->isFunctionPointerType() ? 0 : 1;
2413 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002414 return;
2415 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002416 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002417 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002418 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002419 return;
2420 }
Anders Carlsson77091822008-10-05 18:05:59 +00002421 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002422 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002423 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002424 return;
2425 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002426 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002427 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002428}
2429
Chandler Carruth1b03c872011-07-02 00:01:44 +00002430static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002431 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002432 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002433 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002434
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002435 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002436 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002437 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002438 return;
2439 }
Mike Stumpbf916502009-07-24 19:02:52 +00002440
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002441 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2442 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2443 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002444 return;
2445 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002446 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2447 if (MD->getResultType()->isVoidType()) {
2448 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2449 << Attr.getName() << 1;
2450 return;
2451 }
2452
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002453 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002454}
2455
Chandler Carruth1b03c872011-07-02 00:01:44 +00002456static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002457 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002458 if (Attr.hasParameterOrArguments()) {
2459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002460 return;
2461 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002462
Chandler Carruth87c44602011-07-01 23:49:12 +00002463 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002464 if (isa<CXXRecordDecl>(D)) {
2465 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2466 return;
2467 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002468 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2469 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002470 return;
2471 }
2472
Chandler Carruth87c44602011-07-01 23:49:12 +00002473 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002474
2475 // 'weak' only applies to declarations with external linkage.
2476 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002477 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002478 return;
2479 }
Mike Stumpbf916502009-07-24 19:02:52 +00002480
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002481 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002482}
2483
Chandler Carruth1b03c872011-07-02 00:01:44 +00002484static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002485 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002486 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002487 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002488
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002489
2490 // weak_import only applies to variable & function declarations.
2491 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002492 if (!D->canBeWeakImported(isDef)) {
2493 if (isDef)
2494 S.Diag(Attr.getLoc(),
2495 diag::warn_attribute_weak_import_invalid_on_definition)
2496 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002497 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002498 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002499 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002500 // Nothing to warn about here.
2501 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002502 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002503 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002504
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002505 return;
2506 }
2507
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002508 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002509}
2510
Tanya Lattner0df579e2012-07-09 22:06:01 +00002511// Handles reqd_work_group_size and work_group_size_hint.
2512static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002513 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002514 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2515 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2516
Nate Begeman6f3d8382009-06-26 06:32:41 +00002517 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002518 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002519
2520 unsigned WGSize[3];
2521 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002522 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002523 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002524 if (E->isTypeDependent() || E->isValueDependent() ||
2525 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002526 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002527 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002528 return;
2529 }
2530 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2531 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002532
2533 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2534 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2535 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2536 if (!(A->getXDim() == WGSize[0] &&
2537 A->getYDim() == WGSize[1] &&
2538 A->getZDim() == WGSize[2])) {
2539 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2540 Attr.getName();
2541 }
2542 }
2543
2544 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2545 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2546 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2547 if (!(A->getXDim() == WGSize[0] &&
2548 A->getYDim() == WGSize[1] &&
2549 A->getZDim() == WGSize[2])) {
2550 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2551 Attr.getName();
2552 }
2553 }
2554
2555 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2556 D->addAttr(::new (S.Context)
2557 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2558 WGSize[0], WGSize[1], WGSize[2]));
2559 else
2560 D->addAttr(::new (S.Context)
2561 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2562 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002563}
2564
Rafael Espindola599f1b72012-05-13 03:25:18 +00002565SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2566 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002567 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2568 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002569 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002570 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2571 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002572 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002573 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002574 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002575}
2576
Chandler Carruth1b03c872011-07-02 00:01:44 +00002577static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002578 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002579 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002580 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002581
2582 // Make sure that there is a string literal as the sections's single
2583 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002584 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002585 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002586 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002587 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002588 return;
2589 }
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Chris Lattner797c3c42009-08-10 19:03:04 +00002591 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002592 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002593 if (!Error.empty()) {
2594 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2595 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002596 return;
2597 }
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002599 // This attribute cannot be applied to local variables.
2600 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2601 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2602 return;
2603 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002604 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2605 SE->getString());
2606 if (NewAttr)
2607 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002608}
2609
Chris Lattner6b6b5372008-06-26 18:38:35 +00002610
Chandler Carruth1b03c872011-07-02 00:01:44 +00002611static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002612 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002613 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002615 return;
2616 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002617
Chandler Carruth87c44602011-07-01 23:49:12 +00002618 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002619 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002620 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002621 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002622 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002623 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002624}
2625
Chandler Carruth1b03c872011-07-02 00:01:44 +00002626static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002627 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002628 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002629 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002630 return;
2631 }
Mike Stumpbf916502009-07-24 19:02:52 +00002632
Chandler Carruth87c44602011-07-01 23:49:12 +00002633 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002634 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002635 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002636 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002637 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002638 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002639}
2640
Chandler Carruth1b03c872011-07-02 00:01:44 +00002641static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002642 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002643 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002644 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002645
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002646 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002647}
2648
Chandler Carruth1b03c872011-07-02 00:01:44 +00002649static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002650 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2652 return;
2653 }
Mike Stumpbf916502009-07-24 19:02:52 +00002654
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002655 if (Attr.getNumArgs() != 0) {
2656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2657 return;
2658 }
Mike Stumpbf916502009-07-24 19:02:52 +00002659
Chandler Carruth87c44602011-07-01 23:49:12 +00002660 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002661
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002662 if (!VD || !VD->hasLocalStorage()) {
2663 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2664 return;
2665 }
Mike Stumpbf916502009-07-24 19:02:52 +00002666
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002667 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002668 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002669 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002670 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2671 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002672 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002673 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002674 Attr.getParameterName();
2675 return;
2676 }
Mike Stumpbf916502009-07-24 19:02:52 +00002677
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002678 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2679 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002680 S.Diag(Attr.getParameterLoc(),
2681 diag::err_attribute_cleanup_arg_not_function)
2682 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002683 return;
2684 }
2685
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002686 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002687 S.Diag(Attr.getParameterLoc(),
2688 diag::err_attribute_cleanup_func_must_take_one_arg)
2689 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002690 return;
2691 }
Mike Stumpbf916502009-07-24 19:02:52 +00002692
Anders Carlsson89941c12009-02-07 23:16:50 +00002693 // We're currently more strict than GCC about what function types we accept.
2694 // If this ever proves to be a problem it should be easy to fix.
2695 QualType Ty = S.Context.getPointerType(VD->getType());
2696 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002697 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2698 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002699 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002700 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2701 Attr.getParameterName() << ParamTy << Ty;
2702 return;
2703 }
Mike Stumpbf916502009-07-24 19:02:52 +00002704
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002705 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002706 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002707}
2708
Mike Stumpbf916502009-07-24 19:02:52 +00002709/// Handle __attribute__((format_arg((idx)))) attribute based on
2710/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002711static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002712 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002713 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002714
Chandler Carruth87c44602011-07-01 23:49:12 +00002715 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002716 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002717 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002718 return;
2719 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002720
2721 // In C++ the implicit 'this' function parameter also counts, and they are
2722 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002723 bool HasImplicitThisParam = isInstanceMethod(D);
2724 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002725 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002726
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002727 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002728 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002729 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002730 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2731 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002732 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2733 << "format" << 2 << IdxExpr->getSourceRange();
2734 return;
2735 }
Mike Stumpbf916502009-07-24 19:02:52 +00002736
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002737 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2738 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2739 << "format" << 2 << IdxExpr->getSourceRange();
2740 return;
2741 }
Mike Stumpbf916502009-07-24 19:02:52 +00002742
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002743 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002744
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002745 if (HasImplicitThisParam) {
2746 if (ArgIdx == 0) {
2747 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2748 << "format_arg" << IdxExpr->getSourceRange();
2749 return;
2750 }
2751 ArgIdx--;
2752 }
2753
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002754 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002755 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002756
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002757 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2758 if (not_nsstring_type &&
2759 !isCFStringType(Ty, S.Context) &&
2760 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002761 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002762 // FIXME: Should highlight the actual expression that has the wrong type.
2763 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002764 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002765 << IdxExpr->getSourceRange();
2766 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002767 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002768 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002769 if (!isNSStringType(Ty, S.Context) &&
2770 !isCFStringType(Ty, S.Context) &&
2771 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002772 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002773 // FIXME: Should highlight the actual expression that has the wrong type.
2774 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002775 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002776 << IdxExpr->getSourceRange();
2777 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002778 }
2779
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002780 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002781 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002782}
2783
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002784enum FormatAttrKind {
2785 CFStringFormat,
2786 NSStringFormat,
2787 StrftimeFormat,
2788 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002789 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002790 InvalidFormat
2791};
2792
2793/// getFormatAttrKind - Map from format attribute names to supported format
2794/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002795static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002796 return llvm::StringSwitch<FormatAttrKind>(Format)
2797 // Check for formats that get handled specially.
2798 .Case("NSString", NSStringFormat)
2799 .Case("CFString", CFStringFormat)
2800 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002801
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002802 // Otherwise, check for supported formats.
2803 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2804 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2805 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002806
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002807 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2808 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002809}
2810
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002811/// Handle __attribute__((init_priority(priority))) attributes based on
2812/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002813static void handleInitPriorityAttr(Sema &S, Decl *D,
2814 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002815 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002816 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2817 return;
2818 }
2819
Chandler Carruth87c44602011-07-01 23:49:12 +00002820 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002821 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2822 Attr.setInvalid();
2823 return;
2824 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002825 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002826 if (S.Context.getAsArrayType(T))
2827 T = S.Context.getBaseElementType(T);
2828 if (!T->getAs<RecordType>()) {
2829 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2830 Attr.setInvalid();
2831 return;
2832 }
2833
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002834 if (Attr.getNumArgs() != 1) {
2835 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2836 Attr.setInvalid();
2837 return;
2838 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002839 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002840
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002841 llvm::APSInt priority(32);
2842 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2843 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2844 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2845 << "init_priority" << priorityExpr->getSourceRange();
2846 Attr.setInvalid();
2847 return;
2848 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002849 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002850 if (prioritynum < 101 || prioritynum > 65535) {
2851 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2852 << priorityExpr->getSourceRange();
2853 Attr.setInvalid();
2854 return;
2855 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002856 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002857 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002858}
2859
Rafael Espindola599f1b72012-05-13 03:25:18 +00002860FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2861 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002862 // Check whether we already have an equivalent format attribute.
2863 for (specific_attr_iterator<FormatAttr>
2864 i = D->specific_attr_begin<FormatAttr>(),
2865 e = D->specific_attr_end<FormatAttr>();
2866 i != e ; ++i) {
2867 FormatAttr *f = *i;
2868 if (f->getType() == Format &&
2869 f->getFormatIdx() == FormatIdx &&
2870 f->getFirstArg() == FirstArg) {
2871 // If we don't have a valid location for this attribute, adopt the
2872 // location.
2873 if (f->getLocation().isInvalid())
2874 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002875 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002876 }
2877 }
2878
Rafael Espindola599f1b72012-05-13 03:25:18 +00002879 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2880 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002881}
2882
Mike Stumpbf916502009-07-24 19:02:52 +00002883/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2884/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002885static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002886
Chris Lattner545dd342008-06-28 23:36:30 +00002887 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002888 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002889 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002890 return;
2891 }
2892
Chris Lattner545dd342008-06-28 23:36:30 +00002893 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002894 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002895 return;
2896 }
2897
Chandler Carruth87c44602011-07-01 23:49:12 +00002898 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002899 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002900 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002901 return;
2902 }
2903
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002904 // In C++ the implicit 'this' function parameter also counts, and they are
2905 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002906 bool HasImplicitThisParam = isInstanceMethod(D);
2907 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002908 unsigned FirstIdx = 1;
2909
Chris Lattner5f9e2722011-07-23 10:55:15 +00002910 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002911
2912 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002913 if (Format.startswith("__") && Format.endswith("__"))
2914 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002915
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002916 // Check for supported formats.
2917 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002918
2919 if (Kind == IgnoredFormat)
2920 return;
2921
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002922 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002923 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002924 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002925 return;
2926 }
2927
2928 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002929 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002930 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002931 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2932 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002933 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002934 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002935 return;
2936 }
2937
2938 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002939 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002940 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002941 return;
2942 }
2943
2944 // FIXME: Do we need to bounds check?
2945 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002946
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002947 if (HasImplicitThisParam) {
2948 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002949 S.Diag(Attr.getLoc(),
2950 diag::err_format_attribute_implicit_this_format_string)
2951 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002952 return;
2953 }
2954 ArgIdx--;
2955 }
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Chris Lattner6b6b5372008-06-26 18:38:35 +00002957 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002958 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002959
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002960 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002961 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002962 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2963 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002964 return;
2965 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002966 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002967 // FIXME: do we need to check if the type is NSString*? What are the
2968 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002969 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002970 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002971 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2972 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002973 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002974 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002975 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002976 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002977 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002978 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2979 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002980 return;
2981 }
2982
2983 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002984 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002985 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002986 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2987 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002988 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002989 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002990 return;
2991 }
2992
2993 // check if the function is variadic if the 3rd argument non-zero
2994 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002995 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002996 ++NumArgs; // +1 for ...
2997 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002998 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002999 return;
3000 }
3001 }
3002
Chris Lattner3c73c412008-11-19 08:23:25 +00003003 // strftime requires FirstArg to be 0 because it doesn't read from any
3004 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003005 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003006 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003007 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3008 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003009 return;
3010 }
3011 // if 0 it disables parameter checking (to use with e.g. va_list)
3012 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003013 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003014 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003015 return;
3016 }
3017
Rafael Espindola599f1b72012-05-13 03:25:18 +00003018 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3019 Idx.getZExtValue(),
3020 FirstArg.getZExtValue());
3021 if (NewAttr)
3022 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003023}
3024
Chandler Carruth1b03c872011-07-02 00:01:44 +00003025static void handleTransparentUnionAttr(Sema &S, Decl *D,
3026 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003027 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003028 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003029 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003030
Chris Lattner6b6b5372008-06-26 18:38:35 +00003031
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003032 // Try to find the underlying union declaration.
3033 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003034 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003035 if (TD && TD->getUnderlyingType()->isUnionType())
3036 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3037 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003038 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003039
3040 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003041 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003042 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003043 return;
3044 }
3045
John McCall5e1cdac2011-10-07 06:10:15 +00003046 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003047 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003048 diag::warn_transparent_union_attribute_not_definition);
3049 return;
3050 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003051
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003052 RecordDecl::field_iterator Field = RD->field_begin(),
3053 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003054 if (Field == FieldEnd) {
3055 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3056 return;
3057 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003058
David Blaikie581deb32012-06-06 20:45:41 +00003059 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003060 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003061 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003062 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003063 diag::warn_transparent_union_attribute_floating)
3064 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003065 return;
3066 }
3067
3068 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3069 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3070 for (; Field != FieldEnd; ++Field) {
3071 QualType FieldType = Field->getType();
3072 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3073 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3074 // Warn if we drop the attribute.
3075 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003076 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003077 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003078 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003079 diag::warn_transparent_union_attribute_field_size_align)
3080 << isSize << Field->getDeclName() << FieldBits;
3081 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003082 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003083 diag::note_transparent_union_first_field_size_align)
3084 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003085 return;
3086 }
3087 }
3088
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003089 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003090}
3091
Chandler Carruth1b03c872011-07-02 00:01:44 +00003092static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003093 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003094 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003095 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003096
Peter Collingbourne7a730022010-11-23 20:45:58 +00003097 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003098 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003099
Chris Lattner6b6b5372008-06-26 18:38:35 +00003100 // Make sure that there is a string literal as the annotation's single
3101 // argument.
3102 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003103 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003104 return;
3105 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003106
3107 // Don't duplicate annotations that are already set.
3108 for (specific_attr_iterator<AnnotateAttr>
3109 i = D->specific_attr_begin<AnnotateAttr>(),
3110 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3111 if ((*i)->getAnnotation() == SE->getString())
3112 return;
3113 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003114 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003115 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003116}
3117
Chandler Carruth1b03c872011-07-02 00:01:44 +00003118static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003119 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003120 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003121 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003122 return;
3123 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003124
Sean Huntbbd37c62009-11-21 08:43:09 +00003125 //FIXME: The C++0x version of this attribute has more limited applicabilty
3126 // than GNU's, and should error out when it is used to specify a
3127 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003128
Chris Lattner545dd342008-06-28 23:36:30 +00003129 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003130 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3131 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003132 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003133 }
Mike Stumpbf916502009-07-24 19:02:52 +00003134
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003135 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3136 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003137}
3138
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003139void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3140 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003141 // FIXME: Handle pack-expansions here.
3142 if (DiagnoseUnexpandedParameterPack(E))
3143 return;
3144
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003145 if (E->isTypeDependent() || E->isValueDependent()) {
3146 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003147 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3148 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003149 return;
3150 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003151
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003152 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003153 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003154 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003155 ExprResult ICE
3156 = VerifyIntegerConstantExpression(E, &Alignment,
3157 diag::err_aligned_attribute_argument_not_int,
3158 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003159 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003160 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003161 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003162 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3163 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003164 return;
3165 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003166 if (isDeclSpec) {
3167 // We've already verified it's a power of 2, now let's make sure it's
3168 // 8192 or less.
3169 if (Alignment.getZExtValue() > 8192) {
3170 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3171 << E->getSourceRange();
3172 return;
3173 }
3174 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003175
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003176 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3177 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003178}
3179
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003180void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3181 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003182 // FIXME: Cache the number on the Attr object if non-dependent?
3183 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003184 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3185 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003186 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003187}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003188
Chandler Carruthd309c812011-07-01 23:49:16 +00003189/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003190/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003191///
Mike Stumpbf916502009-07-24 19:02:52 +00003192/// Despite what would be logical, the mode attribute is a decl attribute, not a
3193/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3194/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003195static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003196 // This attribute isn't documented, but glibc uses it. It changes
3197 // the width of an int or unsigned int to the specified size.
3198
3199 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003200 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003201 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003202
Chris Lattnerfbf13472008-06-27 22:18:37 +00003203
3204 IdentifierInfo *Name = Attr.getParameterName();
3205 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003206 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003207 return;
3208 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003209
Chris Lattner5f9e2722011-07-23 10:55:15 +00003210 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003211
3212 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003213 if (Str.startswith("__") && Str.endswith("__"))
3214 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003215
3216 unsigned DestWidth = 0;
3217 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003218 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003219 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003220 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003221 switch (Str[0]) {
3222 case 'Q': DestWidth = 8; break;
3223 case 'H': DestWidth = 16; break;
3224 case 'S': DestWidth = 32; break;
3225 case 'D': DestWidth = 64; break;
3226 case 'X': DestWidth = 96; break;
3227 case 'T': DestWidth = 128; break;
3228 }
3229 if (Str[1] == 'F') {
3230 IntegerMode = false;
3231 } else if (Str[1] == 'C') {
3232 IntegerMode = false;
3233 ComplexMode = true;
3234 } else if (Str[1] != 'I') {
3235 DestWidth = 0;
3236 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003237 break;
3238 case 4:
3239 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3240 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003241 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003242 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003243 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003244 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003245 break;
3246 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003247 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003248 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003249 break;
3250 }
3251
3252 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003253 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003254 OldTy = TD->getUnderlyingType();
3255 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3256 OldTy = VD->getType();
3257 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003258 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003259 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003260 return;
3261 }
Eli Friedman73397492009-03-03 06:41:03 +00003262
John McCall183700f2009-09-21 23:43:11 +00003263 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003264 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3265 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003266 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003267 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3268 } else if (ComplexMode) {
3269 if (!OldTy->isComplexType())
3270 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3271 } else {
3272 if (!OldTy->isFloatingType())
3273 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3274 }
3275
Mike Stump390b4cc2009-05-16 07:39:55 +00003276 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3277 // and friends, at least with glibc.
3278 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3279 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003280 // FIXME: Make sure floating-point mappings are accurate
3281 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003282 QualType NewTy;
3283 switch (DestWidth) {
3284 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003285 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003286 return;
3287 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003288 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003289 return;
3290 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003291 if (!IntegerMode) {
3292 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3293 return;
3294 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003295 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003296 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003297 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003298 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003299 break;
3300 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003301 if (!IntegerMode) {
3302 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3303 return;
3304 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003305 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003306 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003307 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003308 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003309 break;
3310 case 32:
3311 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003312 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003313 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003314 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003315 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003316 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003317 break;
3318 case 64:
3319 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003320 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003321 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003322 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003323 NewTy = S.Context.LongTy;
3324 else
3325 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003326 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003327 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003328 NewTy = S.Context.UnsignedLongTy;
3329 else
3330 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003331 break;
Eli Friedman73397492009-03-03 06:41:03 +00003332 case 96:
3333 NewTy = S.Context.LongDoubleTy;
3334 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003335 case 128:
3336 if (!IntegerMode) {
3337 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3338 return;
3339 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003340 if (OldTy->isSignedIntegerType())
3341 NewTy = S.Context.Int128Ty;
3342 else
3343 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003344 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003345 }
3346
Eli Friedman73397492009-03-03 06:41:03 +00003347 if (ComplexMode) {
3348 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003349 }
3350
3351 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003352 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003353 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003354 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003355 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003356 cast<ValueDecl>(D)->setType(NewTy);
3357}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003358
Chandler Carruth1b03c872011-07-02 00:01:44 +00003359static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003360 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003361 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003362 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003363
Nick Lewycky78d1a102012-07-24 01:40:49 +00003364 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3365 if (!VD->hasGlobalStorage())
3366 S.Diag(Attr.getLoc(),
3367 diag::warn_attribute_requires_functions_or_static_globals)
3368 << Attr.getName();
3369 } else if (!isFunctionOrMethod(D)) {
3370 S.Diag(Attr.getLoc(),
3371 diag::warn_attribute_requires_functions_or_static_globals)
3372 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003373 return;
3374 }
Mike Stumpbf916502009-07-24 19:02:52 +00003375
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003376 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003377}
3378
Chandler Carruth1b03c872011-07-02 00:01:44 +00003379static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003380 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003381 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003382 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003383
Mike Stumpbf916502009-07-24 19:02:52 +00003384
Chandler Carruth87c44602011-07-01 23:49:12 +00003385 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003386 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003387 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003388 return;
3389 }
Mike Stumpbf916502009-07-24 19:02:52 +00003390
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003391 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003392}
3393
Chandler Carruth1b03c872011-07-02 00:01:44 +00003394static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3395 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003396 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003397 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003398 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003399
Chris Lattner7255a2d2010-06-22 00:03:40 +00003400
Chandler Carruth87c44602011-07-01 23:49:12 +00003401 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003402 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003403 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003404 return;
3405 }
3406
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003407 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003408 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003409}
3410
Chandler Carruth1b03c872011-07-02 00:01:44 +00003411static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003412 if (S.LangOpts.CUDA) {
3413 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003414 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3416 return;
3417 }
3418
Chandler Carruth87c44602011-07-01 23:49:12 +00003419 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003421 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003422 return;
3423 }
3424
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003425 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003426 } else {
3427 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3428 }
3429}
3430
Chandler Carruth1b03c872011-07-02 00:01:44 +00003431static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003432 if (S.LangOpts.CUDA) {
3433 // check the attribute arguments.
3434 if (Attr.getNumArgs() != 0) {
3435 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3436 return;
3437 }
3438
Chandler Carruth87c44602011-07-01 23:49:12 +00003439 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003440 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003441 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003442 return;
3443 }
3444
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003445 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003446 } else {
3447 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3448 }
3449}
3450
Chandler Carruth1b03c872011-07-02 00:01:44 +00003451static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003452 if (S.LangOpts.CUDA) {
3453 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003454 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003455 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003456
Chandler Carruth87c44602011-07-01 23:49:12 +00003457 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003458 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003459 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003460 return;
3461 }
3462
Chandler Carruth87c44602011-07-01 23:49:12 +00003463 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003464 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003465 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003466 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3467 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3468 << FD->getType()
3469 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3470 "void");
3471 } else {
3472 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3473 << FD->getType();
3474 }
3475 return;
3476 }
3477
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003478 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003479 } else {
3480 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3481 }
3482}
3483
Chandler Carruth1b03c872011-07-02 00:01:44 +00003484static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003485 if (S.LangOpts.CUDA) {
3486 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003487 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003488 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003489
Peter Collingbourneced76712010-12-01 03:15:31 +00003490
Chandler Carruth87c44602011-07-01 23:49:12 +00003491 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003492 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003493 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003494 return;
3495 }
3496
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003497 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003498 } else {
3499 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3500 }
3501}
3502
Chandler Carruth1b03c872011-07-02 00:01:44 +00003503static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003504 if (S.LangOpts.CUDA) {
3505 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003506 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003507 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003508
Peter Collingbourneced76712010-12-01 03:15:31 +00003509
Chandler Carruth87c44602011-07-01 23:49:12 +00003510 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003511 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003512 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003513 return;
3514 }
3515
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003516 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003517 } else {
3518 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3519 }
3520}
3521
Chandler Carruth1b03c872011-07-02 00:01:44 +00003522static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003523 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003524 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003525 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003526
Chandler Carruth87c44602011-07-01 23:49:12 +00003527 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003528 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003529 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003530 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003531 return;
3532 }
Mike Stumpbf916502009-07-24 19:02:52 +00003533
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003534 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003535 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003536 return;
3537 }
Mike Stumpbf916502009-07-24 19:02:52 +00003538
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003539 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003540}
3541
Chandler Carruth1b03c872011-07-02 00:01:44 +00003542static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003543 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003544
Chandler Carruth87c44602011-07-01 23:49:12 +00003545 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003546 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3547 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003548 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003549 return;
3550
Chandler Carruth87c44602011-07-01 23:49:12 +00003551 if (!isa<ObjCMethodDecl>(D)) {
3552 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3553 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003554 return;
3555 }
3556
Chandler Carruth87c44602011-07-01 23:49:12 +00003557 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003558 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003559 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003560 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003561 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003562 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003563 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003564 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003565 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003566 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003567 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003568 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003569 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003570 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003571 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003572 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003573 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003574 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003575 switch (CC) {
3576 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003577 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003578 break;
3579 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003580 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003581 break;
3582 default:
3583 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003584 }
3585
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003586 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003587 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003588 default:
3589 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003590 }
3591}
3592
Chandler Carruth1b03c872011-07-02 00:01:44 +00003593static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003594 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003595 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003596}
3597
John McCall711c52b2011-01-05 12:14:39 +00003598bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3599 if (attr.isInvalid())
3600 return true;
3601
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003602 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3603 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3604 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003605 attr.setInvalid();
3606 return true;
3607 }
3608
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003609 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3610 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003611 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003612 case AttributeList::AT_CDecl: CC = CC_C; break;
3613 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3614 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3615 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3616 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3617 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003618 Expr *Arg = attr.getArg(0);
3619 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003620 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003621 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3622 << "pcs" << 1;
3623 attr.setInvalid();
3624 return true;
3625 }
3626
Chris Lattner5f9e2722011-07-23 10:55:15 +00003627 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003628 if (StrRef == "aapcs") {
3629 CC = CC_AAPCS;
3630 break;
3631 } else if (StrRef == "aapcs-vfp") {
3632 CC = CC_AAPCS_VFP;
3633 break;
3634 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003635
3636 attr.setInvalid();
3637 Diag(attr.getLoc(), diag::err_invalid_pcs);
3638 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003639 }
David Blaikie7530c032012-01-17 06:56:22 +00003640 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003641 }
3642
3643 return false;
3644}
3645
Chandler Carruth1b03c872011-07-02 00:01:44 +00003646static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003647 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003648
3649 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003650 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003651 return;
3652
Chandler Carruth87c44602011-07-01 23:49:12 +00003653 if (!isa<ObjCMethodDecl>(D)) {
3654 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3655 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003656 return;
3657 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003658
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003659 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003660}
3661
3662/// Checks a regparm attribute, returning true if it is ill-formed and
3663/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003664bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3665 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003666 return true;
3667
Chandler Carruth87c44602011-07-01 23:49:12 +00003668 if (Attr.getNumArgs() != 1) {
3669 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3670 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003671 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003672 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003673
Chandler Carruth87c44602011-07-01 23:49:12 +00003674 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003675 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003676 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003677 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003678 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003679 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003680 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003681 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003682 }
3683
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003684 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003685 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003686 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003687 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003688 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003689 }
3690
John McCall711c52b2011-01-05 12:14:39 +00003691 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003692 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003693 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003694 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003695 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003696 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003697 }
3698
John McCall711c52b2011-01-05 12:14:39 +00003699 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003700}
3701
Chandler Carruth1b03c872011-07-02 00:01:44 +00003702static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003703 if (S.LangOpts.CUDA) {
3704 // check the attribute arguments.
3705 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003706 // FIXME: 0 is not okay.
3707 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003708 return;
3709 }
3710
Chandler Carruth87c44602011-07-01 23:49:12 +00003711 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003712 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003713 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003714 return;
3715 }
3716
3717 Expr *MaxThreadsExpr = Attr.getArg(0);
3718 llvm::APSInt MaxThreads(32);
3719 if (MaxThreadsExpr->isTypeDependent() ||
3720 MaxThreadsExpr->isValueDependent() ||
3721 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3722 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3723 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3724 return;
3725 }
3726
3727 llvm::APSInt MinBlocks(32);
3728 if (Attr.getNumArgs() > 1) {
3729 Expr *MinBlocksExpr = Attr.getArg(1);
3730 if (MinBlocksExpr->isTypeDependent() ||
3731 MinBlocksExpr->isValueDependent() ||
3732 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3733 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3734 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3735 return;
3736 }
3737 }
3738
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003739 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003740 MaxThreads.getZExtValue(),
3741 MinBlocks.getZExtValue()));
3742 } else {
3743 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3744 }
3745}
3746
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003747static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3748 const AttributeList &Attr) {
3749 StringRef AttrName = Attr.getName()->getName();
3750 if (!Attr.getParameterName()) {
3751 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3752 << Attr.getName() << /* arg num = */ 1;
3753 return;
3754 }
3755
3756 if (Attr.getNumArgs() != 2) {
3757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3758 << /* required args = */ 3;
3759 return;
3760 }
3761
3762 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3763
3764 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3765 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3766 << Attr.getName() << ExpectedFunctionOrMethod;
3767 return;
3768 }
3769
3770 uint64_t ArgumentIdx;
3771 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3772 Attr.getLoc(), 2,
3773 Attr.getArg(0), ArgumentIdx))
3774 return;
3775
3776 uint64_t TypeTagIdx;
3777 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3778 Attr.getLoc(), 3,
3779 Attr.getArg(1), TypeTagIdx))
3780 return;
3781
3782 bool IsPointer = (AttrName == "pointer_with_type_tag");
3783 if (IsPointer) {
3784 // Ensure that buffer has a pointer type.
3785 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3786 if (!BufferTy->isPointerType()) {
3787 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3788 << AttrName;
3789 }
3790 }
3791
3792 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3793 S.Context,
3794 ArgumentKind,
3795 ArgumentIdx,
3796 TypeTagIdx,
3797 IsPointer));
3798}
3799
3800static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3801 const AttributeList &Attr) {
3802 IdentifierInfo *PointerKind = Attr.getParameterName();
3803 if (!PointerKind) {
3804 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3805 << "type_tag_for_datatype" << 1;
3806 return;
3807 }
3808
3809 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3810
3811 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3812 Attr.getRange(),
3813 S.Context,
3814 PointerKind,
3815 MatchingCType,
3816 Attr.getLayoutCompatible(),
3817 Attr.getMustBeNull()));
3818}
3819
Chris Lattner0744e5f2008-06-29 00:23:49 +00003820//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003821// Checker-specific attribute handlers.
3822//===----------------------------------------------------------------------===//
3823
John McCallc7ad3812011-01-25 03:31:58 +00003824static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003825 return type->isDependentType() ||
3826 type->isObjCObjectPointerType() ||
3827 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003828}
3829static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003830 return type->isDependentType() ||
3831 type->isPointerType() ||
3832 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003833}
3834
Chandler Carruth1b03c872011-07-02 00:01:44 +00003835static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003836 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003837 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003838 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003839 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003840 return;
3841 }
3842
3843 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003844 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003845 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3846 cf = false;
3847 } else {
3848 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3849 cf = true;
3850 }
3851
3852 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003853 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003854 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003855 return;
3856 }
3857
3858 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003859 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003860 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003861 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003862}
3863
Chandler Carruth1b03c872011-07-02 00:01:44 +00003864static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3865 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003866 if (!isa<ObjCMethodDecl>(D)) {
3867 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003868 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003869 return;
3870 }
3871
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003872 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003873}
3874
Chandler Carruth1b03c872011-07-02 00:01:44 +00003875static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3876 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003877
John McCallc7ad3812011-01-25 03:31:58 +00003878 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003879
Chandler Carruth87c44602011-07-01 23:49:12 +00003880 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003881 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003882 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003883 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003884 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003885 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3886 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003887 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003888 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003889 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003890 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003891 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003892 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003893 return;
3894 }
Mike Stumpbf916502009-07-24 19:02:52 +00003895
John McCallc7ad3812011-01-25 03:31:58 +00003896 bool typeOK;
3897 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003898 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003899 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003900 case AttributeList::AT_NSReturnsAutoreleased:
3901 case AttributeList::AT_NSReturnsRetained:
3902 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003903 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3904 cf = false;
3905 break;
3906
Sean Hunt8e083e72012-06-19 23:57:03 +00003907 case AttributeList::AT_CFReturnsRetained:
3908 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003909 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3910 cf = true;
3911 break;
3912 }
3913
3914 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003915 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003916 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003917 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003918 }
Mike Stumpbf916502009-07-24 19:02:52 +00003919
Chandler Carruth87c44602011-07-01 23:49:12 +00003920 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003921 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003922 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003923 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003924 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003925 S.Context));
3926 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003927 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003928 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003929 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003930 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003931 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003932 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003933 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003934 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003935 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003936 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003937 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003938 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003939 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003940 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003941 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003942 return;
3943 };
3944}
3945
John McCalldc7c5ad2011-07-22 08:53:00 +00003946static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3947 const AttributeList &attr) {
3948 SourceLocation loc = attr.getLoc();
3949
3950 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3951
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003952 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003953 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003954 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003955 return;
3956 }
3957
3958 // Check that the method returns a normal pointer.
3959 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003960
3961 if (!resultType->isReferenceType() &&
3962 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003963 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3964 << SourceRange(loc)
3965 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3966
3967 // Drop the attribute.
3968 return;
3969 }
3970
3971 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003972 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003973}
3974
John McCall8dfac0b2011-09-30 05:12:12 +00003975/// Handle cf_audited_transfer and cf_unknown_transfer.
3976static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3977 if (!isa<FunctionDecl>(D)) {
3978 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003979 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003980 return;
3981 }
3982
Sean Hunt8e083e72012-06-19 23:57:03 +00003983 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00003984
3985 // Check whether there's a conflicting attribute already present.
3986 Attr *Existing;
3987 if (IsAudited) {
3988 Existing = D->getAttr<CFUnknownTransferAttr>();
3989 } else {
3990 Existing = D->getAttr<CFAuditedTransferAttr>();
3991 }
3992 if (Existing) {
3993 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3994 << A.getName()
3995 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3996 << A.getRange() << Existing->getRange();
3997 return;
3998 }
3999
4000 // All clear; add the attribute.
4001 if (IsAudited) {
4002 D->addAttr(
4003 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4004 } else {
4005 D->addAttr(
4006 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4007 }
4008}
4009
John McCallfe98da02011-09-29 07:17:38 +00004010static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4011 const AttributeList &Attr) {
4012 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4013 if (!RD || RD->isUnion()) {
4014 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004015 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004016 }
4017
4018 IdentifierInfo *ParmName = Attr.getParameterName();
4019
4020 // In Objective-C, verify that the type names an Objective-C type.
4021 // We don't want to check this outside of ObjC because people sometimes
4022 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004023 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004024 // Check for an existing type with this name.
4025 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4026 Sema::LookupOrdinaryName);
4027 if (S.LookupName(R, Sc)) {
4028 NamedDecl *Target = R.getFoundDecl();
4029 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4030 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4031 S.Diag(Target->getLocStart(), diag::note_declared_at);
4032 }
4033 }
4034 }
4035
4036 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4037 ParmName));
4038}
4039
Chandler Carruth1b03c872011-07-02 00:01:44 +00004040static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4041 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004042 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004043
Chandler Carruth87c44602011-07-01 23:49:12 +00004044 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004045 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004046}
4047
Chandler Carruth1b03c872011-07-02 00:01:44 +00004048static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4049 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004050 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004051 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004052 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004053 return;
4054 }
4055
Chandler Carruth87c44602011-07-01 23:49:12 +00004056 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004057 QualType type = vd->getType();
4058
4059 if (!type->isDependentType() &&
4060 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004061 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004062 << type;
4063 return;
4064 }
4065
4066 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4067
4068 // If we have no lifetime yet, check the lifetime we're presumably
4069 // going to infer.
4070 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4071 lifetime = type->getObjCARCImplicitLifetime();
4072
4073 switch (lifetime) {
4074 case Qualifiers::OCL_None:
4075 assert(type->isDependentType() &&
4076 "didn't infer lifetime for non-dependent type?");
4077 break;
4078
4079 case Qualifiers::OCL_Weak: // meaningful
4080 case Qualifiers::OCL_Strong: // meaningful
4081 break;
4082
4083 case Qualifiers::OCL_ExplicitNone:
4084 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004085 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004086 << (lifetime == Qualifiers::OCL_Autoreleasing);
4087 break;
4088 }
4089
Chandler Carruth87c44602011-07-01 23:49:12 +00004090 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004091 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004092}
4093
Francois Pichet11542142010-12-19 06:50:37 +00004094//===----------------------------------------------------------------------===//
4095// Microsoft specific attribute handlers.
4096//===----------------------------------------------------------------------===//
4097
Chandler Carruth1b03c872011-07-02 00:01:44 +00004098static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004099 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004100 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004101 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004102 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004103
Francois Pichet11542142010-12-19 06:50:37 +00004104 Expr *Arg = Attr.getArg(0);
4105 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004106 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004107 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4108 << "uuid" << 1;
4109 return;
4110 }
4111
Chris Lattner5f9e2722011-07-23 10:55:15 +00004112 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004113
4114 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4115 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004116
Francois Pichetd3d3be92010-12-20 01:41:49 +00004117 // Validate GUID length.
4118 if (IsCurly && StrRef.size() != 38) {
4119 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4120 return;
4121 }
4122 if (!IsCurly && StrRef.size() != 36) {
4123 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4124 return;
4125 }
4126
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004127 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004128 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004129 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004130 if (IsCurly) // Skip the optional '{'
4131 ++I;
4132
4133 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004134 if (i == 8 || i == 13 || i == 18 || i == 23) {
4135 if (*I != '-') {
4136 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4137 return;
4138 }
4139 } else if (!isxdigit(*I)) {
4140 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4141 return;
4142 }
4143 I++;
4144 }
Francois Pichet11542142010-12-19 06:50:37 +00004145
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004146 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004147 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004148 } else
Francois Pichet11542142010-12-19 06:50:37 +00004149 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004150}
4151
John McCallc052dbb2012-05-22 21:28:12 +00004152static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4153 if (S.LangOpts.MicrosoftExt) {
4154 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004155 if (Kind == AttributeList::AT_SingleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004156 D->addAttr(
4157 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004158 else if (Kind == AttributeList::AT_MultipleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004159 D->addAttr(
4160 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004161 else if (Kind == AttributeList::AT_VirtualInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004162 D->addAttr(
4163 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
4164 } else
4165 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4166}
4167
4168static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4169 if (S.LangOpts.MicrosoftExt) {
4170 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004171 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004172 D->addAttr(
4173 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004174 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004175 D->addAttr(
4176 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004177 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004178 D->addAttr(
4179 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4180 } else
4181 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4182}
4183
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004184static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4185 if (S.LangOpts.MicrosoftExt)
4186 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4187 else
4188 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4189}
4190
Ted Kremenekb71368d2009-05-09 02:44:38 +00004191//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004192// Top Level Sema Entry Points
4193//===----------------------------------------------------------------------===//
4194
Chandler Carruth1b03c872011-07-02 00:01:44 +00004195static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4196 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004197 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004198 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4199 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4200 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004201 default:
4202 break;
4203 }
4204}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004205
Chandler Carruth1b03c872011-07-02 00:01:44 +00004206static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4207 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004208 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004209 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4210 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4211 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004212 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004213 case AttributeList::AT_AddressSpace:
4214 case AttributeList::AT_OpenCLImageAccess:
4215 case AttributeList::AT_ObjCGC:
4216 case AttributeList::AT_VectorSize:
4217 case AttributeList::AT_NeonVectorType:
4218 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004219 // Ignore these, these are type attributes, handled by
4220 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004221 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004222 case AttributeList::AT_CUDADevice:
4223 case AttributeList::AT_CUDAHost:
4224 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004225 // Ignore, this is a non-inheritable attribute, handled
4226 // by ProcessNonInheritableDeclAttr.
4227 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004228 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4229 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4230 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4231 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004232 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004233 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004234 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004235 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004236 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4237 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4238 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004239 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004240 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4241 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4242 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4243 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004244 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4245 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004246 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4247 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004248 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004249 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004250 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4251 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4252 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4253 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4254 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004255 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004256 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004257 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4258 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4259 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4260 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4261 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004262 case AttributeList::AT_ownership_returns:
4263 case AttributeList::AT_ownership_takes:
4264 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004265 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004266 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4267 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4268 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4269 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4270 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4271 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4272 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004273
Sean Hunt8e083e72012-06-19 23:57:03 +00004274 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004275 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004276 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004277 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004278
Sean Hunt8e083e72012-06-19 23:57:03 +00004279 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004280 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4281
Sean Hunt8e083e72012-06-19 23:57:03 +00004282 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004283 handleNSBridgedAttr(S, scope, D, Attr); break;
4284
Sean Hunt8e083e72012-06-19 23:57:03 +00004285 case AttributeList::AT_CFAuditedTransfer:
4286 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004287 handleCFTransferAttr(S, D, Attr); break;
4288
Ted Kremenekb71368d2009-05-09 02:44:38 +00004289 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004290 case AttributeList::AT_CFConsumed:
4291 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4292 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004293 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004294
Sean Hunt8e083e72012-06-19 23:57:03 +00004295 case AttributeList::AT_NSReturnsAutoreleased:
4296 case AttributeList::AT_NSReturnsNotRetained:
4297 case AttributeList::AT_CFReturnsNotRetained:
4298 case AttributeList::AT_NSReturnsRetained:
4299 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004300 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004301
Tanya Lattner0df579e2012-07-09 22:06:01 +00004302 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004303 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004304 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004305
Sean Hunt8e083e72012-06-19 23:57:03 +00004306 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004307 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004308
Sean Hunt8e083e72012-06-19 23:57:03 +00004309 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4310 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4311 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004312 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4313 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004314 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004315 handleArcWeakrefUnavailableAttr (S, D, Attr);
4316 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004317 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004318 handleObjCRootClassAttr(S, D, Attr);
4319 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004320 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004321 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004322 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004323 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4324 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004325 handleReturnsTwiceAttr(S, D, Attr);
4326 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004327 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4328 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4329 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004330 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004331 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4332 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4333 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4334 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004335 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004336 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004337 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004338 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004339 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004340 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004341 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004342 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004343 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4344 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4345 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4346 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4347 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4348 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4349 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4350 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4351 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004352 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004353 // Just ignore
4354 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004355 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004356 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004357 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004358 case AttributeList::AT_StdCall:
4359 case AttributeList::AT_CDecl:
4360 case AttributeList::AT_FastCall:
4361 case AttributeList::AT_ThisCall:
4362 case AttributeList::AT_Pascal:
4363 case AttributeList::AT_Pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004364 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004365 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004366 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004367 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004368 break;
John McCallc052dbb2012-05-22 21:28:12 +00004369
4370 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004371 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004372 handleMsStructAttr(S, D, Attr);
4373 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004374 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004375 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004376 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004377 case AttributeList::AT_SingleInheritance:
4378 case AttributeList::AT_MultipleInheritance:
4379 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004380 handleInheritanceAttr(S, D, Attr);
4381 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004382 case AttributeList::AT_Win64:
4383 case AttributeList::AT_Ptr32:
4384 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004385 handlePortabilityAttr(S, D, Attr);
4386 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004387 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004388 handleForceInlineAttr(S, D, Attr);
4389 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004390
4391 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004392 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004393 handleGuardedVarAttr(S, D, Attr);
4394 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004395 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004396 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004397 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004398 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004399 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004400 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004401 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004402 handleNoAddressSafetyAttr(S, D, Attr);
4403 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004404 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004405 handleNoThreadSafetyAttr(S, D, Attr);
4406 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004407 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004408 handleLockableAttr(S, D, Attr);
4409 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004410 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004411 handleGuardedByAttr(S, D, Attr);
4412 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004413 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004414 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004415 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004416 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004417 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004418 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004419 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004420 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004421 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004422 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004423 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004424 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004425 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004426 handleLockReturnedAttr(S, D, Attr);
4427 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004428 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004429 handleLocksExcludedAttr(S, D, Attr);
4430 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004431 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004432 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004433 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004434 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004435 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004436 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004437 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004438 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004439 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004440 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004441 handleUnlockFunAttr(S, D, Attr);
4442 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004443 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004444 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004445 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004446 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004447 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004448 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004449
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004450 // Type safety attributes.
4451 case AttributeList::AT_ArgumentWithTypeTag:
4452 handleArgumentWithTypeTagAttr(S, D, Attr);
4453 break;
4454 case AttributeList::AT_TypeTagForDatatype:
4455 handleTypeTagForDatatypeAttr(S, D, Attr);
4456 break;
4457
Chris Lattner803d0802008-06-29 00:43:07 +00004458 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004459 // Ask target about the attribute.
4460 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4461 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004462 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4463 diag::warn_unhandled_ms_attribute_ignored :
4464 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004465 break;
4466 }
4467}
4468
Peter Collingbourne60700392011-01-21 02:08:45 +00004469/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4470/// the attribute applies to decls. If the attribute is a type attribute, just
4471/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4472/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004473static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4474 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004475 bool NonInheritable, bool Inheritable) {
4476 if (Attr.isInvalid())
4477 return;
4478
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004479 // Type attributes are still treated as declaration attributes by
4480 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4481 // want to process them, however, because we will simply warn about ignoring
4482 // them. So instead, we will bail out early.
4483 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004484 return;
4485
4486 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004487 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004488
4489 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004490 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004491}
4492
Chris Lattner803d0802008-06-29 00:43:07 +00004493/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4494/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004495void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004496 const AttributeList *AttrList,
4497 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004498 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004499 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004500 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004501
4502 // GCC accepts
4503 // static int a9 __attribute__((weakref));
4504 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004505 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004506 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004507 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004508 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004509 }
4510}
4511
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004512// Annotation attributes are the only attributes allowed after an access
4513// specifier.
4514bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4515 const AttributeList *AttrList) {
4516 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004517 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004518 handleAnnotateAttr(*this, ASDecl, *l);
4519 } else {
4520 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4521 return true;
4522 }
4523 }
4524
4525 return false;
4526}
4527
John McCalle82247a2011-10-01 05:17:03 +00004528/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4529/// contains any decl attributes that we should warn about.
4530static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4531 for ( ; A; A = A->getNext()) {
4532 // Only warn if the attribute is an unignored, non-type attribute.
4533 if (A->isUsedAsTypeAttr()) continue;
4534 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4535
4536 if (A->getKind() == AttributeList::UnknownAttribute) {
4537 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4538 << A->getName() << A->getRange();
4539 } else {
4540 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4541 << A->getName() << A->getRange();
4542 }
4543 }
4544}
4545
4546/// checkUnusedDeclAttributes - Given a declarator which is not being
4547/// used to build a declaration, complain about any decl attributes
4548/// which might be lying around on it.
4549void Sema::checkUnusedDeclAttributes(Declarator &D) {
4550 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4551 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4552 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4553 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4554}
4555
Ryan Flynne25ff832009-07-30 03:15:39 +00004556/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004557/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004558NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4559 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004560 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004561 NamedDecl *NewD = 0;
4562 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004563 FunctionDecl *NewFD;
4564 // FIXME: Missing call to CheckFunctionDeclaration().
4565 // FIXME: Mangling?
4566 // FIXME: Is the qualifier info correct?
4567 // FIXME: Is the DeclContext correct?
4568 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4569 Loc, Loc, DeclarationName(II),
4570 FD->getType(), FD->getTypeSourceInfo(),
4571 SC_None, SC_None,
4572 false/*isInlineSpecified*/,
4573 FD->hasPrototype(),
4574 false/*isConstexprSpecified*/);
4575 NewD = NewFD;
4576
4577 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004578 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004579
4580 // Fake up parameter variables; they are declared as if this were
4581 // a typedef.
4582 QualType FDTy = FD->getType();
4583 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4584 SmallVector<ParmVarDecl*, 16> Params;
4585 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4586 AE = FT->arg_type_end(); AI != AE; ++AI) {
4587 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4588 Param->setScopeInfo(0, Params.size());
4589 Params.push_back(Param);
4590 }
David Blaikie4278c652011-09-21 18:16:56 +00004591 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004592 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004593 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4594 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004595 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004596 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004597 VD->getStorageClass(),
4598 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004599 if (VD->getQualifier()) {
4600 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004601 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004602 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004603 }
4604 return NewD;
4605}
4606
James Dennett1dfbd922012-06-14 21:40:34 +00004607/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004608/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004609void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004610 if (W.getUsed()) return; // only do this once
4611 W.setUsed(true);
4612 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4613 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004614 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004615 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4616 NDId->getName()));
4617 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004618 WeakTopLevelDecl.push_back(NewD);
4619 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4620 // to insert Decl at TU scope, sorry.
4621 DeclContext *SavedContext = CurContext;
4622 CurContext = Context.getTranslationUnitDecl();
4623 PushOnScopeChains(NewD, S);
4624 CurContext = SavedContext;
4625 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004626 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004627 }
4628}
4629
Chris Lattner0744e5f2008-06-29 00:23:49 +00004630/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4631/// it, apply them to D. This is a bit tricky because PD can have attributes
4632/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004633void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4634 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004635 // It's valid to "forward-declare" #pragma weak, in which case we
4636 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004637 if (Inheritable) {
4638 LoadExternalWeakUndeclaredIdentifiers();
4639 if (!WeakUndeclaredIdentifiers.empty()) {
4640 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4641 if (IdentifierInfo *Id = ND->getIdentifier()) {
4642 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4643 = WeakUndeclaredIdentifiers.find(Id);
4644 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4645 WeakInfo W = I->second;
4646 DeclApplyPragmaWeak(S, ND, W);
4647 WeakUndeclaredIdentifiers[Id] = W;
4648 }
John McCalld4aff0e2010-10-27 00:59:00 +00004649 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004650 }
4651 }
4652 }
4653
Chris Lattner0744e5f2008-06-29 00:23:49 +00004654 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004655 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004656 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004657
Chris Lattner0744e5f2008-06-29 00:23:49 +00004658 // Walk the declarator structure, applying decl attributes that were in a type
4659 // position to the decl itself. This handles cases like:
4660 // int *__attr__(x)** D;
4661 // when X is a decl attribute.
4662 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4663 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004664 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004665
Chris Lattner0744e5f2008-06-29 00:23:49 +00004666 // Finally, apply any attributes on the decl itself.
4667 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004668 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004669}
John McCall54abf7d2009-11-04 02:18:39 +00004670
John McCallf85e1932011-06-15 23:02:42 +00004671/// Is the given declaration allowed to use a forbidden type?
4672static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4673 // Private ivars are always okay. Unfortunately, people don't
4674 // always properly make their ivars private, even in system headers.
4675 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004676 // Function declarations in sys headers will be marked unavailable.
4677 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4678 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004679 return false;
4680
4681 // Require it to be declared in a system header.
4682 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4683}
4684
4685/// Handle a delayed forbidden-type diagnostic.
4686static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4687 Decl *decl) {
4688 if (decl && isForbiddenTypeAllowed(S, decl)) {
4689 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4690 "this system declaration uses an unsupported type"));
4691 return;
4692 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004693 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004694 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004695 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004696 // kind of forbidden type messages on unavailable functions.
4697 if (FD->hasAttr<UnavailableAttr>() &&
4698 diag.getForbiddenTypeDiagnostic() ==
4699 diag::err_arc_array_param_no_ownership) {
4700 diag.Triggered = true;
4701 return;
4702 }
4703 }
John McCallf85e1932011-06-15 23:02:42 +00004704
4705 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4706 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4707 diag.Triggered = true;
4708}
4709
John McCall92576642012-05-07 06:16:41 +00004710void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4711 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004712 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004713 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004714
John McCall92576642012-05-07 06:16:41 +00004715 // When delaying diagnostics to run in the context of a parsed
4716 // declaration, we only want to actually emit anything if parsing
4717 // succeeds.
4718 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004719
John McCall92576642012-05-07 06:16:41 +00004720 // We emit all the active diagnostics in this pool or any of its
4721 // parents. In general, we'll get one pool for the decl spec
4722 // and a child pool for each declarator; in a decl group like:
4723 // deprecated_typedef foo, *bar, baz();
4724 // only the declarator pops will be passed decls. This is correct;
4725 // we really do need to consider delayed diagnostics from the decl spec
4726 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004727 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004728 do {
John McCall13489672012-05-07 06:16:58 +00004729 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004730 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4731 // This const_cast is a bit lame. Really, Triggered should be mutable.
4732 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004733 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004734 continue;
4735
John McCalleee1d542011-02-14 07:13:47 +00004736 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004737 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004738 // Don't bother giving deprecation diagnostics if the decl is invalid.
4739 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004740 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004741 break;
4742
4743 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004744 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004745 break;
John McCallf85e1932011-06-15 23:02:42 +00004746
4747 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004748 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004749 break;
John McCall2f514482010-01-27 03:50:35 +00004750 }
4751 }
John McCall92576642012-05-07 06:16:41 +00004752 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004753}
4754
John McCall13489672012-05-07 06:16:58 +00004755/// Given a set of delayed diagnostics, re-emit them as if they had
4756/// been delayed in the current context instead of in the given pool.
4757/// Essentially, this just moves them to the current pool.
4758void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4759 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4760 assert(curPool && "re-emitting in undelayed context not supported");
4761 curPool->steal(pool);
4762}
4763
John McCall54abf7d2009-11-04 02:18:39 +00004764static bool isDeclDeprecated(Decl *D) {
4765 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004766 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004767 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004768 // A category implicitly has the availability of the interface.
4769 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4770 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004771 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4772 return false;
4773}
4774
Eli Friedmanc3b23082012-08-08 21:52:41 +00004775static void
4776DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4777 SourceLocation Loc,
4778 const ObjCInterfaceDecl *UnknownObjCClass) {
4779 DeclarationName Name = D->getDeclName();
4780 if (!Message.empty()) {
4781 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4782 S.Diag(D->getLocation(),
4783 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4784 : diag::note_previous_decl) << Name;
4785 } else if (!UnknownObjCClass) {
4786 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4787 S.Diag(D->getLocation(),
4788 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4789 : diag::note_previous_decl) << Name;
4790 } else {
4791 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4792 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4793 }
4794}
4795
John McCall9c3087b2010-08-26 02:13:20 +00004796void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004797 Decl *Ctx) {
4798 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004799 return;
4800
John McCall2f514482010-01-27 03:50:35 +00004801 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004802 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4803 DD.getDeprecationMessage(), DD.Loc,
4804 DD.getUnknownObjCClass());
John McCall54abf7d2009-11-04 02:18:39 +00004805}
4806
Chris Lattner5f9e2722011-07-23 10:55:15 +00004807void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004808 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004809 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004810 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004811 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004812 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4813 UnknownObjCClass,
4814 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004815 return;
4816 }
4817
4818 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004819 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004820 return;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004821 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass);
John McCall54abf7d2009-11-04 02:18:39 +00004822}