blob: 8ffffab8b59fe04a8135cf717c93075276a3b492 [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 Hutchins0b4db3e2012-09-07 17:34:53 +0000418 if (StrLit->getLength() == 0 ||
419 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000420 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000421 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000422 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000423 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000424 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000425
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000426 // We allow constant strings to be used as a placeholder for expressions
427 // that are not valid C++ syntax, but warn that they are ignored.
428 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
429 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000430 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000431 continue;
432 }
433
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000434 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000435
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000436 // A pointer to member expression of the form &MyClass::mu is treated
437 // specially -- we need to look at the type of the member.
438 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
439 if (UOp->getOpcode() == UO_AddrOf)
440 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
441 if (DRE->getDecl()->isCXXInstanceMember())
442 ArgTy = DRE->getDecl()->getType();
443
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000444 // First see if we can just cast to record type, or point to record type.
445 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000446
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447 // Now check if we index into a record type function param.
448 if(!RT && ParamIdxOk) {
449 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000450 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
451 if(FD && IL) {
452 unsigned int NumParams = FD->getNumParams();
453 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000454 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
455 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
456 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
458 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000459 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000460 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000461 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 }
463 }
464
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000465 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000466
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000467 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000469}
470
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000471//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000472// Attribute Implementations
473//===----------------------------------------------------------------------===//
474
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000475// FIXME: All this manual attribute parsing code is gross. At the
476// least add some helper functions to check most argument patterns (#
477// and types of args).
478
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000479enum ThreadAttributeDeclKind {
480 ThreadExpectedFieldOrGlobalVar,
481 ThreadExpectedFunctionOrMethod,
482 ThreadExpectedClassOrStruct
483};
484
Michael Hanf1aae3b2012-08-03 17:40:43 +0000485static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000486 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000487 assert(!Attr.isInvalid());
488
489 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000490 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000491
492 // D must be either a member field or global (potentially shared) variable.
493 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000494 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
495 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000496 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000497 }
498
Michael Handc691572012-07-23 18:48:41 +0000499 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500}
501
Michael Handc691572012-07-23 18:48:41 +0000502static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
503 if (!checkGuardedVarAttrCommon(S, D, Attr))
504 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000505
Michael Handc691572012-07-23 18:48:41 +0000506 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
507}
508
Michael Hanf1aae3b2012-08-03 17:40:43 +0000509static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000510 const AttributeList &Attr) {
511 if (!checkGuardedVarAttrCommon(S, D, Attr))
512 return;
513
514 if (!threadSafetyCheckIsPointer(S, D, Attr))
515 return;
516
517 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
518}
519
Michael Hanf1aae3b2012-08-03 17:40:43 +0000520static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
521 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000522 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000523 assert(!Attr.isInvalid());
524
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000525 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000526 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000527
528 // D must be either a member field or global (potentially shared) variable.
529 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000530 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
531 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000532 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000533 }
534
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000535 SmallVector<Expr*, 1> Args;
536 // check that all arguments are lockable objects
537 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
538 unsigned Size = Args.size();
539 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000540 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000541
Michael Handc691572012-07-23 18:48:41 +0000542 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000543
Michael Handc691572012-07-23 18:48:41 +0000544 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000545}
546
Michael Handc691572012-07-23 18:48:41 +0000547static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
548 Expr *Arg = 0;
549 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
550 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000551
Michael Handc691572012-07-23 18:48:41 +0000552 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
553}
554
Michael Hanf1aae3b2012-08-03 17:40:43 +0000555static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000556 const AttributeList &Attr) {
557 Expr *Arg = 0;
558 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
559 return;
560
561 if (!threadSafetyCheckIsPointer(S, D, Attr))
562 return;
563
564 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
565 S.Context, Arg));
566}
567
Michael Hanf1aae3b2012-08-03 17:40:43 +0000568static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000570 assert(!Attr.isInvalid());
571
572 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000573 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000574
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000575 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000576 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000577 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
578 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000579 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000580 }
581
Michael Handc691572012-07-23 18:48:41 +0000582 return true;
583}
584
585static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
586 if (!checkLockableAttrCommon(S, D, Attr))
587 return;
588
589 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
590}
591
Michael Hanf1aae3b2012-08-03 17:40:43 +0000592static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000593 const AttributeList &Attr) {
594 if (!checkLockableAttrCommon(S, D, Attr))
595 return;
596
597 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000598}
599
600static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
601 const AttributeList &Attr) {
602 assert(!Attr.isInvalid());
603
604 if (!checkAttributeNumArgs(S, Attr, 0))
605 return;
606
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000607 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000608 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
609 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000610 return;
611 }
612
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000613 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000614 S.Context));
615}
616
Kostya Serebryany71efba02012-01-24 19:25:38 +0000617static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000618 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000619 assert(!Attr.isInvalid());
620
621 if (!checkAttributeNumArgs(S, Attr, 0))
622 return;
623
624 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
626 << Attr.getName() << ExpectedFunctionOrMethod;
627 return;
628 }
629
630 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000631 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000632}
633
Michael Hanf1aae3b2012-08-03 17:40:43 +0000634static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
635 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000636 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000637 assert(!Attr.isInvalid());
638
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000639 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000640 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000641
642 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000643 ValueDecl *VD = dyn_cast<ValueDecl>(D);
644 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000645 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
646 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000647 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000648 }
649
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000650 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000651 QualType QT = VD->getType();
652 if (!QT->isDependentType()) {
653 const RecordType *RT = getRecordType(QT);
654 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000655 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000656 << Attr.getName();
657 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000658 }
659 }
660
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000661 // Check that all arguments are lockable objects.
662 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000663 if (Args.size() == 0)
664 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000665
Michael Handc691572012-07-23 18:48:41 +0000666 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000667}
668
Michael Hanf1aae3b2012-08-03 17:40:43 +0000669static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000670 const AttributeList &Attr) {
671 SmallVector<Expr*, 1> Args;
672 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
673 return;
674
675 Expr **StartArg = &Args[0];
676 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000677 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000678}
679
Michael Hanf1aae3b2012-08-03 17:40:43 +0000680static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000681 const AttributeList &Attr) {
682 SmallVector<Expr*, 1> Args;
683 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
684 return;
685
686 Expr **StartArg = &Args[0];
687 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000688 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000689}
690
Michael Hanf1aae3b2012-08-03 17:40:43 +0000691static bool checkLockFunAttrCommon(Sema &S, Decl *D,
692 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000693 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000694 assert(!Attr.isInvalid());
695
696 // zero or more arguments ok
697
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000698 // check that the attribute is applied to a function
699 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000700 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
701 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000702 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000703 }
704
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000706 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000707
Michael Handc691572012-07-23 18:48:41 +0000708 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000709}
710
Michael Hanf1aae3b2012-08-03 17:40:43 +0000711static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000712 const AttributeList &Attr) {
713 SmallVector<Expr*, 1> Args;
714 if (!checkLockFunAttrCommon(S, D, Attr, Args))
715 return;
716
717 unsigned Size = Args.size();
718 Expr **StartArg = Size == 0 ? 0 : &Args[0];
719 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000720 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000721 StartArg, Size));
722}
723
Michael Hanf1aae3b2012-08-03 17:40:43 +0000724static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 1> Args;
727 if (!checkLockFunAttrCommon(S, D, Attr, Args))
728 return;
729
730 unsigned Size = Args.size();
731 Expr **StartArg = Size == 0 ? 0 : &Args[0];
732 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000733 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000734 StartArg, Size));
735}
736
Michael Hanf1aae3b2012-08-03 17:40:43 +0000737static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
738 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000739 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000740 assert(!Attr.isInvalid());
741
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000742 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000743 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000744
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000745 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000746 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
747 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000748 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000749 }
750
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000751 if (!isIntOrBool(Attr.getArg(0))) {
752 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000753 << Attr.getName();
754 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000755 }
756
757 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000758 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000759
Michael Handc691572012-07-23 18:48:41 +0000760 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761}
762
Michael Hanf1aae3b2012-08-03 17:40:43 +0000763static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000764 const AttributeList &Attr) {
765 SmallVector<Expr*, 2> Args;
766 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
767 return;
768
769 unsigned Size = Args.size();
770 Expr **StartArg = Size == 0 ? 0 : &Args[0];
771 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000772 S.Context,
773 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000774 StartArg, Size));
775}
776
Michael Hanf1aae3b2012-08-03 17:40:43 +0000777static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000778 const AttributeList &Attr) {
779 SmallVector<Expr*, 2> Args;
780 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
781 return;
782
783 unsigned Size = Args.size();
784 Expr **StartArg = Size == 0 ? 0 : &Args[0];
785 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000786 S.Context,
787 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000788 StartArg, Size));
789}
790
Michael Hanf1aae3b2012-08-03 17:40:43 +0000791static bool checkLocksRequiredCommon(Sema &S, Decl *D,
792 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000793 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000794 assert(!Attr.isInvalid());
795
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000796 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000797 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000798
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000799 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000800 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
801 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000802 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000803 }
804
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000805 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000806 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000807 if (Args.size() == 0)
808 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000809
Michael Handc691572012-07-23 18:48:41 +0000810 return true;
811}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000812
Michael Hanf1aae3b2012-08-03 17:40:43 +0000813static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000814 const AttributeList &Attr) {
815 SmallVector<Expr*, 1> Args;
816 if (!checkLocksRequiredCommon(S, D, Attr, Args))
817 return;
818
819 Expr **StartArg = &Args[0];
820 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000821 S.Context,
822 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000823 Args.size()));
824}
825
Michael Hanf1aae3b2012-08-03 17:40:43 +0000826static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000827 const AttributeList &Attr) {
828 SmallVector<Expr*, 1> Args;
829 if (!checkLocksRequiredCommon(S, D, Attr, Args))
830 return;
831
832 Expr **StartArg = &Args[0];
833 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000834 S.Context,
835 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000836 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000837}
838
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000841 assert(!Attr.isInvalid());
842
843 // zero or more arguments ok
844
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000845 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000846 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
847 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000848 return;
849 }
850
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000851 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000852 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000853 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000854 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000855 Expr **StartArg = Size == 0 ? 0 : &Args[0];
856
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000857 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000858 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000859}
860
861static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000862 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000863 assert(!Attr.isInvalid());
864
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000865 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000866 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000867 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000868
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000869 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000870 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
871 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000872 return;
873 }
874
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000875 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000876 return;
877
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000878 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000879 SmallVector<Expr*, 1> Args;
880 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
881 unsigned Size = Args.size();
882 if (Size == 0)
883 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000884
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000885 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
886 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000887}
888
889static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000890 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000891 assert(!Attr.isInvalid());
892
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000893 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000894 return;
895
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000896 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000897 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
898 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000899 return;
900 }
901
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000902 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000903 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000904 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000905 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000906 if (Size == 0)
907 return;
908 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000909
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000910 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000911 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000912}
913
914
Chandler Carruth1b03c872011-07-02 00:01:44 +0000915static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
916 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000917 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000918 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000919 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000920 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921 }
Mike Stumpbf916502009-07-24 19:02:52 +0000922
Chris Lattner6b6b5372008-06-26 18:38:35 +0000923 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000924
925 Expr *sizeExpr;
926
927 // Special case where the argument is a template id.
928 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000929 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000930 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000931 UnqualifiedId id;
932 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hanf1aae3b2012-08-03 17:40:43 +0000933
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000934 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
935 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000936 if (Size.isInvalid())
937 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000938
Douglas Gregor4ac01402011-06-15 16:02:29 +0000939 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000940 } else {
941 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000942 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000943 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000944
Peter Collingbourne7a730022010-11-23 20:45:58 +0000945 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000946 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000947
948 // Instantiate/Install the vector type, and let Sema build the type for us.
949 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000950 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000951 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000952 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000953 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000954
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000955 // Remember this typedef decl, we will need it later for diagnostics.
956 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000957 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000958}
959
Chandler Carruth1b03c872011-07-02 00:01:44 +0000960static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000961 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000962 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000963 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000964
Chandler Carruth87c44602011-07-01 23:49:12 +0000965 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000966 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000967 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000968 // If the alignment is less than or equal to 8 bits, the packed attribute
969 // has no effect.
970 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000971 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000972 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000973 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000974 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000975 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000976 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000977 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000978}
979
Chandler Carruth1b03c872011-07-02 00:01:44 +0000980static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000981 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000982 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000983 else
984 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
985}
986
Chandler Carruth1b03c872011-07-02 00:01:44 +0000987static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000988 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000989 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000990 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000991
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000992 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000993 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000994 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000995 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000996 return;
997 }
998
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000999 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001000}
1001
Ted Kremenek2f041d02011-09-29 07:02:25 +00001002static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1003 // The IBOutlet/IBOutletCollection attributes only apply to instance
1004 // variables or properties of Objective-C classes. The outlet must also
1005 // have an object reference type.
1006 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1007 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001008 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001009 << Attr.getName() << VD->getType() << 0;
1010 return false;
1011 }
1012 }
1013 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1014 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001015 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001016 << Attr.getName() << PD->getType() << 1;
1017 return false;
1018 }
1019 }
1020 else {
1021 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1022 return false;
1023 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001024
Ted Kremenek2f041d02011-09-29 07:02:25 +00001025 return true;
1026}
1027
Chandler Carruth1b03c872011-07-02 00:01:44 +00001028static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001029 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001030 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001031 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001032
1033 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001034 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001035
Ted Kremenek2f041d02011-09-29 07:02:25 +00001036 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001037}
1038
Chandler Carruth1b03c872011-07-02 00:01:44 +00001039static void handleIBOutletCollection(Sema &S, Decl *D,
1040 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001041
1042 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001043 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001044 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1045 return;
1046 }
1047
Ted Kremenek2f041d02011-09-29 07:02:25 +00001048 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001049 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001050
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001051 IdentifierInfo *II = Attr.getParameterName();
1052 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001053 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001054
John McCallb3d87482010-08-24 05:47:05 +00001055 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001056 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001057 if (!TypeRep) {
1058 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1059 return;
1060 }
John McCallb3d87482010-08-24 05:47:05 +00001061 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001062 // Diagnose use of non-object type in iboutletcollection attribute.
1063 // FIXME. Gnu attribute extension ignores use of builtin types in
1064 // attributes. So, __attribute__((iboutletcollection(char))) will be
1065 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001066 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001067 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1068 return;
1069 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001070 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1071 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001072}
1073
Chandler Carruthd309c812011-07-01 23:49:16 +00001074static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001075 if (const RecordType *UT = T->getAsUnionType())
1076 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1077 RecordDecl *UD = UT->getDecl();
1078 for (RecordDecl::field_iterator it = UD->field_begin(),
1079 itend = UD->field_end(); it != itend; ++it) {
1080 QualType QT = it->getType();
1081 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1082 T = QT;
1083 return;
1084 }
1085 }
1086 }
1087}
1088
Nuno Lopes587de5b2012-05-24 00:22:00 +00001089static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001090 if (!isFunctionOrMethod(D)) {
1091 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1092 << "alloc_size" << ExpectedFunctionOrMethod;
1093 return;
1094 }
1095
Nuno Lopes587de5b2012-05-24 00:22:00 +00001096 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1097 return;
1098
1099 // In C++ the implicit 'this' function parameter also counts, and they are
1100 // counted from one.
1101 bool HasImplicitThisParam = isInstanceMethod(D);
1102 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1103
1104 SmallVector<unsigned, 8> SizeArgs;
1105
1106 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1107 E = Attr.arg_end(); I!=E; ++I) {
1108 // The argument must be an integer constant expression.
1109 Expr *Ex = *I;
1110 llvm::APSInt ArgNum;
1111 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1112 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1113 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1114 << "alloc_size" << Ex->getSourceRange();
1115 return;
1116 }
1117
1118 uint64_t x = ArgNum.getZExtValue();
1119
1120 if (x < 1 || x > NumArgs) {
1121 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1122 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1123 return;
1124 }
1125
1126 --x;
1127 if (HasImplicitThisParam) {
1128 if (x == 0) {
1129 S.Diag(Attr.getLoc(),
1130 diag::err_attribute_invalid_implicit_this_argument)
1131 << "alloc_size" << Ex->getSourceRange();
1132 return;
1133 }
1134 --x;
1135 }
1136
1137 // check if the function argument is of an integer type
1138 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1139 if (!T->isIntegerType()) {
1140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1141 << "alloc_size" << Ex->getSourceRange();
1142 return;
1143 }
1144
Nuno Lopes587de5b2012-05-24 00:22:00 +00001145 SizeArgs.push_back(x);
1146 }
1147
1148 // check if the function returns a pointer
1149 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1150 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1151 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1152 }
1153
Nuno Lopes96c67d12012-06-18 16:27:56 +00001154 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1155 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001156}
1157
Chandler Carruth1b03c872011-07-02 00:01:44 +00001158static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001159 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1160 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001161 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001162 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001163 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001164 return;
1165 }
Mike Stumpbf916502009-07-24 19:02:52 +00001166
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001167 // In C++ the implicit 'this' function parameter also counts, and they are
1168 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001169 bool HasImplicitThisParam = isInstanceMethod(D);
1170 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001171
1172 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001173 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001174
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001175 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1176 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001177
1178
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001179 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001180 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001181 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001182 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1183 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001184 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1185 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001186 return;
1187 }
Mike Stumpbf916502009-07-24 19:02:52 +00001188
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001189 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001190
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001191 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001192 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001193 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001194 return;
1195 }
Mike Stumpbf916502009-07-24 19:02:52 +00001196
Ted Kremenek465172f2008-07-21 22:09:15 +00001197 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001198 if (HasImplicitThisParam) {
1199 if (x == 0) {
1200 S.Diag(Attr.getLoc(),
1201 diag::err_attribute_invalid_implicit_this_argument)
1202 << "nonnull" << Ex->getSourceRange();
1203 return;
1204 }
1205 --x;
1206 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001207
1208 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001209 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001210 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001211
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001212 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001213 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001214 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001215 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001216 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001217 }
Mike Stumpbf916502009-07-24 19:02:52 +00001218
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001219 NonNullArgs.push_back(x);
1220 }
Mike Stumpbf916502009-07-24 19:02:52 +00001221
1222 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1223 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001224 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001225 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1226 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001227 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001228 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001229 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001230 }
Mike Stumpbf916502009-07-24 19:02:52 +00001231
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001232 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001233 if (NonNullArgs.empty()) {
1234 // Warn the trivial case only if attribute is not coming from a
1235 // macro instantiation.
1236 if (Attr.getLoc().isFileID())
1237 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001238 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001239 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001240 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001241
1242 unsigned* start = &NonNullArgs[0];
1243 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001244 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001245 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001246 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001247}
1248
Chandler Carruth1b03c872011-07-02 00:01:44 +00001249static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001250 // This attribute must be applied to a function declaration.
1251 // The first argument to the attribute must be a string,
1252 // the name of the resource, for example "malloc".
1253 // The following arguments must be argument indexes, the arguments must be
1254 // of integer type for Returns, otherwise of pointer type.
1255 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001256 // after being held. free() should be __attribute((ownership_takes)), whereas
1257 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001258
1259 if (!AL.getParameterName()) {
1260 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1261 << AL.getName()->getName() << 1;
1262 return;
1263 }
1264 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001265 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001266 switch (AL.getKind()) {
1267 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001268 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001269 if (AL.getNumArgs() < 1) {
1270 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1271 return;
1272 }
Jordy Rose2a479922010-08-12 08:54:03 +00001273 break;
1274 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001275 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001276 if (AL.getNumArgs() < 1) {
1277 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1278 return;
1279 }
Jordy Rose2a479922010-08-12 08:54:03 +00001280 break;
1281 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001282 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001283 if (AL.getNumArgs() > 1) {
1284 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1285 << AL.getNumArgs() + 1;
1286 return;
1287 }
Jordy Rose2a479922010-08-12 08:54:03 +00001288 break;
1289 default:
1290 // This should never happen given how we are called.
1291 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001292 }
1293
Chandler Carruth87c44602011-07-01 23:49:12 +00001294 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001295 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1296 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001297 return;
1298 }
1299
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001300 // In C++ the implicit 'this' function parameter also counts, and they are
1301 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001302 bool HasImplicitThisParam = isInstanceMethod(D);
1303 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001304
Chris Lattner5f9e2722011-07-23 10:55:15 +00001305 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001306
1307 // Normalize the argument, __foo__ becomes foo.
1308 if (Module.startswith("__") && Module.endswith("__"))
1309 Module = Module.substr(2, Module.size() - 4);
1310
Chris Lattner5f9e2722011-07-23 10:55:15 +00001311 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001312
Jordy Rose2a479922010-08-12 08:54:03 +00001313 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1314 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001315
Peter Collingbourne7a730022010-11-23 20:45:58 +00001316 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001317 llvm::APSInt ArgNum(32);
1318 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1319 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1320 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1321 << AL.getName()->getName() << IdxExpr->getSourceRange();
1322 continue;
1323 }
1324
1325 unsigned x = (unsigned) ArgNum.getZExtValue();
1326
1327 if (x > NumArgs || x < 1) {
1328 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1329 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1330 continue;
1331 }
1332 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001333 if (HasImplicitThisParam) {
1334 if (x == 0) {
1335 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1336 << "ownership" << IdxExpr->getSourceRange();
1337 return;
1338 }
1339 --x;
1340 }
1341
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001343 case OwnershipAttr::Takes:
1344 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001345 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001346 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001347 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1348 // FIXME: Should also highlight argument in decl.
1349 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001350 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001351 << "pointer"
1352 << IdxExpr->getSourceRange();
1353 continue;
1354 }
1355 break;
1356 }
Sean Huntcf807c42010-08-18 23:23:40 +00001357 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001358 if (AL.getNumArgs() > 1) {
1359 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001360 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001361 llvm::APSInt ArgNum(32);
1362 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1363 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1364 S.Diag(AL.getLoc(), diag::err_ownership_type)
1365 << "ownership_returns" << "integer"
1366 << IdxExpr->getSourceRange();
1367 return;
1368 }
1369 }
1370 break;
1371 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001372 } // switch
1373
1374 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001375 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001376 i = D->specific_attr_begin<OwnershipAttr>(),
1377 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001378 i != e; ++i) {
1379 if ((*i)->getOwnKind() != K) {
1380 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1381 I!=E; ++I) {
1382 if (x == *I) {
1383 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1384 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001385 }
1386 }
1387 }
1388 }
1389 OwnershipArgs.push_back(x);
1390 }
1391
1392 unsigned* start = OwnershipArgs.data();
1393 unsigned size = OwnershipArgs.size();
1394 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001395
1396 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1397 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1398 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001399 }
Sean Huntcf807c42010-08-18 23:23:40 +00001400
Chandler Carruth87c44602011-07-01 23:49:12 +00001401 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001402 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403}
1404
John McCall332bb2a2011-02-08 22:35:49 +00001405/// Whether this declaration has internal linkage for the purposes of
1406/// things that want to complain about things not have internal linkage.
1407static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1408 switch (D->getLinkage()) {
1409 case NoLinkage:
1410 case InternalLinkage:
1411 return true;
1412
1413 // Template instantiations that go from external to unique-external
1414 // shouldn't get diagnosed.
1415 case UniqueExternalLinkage:
1416 return true;
1417
1418 case ExternalLinkage:
1419 return false;
1420 }
1421 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001422}
1423
Chandler Carruth1b03c872011-07-02 00:01:44 +00001424static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001425 // Check the attribute arguments.
1426 if (Attr.getNumArgs() > 1) {
1427 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1428 return;
1429 }
1430
Chandler Carruth87c44602011-07-01 23:49:12 +00001431 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001433 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001434 return;
1435 }
1436
Chandler Carruth87c44602011-07-01 23:49:12 +00001437 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001438
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001439 // gcc rejects
1440 // class c {
1441 // static int a __attribute__((weakref ("v2")));
1442 // static int b() __attribute__((weakref ("f3")));
1443 // };
1444 // and ignores the attributes of
1445 // void f(void) {
1446 // static int a __attribute__((weakref ("v2")));
1447 // }
1448 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001449 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001450 if (!Ctx->isFileContext()) {
1451 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001452 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001453 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001454 }
1455
1456 // The GCC manual says
1457 //
1458 // At present, a declaration to which `weakref' is attached can only
1459 // be `static'.
1460 //
1461 // It also says
1462 //
1463 // Without a TARGET,
1464 // given as an argument to `weakref' or to `alias', `weakref' is
1465 // equivalent to `weak'.
1466 //
1467 // gcc 4.4.1 will accept
1468 // int a7 __attribute__((weakref));
1469 // as
1470 // int a7 __attribute__((weak));
1471 // This looks like a bug in gcc. We reject that for now. We should revisit
1472 // it if this behaviour is actually used.
1473
John McCall332bb2a2011-02-08 22:35:49 +00001474 if (!hasEffectivelyInternalLinkage(nd)) {
1475 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001476 return;
1477 }
1478
1479 // GCC rejects
1480 // static ((alias ("y"), weakref)).
1481 // Should we? How to check that weakref is before or after alias?
1482
1483 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001484 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001485 Arg = Arg->IgnoreParenCasts();
1486 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1487
Douglas Gregor5cee1192011-07-27 05:40:30 +00001488 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001489 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1490 << "weakref" << 1;
1491 return;
1492 }
1493 // GCC will accept anything as the argument of weakref. Should we
1494 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001495 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001496 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001497 }
1498
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001499 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001500}
1501
Chandler Carruth1b03c872011-07-02 00:01:44 +00001502static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001503 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001504 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001505 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001506 return;
1507 }
Mike Stumpbf916502009-07-24 19:02:52 +00001508
Peter Collingbourne7a730022010-11-23 20:45:58 +00001509 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001510 Arg = Arg->IgnoreParenCasts();
1511 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001512
Douglas Gregor5cee1192011-07-27 05:40:30 +00001513 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001514 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001515 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001516 return;
1517 }
Mike Stumpbf916502009-07-24 19:02:52 +00001518
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001519 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001520 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1521 return;
1522 }
1523
Chris Lattner6b6b5372008-06-26 18:38:35 +00001524 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001525
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001526 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001527 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001528}
1529
Benjamin Krameree409a92012-05-12 21:10:52 +00001530static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1531 // Check the attribute arguments.
1532 if (!checkAttributeNumArgs(S, Attr, 0))
1533 return;
1534
1535 if (!isa<FunctionDecl>(D)) {
1536 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1537 << Attr.getName() << ExpectedFunction;
1538 return;
1539 }
1540
1541 if (D->hasAttr<HotAttr>()) {
1542 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1543 << Attr.getName() << "hot";
1544 return;
1545 }
1546
1547 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1548}
1549
1550static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1551 // Check the attribute arguments.
1552 if (!checkAttributeNumArgs(S, Attr, 0))
1553 return;
1554
1555 if (!isa<FunctionDecl>(D)) {
1556 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1557 << Attr.getName() << ExpectedFunction;
1558 return;
1559 }
1560
1561 if (D->hasAttr<ColdAttr>()) {
1562 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1563 << Attr.getName() << "cold";
1564 return;
1565 }
1566
1567 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1568}
1569
Chandler Carruth1b03c872011-07-02 00:01:44 +00001570static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001571 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001572 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001573 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001574
Chandler Carruth87c44602011-07-01 23:49:12 +00001575 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001576 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001577 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001578 return;
1579 }
1580
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001581 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001582}
1583
Chandler Carruth1b03c872011-07-02 00:01:44 +00001584static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1585 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001586 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001587 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001588 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1589 return;
1590 }
1591
Chandler Carruth87c44602011-07-01 23:49:12 +00001592 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001593 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001594 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001595 return;
1596 }
Mike Stumpbf916502009-07-24 19:02:52 +00001597
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001598 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001599}
1600
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001601static void handleTLSModelAttr(Sema &S, Decl *D,
1602 const AttributeList &Attr) {
1603 // Check the attribute arguments.
1604 if (Attr.getNumArgs() != 1) {
1605 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1606 return;
1607 }
1608
1609 Expr *Arg = Attr.getArg(0);
1610 Arg = Arg->IgnoreParenCasts();
1611 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1612
1613 // Check that it is a string.
1614 if (!Str) {
1615 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1616 return;
1617 }
1618
1619 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1621 << Attr.getName() << ExpectedTLSVar;
1622 return;
1623 }
1624
1625 // Check that the value.
1626 StringRef Model = Str->getString();
1627 if (Model != "global-dynamic" && Model != "local-dynamic"
1628 && Model != "initial-exec" && Model != "local-exec") {
1629 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1630 return;
1631 }
1632
1633 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1634 Model));
1635}
1636
Chandler Carruth1b03c872011-07-02 00:01:44 +00001637static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001638 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001639 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1641 return;
1642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Chandler Carruth87c44602011-07-01 23:49:12 +00001644 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001645 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001646 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001647 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001648 return;
1649 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001650 }
1651
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001652 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001653}
1654
Chandler Carruth1b03c872011-07-02 00:01:44 +00001655static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001656 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001657 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001658 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001659
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001660 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001661}
1662
Chandler Carruth1b03c872011-07-02 00:01:44 +00001663static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001664 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001665 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001666 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001667 else
1668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001669 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001670}
1671
Chandler Carruth1b03c872011-07-02 00:01:44 +00001672static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001673 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001674 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001675 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001676 else
1677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001678 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001679}
1680
Chandler Carruth1b03c872011-07-02 00:01:44 +00001681static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001682 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001683
1684 if (S.CheckNoReturnAttr(attr)) return;
1685
Chandler Carruth87c44602011-07-01 23:49:12 +00001686 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001687 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001688 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001689 return;
1690 }
1691
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001692 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001693}
1694
1695bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001696 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001697 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1698 attr.setInvalid();
1699 return true;
1700 }
1701
1702 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001703}
1704
Chandler Carruth1b03c872011-07-02 00:01:44 +00001705static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1706 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001707
1708 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1709 // because 'analyzer_noreturn' does not impact the type.
1710
Chandler Carruth1731e202011-07-11 23:30:35 +00001711 if(!checkAttributeNumArgs(S, Attr, 0))
1712 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001713
Chandler Carruth87c44602011-07-01 23:49:12 +00001714 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1715 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001716 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1717 && !VD->getType()->isFunctionPointerType())) {
1718 S.Diag(Attr.getLoc(),
1719 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1720 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001721 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001722 return;
1723 }
1724 }
1725
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001726 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001727}
1728
John Thompson35cc9622010-08-09 21:53:52 +00001729// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001730static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001731/*
1732 Returning a Vector Class in Registers
1733
Eric Christopherf48f3672010-12-01 22:13:54 +00001734 According to the PPU ABI specifications, a class with a single member of
1735 vector type is returned in memory when used as the return value of a function.
1736 This results in inefficient code when implementing vector classes. To return
1737 the value in a single vector register, add the vecreturn attribute to the
1738 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001739
1740 Example:
1741
1742 struct Vector
1743 {
1744 __vector float xyzw;
1745 } __attribute__((vecreturn));
1746
1747 Vector Add(Vector lhs, Vector rhs)
1748 {
1749 Vector result;
1750 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1751 return result; // This will be returned in a register
1752 }
1753*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001754 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001755 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001756 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001757 return;
1758 }
1759
Chandler Carruth87c44602011-07-01 23:49:12 +00001760 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001761 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1762 return;
1763 }
1764
Chandler Carruth87c44602011-07-01 23:49:12 +00001765 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001766 int count = 0;
1767
1768 if (!isa<CXXRecordDecl>(record)) {
1769 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1770 return;
1771 }
1772
1773 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1774 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1775 return;
1776 }
1777
Eric Christopherf48f3672010-12-01 22:13:54 +00001778 for (RecordDecl::field_iterator iter = record->field_begin();
1779 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001780 if ((count == 1) || !iter->getType()->isVectorType()) {
1781 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1782 return;
1783 }
1784 count++;
1785 }
1786
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001787 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001788}
1789
Chandler Carruth1b03c872011-07-02 00:01:44 +00001790static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001791 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001792 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001793 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001794 return;
1795 }
1796 // FIXME: Actually store the attribute on the declaration
1797}
1798
Chandler Carruth1b03c872011-07-02 00:01:44 +00001799static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001800 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001801 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001802 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001803 return;
1804 }
Mike Stumpbf916502009-07-24 19:02:52 +00001805
Chandler Carruth87c44602011-07-01 23:49:12 +00001806 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001807 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001808 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001809 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001810 return;
1811 }
Mike Stumpbf916502009-07-24 19:02:52 +00001812
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001813 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001814}
1815
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001816static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1817 const AttributeList &Attr) {
1818 // check the attribute arguments.
1819 if (Attr.hasParameterOrArguments()) {
1820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1821 return;
1822 }
1823
1824 if (!isa<FunctionDecl>(D)) {
1825 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1826 << Attr.getName() << ExpectedFunction;
1827 return;
1828 }
1829
1830 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1831}
1832
Chandler Carruth1b03c872011-07-02 00:01:44 +00001833static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001834 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001835 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001836 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1837 return;
1838 }
Mike Stumpbf916502009-07-24 19:02:52 +00001839
Chandler Carruth87c44602011-07-01 23:49:12 +00001840 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001841 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001842 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1843 return;
1844 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001845 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001846 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001847 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001848 return;
1849 }
Mike Stumpbf916502009-07-24 19:02:52 +00001850
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001851 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001852}
1853
Chandler Carruth1b03c872011-07-02 00:01:44 +00001854static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001855 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001856 if (Attr.getNumArgs() > 1) {
1857 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001858 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001859 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001860
1861 int priority = 65535; // FIXME: Do not hardcode such constants.
1862 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001863 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001864 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001865 if (E->isTypeDependent() || E->isValueDependent() ||
1866 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001867 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001868 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001869 return;
1870 }
1871 priority = Idx.getZExtValue();
1872 }
Mike Stumpbf916502009-07-24 19:02:52 +00001873
Chandler Carruth87c44602011-07-01 23:49:12 +00001874 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001875 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001876 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001877 return;
1878 }
1879
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001880 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001881 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001882}
1883
Chandler Carruth1b03c872011-07-02 00:01:44 +00001884static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001885 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001886 if (Attr.getNumArgs() > 1) {
1887 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001888 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001889 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001890
1891 int priority = 65535; // FIXME: Do not hardcode such constants.
1892 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001893 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001894 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001895 if (E->isTypeDependent() || E->isValueDependent() ||
1896 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001897 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001898 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001899 return;
1900 }
1901 priority = Idx.getZExtValue();
1902 }
Mike Stumpbf916502009-07-24 19:02:52 +00001903
Chandler Carruth87c44602011-07-01 23:49:12 +00001904 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001905 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001906 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001907 return;
1908 }
1909
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001910 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001911 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001912}
1913
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001914template <typename AttrTy>
1915static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1916 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001917 unsigned NumArgs = Attr.getNumArgs();
1918 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001919 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001920 return;
1921 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001922
1923 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001924 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001925 if (NumArgs == 1) {
1926 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001927 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001928 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001929 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001930 return;
1931 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001932 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001933 }
Mike Stumpbf916502009-07-24 19:02:52 +00001934
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001935 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001936}
1937
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001938static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1939 const AttributeList &Attr) {
1940 unsigned NumArgs = Attr.getNumArgs();
1941 if (NumArgs > 0) {
1942 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1943 return;
1944 }
1945
1946 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001947 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001948}
1949
Patrick Beardb2f68202012-04-06 18:12:22 +00001950static void handleObjCRootClassAttr(Sema &S, Decl *D,
1951 const AttributeList &Attr) {
1952 if (!isa<ObjCInterfaceDecl>(D)) {
1953 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1954 return;
1955 }
1956
1957 unsigned NumArgs = Attr.getNumArgs();
1958 if (NumArgs > 0) {
1959 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1960 return;
1961 }
1962
1963 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1964}
1965
Ted Kremenek71207fc2012-01-05 22:47:47 +00001966static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001967 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001968 if (!isa<ObjCInterfaceDecl>(D)) {
1969 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1970 return;
1971 }
1972
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001973 unsigned NumArgs = Attr.getNumArgs();
1974 if (NumArgs > 0) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1976 return;
1977 }
1978
Ted Kremenek71207fc2012-01-05 22:47:47 +00001979 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001980 Attr.getRange(), S.Context));
1981}
1982
Jordy Rosefad5de92012-05-08 03:27:22 +00001983static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1984 IdentifierInfo *Platform,
1985 VersionTuple Introduced,
1986 VersionTuple Deprecated,
1987 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001988 StringRef PlatformName
1989 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1990 if (PlatformName.empty())
1991 PlatformName = Platform->getName();
1992
1993 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1994 // of these steps are needed).
1995 if (!Introduced.empty() && !Deprecated.empty() &&
1996 !(Introduced <= Deprecated)) {
1997 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1998 << 1 << PlatformName << Deprecated.getAsString()
1999 << 0 << Introduced.getAsString();
2000 return true;
2001 }
2002
2003 if (!Introduced.empty() && !Obsoleted.empty() &&
2004 !(Introduced <= Obsoleted)) {
2005 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2006 << 2 << PlatformName << Obsoleted.getAsString()
2007 << 0 << Introduced.getAsString();
2008 return true;
2009 }
2010
2011 if (!Deprecated.empty() && !Obsoleted.empty() &&
2012 !(Deprecated <= Obsoleted)) {
2013 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2014 << 2 << PlatformName << Obsoleted.getAsString()
2015 << 1 << Deprecated.getAsString();
2016 return true;
2017 }
2018
2019 return false;
2020}
2021
Rafael Espindola599f1b72012-05-13 03:25:18 +00002022AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2023 IdentifierInfo *Platform,
2024 VersionTuple Introduced,
2025 VersionTuple Deprecated,
2026 VersionTuple Obsoleted,
2027 bool IsUnavailable,
2028 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002029 VersionTuple MergedIntroduced = Introduced;
2030 VersionTuple MergedDeprecated = Deprecated;
2031 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002032 bool FoundAny = false;
2033
Rafael Espindola98ae8342012-05-10 02:50:16 +00002034 if (D->hasAttrs()) {
2035 AttrVec &Attrs = D->getAttrs();
2036 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2037 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2038 if (!OldAA) {
2039 ++i;
2040 continue;
2041 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002042
Rafael Espindola98ae8342012-05-10 02:50:16 +00002043 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2044 if (OldPlatform != Platform) {
2045 ++i;
2046 continue;
2047 }
2048
2049 FoundAny = true;
2050 VersionTuple OldIntroduced = OldAA->getIntroduced();
2051 VersionTuple OldDeprecated = OldAA->getDeprecated();
2052 VersionTuple OldObsoleted = OldAA->getObsoleted();
2053 bool OldIsUnavailable = OldAA->getUnavailable();
2054 StringRef OldMessage = OldAA->getMessage();
2055
2056 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2057 OldIntroduced != Introduced) ||
2058 (!OldDeprecated.empty() && !Deprecated.empty() &&
2059 OldDeprecated != Deprecated) ||
2060 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2061 OldObsoleted != Obsoleted) ||
2062 (OldIsUnavailable != IsUnavailable) ||
2063 (OldMessage != Message)) {
2064 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2065 Diag(Range.getBegin(), diag::note_previous_attribute);
2066 Attrs.erase(Attrs.begin() + i);
2067 --e;
2068 continue;
2069 }
2070
2071 VersionTuple MergedIntroduced2 = MergedIntroduced;
2072 VersionTuple MergedDeprecated2 = MergedDeprecated;
2073 VersionTuple MergedObsoleted2 = MergedObsoleted;
2074
2075 if (MergedIntroduced2.empty())
2076 MergedIntroduced2 = OldIntroduced;
2077 if (MergedDeprecated2.empty())
2078 MergedDeprecated2 = OldDeprecated;
2079 if (MergedObsoleted2.empty())
2080 MergedObsoleted2 = OldObsoleted;
2081
2082 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2083 MergedIntroduced2, MergedDeprecated2,
2084 MergedObsoleted2)) {
2085 Attrs.erase(Attrs.begin() + i);
2086 --e;
2087 continue;
2088 }
2089
2090 MergedIntroduced = MergedIntroduced2;
2091 MergedDeprecated = MergedDeprecated2;
2092 MergedObsoleted = MergedObsoleted2;
2093 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002094 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002095 }
2096
2097 if (FoundAny &&
2098 MergedIntroduced == Introduced &&
2099 MergedDeprecated == Deprecated &&
2100 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002101 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002102
Rafael Espindola98ae8342012-05-10 02:50:16 +00002103 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002104 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002105 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2106 Introduced, Deprecated,
2107 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002108 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002109 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002110}
2111
Chandler Carruth1b03c872011-07-02 00:01:44 +00002112static void handleAvailabilityAttr(Sema &S, Decl *D,
2113 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002114 IdentifierInfo *Platform = Attr.getParameterName();
2115 SourceLocation PlatformLoc = Attr.getParameterLoc();
2116
Rafael Espindola3b294362012-05-06 19:56:25 +00002117 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002118 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2119 << Platform;
2120
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002121 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2122 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2123 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002124 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002125 StringRef Str;
2126 const StringLiteral *SE =
2127 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2128 if (SE)
2129 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002130
Rafael Espindola599f1b72012-05-13 03:25:18 +00002131 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2132 Platform,
2133 Introduced.Version,
2134 Deprecated.Version,
2135 Obsoleted.Version,
2136 IsUnavailable, Str);
2137 if (NewAttr)
2138 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002139}
2140
Rafael Espindola599f1b72012-05-13 03:25:18 +00002141VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2142 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002143 if (isa<TypedefNameDecl>(D)) {
2144 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002145 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002146 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002147 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2148 if (ExistingAttr) {
2149 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2150 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002151 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002152 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2153 Diag(Range.getBegin(), diag::note_previous_attribute);
2154 D->dropAttr<VisibilityAttr>();
2155 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002156 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002157}
2158
Chandler Carruth1b03c872011-07-02 00:01:44 +00002159static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002160 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002161 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002162 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002163
Peter Collingbourne7a730022010-11-23 20:45:58 +00002164 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002165 Arg = Arg->IgnoreParenCasts();
2166 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002167
Douglas Gregor5cee1192011-07-27 05:40:30 +00002168 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002170 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002171 return;
2172 }
Mike Stumpbf916502009-07-24 19:02:52 +00002173
Chris Lattner5f9e2722011-07-23 10:55:15 +00002174 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002175 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002176
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002177 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002178 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002179 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002180 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002181 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002182 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002183 else if (TypeStr == "protected") {
2184 // Complain about attempts to use protected visibility on targets
2185 // (like Darwin) that don't support it.
2186 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2187 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2188 type = VisibilityAttr::Default;
2189 } else {
2190 type = VisibilityAttr::Protected;
2191 }
2192 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002193 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002194 return;
2195 }
Mike Stumpbf916502009-07-24 19:02:52 +00002196
Rafael Espindola599f1b72012-05-13 03:25:18 +00002197 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2198 if (NewAttr)
2199 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002200}
2201
Chandler Carruth1b03c872011-07-02 00:01:44 +00002202static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2203 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002204 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2205 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002206 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002207 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002208 return;
2209 }
2210
Chandler Carruth87c44602011-07-01 23:49:12 +00002211 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2212 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2213 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002214 << "objc_method_family" << 1;
2215 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002216 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002217 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002218 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002219 return;
2220 }
2221
Chris Lattner5f9e2722011-07-23 10:55:15 +00002222 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002223 ObjCMethodFamilyAttr::FamilyKind family;
2224 if (param == "none")
2225 family = ObjCMethodFamilyAttr::OMF_None;
2226 else if (param == "alloc")
2227 family = ObjCMethodFamilyAttr::OMF_alloc;
2228 else if (param == "copy")
2229 family = ObjCMethodFamilyAttr::OMF_copy;
2230 else if (param == "init")
2231 family = ObjCMethodFamilyAttr::OMF_init;
2232 else if (param == "mutableCopy")
2233 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2234 else if (param == "new")
2235 family = ObjCMethodFamilyAttr::OMF_new;
2236 else {
2237 // Just warn and ignore it. This is future-proof against new
2238 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002239 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002240 return;
2241 }
2242
John McCallf85e1932011-06-15 23:02:42 +00002243 if (family == ObjCMethodFamilyAttr::OMF_init &&
2244 !method->getResultType()->isObjCObjectPointerType()) {
2245 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2246 << method->getResultType();
2247 // Ignore the attribute.
2248 return;
2249 }
2250
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002251 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002252 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002253}
2254
Chandler Carruth1b03c872011-07-02 00:01:44 +00002255static void handleObjCExceptionAttr(Sema &S, Decl *D,
2256 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002257 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002258 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002259
Chris Lattner0db29ec2009-02-14 08:09:34 +00002260 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2261 if (OCI == 0) {
2262 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2263 return;
2264 }
Mike Stumpbf916502009-07-24 19:02:52 +00002265
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002266 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002267}
2268
Chandler Carruth1b03c872011-07-02 00:01:44 +00002269static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002270 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002271 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002272 return;
2273 }
Richard Smith162e1c12011-04-15 14:24:37 +00002274 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002275 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002276 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002277 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2278 return;
2279 }
2280 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002281 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2282 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002283 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002284 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2285 return;
2286 }
2287 }
2288 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002289 // It is okay to include this attribute on properties, e.g.:
2290 //
2291 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2292 //
2293 // In this case it follows tradition and suppresses an error in the above
2294 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002295 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002296 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002297 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002298}
2299
Mike Stumpbf916502009-07-24 19:02:52 +00002300static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002301handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002302 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002304 return;
2305 }
2306
2307 if (!isa<FunctionDecl>(D)) {
2308 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2309 return;
2310 }
2311
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002312 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002313}
2314
Chandler Carruth1b03c872011-07-02 00:01:44 +00002315static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002316 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002317 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002318 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002319 return;
2320 }
Mike Stumpbf916502009-07-24 19:02:52 +00002321
Steve Naroff9eae5762008-09-18 16:44:58 +00002322 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002323 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002324 return;
2325 }
Mike Stumpbf916502009-07-24 19:02:52 +00002326
Sean Huntcf807c42010-08-18 23:23:40 +00002327 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002328 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002329 type = BlocksAttr::ByRef;
2330 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002331 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002332 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002333 return;
2334 }
Mike Stumpbf916502009-07-24 19:02:52 +00002335
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002336 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002337}
2338
Chandler Carruth1b03c872011-07-02 00:01:44 +00002339static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002340 // check the attribute arguments.
2341 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002342 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002343 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002344 }
2345
John McCall3323fad2011-09-09 07:56:05 +00002346 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002347 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002348 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002349 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002350 if (E->isTypeDependent() || E->isValueDependent() ||
2351 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002352 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002353 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002354 return;
2355 }
Mike Stumpbf916502009-07-24 19:02:52 +00002356
John McCall3323fad2011-09-09 07:56:05 +00002357 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002358 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2359 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002360 return;
2361 }
John McCall3323fad2011-09-09 07:56:05 +00002362
2363 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002364 }
2365
John McCall3323fad2011-09-09 07:56:05 +00002366 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002367 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002368 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002369 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002370 if (E->isTypeDependent() || E->isValueDependent() ||
2371 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002372 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002373 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002374 return;
2375 }
2376 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002377
John McCall3323fad2011-09-09 07:56:05 +00002378 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002379 // FIXME: This error message could be improved, it would be nice
2380 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002381 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2382 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002383 return;
2384 }
2385 }
2386
Chandler Carruth87c44602011-07-01 23:49:12 +00002387 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002388 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002389 if (isa<FunctionNoProtoType>(FT)) {
2390 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2391 return;
2392 }
Mike Stumpbf916502009-07-24 19:02:52 +00002393
Chris Lattner897cd902009-03-17 23:03:47 +00002394 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002395 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002396 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002397 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002398 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002399 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002400 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002401 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002402 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002403 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2404 if (!BD->isVariadic()) {
2405 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2406 return;
2407 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002408 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002409 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002410 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002411 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002412 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002413 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002414 int m = Ty->isFunctionPointerType() ? 0 : 1;
2415 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002416 return;
2417 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002418 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002420 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002421 return;
2422 }
Anders Carlsson77091822008-10-05 18:05:59 +00002423 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002424 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002425 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002426 return;
2427 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002428 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002429 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002430}
2431
Chandler Carruth1b03c872011-07-02 00:01:44 +00002432static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002433 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002434 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002435 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002436
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002437 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002439 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002440 return;
2441 }
Mike Stumpbf916502009-07-24 19:02:52 +00002442
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002443 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2444 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2445 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002446 return;
2447 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002448 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2449 if (MD->getResultType()->isVoidType()) {
2450 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2451 << Attr.getName() << 1;
2452 return;
2453 }
2454
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002455 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002456}
2457
Chandler Carruth1b03c872011-07-02 00:01:44 +00002458static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002459 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002460 if (Attr.hasParameterOrArguments()) {
2461 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002462 return;
2463 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002464
Chandler Carruth87c44602011-07-01 23:49:12 +00002465 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002466 if (isa<CXXRecordDecl>(D)) {
2467 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2468 return;
2469 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2471 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002472 return;
2473 }
2474
Chandler Carruth87c44602011-07-01 23:49:12 +00002475 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002476
2477 // 'weak' only applies to declarations with external linkage.
2478 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002479 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002480 return;
2481 }
Mike Stumpbf916502009-07-24 19:02:52 +00002482
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002483 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002484}
2485
Chandler Carruth1b03c872011-07-02 00:01:44 +00002486static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002487 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002488 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002489 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002490
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002491
2492 // weak_import only applies to variable & function declarations.
2493 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002494 if (!D->canBeWeakImported(isDef)) {
2495 if (isDef)
2496 S.Diag(Attr.getLoc(),
2497 diag::warn_attribute_weak_import_invalid_on_definition)
2498 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002499 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002500 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002501 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002502 // Nothing to warn about here.
2503 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002504 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002505 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002506
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002507 return;
2508 }
2509
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002510 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002511}
2512
Tanya Lattner0df579e2012-07-09 22:06:01 +00002513// Handles reqd_work_group_size and work_group_size_hint.
2514static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002515 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002516 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2517 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2518
Nate Begeman6f3d8382009-06-26 06:32:41 +00002519 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002520 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002521
2522 unsigned WGSize[3];
2523 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002524 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002525 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002526 if (E->isTypeDependent() || E->isValueDependent() ||
2527 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002528 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002529 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002530 return;
2531 }
2532 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2533 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002534
2535 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2536 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2537 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2538 if (!(A->getXDim() == WGSize[0] &&
2539 A->getYDim() == WGSize[1] &&
2540 A->getZDim() == WGSize[2])) {
2541 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2542 Attr.getName();
2543 }
2544 }
2545
2546 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2547 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2548 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2549 if (!(A->getXDim() == WGSize[0] &&
2550 A->getYDim() == WGSize[1] &&
2551 A->getZDim() == WGSize[2])) {
2552 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2553 Attr.getName();
2554 }
2555 }
2556
2557 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2558 D->addAttr(::new (S.Context)
2559 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2560 WGSize[0], WGSize[1], WGSize[2]));
2561 else
2562 D->addAttr(::new (S.Context)
2563 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2564 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002565}
2566
Rafael Espindola599f1b72012-05-13 03:25:18 +00002567SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2568 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002569 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2570 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002571 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002572 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2573 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002574 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002575 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002576 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002577}
2578
Chandler Carruth1b03c872011-07-02 00:01:44 +00002579static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002580 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002581 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002582 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002583
2584 // Make sure that there is a string literal as the sections's single
2585 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002586 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002587 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002588 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002589 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002590 return;
2591 }
Mike Stump1eb44332009-09-09 15:08:12 +00002592
Chris Lattner797c3c42009-08-10 19:03:04 +00002593 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002594 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002595 if (!Error.empty()) {
2596 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2597 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002598 return;
2599 }
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002601 // This attribute cannot be applied to local variables.
2602 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2603 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2604 return;
2605 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002606 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2607 SE->getString());
2608 if (NewAttr)
2609 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002610}
2611
Chris Lattner6b6b5372008-06-26 18:38:35 +00002612
Chandler Carruth1b03c872011-07-02 00:01:44 +00002613static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002614 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002615 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002617 return;
2618 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002619
Chandler Carruth87c44602011-07-01 23:49:12 +00002620 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002621 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002622 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002623 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002624 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002625 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002626}
2627
Chandler Carruth1b03c872011-07-02 00:01:44 +00002628static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002629 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002630 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002631 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002632 return;
2633 }
Mike Stumpbf916502009-07-24 19:02:52 +00002634
Chandler Carruth87c44602011-07-01 23:49:12 +00002635 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002636 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002637 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002638 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002639 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002640 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002641}
2642
Chandler Carruth1b03c872011-07-02 00:01:44 +00002643static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002644 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002645 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002646 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002647
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002648 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002649}
2650
Chandler Carruth1b03c872011-07-02 00:01:44 +00002651static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002652 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2654 return;
2655 }
Mike Stumpbf916502009-07-24 19:02:52 +00002656
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002657 if (Attr.getNumArgs() != 0) {
2658 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2659 return;
2660 }
Mike Stumpbf916502009-07-24 19:02:52 +00002661
Chandler Carruth87c44602011-07-01 23:49:12 +00002662 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002663
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002664 if (!VD || !VD->hasLocalStorage()) {
2665 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2666 return;
2667 }
Mike Stumpbf916502009-07-24 19:02:52 +00002668
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002669 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002670 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002671 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002672 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2673 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002674 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002675 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002676 Attr.getParameterName();
2677 return;
2678 }
Mike Stumpbf916502009-07-24 19:02:52 +00002679
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002680 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2681 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002682 S.Diag(Attr.getParameterLoc(),
2683 diag::err_attribute_cleanup_arg_not_function)
2684 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002685 return;
2686 }
2687
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002688 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002689 S.Diag(Attr.getParameterLoc(),
2690 diag::err_attribute_cleanup_func_must_take_one_arg)
2691 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002692 return;
2693 }
Mike Stumpbf916502009-07-24 19:02:52 +00002694
Anders Carlsson89941c12009-02-07 23:16:50 +00002695 // We're currently more strict than GCC about what function types we accept.
2696 // If this ever proves to be a problem it should be easy to fix.
2697 QualType Ty = S.Context.getPointerType(VD->getType());
2698 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002699 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2700 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002701 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002702 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2703 Attr.getParameterName() << ParamTy << Ty;
2704 return;
2705 }
Mike Stumpbf916502009-07-24 19:02:52 +00002706
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002707 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002708 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002709}
2710
Mike Stumpbf916502009-07-24 19:02:52 +00002711/// Handle __attribute__((format_arg((idx)))) attribute based on
2712/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002713static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002714 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002715 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002716
Chandler Carruth87c44602011-07-01 23:49:12 +00002717 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002718 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002719 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002720 return;
2721 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002722
2723 // In C++ the implicit 'this' function parameter also counts, and they are
2724 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002725 bool HasImplicitThisParam = isInstanceMethod(D);
2726 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002727 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002728
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002729 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002730 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002731 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002732 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2733 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002734 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2735 << "format" << 2 << IdxExpr->getSourceRange();
2736 return;
2737 }
Mike Stumpbf916502009-07-24 19:02:52 +00002738
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002739 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2740 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2741 << "format" << 2 << IdxExpr->getSourceRange();
2742 return;
2743 }
Mike Stumpbf916502009-07-24 19:02:52 +00002744
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002745 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002746
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002747 if (HasImplicitThisParam) {
2748 if (ArgIdx == 0) {
2749 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2750 << "format_arg" << IdxExpr->getSourceRange();
2751 return;
2752 }
2753 ArgIdx--;
2754 }
2755
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002756 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002757 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002758
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002759 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2760 if (not_nsstring_type &&
2761 !isCFStringType(Ty, S.Context) &&
2762 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002763 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002764 // FIXME: Should highlight the actual expression that has the wrong type.
2765 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002766 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002767 << IdxExpr->getSourceRange();
2768 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002769 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002770 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002771 if (!isNSStringType(Ty, S.Context) &&
2772 !isCFStringType(Ty, S.Context) &&
2773 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002774 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002775 // FIXME: Should highlight the actual expression that has the wrong type.
2776 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002777 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002778 << IdxExpr->getSourceRange();
2779 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002780 }
2781
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002782 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002783 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002784}
2785
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002786enum FormatAttrKind {
2787 CFStringFormat,
2788 NSStringFormat,
2789 StrftimeFormat,
2790 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002791 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002792 InvalidFormat
2793};
2794
2795/// getFormatAttrKind - Map from format attribute names to supported format
2796/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002797static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002798 return llvm::StringSwitch<FormatAttrKind>(Format)
2799 // Check for formats that get handled specially.
2800 .Case("NSString", NSStringFormat)
2801 .Case("CFString", CFStringFormat)
2802 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002803
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002804 // Otherwise, check for supported formats.
2805 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2806 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2807 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002808
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002809 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2810 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002811}
2812
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002813/// Handle __attribute__((init_priority(priority))) attributes based on
2814/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002815static void handleInitPriorityAttr(Sema &S, Decl *D,
2816 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002817 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002818 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2819 return;
2820 }
2821
Chandler Carruth87c44602011-07-01 23:49:12 +00002822 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002823 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2824 Attr.setInvalid();
2825 return;
2826 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002827 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002828 if (S.Context.getAsArrayType(T))
2829 T = S.Context.getBaseElementType(T);
2830 if (!T->getAs<RecordType>()) {
2831 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2832 Attr.setInvalid();
2833 return;
2834 }
2835
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002836 if (Attr.getNumArgs() != 1) {
2837 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2838 Attr.setInvalid();
2839 return;
2840 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002841 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002842
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002843 llvm::APSInt priority(32);
2844 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2845 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2846 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2847 << "init_priority" << priorityExpr->getSourceRange();
2848 Attr.setInvalid();
2849 return;
2850 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002851 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002852 if (prioritynum < 101 || prioritynum > 65535) {
2853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2854 << priorityExpr->getSourceRange();
2855 Attr.setInvalid();
2856 return;
2857 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002858 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002859 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002860}
2861
Rafael Espindola599f1b72012-05-13 03:25:18 +00002862FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2863 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002864 // Check whether we already have an equivalent format attribute.
2865 for (specific_attr_iterator<FormatAttr>
2866 i = D->specific_attr_begin<FormatAttr>(),
2867 e = D->specific_attr_end<FormatAttr>();
2868 i != e ; ++i) {
2869 FormatAttr *f = *i;
2870 if (f->getType() == Format &&
2871 f->getFormatIdx() == FormatIdx &&
2872 f->getFirstArg() == FirstArg) {
2873 // If we don't have a valid location for this attribute, adopt the
2874 // location.
2875 if (f->getLocation().isInvalid())
2876 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002877 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002878 }
2879 }
2880
Rafael Espindola599f1b72012-05-13 03:25:18 +00002881 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2882 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002883}
2884
Mike Stumpbf916502009-07-24 19:02:52 +00002885/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2886/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002887static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002888
Chris Lattner545dd342008-06-28 23:36:30 +00002889 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002890 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002891 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002892 return;
2893 }
2894
Chris Lattner545dd342008-06-28 23:36:30 +00002895 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002896 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002897 return;
2898 }
2899
Chandler Carruth87c44602011-07-01 23:49:12 +00002900 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002901 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002902 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002903 return;
2904 }
2905
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002906 // In C++ the implicit 'this' function parameter also counts, and they are
2907 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002908 bool HasImplicitThisParam = isInstanceMethod(D);
2909 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002910 unsigned FirstIdx = 1;
2911
Chris Lattner5f9e2722011-07-23 10:55:15 +00002912 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002913
2914 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002915 if (Format.startswith("__") && Format.endswith("__"))
2916 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002917
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002918 // Check for supported formats.
2919 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002920
2921 if (Kind == IgnoredFormat)
2922 return;
2923
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002924 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002925 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002926 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002927 return;
2928 }
2929
2930 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002931 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002932 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002933 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2934 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002935 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002936 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002937 return;
2938 }
2939
2940 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002941 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002942 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002943 return;
2944 }
2945
2946 // FIXME: Do we need to bounds check?
2947 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002948
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002949 if (HasImplicitThisParam) {
2950 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002951 S.Diag(Attr.getLoc(),
2952 diag::err_format_attribute_implicit_this_format_string)
2953 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002954 return;
2955 }
2956 ArgIdx--;
2957 }
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Chris Lattner6b6b5372008-06-26 18:38:35 +00002959 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002960 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002961
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002962 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002963 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002964 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2965 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002966 return;
2967 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002968 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002969 // FIXME: do we need to check if the type is NSString*? What are the
2970 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002971 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002972 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002973 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2974 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002975 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002976 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002977 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002978 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002979 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002980 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2981 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002982 return;
2983 }
2984
2985 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002986 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002987 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002988 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2989 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002990 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002991 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002992 return;
2993 }
2994
2995 // check if the function is variadic if the 3rd argument non-zero
2996 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002997 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002998 ++NumArgs; // +1 for ...
2999 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003000 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003001 return;
3002 }
3003 }
3004
Chris Lattner3c73c412008-11-19 08:23:25 +00003005 // strftime requires FirstArg to be 0 because it doesn't read from any
3006 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003007 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003008 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003009 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3010 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003011 return;
3012 }
3013 // if 0 it disables parameter checking (to use with e.g. va_list)
3014 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003015 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003016 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003017 return;
3018 }
3019
Rafael Espindola599f1b72012-05-13 03:25:18 +00003020 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3021 Idx.getZExtValue(),
3022 FirstArg.getZExtValue());
3023 if (NewAttr)
3024 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003025}
3026
Chandler Carruth1b03c872011-07-02 00:01:44 +00003027static void handleTransparentUnionAttr(Sema &S, Decl *D,
3028 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003029 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003030 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003031 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003032
Chris Lattner6b6b5372008-06-26 18:38:35 +00003033
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003034 // Try to find the underlying union declaration.
3035 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003036 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003037 if (TD && TD->getUnderlyingType()->isUnionType())
3038 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3039 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003040 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003041
3042 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003043 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003044 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003045 return;
3046 }
3047
John McCall5e1cdac2011-10-07 06:10:15 +00003048 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003049 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003050 diag::warn_transparent_union_attribute_not_definition);
3051 return;
3052 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003053
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003054 RecordDecl::field_iterator Field = RD->field_begin(),
3055 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003056 if (Field == FieldEnd) {
3057 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3058 return;
3059 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003060
David Blaikie581deb32012-06-06 20:45:41 +00003061 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003062 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003063 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003064 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003065 diag::warn_transparent_union_attribute_floating)
3066 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003067 return;
3068 }
3069
3070 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3071 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3072 for (; Field != FieldEnd; ++Field) {
3073 QualType FieldType = Field->getType();
3074 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3075 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3076 // Warn if we drop the attribute.
3077 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003078 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003079 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003080 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003081 diag::warn_transparent_union_attribute_field_size_align)
3082 << isSize << Field->getDeclName() << FieldBits;
3083 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003084 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003085 diag::note_transparent_union_first_field_size_align)
3086 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003087 return;
3088 }
3089 }
3090
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003091 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003092}
3093
Chandler Carruth1b03c872011-07-02 00:01:44 +00003094static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003095 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003096 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003097 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003098
Peter Collingbourne7a730022010-11-23 20:45:58 +00003099 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003100 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003101
Chris Lattner6b6b5372008-06-26 18:38:35 +00003102 // Make sure that there is a string literal as the annotation's single
3103 // argument.
3104 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003105 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003106 return;
3107 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003108
3109 // Don't duplicate annotations that are already set.
3110 for (specific_attr_iterator<AnnotateAttr>
3111 i = D->specific_attr_begin<AnnotateAttr>(),
3112 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3113 if ((*i)->getAnnotation() == SE->getString())
3114 return;
3115 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003116 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003117 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003118}
3119
Chandler Carruth1b03c872011-07-02 00:01:44 +00003120static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003121 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003122 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003123 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003124 return;
3125 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003126
Sean Huntbbd37c62009-11-21 08:43:09 +00003127 //FIXME: The C++0x version of this attribute has more limited applicabilty
3128 // than GNU's, and should error out when it is used to specify a
3129 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003130
Chris Lattner545dd342008-06-28 23:36:30 +00003131 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003132 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3133 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003134 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003135 }
Mike Stumpbf916502009-07-24 19:02:52 +00003136
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003137 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3138 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003139}
3140
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003141void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3142 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003143 // FIXME: Handle pack-expansions here.
3144 if (DiagnoseUnexpandedParameterPack(E))
3145 return;
3146
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003147 if (E->isTypeDependent() || E->isValueDependent()) {
3148 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003149 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3150 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003151 return;
3152 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003153
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003154 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003155 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003156 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003157 ExprResult ICE
3158 = VerifyIntegerConstantExpression(E, &Alignment,
3159 diag::err_aligned_attribute_argument_not_int,
3160 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003161 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003162 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003163 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003164 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3165 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003166 return;
3167 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003168 if (isDeclSpec) {
3169 // We've already verified it's a power of 2, now let's make sure it's
3170 // 8192 or less.
3171 if (Alignment.getZExtValue() > 8192) {
3172 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3173 << E->getSourceRange();
3174 return;
3175 }
3176 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003177
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003178 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3179 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003180}
3181
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003182void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3183 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003184 // FIXME: Cache the number on the Attr object if non-dependent?
3185 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003186 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3187 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003188 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003189}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003190
Chandler Carruthd309c812011-07-01 23:49:16 +00003191/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003192/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003193///
Mike Stumpbf916502009-07-24 19:02:52 +00003194/// Despite what would be logical, the mode attribute is a decl attribute, not a
3195/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3196/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003197static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003198 // This attribute isn't documented, but glibc uses it. It changes
3199 // the width of an int or unsigned int to the specified size.
3200
3201 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003202 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003203 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003204
Chris Lattnerfbf13472008-06-27 22:18:37 +00003205
3206 IdentifierInfo *Name = Attr.getParameterName();
3207 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003208 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003209 return;
3210 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003211
Chris Lattner5f9e2722011-07-23 10:55:15 +00003212 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003213
3214 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003215 if (Str.startswith("__") && Str.endswith("__"))
3216 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003217
3218 unsigned DestWidth = 0;
3219 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003220 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003221 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003222 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003223 switch (Str[0]) {
3224 case 'Q': DestWidth = 8; break;
3225 case 'H': DestWidth = 16; break;
3226 case 'S': DestWidth = 32; break;
3227 case 'D': DestWidth = 64; break;
3228 case 'X': DestWidth = 96; break;
3229 case 'T': DestWidth = 128; break;
3230 }
3231 if (Str[1] == 'F') {
3232 IntegerMode = false;
3233 } else if (Str[1] == 'C') {
3234 IntegerMode = false;
3235 ComplexMode = true;
3236 } else if (Str[1] != 'I') {
3237 DestWidth = 0;
3238 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003239 break;
3240 case 4:
3241 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3242 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003243 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003244 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003245 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003246 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003247 break;
3248 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003249 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003250 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003251 break;
3252 }
3253
3254 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003255 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003256 OldTy = TD->getUnderlyingType();
3257 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3258 OldTy = VD->getType();
3259 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003260 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003261 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003262 return;
3263 }
Eli Friedman73397492009-03-03 06:41:03 +00003264
John McCall183700f2009-09-21 23:43:11 +00003265 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003266 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3267 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003268 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003269 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3270 } else if (ComplexMode) {
3271 if (!OldTy->isComplexType())
3272 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3273 } else {
3274 if (!OldTy->isFloatingType())
3275 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3276 }
3277
Mike Stump390b4cc2009-05-16 07:39:55 +00003278 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3279 // and friends, at least with glibc.
3280 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3281 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003282 // FIXME: Make sure floating-point mappings are accurate
3283 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003284 QualType NewTy;
3285 switch (DestWidth) {
3286 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003287 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003288 return;
3289 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003290 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003291 return;
3292 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003293 if (!IntegerMode) {
3294 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3295 return;
3296 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003297 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003298 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003299 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003300 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003301 break;
3302 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003303 if (!IntegerMode) {
3304 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3305 return;
3306 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003307 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003308 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003309 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003310 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003311 break;
3312 case 32:
3313 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003314 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003315 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003316 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003317 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003318 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003319 break;
3320 case 64:
3321 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003322 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003323 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003324 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003325 NewTy = S.Context.LongTy;
3326 else
3327 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003328 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003329 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003330 NewTy = S.Context.UnsignedLongTy;
3331 else
3332 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003333 break;
Eli Friedman73397492009-03-03 06:41:03 +00003334 case 96:
3335 NewTy = S.Context.LongDoubleTy;
3336 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003337 case 128:
3338 if (!IntegerMode) {
3339 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3340 return;
3341 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003342 if (OldTy->isSignedIntegerType())
3343 NewTy = S.Context.Int128Ty;
3344 else
3345 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003346 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003347 }
3348
Eli Friedman73397492009-03-03 06:41:03 +00003349 if (ComplexMode) {
3350 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003351 }
3352
3353 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003354 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003355 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003356 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003357 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003358 cast<ValueDecl>(D)->setType(NewTy);
3359}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003360
Chandler Carruth1b03c872011-07-02 00:01:44 +00003361static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003362 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003363 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003364 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003365
Nick Lewycky78d1a102012-07-24 01:40:49 +00003366 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3367 if (!VD->hasGlobalStorage())
3368 S.Diag(Attr.getLoc(),
3369 diag::warn_attribute_requires_functions_or_static_globals)
3370 << Attr.getName();
3371 } else if (!isFunctionOrMethod(D)) {
3372 S.Diag(Attr.getLoc(),
3373 diag::warn_attribute_requires_functions_or_static_globals)
3374 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003375 return;
3376 }
Mike Stumpbf916502009-07-24 19:02:52 +00003377
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003378 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003379}
3380
Chandler Carruth1b03c872011-07-02 00:01:44 +00003381static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003382 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003383 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003384 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003385
Mike Stumpbf916502009-07-24 19:02:52 +00003386
Chandler Carruth87c44602011-07-01 23:49:12 +00003387 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003388 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003389 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003390 return;
3391 }
Mike Stumpbf916502009-07-24 19:02:52 +00003392
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003393 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003394}
3395
Chandler Carruth1b03c872011-07-02 00:01:44 +00003396static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3397 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003398 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003399 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003400 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003401
Chris Lattner7255a2d2010-06-22 00:03:40 +00003402
Chandler Carruth87c44602011-07-01 23:49:12 +00003403 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003404 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003405 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003406 return;
3407 }
3408
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003409 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003410 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003411}
3412
Chandler Carruth1b03c872011-07-02 00:01:44 +00003413static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003414 if (S.LangOpts.CUDA) {
3415 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003416 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003417 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3418 return;
3419 }
3420
Chandler Carruth87c44602011-07-01 23:49:12 +00003421 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003422 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003423 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003424 return;
3425 }
3426
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003427 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003428 } else {
3429 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3430 }
3431}
3432
Chandler Carruth1b03c872011-07-02 00:01:44 +00003433static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003434 if (S.LangOpts.CUDA) {
3435 // check the attribute arguments.
3436 if (Attr.getNumArgs() != 0) {
3437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3438 return;
3439 }
3440
Chandler Carruth87c44602011-07-01 23:49:12 +00003441 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003443 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003444 return;
3445 }
3446
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003447 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003448 } else {
3449 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3450 }
3451}
3452
Chandler Carruth1b03c872011-07-02 00:01:44 +00003453static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003454 if (S.LangOpts.CUDA) {
3455 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003456 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003457 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003458
Chandler Carruth87c44602011-07-01 23:49:12 +00003459 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003460 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003461 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003462 return;
3463 }
3464
Chandler Carruth87c44602011-07-01 23:49:12 +00003465 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003466 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003467 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003468 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3469 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3470 << FD->getType()
3471 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3472 "void");
3473 } else {
3474 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3475 << FD->getType();
3476 }
3477 return;
3478 }
3479
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003480 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003481 } else {
3482 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3483 }
3484}
3485
Chandler Carruth1b03c872011-07-02 00:01:44 +00003486static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003487 if (S.LangOpts.CUDA) {
3488 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003489 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003490 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003491
Peter Collingbourneced76712010-12-01 03:15:31 +00003492
Chandler Carruth87c44602011-07-01 23:49:12 +00003493 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003494 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003495 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003496 return;
3497 }
3498
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003499 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003500 } else {
3501 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3502 }
3503}
3504
Chandler Carruth1b03c872011-07-02 00:01:44 +00003505static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003506 if (S.LangOpts.CUDA) {
3507 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003508 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003509 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003510
Peter Collingbourneced76712010-12-01 03:15:31 +00003511
Chandler Carruth87c44602011-07-01 23:49:12 +00003512 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003513 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003514 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003515 return;
3516 }
3517
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003518 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003519 } else {
3520 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3521 }
3522}
3523
Chandler Carruth1b03c872011-07-02 00:01:44 +00003524static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003525 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003526 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003527 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003528
Chandler Carruth87c44602011-07-01 23:49:12 +00003529 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003530 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003532 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003533 return;
3534 }
Mike Stumpbf916502009-07-24 19:02:52 +00003535
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003536 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003537 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003538 return;
3539 }
Mike Stumpbf916502009-07-24 19:02:52 +00003540
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003541 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003542}
3543
Chandler Carruth1b03c872011-07-02 00:01:44 +00003544static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003545 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003546
Chandler Carruth87c44602011-07-01 23:49:12 +00003547 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003548 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3549 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003550 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003551 return;
3552
Chandler Carruth87c44602011-07-01 23:49:12 +00003553 if (!isa<ObjCMethodDecl>(D)) {
3554 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3555 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003556 return;
3557 }
3558
Chandler Carruth87c44602011-07-01 23:49:12 +00003559 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003560 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003561 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003562 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003563 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003564 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003565 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003566 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003567 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003568 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003569 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003570 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003571 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003572 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003573 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003574 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003575 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003576 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003577 switch (CC) {
3578 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003579 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003580 break;
3581 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003582 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003583 break;
3584 default:
3585 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003586 }
3587
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003588 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003589 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003590 default:
3591 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003592 }
3593}
3594
Chandler Carruth1b03c872011-07-02 00:01:44 +00003595static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003596 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003597 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003598}
3599
John McCall711c52b2011-01-05 12:14:39 +00003600bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3601 if (attr.isInvalid())
3602 return true;
3603
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003604 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3605 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3606 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003607 attr.setInvalid();
3608 return true;
3609 }
3610
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003611 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3612 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003613 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003614 case AttributeList::AT_CDecl: CC = CC_C; break;
3615 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3616 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3617 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3618 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3619 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003620 Expr *Arg = attr.getArg(0);
3621 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003622 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003623 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3624 << "pcs" << 1;
3625 attr.setInvalid();
3626 return true;
3627 }
3628
Chris Lattner5f9e2722011-07-23 10:55:15 +00003629 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003630 if (StrRef == "aapcs") {
3631 CC = CC_AAPCS;
3632 break;
3633 } else if (StrRef == "aapcs-vfp") {
3634 CC = CC_AAPCS_VFP;
3635 break;
3636 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003637
3638 attr.setInvalid();
3639 Diag(attr.getLoc(), diag::err_invalid_pcs);
3640 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003641 }
David Blaikie7530c032012-01-17 06:56:22 +00003642 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003643 }
3644
3645 return false;
3646}
3647
Chandler Carruth1b03c872011-07-02 00:01:44 +00003648static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003649 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003650
3651 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003652 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003653 return;
3654
Chandler Carruth87c44602011-07-01 23:49:12 +00003655 if (!isa<ObjCMethodDecl>(D)) {
3656 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3657 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003658 return;
3659 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003660
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003661 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003662}
3663
3664/// Checks a regparm attribute, returning true if it is ill-formed and
3665/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003666bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3667 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003668 return true;
3669
Chandler Carruth87c44602011-07-01 23:49:12 +00003670 if (Attr.getNumArgs() != 1) {
3671 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3672 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003673 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003674 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003675
Chandler Carruth87c44602011-07-01 23:49:12 +00003676 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003677 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003678 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003679 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003680 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003681 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003682 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003683 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003684 }
3685
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003686 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003687 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003688 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003689 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003690 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003691 }
3692
John McCall711c52b2011-01-05 12:14:39 +00003693 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003694 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003695 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003696 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003697 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003698 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003699 }
3700
John McCall711c52b2011-01-05 12:14:39 +00003701 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003702}
3703
Chandler Carruth1b03c872011-07-02 00:01:44 +00003704static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003705 if (S.LangOpts.CUDA) {
3706 // check the attribute arguments.
3707 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003708 // FIXME: 0 is not okay.
3709 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003710 return;
3711 }
3712
Chandler Carruth87c44602011-07-01 23:49:12 +00003713 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003714 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003715 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003716 return;
3717 }
3718
3719 Expr *MaxThreadsExpr = Attr.getArg(0);
3720 llvm::APSInt MaxThreads(32);
3721 if (MaxThreadsExpr->isTypeDependent() ||
3722 MaxThreadsExpr->isValueDependent() ||
3723 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3724 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3725 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3726 return;
3727 }
3728
3729 llvm::APSInt MinBlocks(32);
3730 if (Attr.getNumArgs() > 1) {
3731 Expr *MinBlocksExpr = Attr.getArg(1);
3732 if (MinBlocksExpr->isTypeDependent() ||
3733 MinBlocksExpr->isValueDependent() ||
3734 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3735 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3736 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3737 return;
3738 }
3739 }
3740
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003741 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003742 MaxThreads.getZExtValue(),
3743 MinBlocks.getZExtValue()));
3744 } else {
3745 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3746 }
3747}
3748
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003749static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3750 const AttributeList &Attr) {
3751 StringRef AttrName = Attr.getName()->getName();
3752 if (!Attr.getParameterName()) {
3753 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3754 << Attr.getName() << /* arg num = */ 1;
3755 return;
3756 }
3757
3758 if (Attr.getNumArgs() != 2) {
3759 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3760 << /* required args = */ 3;
3761 return;
3762 }
3763
3764 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3765
3766 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3767 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3768 << Attr.getName() << ExpectedFunctionOrMethod;
3769 return;
3770 }
3771
3772 uint64_t ArgumentIdx;
3773 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3774 Attr.getLoc(), 2,
3775 Attr.getArg(0), ArgumentIdx))
3776 return;
3777
3778 uint64_t TypeTagIdx;
3779 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3780 Attr.getLoc(), 3,
3781 Attr.getArg(1), TypeTagIdx))
3782 return;
3783
3784 bool IsPointer = (AttrName == "pointer_with_type_tag");
3785 if (IsPointer) {
3786 // Ensure that buffer has a pointer type.
3787 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3788 if (!BufferTy->isPointerType()) {
3789 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3790 << AttrName;
3791 }
3792 }
3793
3794 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3795 S.Context,
3796 ArgumentKind,
3797 ArgumentIdx,
3798 TypeTagIdx,
3799 IsPointer));
3800}
3801
3802static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3803 const AttributeList &Attr) {
3804 IdentifierInfo *PointerKind = Attr.getParameterName();
3805 if (!PointerKind) {
3806 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3807 << "type_tag_for_datatype" << 1;
3808 return;
3809 }
3810
3811 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3812
3813 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3814 Attr.getRange(),
3815 S.Context,
3816 PointerKind,
3817 MatchingCType,
3818 Attr.getLayoutCompatible(),
3819 Attr.getMustBeNull()));
3820}
3821
Chris Lattner0744e5f2008-06-29 00:23:49 +00003822//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003823// Checker-specific attribute handlers.
3824//===----------------------------------------------------------------------===//
3825
John McCallc7ad3812011-01-25 03:31:58 +00003826static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003827 return type->isDependentType() ||
3828 type->isObjCObjectPointerType() ||
3829 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003830}
3831static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003832 return type->isDependentType() ||
3833 type->isPointerType() ||
3834 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003835}
3836
Chandler Carruth1b03c872011-07-02 00:01:44 +00003837static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003838 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003839 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003840 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003841 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003842 return;
3843 }
3844
3845 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003846 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003847 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3848 cf = false;
3849 } else {
3850 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3851 cf = true;
3852 }
3853
3854 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003855 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003856 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003857 return;
3858 }
3859
3860 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003861 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003862 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003863 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003864}
3865
Chandler Carruth1b03c872011-07-02 00:01:44 +00003866static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3867 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003868 if (!isa<ObjCMethodDecl>(D)) {
3869 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003870 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003871 return;
3872 }
3873
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003874 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003875}
3876
Chandler Carruth1b03c872011-07-02 00:01:44 +00003877static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3878 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003879
John McCallc7ad3812011-01-25 03:31:58 +00003880 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003881
Chandler Carruth87c44602011-07-01 23:49:12 +00003882 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003883 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003884 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003885 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003886 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003887 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3888 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003889 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003890 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003891 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003892 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003893 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003894 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003895 return;
3896 }
Mike Stumpbf916502009-07-24 19:02:52 +00003897
John McCallc7ad3812011-01-25 03:31:58 +00003898 bool typeOK;
3899 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003900 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003901 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003902 case AttributeList::AT_NSReturnsAutoreleased:
3903 case AttributeList::AT_NSReturnsRetained:
3904 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003905 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3906 cf = false;
3907 break;
3908
Sean Hunt8e083e72012-06-19 23:57:03 +00003909 case AttributeList::AT_CFReturnsRetained:
3910 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003911 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3912 cf = true;
3913 break;
3914 }
3915
3916 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003917 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003918 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003919 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003920 }
Mike Stumpbf916502009-07-24 19:02:52 +00003921
Chandler Carruth87c44602011-07-01 23:49:12 +00003922 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003923 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003924 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003925 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003926 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003927 S.Context));
3928 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003929 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003930 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003931 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003932 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003933 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003934 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003935 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003936 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003937 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003938 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003939 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003940 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003941 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003942 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003943 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003944 return;
3945 };
3946}
3947
John McCalldc7c5ad2011-07-22 08:53:00 +00003948static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3949 const AttributeList &attr) {
3950 SourceLocation loc = attr.getLoc();
3951
3952 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3953
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003954 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003955 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003956 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003957 return;
3958 }
3959
3960 // Check that the method returns a normal pointer.
3961 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003962
3963 if (!resultType->isReferenceType() &&
3964 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003965 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3966 << SourceRange(loc)
3967 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3968
3969 // Drop the attribute.
3970 return;
3971 }
3972
3973 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003974 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003975}
3976
John McCall8dfac0b2011-09-30 05:12:12 +00003977/// Handle cf_audited_transfer and cf_unknown_transfer.
3978static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3979 if (!isa<FunctionDecl>(D)) {
3980 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003981 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003982 return;
3983 }
3984
Sean Hunt8e083e72012-06-19 23:57:03 +00003985 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00003986
3987 // Check whether there's a conflicting attribute already present.
3988 Attr *Existing;
3989 if (IsAudited) {
3990 Existing = D->getAttr<CFUnknownTransferAttr>();
3991 } else {
3992 Existing = D->getAttr<CFAuditedTransferAttr>();
3993 }
3994 if (Existing) {
3995 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3996 << A.getName()
3997 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3998 << A.getRange() << Existing->getRange();
3999 return;
4000 }
4001
4002 // All clear; add the attribute.
4003 if (IsAudited) {
4004 D->addAttr(
4005 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4006 } else {
4007 D->addAttr(
4008 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4009 }
4010}
4011
John McCallfe98da02011-09-29 07:17:38 +00004012static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4013 const AttributeList &Attr) {
4014 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4015 if (!RD || RD->isUnion()) {
4016 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004017 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004018 }
4019
4020 IdentifierInfo *ParmName = Attr.getParameterName();
4021
4022 // In Objective-C, verify that the type names an Objective-C type.
4023 // We don't want to check this outside of ObjC because people sometimes
4024 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004025 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004026 // Check for an existing type with this name.
4027 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4028 Sema::LookupOrdinaryName);
4029 if (S.LookupName(R, Sc)) {
4030 NamedDecl *Target = R.getFoundDecl();
4031 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4032 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4033 S.Diag(Target->getLocStart(), diag::note_declared_at);
4034 }
4035 }
4036 }
4037
4038 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4039 ParmName));
4040}
4041
Chandler Carruth1b03c872011-07-02 00:01:44 +00004042static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4043 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004044 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004045
Chandler Carruth87c44602011-07-01 23:49:12 +00004046 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004047 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004048}
4049
Chandler Carruth1b03c872011-07-02 00:01:44 +00004050static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4051 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004052 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004053 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004054 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004055 return;
4056 }
4057
Chandler Carruth87c44602011-07-01 23:49:12 +00004058 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004059 QualType type = vd->getType();
4060
4061 if (!type->isDependentType() &&
4062 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004063 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004064 << type;
4065 return;
4066 }
4067
4068 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4069
4070 // If we have no lifetime yet, check the lifetime we're presumably
4071 // going to infer.
4072 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4073 lifetime = type->getObjCARCImplicitLifetime();
4074
4075 switch (lifetime) {
4076 case Qualifiers::OCL_None:
4077 assert(type->isDependentType() &&
4078 "didn't infer lifetime for non-dependent type?");
4079 break;
4080
4081 case Qualifiers::OCL_Weak: // meaningful
4082 case Qualifiers::OCL_Strong: // meaningful
4083 break;
4084
4085 case Qualifiers::OCL_ExplicitNone:
4086 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004087 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004088 << (lifetime == Qualifiers::OCL_Autoreleasing);
4089 break;
4090 }
4091
Chandler Carruth87c44602011-07-01 23:49:12 +00004092 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004093 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004094}
4095
Francois Pichet11542142010-12-19 06:50:37 +00004096//===----------------------------------------------------------------------===//
4097// Microsoft specific attribute handlers.
4098//===----------------------------------------------------------------------===//
4099
Chandler Carruth1b03c872011-07-02 00:01:44 +00004100static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004101 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004102 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004103 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004104 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004105
Francois Pichet11542142010-12-19 06:50:37 +00004106 Expr *Arg = Attr.getArg(0);
4107 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004108 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4110 << "uuid" << 1;
4111 return;
4112 }
4113
Chris Lattner5f9e2722011-07-23 10:55:15 +00004114 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004115
4116 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4117 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004118
Francois Pichetd3d3be92010-12-20 01:41:49 +00004119 // Validate GUID length.
4120 if (IsCurly && StrRef.size() != 38) {
4121 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4122 return;
4123 }
4124 if (!IsCurly && StrRef.size() != 36) {
4125 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4126 return;
4127 }
4128
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004129 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004130 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004131 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004132 if (IsCurly) // Skip the optional '{'
4133 ++I;
4134
4135 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004136 if (i == 8 || i == 13 || i == 18 || i == 23) {
4137 if (*I != '-') {
4138 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4139 return;
4140 }
4141 } else if (!isxdigit(*I)) {
4142 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4143 return;
4144 }
4145 I++;
4146 }
Francois Pichet11542142010-12-19 06:50:37 +00004147
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004148 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004149 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004150 } else
Francois Pichet11542142010-12-19 06:50:37 +00004151 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004152}
4153
John McCallc052dbb2012-05-22 21:28:12 +00004154static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Joao Matos679fc932012-09-04 17:18:12 +00004155 if (S.LangOpts.MicrosoftExt) {
4156 AttributeList::Kind Kind = Attr.getKind();
4157 if (Kind == AttributeList::AT_SingleInheritance)
4158 D->addAttr(
4159 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4160 else if (Kind == AttributeList::AT_MultipleInheritance)
4161 D->addAttr(
4162 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4163 else if (Kind == AttributeList::AT_VirtualInheritance)
4164 D->addAttr(
4165 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
4166 } else
John McCallc052dbb2012-05-22 21:28:12 +00004167 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4168}
4169
4170static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4171 if (S.LangOpts.MicrosoftExt) {
4172 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004173 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004174 D->addAttr(
4175 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004176 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004177 D->addAttr(
4178 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004179 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004180 D->addAttr(
4181 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4182 } else
4183 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4184}
4185
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004186static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4187 if (S.LangOpts.MicrosoftExt)
4188 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4189 else
4190 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4191}
4192
Ted Kremenekb71368d2009-05-09 02:44:38 +00004193//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004194// Top Level Sema Entry Points
4195//===----------------------------------------------------------------------===//
4196
Chandler Carruth1b03c872011-07-02 00:01:44 +00004197static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4198 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004199 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004200 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4201 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4202 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004203 default:
4204 break;
4205 }
4206}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004207
Chandler Carruth1b03c872011-07-02 00:01:44 +00004208static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4209 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004210 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004211 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4212 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4213 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004214 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004215 case AttributeList::AT_AddressSpace:
4216 case AttributeList::AT_OpenCLImageAccess:
4217 case AttributeList::AT_ObjCGC:
4218 case AttributeList::AT_VectorSize:
4219 case AttributeList::AT_NeonVectorType:
4220 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004221 // Ignore these, these are type attributes, handled by
4222 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004223 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004224 case AttributeList::AT_CUDADevice:
4225 case AttributeList::AT_CUDAHost:
4226 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004227 // Ignore, this is a non-inheritable attribute, handled
4228 // by ProcessNonInheritableDeclAttr.
4229 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004230 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4231 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4232 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4233 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004234 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004235 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004236 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004237 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004238 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4239 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4240 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004241 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004242 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4243 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4244 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4245 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004246 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4247 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004248 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4249 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004250 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004251 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004252 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4253 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4254 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4255 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4256 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004257 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004258 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004259 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4260 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4261 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4262 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4263 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004264 case AttributeList::AT_ownership_returns:
4265 case AttributeList::AT_ownership_takes:
4266 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004267 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004268 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4269 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4270 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4271 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4272 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4273 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4274 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004275
Sean Hunt8e083e72012-06-19 23:57:03 +00004276 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004277 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004278 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004279 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004280
Sean Hunt8e083e72012-06-19 23:57:03 +00004281 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004282 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4283
Sean Hunt8e083e72012-06-19 23:57:03 +00004284 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004285 handleNSBridgedAttr(S, scope, D, Attr); break;
4286
Sean Hunt8e083e72012-06-19 23:57:03 +00004287 case AttributeList::AT_CFAuditedTransfer:
4288 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004289 handleCFTransferAttr(S, D, Attr); break;
4290
Ted Kremenekb71368d2009-05-09 02:44:38 +00004291 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004292 case AttributeList::AT_CFConsumed:
4293 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4294 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004295 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004296
Sean Hunt8e083e72012-06-19 23:57:03 +00004297 case AttributeList::AT_NSReturnsAutoreleased:
4298 case AttributeList::AT_NSReturnsNotRetained:
4299 case AttributeList::AT_CFReturnsNotRetained:
4300 case AttributeList::AT_NSReturnsRetained:
4301 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004302 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004303
Tanya Lattner0df579e2012-07-09 22:06:01 +00004304 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004305 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004306 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004307
Sean Hunt8e083e72012-06-19 23:57:03 +00004308 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004309 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004310
Sean Hunt8e083e72012-06-19 23:57:03 +00004311 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4312 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4313 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004314 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4315 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004316 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004317 handleArcWeakrefUnavailableAttr (S, D, Attr);
4318 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004319 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004320 handleObjCRootClassAttr(S, D, Attr);
4321 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004322 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004323 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004324 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004325 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4326 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004327 handleReturnsTwiceAttr(S, D, Attr);
4328 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004329 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4330 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4331 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004332 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004333 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4334 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4335 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4336 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004337 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004338 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004339 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004340 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004341 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004342 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004343 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004344 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004345 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4346 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4347 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4348 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4349 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4350 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4351 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4352 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4353 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004354 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004355 // Just ignore
4356 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004357 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004358 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004359 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004360 case AttributeList::AT_StdCall:
4361 case AttributeList::AT_CDecl:
4362 case AttributeList::AT_FastCall:
4363 case AttributeList::AT_ThisCall:
4364 case AttributeList::AT_Pascal:
4365 case AttributeList::AT_Pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004366 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004367 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004368 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004369 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004370 break;
John McCallc052dbb2012-05-22 21:28:12 +00004371
4372 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004373 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004374 handleMsStructAttr(S, D, Attr);
4375 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004376 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004377 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004378 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004379 case AttributeList::AT_SingleInheritance:
4380 case AttributeList::AT_MultipleInheritance:
4381 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004382 handleInheritanceAttr(S, D, Attr);
4383 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004384 case AttributeList::AT_Win64:
4385 case AttributeList::AT_Ptr32:
4386 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004387 handlePortabilityAttr(S, D, Attr);
4388 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004389 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004390 handleForceInlineAttr(S, D, Attr);
4391 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004392
4393 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004394 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004395 handleGuardedVarAttr(S, D, Attr);
4396 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004397 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004398 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004399 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004400 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004401 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004402 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004403 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004404 handleNoAddressSafetyAttr(S, D, Attr);
4405 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004406 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004407 handleNoThreadSafetyAttr(S, D, Attr);
4408 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004409 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004410 handleLockableAttr(S, D, Attr);
4411 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004412 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004413 handleGuardedByAttr(S, D, Attr);
4414 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004415 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004416 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004417 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004418 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004419 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004420 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004421 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004422 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004423 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004424 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004425 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004426 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004427 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004428 handleLockReturnedAttr(S, D, Attr);
4429 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004430 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004431 handleLocksExcludedAttr(S, D, Attr);
4432 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004433 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004434 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004435 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004436 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004437 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004438 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004439 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004440 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004441 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004442 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004443 handleUnlockFunAttr(S, D, Attr);
4444 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004445 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004446 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004447 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004448 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004449 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004450 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004451
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004452 // Type safety attributes.
4453 case AttributeList::AT_ArgumentWithTypeTag:
4454 handleArgumentWithTypeTagAttr(S, D, Attr);
4455 break;
4456 case AttributeList::AT_TypeTagForDatatype:
4457 handleTypeTagForDatatypeAttr(S, D, Attr);
4458 break;
4459
Chris Lattner803d0802008-06-29 00:43:07 +00004460 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004461 // Ask target about the attribute.
4462 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4463 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004464 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4465 diag::warn_unhandled_ms_attribute_ignored :
4466 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004467 break;
4468 }
4469}
4470
Peter Collingbourne60700392011-01-21 02:08:45 +00004471/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4472/// the attribute applies to decls. If the attribute is a type attribute, just
4473/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4474/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004475static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4476 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004477 bool NonInheritable, bool Inheritable) {
4478 if (Attr.isInvalid())
4479 return;
4480
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004481 // Type attributes are still treated as declaration attributes by
4482 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4483 // want to process them, however, because we will simply warn about ignoring
4484 // them. So instead, we will bail out early.
4485 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004486 return;
4487
4488 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004489 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004490
4491 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004492 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004493}
4494
Chris Lattner803d0802008-06-29 00:43:07 +00004495/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4496/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004497void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004498 const AttributeList *AttrList,
4499 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004500 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004501 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004502 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004503
4504 // GCC accepts
4505 // static int a9 __attribute__((weakref));
4506 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004507 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004508 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004509 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004510 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004511 }
4512}
4513
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004514// Annotation attributes are the only attributes allowed after an access
4515// specifier.
4516bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4517 const AttributeList *AttrList) {
4518 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004519 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004520 handleAnnotateAttr(*this, ASDecl, *l);
4521 } else {
4522 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4523 return true;
4524 }
4525 }
4526
4527 return false;
4528}
4529
John McCalle82247a2011-10-01 05:17:03 +00004530/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4531/// contains any decl attributes that we should warn about.
4532static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4533 for ( ; A; A = A->getNext()) {
4534 // Only warn if the attribute is an unignored, non-type attribute.
4535 if (A->isUsedAsTypeAttr()) continue;
4536 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4537
4538 if (A->getKind() == AttributeList::UnknownAttribute) {
4539 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4540 << A->getName() << A->getRange();
4541 } else {
4542 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4543 << A->getName() << A->getRange();
4544 }
4545 }
4546}
4547
4548/// checkUnusedDeclAttributes - Given a declarator which is not being
4549/// used to build a declaration, complain about any decl attributes
4550/// which might be lying around on it.
4551void Sema::checkUnusedDeclAttributes(Declarator &D) {
4552 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4553 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4554 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4555 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4556}
4557
Ryan Flynne25ff832009-07-30 03:15:39 +00004558/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004559/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004560NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4561 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004562 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004563 NamedDecl *NewD = 0;
4564 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004565 FunctionDecl *NewFD;
4566 // FIXME: Missing call to CheckFunctionDeclaration().
4567 // FIXME: Mangling?
4568 // FIXME: Is the qualifier info correct?
4569 // FIXME: Is the DeclContext correct?
4570 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4571 Loc, Loc, DeclarationName(II),
4572 FD->getType(), FD->getTypeSourceInfo(),
4573 SC_None, SC_None,
4574 false/*isInlineSpecified*/,
4575 FD->hasPrototype(),
4576 false/*isConstexprSpecified*/);
4577 NewD = NewFD;
4578
4579 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004580 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004581
4582 // Fake up parameter variables; they are declared as if this were
4583 // a typedef.
4584 QualType FDTy = FD->getType();
4585 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4586 SmallVector<ParmVarDecl*, 16> Params;
4587 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4588 AE = FT->arg_type_end(); AI != AE; ++AI) {
4589 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4590 Param->setScopeInfo(0, Params.size());
4591 Params.push_back(Param);
4592 }
David Blaikie4278c652011-09-21 18:16:56 +00004593 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004594 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004595 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4596 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004597 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004598 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004599 VD->getStorageClass(),
4600 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004601 if (VD->getQualifier()) {
4602 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004603 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004604 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004605 }
4606 return NewD;
4607}
4608
James Dennett1dfbd922012-06-14 21:40:34 +00004609/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004610/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004611void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004612 if (W.getUsed()) return; // only do this once
4613 W.setUsed(true);
4614 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4615 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004616 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004617 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4618 NDId->getName()));
4619 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004620 WeakTopLevelDecl.push_back(NewD);
4621 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4622 // to insert Decl at TU scope, sorry.
4623 DeclContext *SavedContext = CurContext;
4624 CurContext = Context.getTranslationUnitDecl();
4625 PushOnScopeChains(NewD, S);
4626 CurContext = SavedContext;
4627 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004628 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004629 }
4630}
4631
Chris Lattner0744e5f2008-06-29 00:23:49 +00004632/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4633/// it, apply them to D. This is a bit tricky because PD can have attributes
4634/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004635void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4636 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004637 // It's valid to "forward-declare" #pragma weak, in which case we
4638 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004639 if (Inheritable) {
4640 LoadExternalWeakUndeclaredIdentifiers();
4641 if (!WeakUndeclaredIdentifiers.empty()) {
4642 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4643 if (IdentifierInfo *Id = ND->getIdentifier()) {
4644 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4645 = WeakUndeclaredIdentifiers.find(Id);
4646 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4647 WeakInfo W = I->second;
4648 DeclApplyPragmaWeak(S, ND, W);
4649 WeakUndeclaredIdentifiers[Id] = W;
4650 }
John McCalld4aff0e2010-10-27 00:59:00 +00004651 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004652 }
4653 }
4654 }
4655
Chris Lattner0744e5f2008-06-29 00:23:49 +00004656 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004657 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004658 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004659
Chris Lattner0744e5f2008-06-29 00:23:49 +00004660 // Walk the declarator structure, applying decl attributes that were in a type
4661 // position to the decl itself. This handles cases like:
4662 // int *__attr__(x)** D;
4663 // when X is a decl attribute.
4664 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4665 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004666 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004667
Chris Lattner0744e5f2008-06-29 00:23:49 +00004668 // Finally, apply any attributes on the decl itself.
4669 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004670 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004671}
John McCall54abf7d2009-11-04 02:18:39 +00004672
John McCallf85e1932011-06-15 23:02:42 +00004673/// Is the given declaration allowed to use a forbidden type?
4674static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4675 // Private ivars are always okay. Unfortunately, people don't
4676 // always properly make their ivars private, even in system headers.
4677 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004678 // Function declarations in sys headers will be marked unavailable.
4679 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4680 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004681 return false;
4682
4683 // Require it to be declared in a system header.
4684 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4685}
4686
4687/// Handle a delayed forbidden-type diagnostic.
4688static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4689 Decl *decl) {
4690 if (decl && isForbiddenTypeAllowed(S, decl)) {
4691 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4692 "this system declaration uses an unsupported type"));
4693 return;
4694 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004695 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004696 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004697 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004698 // kind of forbidden type messages on unavailable functions.
4699 if (FD->hasAttr<UnavailableAttr>() &&
4700 diag.getForbiddenTypeDiagnostic() ==
4701 diag::err_arc_array_param_no_ownership) {
4702 diag.Triggered = true;
4703 return;
4704 }
4705 }
John McCallf85e1932011-06-15 23:02:42 +00004706
4707 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4708 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4709 diag.Triggered = true;
4710}
4711
John McCall92576642012-05-07 06:16:41 +00004712void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4713 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004714 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004715 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004716
John McCall92576642012-05-07 06:16:41 +00004717 // When delaying diagnostics to run in the context of a parsed
4718 // declaration, we only want to actually emit anything if parsing
4719 // succeeds.
4720 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004721
John McCall92576642012-05-07 06:16:41 +00004722 // We emit all the active diagnostics in this pool or any of its
4723 // parents. In general, we'll get one pool for the decl spec
4724 // and a child pool for each declarator; in a decl group like:
4725 // deprecated_typedef foo, *bar, baz();
4726 // only the declarator pops will be passed decls. This is correct;
4727 // we really do need to consider delayed diagnostics from the decl spec
4728 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004729 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004730 do {
John McCall13489672012-05-07 06:16:58 +00004731 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004732 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4733 // This const_cast is a bit lame. Really, Triggered should be mutable.
4734 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004735 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004736 continue;
4737
John McCalleee1d542011-02-14 07:13:47 +00004738 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004739 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004740 // Don't bother giving deprecation diagnostics if the decl is invalid.
4741 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004742 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004743 break;
4744
4745 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004746 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004747 break;
John McCallf85e1932011-06-15 23:02:42 +00004748
4749 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004750 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004751 break;
John McCall2f514482010-01-27 03:50:35 +00004752 }
4753 }
John McCall92576642012-05-07 06:16:41 +00004754 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004755}
4756
John McCall13489672012-05-07 06:16:58 +00004757/// Given a set of delayed diagnostics, re-emit them as if they had
4758/// been delayed in the current context instead of in the given pool.
4759/// Essentially, this just moves them to the current pool.
4760void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4761 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4762 assert(curPool && "re-emitting in undelayed context not supported");
4763 curPool->steal(pool);
4764}
4765
John McCall54abf7d2009-11-04 02:18:39 +00004766static bool isDeclDeprecated(Decl *D) {
4767 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004768 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004769 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004770 // A category implicitly has the availability of the interface.
4771 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4772 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004773 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4774 return false;
4775}
4776
Eli Friedmanc3b23082012-08-08 21:52:41 +00004777static void
4778DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4779 SourceLocation Loc,
4780 const ObjCInterfaceDecl *UnknownObjCClass) {
4781 DeclarationName Name = D->getDeclName();
4782 if (!Message.empty()) {
4783 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4784 S.Diag(D->getLocation(),
4785 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4786 : diag::note_previous_decl) << Name;
4787 } else if (!UnknownObjCClass) {
4788 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4789 S.Diag(D->getLocation(),
4790 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4791 : diag::note_previous_decl) << Name;
4792 } else {
4793 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4794 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4795 }
4796}
4797
John McCall9c3087b2010-08-26 02:13:20 +00004798void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004799 Decl *Ctx) {
4800 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004801 return;
4802
John McCall2f514482010-01-27 03:50:35 +00004803 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004804 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4805 DD.getDeprecationMessage(), DD.Loc,
4806 DD.getUnknownObjCClass());
John McCall54abf7d2009-11-04 02:18:39 +00004807}
4808
Chris Lattner5f9e2722011-07-23 10:55:15 +00004809void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004810 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004811 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004812 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004813 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004814 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4815 UnknownObjCClass,
4816 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004817 return;
4818 }
4819
4820 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004821 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004822 return;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004823 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass);
John McCall54abf7d2009-11-04 02:18:39 +00004824}