blob: caa7b2f65a9dfce81f935e6b63b4667ce89ac5f8 [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)) {
418 // Ignore empty strings without warnings
419 if (StrLit->getLength() == 0)
420 continue;
421
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000422 // We allow constant strings to be used as a placeholder for expressions
423 // that are not valid C++ syntax, but warn that they are ignored.
424 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
425 Attr.getName();
426 continue;
427 }
428
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000429 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000430
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000431 // A pointer to member expression of the form &MyClass::mu is treated
432 // specially -- we need to look at the type of the member.
433 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
434 if (UOp->getOpcode() == UO_AddrOf)
435 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
436 if (DRE->getDecl()->isCXXInstanceMember())
437 ArgTy = DRE->getDecl()->getType();
438
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000439 // First see if we can just cast to record type, or point to record type.
440 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000441
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000442 // Now check if we index into a record type function param.
443 if(!RT && ParamIdxOk) {
444 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000445 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
446 if(FD && IL) {
447 unsigned int NumParams = FD->getNumParams();
448 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000449 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
450 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
451 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000452 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
453 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000454 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000455 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000456 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457 }
458 }
459
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000460 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000461
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000462 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000463 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000464}
465
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000466//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000467// Attribute Implementations
468//===----------------------------------------------------------------------===//
469
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000470// FIXME: All this manual attribute parsing code is gross. At the
471// least add some helper functions to check most argument patterns (#
472// and types of args).
473
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000474enum ThreadAttributeDeclKind {
475 ThreadExpectedFieldOrGlobalVar,
476 ThreadExpectedFunctionOrMethod,
477 ThreadExpectedClassOrStruct
478};
479
Michael Hanf1aae3b2012-08-03 17:40:43 +0000480static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000481 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000482 assert(!Attr.isInvalid());
483
484 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000485 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000486
487 // D must be either a member field or global (potentially shared) variable.
488 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000489 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
490 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000491 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000492 }
493
Michael Handc691572012-07-23 18:48:41 +0000494 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000495}
496
Michael Handc691572012-07-23 18:48:41 +0000497static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
498 if (!checkGuardedVarAttrCommon(S, D, Attr))
499 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000500
Michael Handc691572012-07-23 18:48:41 +0000501 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
502}
503
Michael Hanf1aae3b2012-08-03 17:40:43 +0000504static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000505 const AttributeList &Attr) {
506 if (!checkGuardedVarAttrCommon(S, D, Attr))
507 return;
508
509 if (!threadSafetyCheckIsPointer(S, D, Attr))
510 return;
511
512 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
513}
514
Michael Hanf1aae3b2012-08-03 17:40:43 +0000515static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
516 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000517 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000518 assert(!Attr.isInvalid());
519
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000520 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000521 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000522
523 // D must be either a member field or global (potentially shared) variable.
524 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000525 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
526 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000527 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000528 }
529
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000530 SmallVector<Expr*, 1> Args;
531 // check that all arguments are lockable objects
532 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
533 unsigned Size = Args.size();
534 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000535 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000536
Michael Handc691572012-07-23 18:48:41 +0000537 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000538
Michael Handc691572012-07-23 18:48:41 +0000539 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540}
541
Michael Handc691572012-07-23 18:48:41 +0000542static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
543 Expr *Arg = 0;
544 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
545 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000546
Michael Handc691572012-07-23 18:48:41 +0000547 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
548}
549
Michael Hanf1aae3b2012-08-03 17:40:43 +0000550static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000551 const AttributeList &Attr) {
552 Expr *Arg = 0;
553 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
554 return;
555
556 if (!threadSafetyCheckIsPointer(S, D, Attr))
557 return;
558
559 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
560 S.Context, Arg));
561}
562
Michael Hanf1aae3b2012-08-03 17:40:43 +0000563static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000564 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000565 assert(!Attr.isInvalid());
566
567 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000568 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000569
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000570 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000571 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000572 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
573 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000574 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000575 }
576
Michael Handc691572012-07-23 18:48:41 +0000577 return true;
578}
579
580static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
581 if (!checkLockableAttrCommon(S, D, Attr))
582 return;
583
584 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
585}
586
Michael Hanf1aae3b2012-08-03 17:40:43 +0000587static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000588 const AttributeList &Attr) {
589 if (!checkLockableAttrCommon(S, D, Attr))
590 return;
591
592 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000593}
594
595static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
596 const AttributeList &Attr) {
597 assert(!Attr.isInvalid());
598
599 if (!checkAttributeNumArgs(S, Attr, 0))
600 return;
601
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000602 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000603 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
604 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000605 return;
606 }
607
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000608 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000609 S.Context));
610}
611
Kostya Serebryany71efba02012-01-24 19:25:38 +0000612static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000613 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000614 assert(!Attr.isInvalid());
615
616 if (!checkAttributeNumArgs(S, Attr, 0))
617 return;
618
619 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
620 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
621 << Attr.getName() << ExpectedFunctionOrMethod;
622 return;
623 }
624
625 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000626 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000627}
628
Michael Hanf1aae3b2012-08-03 17:40:43 +0000629static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
630 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000631 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000632 assert(!Attr.isInvalid());
633
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000634 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000635 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000636
637 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000638 ValueDecl *VD = dyn_cast<ValueDecl>(D);
639 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000640 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
641 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000642 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000643 }
644
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000645 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000646 QualType QT = VD->getType();
647 if (!QT->isDependentType()) {
648 const RecordType *RT = getRecordType(QT);
649 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000650 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000651 << Attr.getName();
652 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000653 }
654 }
655
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000656 // Check that all arguments are lockable objects.
657 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000658 if (Args.size() == 0)
659 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000660
Michael Handc691572012-07-23 18:48:41 +0000661 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000662}
663
Michael Hanf1aae3b2012-08-03 17:40:43 +0000664static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000665 const AttributeList &Attr) {
666 SmallVector<Expr*, 1> Args;
667 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
668 return;
669
670 Expr **StartArg = &Args[0];
671 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000672 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000673}
674
Michael Hanf1aae3b2012-08-03 17:40:43 +0000675static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000676 const AttributeList &Attr) {
677 SmallVector<Expr*, 1> Args;
678 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
679 return;
680
681 Expr **StartArg = &Args[0];
682 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000683 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000684}
685
Michael Hanf1aae3b2012-08-03 17:40:43 +0000686static bool checkLockFunAttrCommon(Sema &S, Decl *D,
687 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000688 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000689 assert(!Attr.isInvalid());
690
691 // zero or more arguments ok
692
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000693 // check that the attribute is applied to a function
694 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000695 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
696 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000697 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000698 }
699
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000700 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000701 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000702
Michael Handc691572012-07-23 18:48:41 +0000703 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000704}
705
Michael Hanf1aae3b2012-08-03 17:40:43 +0000706static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000707 const AttributeList &Attr) {
708 SmallVector<Expr*, 1> Args;
709 if (!checkLockFunAttrCommon(S, D, Attr, Args))
710 return;
711
712 unsigned Size = Args.size();
713 Expr **StartArg = Size == 0 ? 0 : &Args[0];
714 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000715 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000716 StartArg, Size));
717}
718
Michael Hanf1aae3b2012-08-03 17:40:43 +0000719static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000720 const AttributeList &Attr) {
721 SmallVector<Expr*, 1> Args;
722 if (!checkLockFunAttrCommon(S, D, Attr, Args))
723 return;
724
725 unsigned Size = Args.size();
726 Expr **StartArg = Size == 0 ? 0 : &Args[0];
727 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000728 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000729 StartArg, Size));
730}
731
Michael Hanf1aae3b2012-08-03 17:40:43 +0000732static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
733 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000734 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000735 assert(!Attr.isInvalid());
736
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000737 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000738 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000739
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000740 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000741 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
742 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000743 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000744 }
745
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000746 if (!isIntOrBool(Attr.getArg(0))) {
747 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000748 << Attr.getName();
749 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000750 }
751
752 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000753 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000754
Michael Handc691572012-07-23 18:48:41 +0000755 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000756}
757
Michael Hanf1aae3b2012-08-03 17:40:43 +0000758static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000759 const AttributeList &Attr) {
760 SmallVector<Expr*, 2> Args;
761 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
762 return;
763
764 unsigned Size = Args.size();
765 Expr **StartArg = Size == 0 ? 0 : &Args[0];
766 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000767 S.Context,
768 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000769 StartArg, Size));
770}
771
Michael Hanf1aae3b2012-08-03 17:40:43 +0000772static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000773 const AttributeList &Attr) {
774 SmallVector<Expr*, 2> Args;
775 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
776 return;
777
778 unsigned Size = Args.size();
779 Expr **StartArg = Size == 0 ? 0 : &Args[0];
780 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000781 S.Context,
782 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000783 StartArg, Size));
784}
785
Michael Hanf1aae3b2012-08-03 17:40:43 +0000786static bool checkLocksRequiredCommon(Sema &S, Decl *D,
787 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000788 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000789 assert(!Attr.isInvalid());
790
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000791 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000792 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000793
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000794 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000795 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
796 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000797 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000798 }
799
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000800 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000801 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000802 if (Args.size() == 0)
803 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000804
Michael Handc691572012-07-23 18:48:41 +0000805 return true;
806}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000807
Michael Hanf1aae3b2012-08-03 17:40:43 +0000808static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000809 const AttributeList &Attr) {
810 SmallVector<Expr*, 1> Args;
811 if (!checkLocksRequiredCommon(S, D, Attr, Args))
812 return;
813
814 Expr **StartArg = &Args[0];
815 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000816 S.Context,
817 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000818 Args.size()));
819}
820
Michael Hanf1aae3b2012-08-03 17:40:43 +0000821static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000822 const AttributeList &Attr) {
823 SmallVector<Expr*, 1> Args;
824 if (!checkLocksRequiredCommon(S, D, Attr, Args))
825 return;
826
827 Expr **StartArg = &Args[0];
828 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000829 S.Context,
830 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000831 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000832}
833
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000834static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000835 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000836 assert(!Attr.isInvalid());
837
838 // zero or more arguments ok
839
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000841 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
842 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000843 return;
844 }
845
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000846 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000847 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000848 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000849 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000850 Expr **StartArg = Size == 0 ? 0 : &Args[0];
851
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000852 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000853 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000854}
855
856static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000857 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000858 assert(!Attr.isInvalid());
859
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000860 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000861 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000862 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000863
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000864 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000865 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
866 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000867 return;
868 }
869
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000870 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000871 return;
872
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000873 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000874 SmallVector<Expr*, 1> Args;
875 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
876 unsigned Size = Args.size();
877 if (Size == 0)
878 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000879
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000880 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
881 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000882}
883
884static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000885 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000886 assert(!Attr.isInvalid());
887
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000888 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000889 return;
890
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000891 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000892 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
893 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000894 return;
895 }
896
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000897 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000898 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000899 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000900 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000901 if (Size == 0)
902 return;
903 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000904
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000905 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000906 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000907}
908
909
Chandler Carruth1b03c872011-07-02 00:01:44 +0000910static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
911 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000912 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000913 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000914 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000915 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000916 }
Mike Stumpbf916502009-07-24 19:02:52 +0000917
Chris Lattner6b6b5372008-06-26 18:38:35 +0000918 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000919
920 Expr *sizeExpr;
921
922 // Special case where the argument is a template id.
923 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000924 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000925 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000926 UnqualifiedId id;
927 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hanf1aae3b2012-08-03 17:40:43 +0000928
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000929 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
930 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000931 if (Size.isInvalid())
932 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000933
Douglas Gregor4ac01402011-06-15 16:02:29 +0000934 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000935 } else {
936 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000937 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000938 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000939
Peter Collingbourne7a730022010-11-23 20:45:58 +0000940 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000941 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000942
943 // Instantiate/Install the vector type, and let Sema build the type for us.
944 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000945 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000946 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000947 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000948 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000949
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000950 // Remember this typedef decl, we will need it later for diagnostics.
951 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000952 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000953}
954
Chandler Carruth1b03c872011-07-02 00:01:44 +0000955static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000956 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000957 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000958 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000959
Chandler Carruth87c44602011-07-01 23:49:12 +0000960 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000961 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000962 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000963 // If the alignment is less than or equal to 8 bits, the packed attribute
964 // has no effect.
965 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000966 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000967 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000968 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000969 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000970 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000971 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000972 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973}
974
Chandler Carruth1b03c872011-07-02 00:01:44 +0000975static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000976 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000977 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000978 else
979 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
980}
981
Chandler Carruth1b03c872011-07-02 00:01:44 +0000982static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000983 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000984 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000985 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000986
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000987 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000988 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000989 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000990 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000991 return;
992 }
993
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000994 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000995}
996
Ted Kremenek2f041d02011-09-29 07:02:25 +0000997static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
998 // The IBOutlet/IBOutletCollection attributes only apply to instance
999 // variables or properties of Objective-C classes. The outlet must also
1000 // have an object reference type.
1001 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1002 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001003 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001004 << Attr.getName() << VD->getType() << 0;
1005 return false;
1006 }
1007 }
1008 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1009 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001010 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001011 << Attr.getName() << PD->getType() << 1;
1012 return false;
1013 }
1014 }
1015 else {
1016 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1017 return false;
1018 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001019
Ted Kremenek2f041d02011-09-29 07:02:25 +00001020 return true;
1021}
1022
Chandler Carruth1b03c872011-07-02 00:01:44 +00001023static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001024 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001025 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001026 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001027
1028 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001029 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001030
Ted Kremenek2f041d02011-09-29 07:02:25 +00001031 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001032}
1033
Chandler Carruth1b03c872011-07-02 00:01:44 +00001034static void handleIBOutletCollection(Sema &S, Decl *D,
1035 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001036
1037 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001038 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001039 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1040 return;
1041 }
1042
Ted Kremenek2f041d02011-09-29 07:02:25 +00001043 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001044 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001045
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001046 IdentifierInfo *II = Attr.getParameterName();
1047 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001048 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001049
John McCallb3d87482010-08-24 05:47:05 +00001050 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001051 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001052 if (!TypeRep) {
1053 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1054 return;
1055 }
John McCallb3d87482010-08-24 05:47:05 +00001056 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001057 // Diagnose use of non-object type in iboutletcollection attribute.
1058 // FIXME. Gnu attribute extension ignores use of builtin types in
1059 // attributes. So, __attribute__((iboutletcollection(char))) will be
1060 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001061 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001062 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1063 return;
1064 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001065 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1066 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001067}
1068
Chandler Carruthd309c812011-07-01 23:49:16 +00001069static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001070 if (const RecordType *UT = T->getAsUnionType())
1071 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1072 RecordDecl *UD = UT->getDecl();
1073 for (RecordDecl::field_iterator it = UD->field_begin(),
1074 itend = UD->field_end(); it != itend; ++it) {
1075 QualType QT = it->getType();
1076 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1077 T = QT;
1078 return;
1079 }
1080 }
1081 }
1082}
1083
Nuno Lopes587de5b2012-05-24 00:22:00 +00001084static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001085 if (!isFunctionOrMethod(D)) {
1086 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1087 << "alloc_size" << ExpectedFunctionOrMethod;
1088 return;
1089 }
1090
Nuno Lopes587de5b2012-05-24 00:22:00 +00001091 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1092 return;
1093
1094 // In C++ the implicit 'this' function parameter also counts, and they are
1095 // counted from one.
1096 bool HasImplicitThisParam = isInstanceMethod(D);
1097 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1098
1099 SmallVector<unsigned, 8> SizeArgs;
1100
1101 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1102 E = Attr.arg_end(); I!=E; ++I) {
1103 // The argument must be an integer constant expression.
1104 Expr *Ex = *I;
1105 llvm::APSInt ArgNum;
1106 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1107 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1108 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1109 << "alloc_size" << Ex->getSourceRange();
1110 return;
1111 }
1112
1113 uint64_t x = ArgNum.getZExtValue();
1114
1115 if (x < 1 || x > NumArgs) {
1116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1117 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1118 return;
1119 }
1120
1121 --x;
1122 if (HasImplicitThisParam) {
1123 if (x == 0) {
1124 S.Diag(Attr.getLoc(),
1125 diag::err_attribute_invalid_implicit_this_argument)
1126 << "alloc_size" << Ex->getSourceRange();
1127 return;
1128 }
1129 --x;
1130 }
1131
1132 // check if the function argument is of an integer type
1133 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1134 if (!T->isIntegerType()) {
1135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1136 << "alloc_size" << Ex->getSourceRange();
1137 return;
1138 }
1139
Nuno Lopes587de5b2012-05-24 00:22:00 +00001140 SizeArgs.push_back(x);
1141 }
1142
1143 // check if the function returns a pointer
1144 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1145 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1146 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1147 }
1148
Nuno Lopes96c67d12012-06-18 16:27:56 +00001149 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1150 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001151}
1152
Chandler Carruth1b03c872011-07-02 00:01:44 +00001153static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001154 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1155 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001156 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001157 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001158 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001159 return;
1160 }
Mike Stumpbf916502009-07-24 19:02:52 +00001161
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001162 // In C++ the implicit 'this' function parameter also counts, and they are
1163 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001164 bool HasImplicitThisParam = isInstanceMethod(D);
1165 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001166
1167 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001168 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001169
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001170 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1171 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001172
1173
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001174 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001175 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001176 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001177 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1178 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001179 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1180 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001181 return;
1182 }
Mike Stumpbf916502009-07-24 19:02:52 +00001183
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001184 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001185
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001186 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001187 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001188 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001189 return;
1190 }
Mike Stumpbf916502009-07-24 19:02:52 +00001191
Ted Kremenek465172f2008-07-21 22:09:15 +00001192 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001193 if (HasImplicitThisParam) {
1194 if (x == 0) {
1195 S.Diag(Attr.getLoc(),
1196 diag::err_attribute_invalid_implicit_this_argument)
1197 << "nonnull" << Ex->getSourceRange();
1198 return;
1199 }
1200 --x;
1201 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001202
1203 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001204 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001205 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001206
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001207 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001208 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001209 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001210 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001211 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001212 }
Mike Stumpbf916502009-07-24 19:02:52 +00001213
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001214 NonNullArgs.push_back(x);
1215 }
Mike Stumpbf916502009-07-24 19:02:52 +00001216
1217 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1218 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001219 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001220 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1221 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001222 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001223 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001224 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001225 }
Mike Stumpbf916502009-07-24 19:02:52 +00001226
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001227 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001228 if (NonNullArgs.empty()) {
1229 // Warn the trivial case only if attribute is not coming from a
1230 // macro instantiation.
1231 if (Attr.getLoc().isFileID())
1232 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001233 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001234 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001235 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001236
1237 unsigned* start = &NonNullArgs[0];
1238 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001239 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001240 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001241 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001242}
1243
Chandler Carruth1b03c872011-07-02 00:01:44 +00001244static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001245 // This attribute must be applied to a function declaration.
1246 // The first argument to the attribute must be a string,
1247 // the name of the resource, for example "malloc".
1248 // The following arguments must be argument indexes, the arguments must be
1249 // of integer type for Returns, otherwise of pointer type.
1250 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001251 // after being held. free() should be __attribute((ownership_takes)), whereas
1252 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001253
1254 if (!AL.getParameterName()) {
1255 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1256 << AL.getName()->getName() << 1;
1257 return;
1258 }
1259 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001260 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001261 switch (AL.getKind()) {
1262 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001263 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001264 if (AL.getNumArgs() < 1) {
1265 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1266 return;
1267 }
Jordy Rose2a479922010-08-12 08:54:03 +00001268 break;
1269 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001270 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001271 if (AL.getNumArgs() < 1) {
1272 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1273 return;
1274 }
Jordy Rose2a479922010-08-12 08:54:03 +00001275 break;
1276 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001277 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001278 if (AL.getNumArgs() > 1) {
1279 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1280 << AL.getNumArgs() + 1;
1281 return;
1282 }
Jordy Rose2a479922010-08-12 08:54:03 +00001283 break;
1284 default:
1285 // This should never happen given how we are called.
1286 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001287 }
1288
Chandler Carruth87c44602011-07-01 23:49:12 +00001289 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001290 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1291 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001292 return;
1293 }
1294
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001295 // In C++ the implicit 'this' function parameter also counts, and they are
1296 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001297 bool HasImplicitThisParam = isInstanceMethod(D);
1298 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001299
Chris Lattner5f9e2722011-07-23 10:55:15 +00001300 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001301
1302 // Normalize the argument, __foo__ becomes foo.
1303 if (Module.startswith("__") && Module.endswith("__"))
1304 Module = Module.substr(2, Module.size() - 4);
1305
Chris Lattner5f9e2722011-07-23 10:55:15 +00001306 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001307
Jordy Rose2a479922010-08-12 08:54:03 +00001308 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1309 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001310
Peter Collingbourne7a730022010-11-23 20:45:58 +00001311 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001312 llvm::APSInt ArgNum(32);
1313 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1314 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1315 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1316 << AL.getName()->getName() << IdxExpr->getSourceRange();
1317 continue;
1318 }
1319
1320 unsigned x = (unsigned) ArgNum.getZExtValue();
1321
1322 if (x > NumArgs || x < 1) {
1323 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1324 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1325 continue;
1326 }
1327 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001328 if (HasImplicitThisParam) {
1329 if (x == 0) {
1330 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1331 << "ownership" << IdxExpr->getSourceRange();
1332 return;
1333 }
1334 --x;
1335 }
1336
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001337 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001338 case OwnershipAttr::Takes:
1339 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001340 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001341 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1343 // FIXME: Should also highlight argument in decl.
1344 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001345 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001346 << "pointer"
1347 << IdxExpr->getSourceRange();
1348 continue;
1349 }
1350 break;
1351 }
Sean Huntcf807c42010-08-18 23:23:40 +00001352 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001353 if (AL.getNumArgs() > 1) {
1354 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001355 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001356 llvm::APSInt ArgNum(32);
1357 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1358 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1359 S.Diag(AL.getLoc(), diag::err_ownership_type)
1360 << "ownership_returns" << "integer"
1361 << IdxExpr->getSourceRange();
1362 return;
1363 }
1364 }
1365 break;
1366 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001367 } // switch
1368
1369 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001370 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001371 i = D->specific_attr_begin<OwnershipAttr>(),
1372 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001373 i != e; ++i) {
1374 if ((*i)->getOwnKind() != K) {
1375 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1376 I!=E; ++I) {
1377 if (x == *I) {
1378 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1379 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001380 }
1381 }
1382 }
1383 }
1384 OwnershipArgs.push_back(x);
1385 }
1386
1387 unsigned* start = OwnershipArgs.data();
1388 unsigned size = OwnershipArgs.size();
1389 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001390
1391 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1392 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1393 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001394 }
Sean Huntcf807c42010-08-18 23:23:40 +00001395
Chandler Carruth87c44602011-07-01 23:49:12 +00001396 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001397 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001398}
1399
John McCall332bb2a2011-02-08 22:35:49 +00001400/// Whether this declaration has internal linkage for the purposes of
1401/// things that want to complain about things not have internal linkage.
1402static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1403 switch (D->getLinkage()) {
1404 case NoLinkage:
1405 case InternalLinkage:
1406 return true;
1407
1408 // Template instantiations that go from external to unique-external
1409 // shouldn't get diagnosed.
1410 case UniqueExternalLinkage:
1411 return true;
1412
1413 case ExternalLinkage:
1414 return false;
1415 }
1416 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001417}
1418
Chandler Carruth1b03c872011-07-02 00:01:44 +00001419static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001420 // Check the attribute arguments.
1421 if (Attr.getNumArgs() > 1) {
1422 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1423 return;
1424 }
1425
Chandler Carruth87c44602011-07-01 23:49:12 +00001426 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001427 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001428 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001429 return;
1430 }
1431
Chandler Carruth87c44602011-07-01 23:49:12 +00001432 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001433
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001434 // gcc rejects
1435 // class c {
1436 // static int a __attribute__((weakref ("v2")));
1437 // static int b() __attribute__((weakref ("f3")));
1438 // };
1439 // and ignores the attributes of
1440 // void f(void) {
1441 // static int a __attribute__((weakref ("v2")));
1442 // }
1443 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001444 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001445 if (!Ctx->isFileContext()) {
1446 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001447 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001448 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001449 }
1450
1451 // The GCC manual says
1452 //
1453 // At present, a declaration to which `weakref' is attached can only
1454 // be `static'.
1455 //
1456 // It also says
1457 //
1458 // Without a TARGET,
1459 // given as an argument to `weakref' or to `alias', `weakref' is
1460 // equivalent to `weak'.
1461 //
1462 // gcc 4.4.1 will accept
1463 // int a7 __attribute__((weakref));
1464 // as
1465 // int a7 __attribute__((weak));
1466 // This looks like a bug in gcc. We reject that for now. We should revisit
1467 // it if this behaviour is actually used.
1468
John McCall332bb2a2011-02-08 22:35:49 +00001469 if (!hasEffectivelyInternalLinkage(nd)) {
1470 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001471 return;
1472 }
1473
1474 // GCC rejects
1475 // static ((alias ("y"), weakref)).
1476 // Should we? How to check that weakref is before or after alias?
1477
1478 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001479 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001480 Arg = Arg->IgnoreParenCasts();
1481 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1482
Douglas Gregor5cee1192011-07-27 05:40:30 +00001483 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001484 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1485 << "weakref" << 1;
1486 return;
1487 }
1488 // GCC will accept anything as the argument of weakref. Should we
1489 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001490 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001491 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001492 }
1493
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001494 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001495}
1496
Chandler Carruth1b03c872011-07-02 00:01:44 +00001497static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001498 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001499 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001500 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001501 return;
1502 }
Mike Stumpbf916502009-07-24 19:02:52 +00001503
Peter Collingbourne7a730022010-11-23 20:45:58 +00001504 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001505 Arg = Arg->IgnoreParenCasts();
1506 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001507
Douglas Gregor5cee1192011-07-27 05:40:30 +00001508 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001509 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001510 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001511 return;
1512 }
Mike Stumpbf916502009-07-24 19:02:52 +00001513
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001514 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001515 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1516 return;
1517 }
1518
Chris Lattner6b6b5372008-06-26 18:38:35 +00001519 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001520
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001521 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001522 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001523}
1524
Benjamin Krameree409a92012-05-12 21:10:52 +00001525static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1526 // Check the attribute arguments.
1527 if (!checkAttributeNumArgs(S, Attr, 0))
1528 return;
1529
1530 if (!isa<FunctionDecl>(D)) {
1531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1532 << Attr.getName() << ExpectedFunction;
1533 return;
1534 }
1535
1536 if (D->hasAttr<HotAttr>()) {
1537 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1538 << Attr.getName() << "hot";
1539 return;
1540 }
1541
1542 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1543}
1544
1545static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1546 // Check the attribute arguments.
1547 if (!checkAttributeNumArgs(S, Attr, 0))
1548 return;
1549
1550 if (!isa<FunctionDecl>(D)) {
1551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1552 << Attr.getName() << ExpectedFunction;
1553 return;
1554 }
1555
1556 if (D->hasAttr<ColdAttr>()) {
1557 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1558 << Attr.getName() << "cold";
1559 return;
1560 }
1561
1562 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1563}
1564
Chandler Carruth1b03c872011-07-02 00:01:44 +00001565static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001566 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001567 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001568 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001569
Chandler Carruth87c44602011-07-01 23:49:12 +00001570 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001571 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001572 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001573 return;
1574 }
1575
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001576 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001577}
1578
Chandler Carruth1b03c872011-07-02 00:01:44 +00001579static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1580 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001581 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001582 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001583 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1584 return;
1585 }
1586
Chandler Carruth87c44602011-07-01 23:49:12 +00001587 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001588 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001589 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001590 return;
1591 }
Mike Stumpbf916502009-07-24 19:02:52 +00001592
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001593 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001594}
1595
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001596static void handleTLSModelAttr(Sema &S, Decl *D,
1597 const AttributeList &Attr) {
1598 // Check the attribute arguments.
1599 if (Attr.getNumArgs() != 1) {
1600 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1601 return;
1602 }
1603
1604 Expr *Arg = Attr.getArg(0);
1605 Arg = Arg->IgnoreParenCasts();
1606 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1607
1608 // Check that it is a string.
1609 if (!Str) {
1610 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1611 return;
1612 }
1613
1614 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1616 << Attr.getName() << ExpectedTLSVar;
1617 return;
1618 }
1619
1620 // Check that the value.
1621 StringRef Model = Str->getString();
1622 if (Model != "global-dynamic" && Model != "local-dynamic"
1623 && Model != "initial-exec" && Model != "local-exec") {
1624 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1625 return;
1626 }
1627
1628 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1629 Model));
1630}
1631
Chandler Carruth1b03c872011-07-02 00:01:44 +00001632static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001633 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001634 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001635 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1636 return;
1637 }
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Chandler Carruth87c44602011-07-01 23:49:12 +00001639 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001640 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001641 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001642 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001643 return;
1644 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001645 }
1646
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001647 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001648}
1649
Chandler Carruth1b03c872011-07-02 00:01:44 +00001650static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001651 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001652 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001653 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001654
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001655 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001656}
1657
Chandler Carruth1b03c872011-07-02 00:01:44 +00001658static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001659 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001660 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001661 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001662 else
1663 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001664 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001665}
1666
Chandler Carruth1b03c872011-07-02 00:01:44 +00001667static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001668 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001669 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001670 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001671 else
1672 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001673 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001674}
1675
Chandler Carruth1b03c872011-07-02 00:01:44 +00001676static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001677 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001678
1679 if (S.CheckNoReturnAttr(attr)) return;
1680
Chandler Carruth87c44602011-07-01 23:49:12 +00001681 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001682 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001683 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001684 return;
1685 }
1686
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001687 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001688}
1689
1690bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001691 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001692 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1693 attr.setInvalid();
1694 return true;
1695 }
1696
1697 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001698}
1699
Chandler Carruth1b03c872011-07-02 00:01:44 +00001700static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1701 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001702
1703 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1704 // because 'analyzer_noreturn' does not impact the type.
1705
Chandler Carruth1731e202011-07-11 23:30:35 +00001706 if(!checkAttributeNumArgs(S, Attr, 0))
1707 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001708
Chandler Carruth87c44602011-07-01 23:49:12 +00001709 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1710 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001711 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1712 && !VD->getType()->isFunctionPointerType())) {
1713 S.Diag(Attr.getLoc(),
1714 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1715 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001716 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001717 return;
1718 }
1719 }
1720
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001721 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001722}
1723
John Thompson35cc9622010-08-09 21:53:52 +00001724// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001725static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001726/*
1727 Returning a Vector Class in Registers
1728
Eric Christopherf48f3672010-12-01 22:13:54 +00001729 According to the PPU ABI specifications, a class with a single member of
1730 vector type is returned in memory when used as the return value of a function.
1731 This results in inefficient code when implementing vector classes. To return
1732 the value in a single vector register, add the vecreturn attribute to the
1733 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001734
1735 Example:
1736
1737 struct Vector
1738 {
1739 __vector float xyzw;
1740 } __attribute__((vecreturn));
1741
1742 Vector Add(Vector lhs, Vector rhs)
1743 {
1744 Vector result;
1745 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1746 return result; // This will be returned in a register
1747 }
1748*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001749 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001751 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001752 return;
1753 }
1754
Chandler Carruth87c44602011-07-01 23:49:12 +00001755 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001756 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1757 return;
1758 }
1759
Chandler Carruth87c44602011-07-01 23:49:12 +00001760 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001761 int count = 0;
1762
1763 if (!isa<CXXRecordDecl>(record)) {
1764 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1765 return;
1766 }
1767
1768 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1769 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1770 return;
1771 }
1772
Eric Christopherf48f3672010-12-01 22:13:54 +00001773 for (RecordDecl::field_iterator iter = record->field_begin();
1774 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001775 if ((count == 1) || !iter->getType()->isVectorType()) {
1776 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1777 return;
1778 }
1779 count++;
1780 }
1781
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001782 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001783}
1784
Chandler Carruth1b03c872011-07-02 00:01:44 +00001785static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001786 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001787 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001788 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001789 return;
1790 }
1791 // FIXME: Actually store the attribute on the declaration
1792}
1793
Chandler Carruth1b03c872011-07-02 00:01:44 +00001794static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001795 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001796 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001798 return;
1799 }
Mike Stumpbf916502009-07-24 19:02:52 +00001800
Chandler Carruth87c44602011-07-01 23:49:12 +00001801 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001802 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001803 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001804 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001805 return;
1806 }
Mike Stumpbf916502009-07-24 19:02:52 +00001807
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001808 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001809}
1810
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001811static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1812 const AttributeList &Attr) {
1813 // check the attribute arguments.
1814 if (Attr.hasParameterOrArguments()) {
1815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1816 return;
1817 }
1818
1819 if (!isa<FunctionDecl>(D)) {
1820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1821 << Attr.getName() << ExpectedFunction;
1822 return;
1823 }
1824
1825 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1826}
1827
Chandler Carruth1b03c872011-07-02 00:01:44 +00001828static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001829 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001830 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001831 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1832 return;
1833 }
Mike Stumpbf916502009-07-24 19:02:52 +00001834
Chandler Carruth87c44602011-07-01 23:49:12 +00001835 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001836 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001837 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1838 return;
1839 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001840 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001841 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001842 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001843 return;
1844 }
Mike Stumpbf916502009-07-24 19:02:52 +00001845
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001846 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001847}
1848
Chandler Carruth1b03c872011-07-02 00:01:44 +00001849static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001850 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001851 if (Attr.getNumArgs() > 1) {
1852 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001853 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001854 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001855
1856 int priority = 65535; // FIXME: Do not hardcode such constants.
1857 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001858 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001859 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001860 if (E->isTypeDependent() || E->isValueDependent() ||
1861 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001862 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001863 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001864 return;
1865 }
1866 priority = Idx.getZExtValue();
1867 }
Mike Stumpbf916502009-07-24 19:02:52 +00001868
Chandler Carruth87c44602011-07-01 23:49:12 +00001869 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001870 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001871 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001872 return;
1873 }
1874
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001875 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001876 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001877}
1878
Chandler Carruth1b03c872011-07-02 00:01:44 +00001879static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001880 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001881 if (Attr.getNumArgs() > 1) {
1882 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001883 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001884 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001885
1886 int priority = 65535; // FIXME: Do not hardcode such constants.
1887 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001888 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001889 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001890 if (E->isTypeDependent() || E->isValueDependent() ||
1891 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001892 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001893 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001894 return;
1895 }
1896 priority = Idx.getZExtValue();
1897 }
Mike Stumpbf916502009-07-24 19:02:52 +00001898
Chandler Carruth87c44602011-07-01 23:49:12 +00001899 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001900 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001901 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001902 return;
1903 }
1904
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001905 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001906 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001907}
1908
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001909template <typename AttrTy>
1910static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1911 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001912 unsigned NumArgs = Attr.getNumArgs();
1913 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001914 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001915 return;
1916 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001917
1918 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001919 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001920 if (NumArgs == 1) {
1921 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001922 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001923 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001924 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001925 return;
1926 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001927 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001928 }
Mike Stumpbf916502009-07-24 19:02:52 +00001929
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001930 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001931}
1932
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001933static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1934 const AttributeList &Attr) {
1935 unsigned NumArgs = Attr.getNumArgs();
1936 if (NumArgs > 0) {
1937 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1938 return;
1939 }
1940
1941 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001942 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001943}
1944
Patrick Beardb2f68202012-04-06 18:12:22 +00001945static void handleObjCRootClassAttr(Sema &S, Decl *D,
1946 const AttributeList &Attr) {
1947 if (!isa<ObjCInterfaceDecl>(D)) {
1948 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1949 return;
1950 }
1951
1952 unsigned NumArgs = Attr.getNumArgs();
1953 if (NumArgs > 0) {
1954 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1955 return;
1956 }
1957
1958 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1959}
1960
Ted Kremenek71207fc2012-01-05 22:47:47 +00001961static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001962 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001963 if (!isa<ObjCInterfaceDecl>(D)) {
1964 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1965 return;
1966 }
1967
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001968 unsigned NumArgs = Attr.getNumArgs();
1969 if (NumArgs > 0) {
1970 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1971 return;
1972 }
1973
Ted Kremenek71207fc2012-01-05 22:47:47 +00001974 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001975 Attr.getRange(), S.Context));
1976}
1977
Jordy Rosefad5de92012-05-08 03:27:22 +00001978static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1979 IdentifierInfo *Platform,
1980 VersionTuple Introduced,
1981 VersionTuple Deprecated,
1982 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001983 StringRef PlatformName
1984 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1985 if (PlatformName.empty())
1986 PlatformName = Platform->getName();
1987
1988 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1989 // of these steps are needed).
1990 if (!Introduced.empty() && !Deprecated.empty() &&
1991 !(Introduced <= Deprecated)) {
1992 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1993 << 1 << PlatformName << Deprecated.getAsString()
1994 << 0 << Introduced.getAsString();
1995 return true;
1996 }
1997
1998 if (!Introduced.empty() && !Obsoleted.empty() &&
1999 !(Introduced <= Obsoleted)) {
2000 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2001 << 2 << PlatformName << Obsoleted.getAsString()
2002 << 0 << Introduced.getAsString();
2003 return true;
2004 }
2005
2006 if (!Deprecated.empty() && !Obsoleted.empty() &&
2007 !(Deprecated <= Obsoleted)) {
2008 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2009 << 2 << PlatformName << Obsoleted.getAsString()
2010 << 1 << Deprecated.getAsString();
2011 return true;
2012 }
2013
2014 return false;
2015}
2016
Rafael Espindola599f1b72012-05-13 03:25:18 +00002017AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2018 IdentifierInfo *Platform,
2019 VersionTuple Introduced,
2020 VersionTuple Deprecated,
2021 VersionTuple Obsoleted,
2022 bool IsUnavailable,
2023 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002024 VersionTuple MergedIntroduced = Introduced;
2025 VersionTuple MergedDeprecated = Deprecated;
2026 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002027 bool FoundAny = false;
2028
Rafael Espindola98ae8342012-05-10 02:50:16 +00002029 if (D->hasAttrs()) {
2030 AttrVec &Attrs = D->getAttrs();
2031 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2032 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2033 if (!OldAA) {
2034 ++i;
2035 continue;
2036 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002037
Rafael Espindola98ae8342012-05-10 02:50:16 +00002038 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2039 if (OldPlatform != Platform) {
2040 ++i;
2041 continue;
2042 }
2043
2044 FoundAny = true;
2045 VersionTuple OldIntroduced = OldAA->getIntroduced();
2046 VersionTuple OldDeprecated = OldAA->getDeprecated();
2047 VersionTuple OldObsoleted = OldAA->getObsoleted();
2048 bool OldIsUnavailable = OldAA->getUnavailable();
2049 StringRef OldMessage = OldAA->getMessage();
2050
2051 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2052 OldIntroduced != Introduced) ||
2053 (!OldDeprecated.empty() && !Deprecated.empty() &&
2054 OldDeprecated != Deprecated) ||
2055 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2056 OldObsoleted != Obsoleted) ||
2057 (OldIsUnavailable != IsUnavailable) ||
2058 (OldMessage != Message)) {
2059 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2060 Diag(Range.getBegin(), diag::note_previous_attribute);
2061 Attrs.erase(Attrs.begin() + i);
2062 --e;
2063 continue;
2064 }
2065
2066 VersionTuple MergedIntroduced2 = MergedIntroduced;
2067 VersionTuple MergedDeprecated2 = MergedDeprecated;
2068 VersionTuple MergedObsoleted2 = MergedObsoleted;
2069
2070 if (MergedIntroduced2.empty())
2071 MergedIntroduced2 = OldIntroduced;
2072 if (MergedDeprecated2.empty())
2073 MergedDeprecated2 = OldDeprecated;
2074 if (MergedObsoleted2.empty())
2075 MergedObsoleted2 = OldObsoleted;
2076
2077 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2078 MergedIntroduced2, MergedDeprecated2,
2079 MergedObsoleted2)) {
2080 Attrs.erase(Attrs.begin() + i);
2081 --e;
2082 continue;
2083 }
2084
2085 MergedIntroduced = MergedIntroduced2;
2086 MergedDeprecated = MergedDeprecated2;
2087 MergedObsoleted = MergedObsoleted2;
2088 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002089 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002090 }
2091
2092 if (FoundAny &&
2093 MergedIntroduced == Introduced &&
2094 MergedDeprecated == Deprecated &&
2095 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002096 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002097
Rafael Espindola98ae8342012-05-10 02:50:16 +00002098 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002099 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002100 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2101 Introduced, Deprecated,
2102 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002103 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002104 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002105}
2106
Chandler Carruth1b03c872011-07-02 00:01:44 +00002107static void handleAvailabilityAttr(Sema &S, Decl *D,
2108 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002109 IdentifierInfo *Platform = Attr.getParameterName();
2110 SourceLocation PlatformLoc = Attr.getParameterLoc();
2111
Rafael Espindola3b294362012-05-06 19:56:25 +00002112 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002113 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2114 << Platform;
2115
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002116 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2117 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2118 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002119 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002120 StringRef Str;
2121 const StringLiteral *SE =
2122 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2123 if (SE)
2124 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002125
Rafael Espindola599f1b72012-05-13 03:25:18 +00002126 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2127 Platform,
2128 Introduced.Version,
2129 Deprecated.Version,
2130 Obsoleted.Version,
2131 IsUnavailable, Str);
2132 if (NewAttr)
2133 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002134}
2135
Rafael Espindola599f1b72012-05-13 03:25:18 +00002136VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2137 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002138 if (isa<TypedefNameDecl>(D)) {
2139 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002140 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002141 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002142 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2143 if (ExistingAttr) {
2144 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2145 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002146 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002147 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2148 Diag(Range.getBegin(), diag::note_previous_attribute);
2149 D->dropAttr<VisibilityAttr>();
2150 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002151 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002152}
2153
Chandler Carruth1b03c872011-07-02 00:01:44 +00002154static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002155 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002156 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002157 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002158
Peter Collingbourne7a730022010-11-23 20:45:58 +00002159 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002160 Arg = Arg->IgnoreParenCasts();
2161 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002162
Douglas Gregor5cee1192011-07-27 05:40:30 +00002163 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002164 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002165 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002166 return;
2167 }
Mike Stumpbf916502009-07-24 19:02:52 +00002168
Chris Lattner5f9e2722011-07-23 10:55:15 +00002169 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002170 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002171
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002172 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002173 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002174 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002175 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002176 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002177 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002178 else if (TypeStr == "protected") {
2179 // Complain about attempts to use protected visibility on targets
2180 // (like Darwin) that don't support it.
2181 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2182 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2183 type = VisibilityAttr::Default;
2184 } else {
2185 type = VisibilityAttr::Protected;
2186 }
2187 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002188 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002189 return;
2190 }
Mike Stumpbf916502009-07-24 19:02:52 +00002191
Rafael Espindola599f1b72012-05-13 03:25:18 +00002192 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2193 if (NewAttr)
2194 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002195}
2196
Chandler Carruth1b03c872011-07-02 00:01:44 +00002197static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2198 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002199 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2200 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002202 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002203 return;
2204 }
2205
Chandler Carruth87c44602011-07-01 23:49:12 +00002206 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2207 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002209 << "objc_method_family" << 1;
2210 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002211 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002212 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002213 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002214 return;
2215 }
2216
Chris Lattner5f9e2722011-07-23 10:55:15 +00002217 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002218 ObjCMethodFamilyAttr::FamilyKind family;
2219 if (param == "none")
2220 family = ObjCMethodFamilyAttr::OMF_None;
2221 else if (param == "alloc")
2222 family = ObjCMethodFamilyAttr::OMF_alloc;
2223 else if (param == "copy")
2224 family = ObjCMethodFamilyAttr::OMF_copy;
2225 else if (param == "init")
2226 family = ObjCMethodFamilyAttr::OMF_init;
2227 else if (param == "mutableCopy")
2228 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2229 else if (param == "new")
2230 family = ObjCMethodFamilyAttr::OMF_new;
2231 else {
2232 // Just warn and ignore it. This is future-proof against new
2233 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002234 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002235 return;
2236 }
2237
John McCallf85e1932011-06-15 23:02:42 +00002238 if (family == ObjCMethodFamilyAttr::OMF_init &&
2239 !method->getResultType()->isObjCObjectPointerType()) {
2240 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2241 << method->getResultType();
2242 // Ignore the attribute.
2243 return;
2244 }
2245
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002246 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002247 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002248}
2249
Chandler Carruth1b03c872011-07-02 00:01:44 +00002250static void handleObjCExceptionAttr(Sema &S, Decl *D,
2251 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002252 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002253 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002254
Chris Lattner0db29ec2009-02-14 08:09:34 +00002255 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2256 if (OCI == 0) {
2257 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2258 return;
2259 }
Mike Stumpbf916502009-07-24 19:02:52 +00002260
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002261 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002262}
2263
Chandler Carruth1b03c872011-07-02 00:01:44 +00002264static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002265 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002266 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002267 return;
2268 }
Richard Smith162e1c12011-04-15 14:24:37 +00002269 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002270 QualType T = TD->getUnderlyingType();
2271 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002272 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002273 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2274 return;
2275 }
2276 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002277 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2278 QualType T = PD->getType();
2279 if (!T->isPointerType() ||
2280 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
2281 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2282 return;
2283 }
2284 }
2285 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002286 // It is okay to include this attribute on properties, e.g.:
2287 //
2288 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2289 //
2290 // In this case it follows tradition and suppresses an error in the above
2291 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002292 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002293 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002294 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002295}
2296
Mike Stumpbf916502009-07-24 19:02:52 +00002297static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002298handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002299 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002300 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002301 return;
2302 }
2303
2304 if (!isa<FunctionDecl>(D)) {
2305 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2306 return;
2307 }
2308
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002309 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002310}
2311
Chandler Carruth1b03c872011-07-02 00:01:44 +00002312static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002313 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002315 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002316 return;
2317 }
Mike Stumpbf916502009-07-24 19:02:52 +00002318
Steve Naroff9eae5762008-09-18 16:44:58 +00002319 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002320 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002321 return;
2322 }
Mike Stumpbf916502009-07-24 19:02:52 +00002323
Sean Huntcf807c42010-08-18 23:23:40 +00002324 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002325 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002326 type = BlocksAttr::ByRef;
2327 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002328 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002329 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002330 return;
2331 }
Mike Stumpbf916502009-07-24 19:02:52 +00002332
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002333 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002334}
2335
Chandler Carruth1b03c872011-07-02 00:01:44 +00002336static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002337 // check the attribute arguments.
2338 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002339 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002340 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002341 }
2342
John McCall3323fad2011-09-09 07:56:05 +00002343 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002344 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002345 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002346 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002347 if (E->isTypeDependent() || E->isValueDependent() ||
2348 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002349 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002350 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002351 return;
2352 }
Mike Stumpbf916502009-07-24 19:02:52 +00002353
John McCall3323fad2011-09-09 07:56:05 +00002354 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002355 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2356 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002357 return;
2358 }
John McCall3323fad2011-09-09 07:56:05 +00002359
2360 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002361 }
2362
John McCall3323fad2011-09-09 07:56:05 +00002363 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002364 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002365 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002366 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002367 if (E->isTypeDependent() || E->isValueDependent() ||
2368 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002369 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002370 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002371 return;
2372 }
2373 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002374
John McCall3323fad2011-09-09 07:56:05 +00002375 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002376 // FIXME: This error message could be improved, it would be nice
2377 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002378 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2379 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002380 return;
2381 }
2382 }
2383
Chandler Carruth87c44602011-07-01 23:49:12 +00002384 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002385 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002386 if (isa<FunctionNoProtoType>(FT)) {
2387 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2388 return;
2389 }
Mike Stumpbf916502009-07-24 19:02:52 +00002390
Chris Lattner897cd902009-03-17 23:03:47 +00002391 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002392 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002393 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002394 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002395 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002396 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002397 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002398 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002399 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002400 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2401 if (!BD->isVariadic()) {
2402 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2403 return;
2404 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002405 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002406 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002407 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002408 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002409 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002410 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002411 int m = Ty->isFunctionPointerType() ? 0 : 1;
2412 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002413 return;
2414 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002415 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002417 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002418 return;
2419 }
Anders Carlsson77091822008-10-05 18:05:59 +00002420 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002421 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002422 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002423 return;
2424 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002425 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002426 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002427}
2428
Chandler Carruth1b03c872011-07-02 00:01:44 +00002429static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002430 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002431 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002432 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002433
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002434 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002436 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002437 return;
2438 }
Mike Stumpbf916502009-07-24 19:02:52 +00002439
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002440 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2441 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2442 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002443 return;
2444 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002445 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2446 if (MD->getResultType()->isVoidType()) {
2447 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2448 << Attr.getName() << 1;
2449 return;
2450 }
2451
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002452 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002453}
2454
Chandler Carruth1b03c872011-07-02 00:01:44 +00002455static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002456 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002457 if (Attr.hasParameterOrArguments()) {
2458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002459 return;
2460 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002461
Chandler Carruth87c44602011-07-01 23:49:12 +00002462 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002463 if (isa<CXXRecordDecl>(D)) {
2464 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2465 return;
2466 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002467 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2468 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002469 return;
2470 }
2471
Chandler Carruth87c44602011-07-01 23:49:12 +00002472 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002473
2474 // 'weak' only applies to declarations with external linkage.
2475 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002476 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002477 return;
2478 }
Mike Stumpbf916502009-07-24 19:02:52 +00002479
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002480 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002481}
2482
Chandler Carruth1b03c872011-07-02 00:01:44 +00002483static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002484 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002485 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002486 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002487
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002488
2489 // weak_import only applies to variable & function declarations.
2490 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002491 if (!D->canBeWeakImported(isDef)) {
2492 if (isDef)
2493 S.Diag(Attr.getLoc(),
2494 diag::warn_attribute_weak_import_invalid_on_definition)
2495 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002496 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002497 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002498 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002499 // Nothing to warn about here.
2500 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002501 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002502 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002503
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002504 return;
2505 }
2506
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002507 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002508}
2509
Tanya Lattner0df579e2012-07-09 22:06:01 +00002510// Handles reqd_work_group_size and work_group_size_hint.
2511static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002512 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002513 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2514 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2515
Nate Begeman6f3d8382009-06-26 06:32:41 +00002516 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002517 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002518
2519 unsigned WGSize[3];
2520 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002521 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002522 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002523 if (E->isTypeDependent() || E->isValueDependent() ||
2524 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002526 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002527 return;
2528 }
2529 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2530 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002531
2532 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2533 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2534 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2535 if (!(A->getXDim() == WGSize[0] &&
2536 A->getYDim() == WGSize[1] &&
2537 A->getZDim() == WGSize[2])) {
2538 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2539 Attr.getName();
2540 }
2541 }
2542
2543 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2544 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2545 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2546 if (!(A->getXDim() == WGSize[0] &&
2547 A->getYDim() == WGSize[1] &&
2548 A->getZDim() == WGSize[2])) {
2549 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2550 Attr.getName();
2551 }
2552 }
2553
2554 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2555 D->addAttr(::new (S.Context)
2556 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2557 WGSize[0], WGSize[1], WGSize[2]));
2558 else
2559 D->addAttr(::new (S.Context)
2560 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2561 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002562}
2563
Rafael Espindola599f1b72012-05-13 03:25:18 +00002564SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2565 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002566 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2567 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002568 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002569 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2570 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002571 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002572 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002573 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002574}
2575
Chandler Carruth1b03c872011-07-02 00:01:44 +00002576static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002577 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002578 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002579 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002580
2581 // Make sure that there is a string literal as the sections's single
2582 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002583 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002584 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002585 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002586 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002587 return;
2588 }
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Chris Lattner797c3c42009-08-10 19:03:04 +00002590 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002591 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002592 if (!Error.empty()) {
2593 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2594 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002595 return;
2596 }
Mike Stump1eb44332009-09-09 15:08:12 +00002597
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002598 // This attribute cannot be applied to local variables.
2599 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2600 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2601 return;
2602 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002603 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2604 SE->getString());
2605 if (NewAttr)
2606 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002607}
2608
Chris Lattner6b6b5372008-06-26 18:38:35 +00002609
Chandler Carruth1b03c872011-07-02 00:01:44 +00002610static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002611 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002612 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002613 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002614 return;
2615 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002616
Chandler Carruth87c44602011-07-01 23:49:12 +00002617 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002618 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002619 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002620 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002621 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002622 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002623}
2624
Chandler Carruth1b03c872011-07-02 00:01:44 +00002625static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002626 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002627 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002628 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002629 return;
2630 }
Mike Stumpbf916502009-07-24 19:02:52 +00002631
Chandler Carruth87c44602011-07-01 23:49:12 +00002632 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002633 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002634 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002635 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002636 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002637 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002638}
2639
Chandler Carruth1b03c872011-07-02 00:01:44 +00002640static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002641 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002642 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002643 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002644
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002645 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002646}
2647
Chandler Carruth1b03c872011-07-02 00:01:44 +00002648static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002649 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2651 return;
2652 }
Mike Stumpbf916502009-07-24 19:02:52 +00002653
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002654 if (Attr.getNumArgs() != 0) {
2655 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2656 return;
2657 }
Mike Stumpbf916502009-07-24 19:02:52 +00002658
Chandler Carruth87c44602011-07-01 23:49:12 +00002659 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002660
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002661 if (!VD || !VD->hasLocalStorage()) {
2662 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2663 return;
2664 }
Mike Stumpbf916502009-07-24 19:02:52 +00002665
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002666 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002667 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002668 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002669 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2670 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002671 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002672 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002673 Attr.getParameterName();
2674 return;
2675 }
Mike Stumpbf916502009-07-24 19:02:52 +00002676
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002677 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2678 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002679 S.Diag(Attr.getParameterLoc(),
2680 diag::err_attribute_cleanup_arg_not_function)
2681 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002682 return;
2683 }
2684
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002685 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002686 S.Diag(Attr.getParameterLoc(),
2687 diag::err_attribute_cleanup_func_must_take_one_arg)
2688 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002689 return;
2690 }
Mike Stumpbf916502009-07-24 19:02:52 +00002691
Anders Carlsson89941c12009-02-07 23:16:50 +00002692 // We're currently more strict than GCC about what function types we accept.
2693 // If this ever proves to be a problem it should be easy to fix.
2694 QualType Ty = S.Context.getPointerType(VD->getType());
2695 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002696 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2697 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002698 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002699 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2700 Attr.getParameterName() << ParamTy << Ty;
2701 return;
2702 }
Mike Stumpbf916502009-07-24 19:02:52 +00002703
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002704 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002705 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002706}
2707
Mike Stumpbf916502009-07-24 19:02:52 +00002708/// Handle __attribute__((format_arg((idx)))) attribute based on
2709/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002710static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002711 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002712 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002713
Chandler Carruth87c44602011-07-01 23:49:12 +00002714 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002716 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002717 return;
2718 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002719
2720 // In C++ the implicit 'this' function parameter also counts, and they are
2721 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002722 bool HasImplicitThisParam = isInstanceMethod(D);
2723 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002724 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002725
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002726 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002727 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002728 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002729 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2730 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002731 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2732 << "format" << 2 << IdxExpr->getSourceRange();
2733 return;
2734 }
Mike Stumpbf916502009-07-24 19:02:52 +00002735
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002736 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2738 << "format" << 2 << IdxExpr->getSourceRange();
2739 return;
2740 }
Mike Stumpbf916502009-07-24 19:02:52 +00002741
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002742 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002743
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002744 if (HasImplicitThisParam) {
2745 if (ArgIdx == 0) {
2746 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2747 << "format_arg" << IdxExpr->getSourceRange();
2748 return;
2749 }
2750 ArgIdx--;
2751 }
2752
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002753 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002754 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002755
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002756 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2757 if (not_nsstring_type &&
2758 !isCFStringType(Ty, S.Context) &&
2759 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002760 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002761 // FIXME: Should highlight the actual expression that has the wrong type.
2762 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002763 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002764 << IdxExpr->getSourceRange();
2765 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002766 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002767 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002768 if (!isNSStringType(Ty, S.Context) &&
2769 !isCFStringType(Ty, S.Context) &&
2770 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002771 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002772 // FIXME: Should highlight the actual expression that has the wrong type.
2773 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002774 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002775 << IdxExpr->getSourceRange();
2776 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002777 }
2778
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002779 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002780 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002781}
2782
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002783enum FormatAttrKind {
2784 CFStringFormat,
2785 NSStringFormat,
2786 StrftimeFormat,
2787 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002788 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002789 InvalidFormat
2790};
2791
2792/// getFormatAttrKind - Map from format attribute names to supported format
2793/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002794static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002795 return llvm::StringSwitch<FormatAttrKind>(Format)
2796 // Check for formats that get handled specially.
2797 .Case("NSString", NSStringFormat)
2798 .Case("CFString", CFStringFormat)
2799 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002800
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002801 // Otherwise, check for supported formats.
2802 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2803 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2804 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002805
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002806 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2807 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002808}
2809
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002810/// Handle __attribute__((init_priority(priority))) attributes based on
2811/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002812static void handleInitPriorityAttr(Sema &S, Decl *D,
2813 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002814 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002815 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2816 return;
2817 }
2818
Chandler Carruth87c44602011-07-01 23:49:12 +00002819 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002820 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2821 Attr.setInvalid();
2822 return;
2823 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002824 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002825 if (S.Context.getAsArrayType(T))
2826 T = S.Context.getBaseElementType(T);
2827 if (!T->getAs<RecordType>()) {
2828 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2829 Attr.setInvalid();
2830 return;
2831 }
2832
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002833 if (Attr.getNumArgs() != 1) {
2834 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2835 Attr.setInvalid();
2836 return;
2837 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002838 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002839
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002840 llvm::APSInt priority(32);
2841 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2842 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2843 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2844 << "init_priority" << priorityExpr->getSourceRange();
2845 Attr.setInvalid();
2846 return;
2847 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002848 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002849 if (prioritynum < 101 || prioritynum > 65535) {
2850 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2851 << priorityExpr->getSourceRange();
2852 Attr.setInvalid();
2853 return;
2854 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002855 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002856 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002857}
2858
Rafael Espindola599f1b72012-05-13 03:25:18 +00002859FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2860 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002861 // Check whether we already have an equivalent format attribute.
2862 for (specific_attr_iterator<FormatAttr>
2863 i = D->specific_attr_begin<FormatAttr>(),
2864 e = D->specific_attr_end<FormatAttr>();
2865 i != e ; ++i) {
2866 FormatAttr *f = *i;
2867 if (f->getType() == Format &&
2868 f->getFormatIdx() == FormatIdx &&
2869 f->getFirstArg() == FirstArg) {
2870 // If we don't have a valid location for this attribute, adopt the
2871 // location.
2872 if (f->getLocation().isInvalid())
2873 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002874 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002875 }
2876 }
2877
Rafael Espindola599f1b72012-05-13 03:25:18 +00002878 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2879 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002880}
2881
Mike Stumpbf916502009-07-24 19:02:52 +00002882/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2883/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002884static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002885
Chris Lattner545dd342008-06-28 23:36:30 +00002886 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002887 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002888 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002889 return;
2890 }
2891
Chris Lattner545dd342008-06-28 23:36:30 +00002892 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002893 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002894 return;
2895 }
2896
Chandler Carruth87c44602011-07-01 23:49:12 +00002897 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002898 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002899 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002900 return;
2901 }
2902
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002903 // In C++ the implicit 'this' function parameter also counts, and they are
2904 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002905 bool HasImplicitThisParam = isInstanceMethod(D);
2906 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002907 unsigned FirstIdx = 1;
2908
Chris Lattner5f9e2722011-07-23 10:55:15 +00002909 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002910
2911 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002912 if (Format.startswith("__") && Format.endswith("__"))
2913 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002914
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002915 // Check for supported formats.
2916 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002917
2918 if (Kind == IgnoredFormat)
2919 return;
2920
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002921 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002922 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002923 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002924 return;
2925 }
2926
2927 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002928 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002929 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002930 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2931 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002932 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002933 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002934 return;
2935 }
2936
2937 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002938 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002939 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002940 return;
2941 }
2942
2943 // FIXME: Do we need to bounds check?
2944 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002945
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002946 if (HasImplicitThisParam) {
2947 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002948 S.Diag(Attr.getLoc(),
2949 diag::err_format_attribute_implicit_this_format_string)
2950 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002951 return;
2952 }
2953 ArgIdx--;
2954 }
Mike Stump1eb44332009-09-09 15:08:12 +00002955
Chris Lattner6b6b5372008-06-26 18:38:35 +00002956 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002957 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002958
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002959 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002960 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002961 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2962 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002963 return;
2964 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002965 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002966 // FIXME: do we need to check if the type is NSString*? What are the
2967 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002968 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002969 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002970 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2971 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002972 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002973 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002974 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002975 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002976 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002977 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2978 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002979 return;
2980 }
2981
2982 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002983 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002984 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002985 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2986 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002987 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002988 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002989 return;
2990 }
2991
2992 // check if the function is variadic if the 3rd argument non-zero
2993 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002994 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002995 ++NumArgs; // +1 for ...
2996 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002997 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002998 return;
2999 }
3000 }
3001
Chris Lattner3c73c412008-11-19 08:23:25 +00003002 // strftime requires FirstArg to be 0 because it doesn't read from any
3003 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003004 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003005 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003006 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3007 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003008 return;
3009 }
3010 // if 0 it disables parameter checking (to use with e.g. va_list)
3011 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003012 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003013 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003014 return;
3015 }
3016
Rafael Espindola599f1b72012-05-13 03:25:18 +00003017 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3018 Idx.getZExtValue(),
3019 FirstArg.getZExtValue());
3020 if (NewAttr)
3021 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003022}
3023
Chandler Carruth1b03c872011-07-02 00:01:44 +00003024static void handleTransparentUnionAttr(Sema &S, Decl *D,
3025 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003026 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003027 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003028 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003029
Chris Lattner6b6b5372008-06-26 18:38:35 +00003030
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003031 // Try to find the underlying union declaration.
3032 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003033 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003034 if (TD && TD->getUnderlyingType()->isUnionType())
3035 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3036 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003037 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003038
3039 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003040 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003041 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003042 return;
3043 }
3044
John McCall5e1cdac2011-10-07 06:10:15 +00003045 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003046 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003047 diag::warn_transparent_union_attribute_not_definition);
3048 return;
3049 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003050
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003051 RecordDecl::field_iterator Field = RD->field_begin(),
3052 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003053 if (Field == FieldEnd) {
3054 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3055 return;
3056 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003057
David Blaikie581deb32012-06-06 20:45:41 +00003058 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003059 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003060 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003061 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003062 diag::warn_transparent_union_attribute_floating)
3063 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003064 return;
3065 }
3066
3067 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3068 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3069 for (; Field != FieldEnd; ++Field) {
3070 QualType FieldType = Field->getType();
3071 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3072 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3073 // Warn if we drop the attribute.
3074 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003075 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003076 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003077 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003078 diag::warn_transparent_union_attribute_field_size_align)
3079 << isSize << Field->getDeclName() << FieldBits;
3080 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003081 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003082 diag::note_transparent_union_first_field_size_align)
3083 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003084 return;
3085 }
3086 }
3087
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003088 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003089}
3090
Chandler Carruth1b03c872011-07-02 00:01:44 +00003091static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003092 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003093 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003094 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003095
Peter Collingbourne7a730022010-11-23 20:45:58 +00003096 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003097 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003098
Chris Lattner6b6b5372008-06-26 18:38:35 +00003099 // Make sure that there is a string literal as the annotation's single
3100 // argument.
3101 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003102 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003103 return;
3104 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003105
3106 // Don't duplicate annotations that are already set.
3107 for (specific_attr_iterator<AnnotateAttr>
3108 i = D->specific_attr_begin<AnnotateAttr>(),
3109 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3110 if ((*i)->getAnnotation() == SE->getString())
3111 return;
3112 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003113 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003114 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003115}
3116
Chandler Carruth1b03c872011-07-02 00:01:44 +00003117static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003118 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003119 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003120 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003121 return;
3122 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003123
Sean Huntbbd37c62009-11-21 08:43:09 +00003124 //FIXME: The C++0x version of this attribute has more limited applicabilty
3125 // than GNU's, and should error out when it is used to specify a
3126 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003127
Chris Lattner545dd342008-06-28 23:36:30 +00003128 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003129 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3130 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003132 }
Mike Stumpbf916502009-07-24 19:02:52 +00003133
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003134 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3135 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003136}
3137
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003138void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3139 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003140 // FIXME: Handle pack-expansions here.
3141 if (DiagnoseUnexpandedParameterPack(E))
3142 return;
3143
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003144 if (E->isTypeDependent() || E->isValueDependent()) {
3145 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003146 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3147 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003148 return;
3149 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003150
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003151 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003152 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003153 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003154 ExprResult ICE
3155 = VerifyIntegerConstantExpression(E, &Alignment,
3156 diag::err_aligned_attribute_argument_not_int,
3157 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003158 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003159 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003160 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003161 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3162 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003163 return;
3164 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003165 if (isDeclSpec) {
3166 // We've already verified it's a power of 2, now let's make sure it's
3167 // 8192 or less.
3168 if (Alignment.getZExtValue() > 8192) {
3169 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3170 << E->getSourceRange();
3171 return;
3172 }
3173 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003174
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003175 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3176 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003177}
3178
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003179void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3180 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003181 // FIXME: Cache the number on the Attr object if non-dependent?
3182 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003183 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3184 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003185 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003186}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003187
Chandler Carruthd309c812011-07-01 23:49:16 +00003188/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003189/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003190///
Mike Stumpbf916502009-07-24 19:02:52 +00003191/// Despite what would be logical, the mode attribute is a decl attribute, not a
3192/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3193/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003194static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003195 // This attribute isn't documented, but glibc uses it. It changes
3196 // the width of an int or unsigned int to the specified size.
3197
3198 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003199 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003200 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003201
Chris Lattnerfbf13472008-06-27 22:18:37 +00003202
3203 IdentifierInfo *Name = Attr.getParameterName();
3204 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003205 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003206 return;
3207 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003208
Chris Lattner5f9e2722011-07-23 10:55:15 +00003209 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003210
3211 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003212 if (Str.startswith("__") && Str.endswith("__"))
3213 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003214
3215 unsigned DestWidth = 0;
3216 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003217 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003218 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003219 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003220 switch (Str[0]) {
3221 case 'Q': DestWidth = 8; break;
3222 case 'H': DestWidth = 16; break;
3223 case 'S': DestWidth = 32; break;
3224 case 'D': DestWidth = 64; break;
3225 case 'X': DestWidth = 96; break;
3226 case 'T': DestWidth = 128; break;
3227 }
3228 if (Str[1] == 'F') {
3229 IntegerMode = false;
3230 } else if (Str[1] == 'C') {
3231 IntegerMode = false;
3232 ComplexMode = true;
3233 } else if (Str[1] != 'I') {
3234 DestWidth = 0;
3235 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003236 break;
3237 case 4:
3238 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3239 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003240 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003241 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003242 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003243 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003244 break;
3245 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003246 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003247 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003248 break;
3249 }
3250
3251 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003252 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003253 OldTy = TD->getUnderlyingType();
3254 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3255 OldTy = VD->getType();
3256 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003257 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003258 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003259 return;
3260 }
Eli Friedman73397492009-03-03 06:41:03 +00003261
John McCall183700f2009-09-21 23:43:11 +00003262 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003263 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3264 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003265 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003266 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3267 } else if (ComplexMode) {
3268 if (!OldTy->isComplexType())
3269 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3270 } else {
3271 if (!OldTy->isFloatingType())
3272 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3273 }
3274
Mike Stump390b4cc2009-05-16 07:39:55 +00003275 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3276 // and friends, at least with glibc.
3277 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3278 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003279 // FIXME: Make sure floating-point mappings are accurate
3280 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003281 QualType NewTy;
3282 switch (DestWidth) {
3283 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003284 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003285 return;
3286 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003287 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003288 return;
3289 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003290 if (!IntegerMode) {
3291 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3292 return;
3293 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003294 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003295 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003296 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003297 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003298 break;
3299 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003300 if (!IntegerMode) {
3301 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3302 return;
3303 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003304 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003305 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003306 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003307 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003308 break;
3309 case 32:
3310 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003311 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003312 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003313 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003314 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003315 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003316 break;
3317 case 64:
3318 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003319 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003320 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003321 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003322 NewTy = S.Context.LongTy;
3323 else
3324 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003325 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003326 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003327 NewTy = S.Context.UnsignedLongTy;
3328 else
3329 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003330 break;
Eli Friedman73397492009-03-03 06:41:03 +00003331 case 96:
3332 NewTy = S.Context.LongDoubleTy;
3333 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003334 case 128:
3335 if (!IntegerMode) {
3336 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3337 return;
3338 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003339 if (OldTy->isSignedIntegerType())
3340 NewTy = S.Context.Int128Ty;
3341 else
3342 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003343 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003344 }
3345
Eli Friedman73397492009-03-03 06:41:03 +00003346 if (ComplexMode) {
3347 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003348 }
3349
3350 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003351 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003352 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003353 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003354 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003355 cast<ValueDecl>(D)->setType(NewTy);
3356}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003357
Chandler Carruth1b03c872011-07-02 00:01:44 +00003358static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003359 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003360 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003361 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003362
Nick Lewycky78d1a102012-07-24 01:40:49 +00003363 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3364 if (!VD->hasGlobalStorage())
3365 S.Diag(Attr.getLoc(),
3366 diag::warn_attribute_requires_functions_or_static_globals)
3367 << Attr.getName();
3368 } else if (!isFunctionOrMethod(D)) {
3369 S.Diag(Attr.getLoc(),
3370 diag::warn_attribute_requires_functions_or_static_globals)
3371 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003372 return;
3373 }
Mike Stumpbf916502009-07-24 19:02:52 +00003374
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003375 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003376}
3377
Chandler Carruth1b03c872011-07-02 00:01:44 +00003378static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003379 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003380 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003381 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003382
Mike Stumpbf916502009-07-24 19:02:52 +00003383
Chandler Carruth87c44602011-07-01 23:49:12 +00003384 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003385 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003386 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003387 return;
3388 }
Mike Stumpbf916502009-07-24 19:02:52 +00003389
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003390 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003391}
3392
Chandler Carruth1b03c872011-07-02 00:01:44 +00003393static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3394 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003395 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003396 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003397 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003398
Chris Lattner7255a2d2010-06-22 00:03:40 +00003399
Chandler Carruth87c44602011-07-01 23:49:12 +00003400 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003401 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003402 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003403 return;
3404 }
3405
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003406 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003407 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003408}
3409
Chandler Carruth1b03c872011-07-02 00:01:44 +00003410static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003411 if (S.LangOpts.CUDA) {
3412 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003413 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3415 return;
3416 }
3417
Chandler Carruth87c44602011-07-01 23:49:12 +00003418 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003420 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003421 return;
3422 }
3423
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003424 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003425 } else {
3426 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3427 }
3428}
3429
Chandler Carruth1b03c872011-07-02 00:01:44 +00003430static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003431 if (S.LangOpts.CUDA) {
3432 // check the attribute arguments.
3433 if (Attr.getNumArgs() != 0) {
3434 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3435 return;
3436 }
3437
Chandler Carruth87c44602011-07-01 23:49:12 +00003438 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003440 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003441 return;
3442 }
3443
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003444 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003445 } else {
3446 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3447 }
3448}
3449
Chandler Carruth1b03c872011-07-02 00:01:44 +00003450static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003451 if (S.LangOpts.CUDA) {
3452 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003453 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003454 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003455
Chandler Carruth87c44602011-07-01 23:49:12 +00003456 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003457 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003458 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003459 return;
3460 }
3461
Chandler Carruth87c44602011-07-01 23:49:12 +00003462 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003463 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003464 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003465 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3466 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3467 << FD->getType()
3468 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3469 "void");
3470 } else {
3471 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3472 << FD->getType();
3473 }
3474 return;
3475 }
3476
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003477 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003478 } else {
3479 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3480 }
3481}
3482
Chandler Carruth1b03c872011-07-02 00:01:44 +00003483static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003484 if (S.LangOpts.CUDA) {
3485 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003486 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003487 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003488
Peter Collingbourneced76712010-12-01 03:15:31 +00003489
Chandler Carruth87c44602011-07-01 23:49:12 +00003490 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003491 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003492 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003493 return;
3494 }
3495
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003496 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003497 } else {
3498 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3499 }
3500}
3501
Chandler Carruth1b03c872011-07-02 00:01:44 +00003502static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003503 if (S.LangOpts.CUDA) {
3504 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003505 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003506 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003507
Peter Collingbourneced76712010-12-01 03:15:31 +00003508
Chandler Carruth87c44602011-07-01 23:49:12 +00003509 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003510 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003511 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003512 return;
3513 }
3514
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003515 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003516 } else {
3517 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3518 }
3519}
3520
Chandler Carruth1b03c872011-07-02 00:01:44 +00003521static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003522 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003523 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003524 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003525
Chandler Carruth87c44602011-07-01 23:49:12 +00003526 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003527 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003528 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003529 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003530 return;
3531 }
Mike Stumpbf916502009-07-24 19:02:52 +00003532
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003533 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003534 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003535 return;
3536 }
Mike Stumpbf916502009-07-24 19:02:52 +00003537
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003538 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003539}
3540
Chandler Carruth1b03c872011-07-02 00:01:44 +00003541static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003542 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003543
Chandler Carruth87c44602011-07-01 23:49:12 +00003544 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003545 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3546 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003547 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003548 return;
3549
Chandler Carruth87c44602011-07-01 23:49:12 +00003550 if (!isa<ObjCMethodDecl>(D)) {
3551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3552 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003553 return;
3554 }
3555
Chandler Carruth87c44602011-07-01 23:49:12 +00003556 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003557 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003558 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003559 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003560 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003561 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003562 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003563 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003564 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003565 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003566 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003567 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003568 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003569 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003570 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003571 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003572 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003573 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003574 switch (CC) {
3575 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003576 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003577 break;
3578 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003579 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003580 break;
3581 default:
3582 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003583 }
3584
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003585 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003586 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003587 default:
3588 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003589 }
3590}
3591
Chandler Carruth1b03c872011-07-02 00:01:44 +00003592static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003593 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003594 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003595}
3596
John McCall711c52b2011-01-05 12:14:39 +00003597bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3598 if (attr.isInvalid())
3599 return true;
3600
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003601 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3602 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3603 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003604 attr.setInvalid();
3605 return true;
3606 }
3607
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003608 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3609 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003610 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003611 case AttributeList::AT_CDecl: CC = CC_C; break;
3612 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3613 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3614 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3615 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3616 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003617 Expr *Arg = attr.getArg(0);
3618 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003619 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003620 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3621 << "pcs" << 1;
3622 attr.setInvalid();
3623 return true;
3624 }
3625
Chris Lattner5f9e2722011-07-23 10:55:15 +00003626 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003627 if (StrRef == "aapcs") {
3628 CC = CC_AAPCS;
3629 break;
3630 } else if (StrRef == "aapcs-vfp") {
3631 CC = CC_AAPCS_VFP;
3632 break;
3633 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003634
3635 attr.setInvalid();
3636 Diag(attr.getLoc(), diag::err_invalid_pcs);
3637 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003638 }
David Blaikie7530c032012-01-17 06:56:22 +00003639 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003640 }
3641
3642 return false;
3643}
3644
Chandler Carruth1b03c872011-07-02 00:01:44 +00003645static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003646 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003647
3648 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003649 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003650 return;
3651
Chandler Carruth87c44602011-07-01 23:49:12 +00003652 if (!isa<ObjCMethodDecl>(D)) {
3653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3654 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003655 return;
3656 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003657
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003658 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003659}
3660
3661/// Checks a regparm attribute, returning true if it is ill-formed and
3662/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003663bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3664 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003665 return true;
3666
Chandler Carruth87c44602011-07-01 23:49:12 +00003667 if (Attr.getNumArgs() != 1) {
3668 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3669 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003670 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003671 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003672
Chandler Carruth87c44602011-07-01 23:49:12 +00003673 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003674 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003675 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003676 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003677 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003678 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003679 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003680 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003681 }
3682
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003683 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003684 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003685 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003686 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003687 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003688 }
3689
John McCall711c52b2011-01-05 12:14:39 +00003690 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003691 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003692 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003693 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003694 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003695 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003696 }
3697
John McCall711c52b2011-01-05 12:14:39 +00003698 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003699}
3700
Chandler Carruth1b03c872011-07-02 00:01:44 +00003701static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003702 if (S.LangOpts.CUDA) {
3703 // check the attribute arguments.
3704 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003705 // FIXME: 0 is not okay.
3706 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003707 return;
3708 }
3709
Chandler Carruth87c44602011-07-01 23:49:12 +00003710 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003711 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003712 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003713 return;
3714 }
3715
3716 Expr *MaxThreadsExpr = Attr.getArg(0);
3717 llvm::APSInt MaxThreads(32);
3718 if (MaxThreadsExpr->isTypeDependent() ||
3719 MaxThreadsExpr->isValueDependent() ||
3720 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3721 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3722 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3723 return;
3724 }
3725
3726 llvm::APSInt MinBlocks(32);
3727 if (Attr.getNumArgs() > 1) {
3728 Expr *MinBlocksExpr = Attr.getArg(1);
3729 if (MinBlocksExpr->isTypeDependent() ||
3730 MinBlocksExpr->isValueDependent() ||
3731 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3732 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3733 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3734 return;
3735 }
3736 }
3737
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003738 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003739 MaxThreads.getZExtValue(),
3740 MinBlocks.getZExtValue()));
3741 } else {
3742 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3743 }
3744}
3745
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003746static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3747 const AttributeList &Attr) {
3748 StringRef AttrName = Attr.getName()->getName();
3749 if (!Attr.getParameterName()) {
3750 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3751 << Attr.getName() << /* arg num = */ 1;
3752 return;
3753 }
3754
3755 if (Attr.getNumArgs() != 2) {
3756 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3757 << /* required args = */ 3;
3758 return;
3759 }
3760
3761 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3762
3763 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3764 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3765 << Attr.getName() << ExpectedFunctionOrMethod;
3766 return;
3767 }
3768
3769 uint64_t ArgumentIdx;
3770 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3771 Attr.getLoc(), 2,
3772 Attr.getArg(0), ArgumentIdx))
3773 return;
3774
3775 uint64_t TypeTagIdx;
3776 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3777 Attr.getLoc(), 3,
3778 Attr.getArg(1), TypeTagIdx))
3779 return;
3780
3781 bool IsPointer = (AttrName == "pointer_with_type_tag");
3782 if (IsPointer) {
3783 // Ensure that buffer has a pointer type.
3784 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3785 if (!BufferTy->isPointerType()) {
3786 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3787 << AttrName;
3788 }
3789 }
3790
3791 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3792 S.Context,
3793 ArgumentKind,
3794 ArgumentIdx,
3795 TypeTagIdx,
3796 IsPointer));
3797}
3798
3799static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3800 const AttributeList &Attr) {
3801 IdentifierInfo *PointerKind = Attr.getParameterName();
3802 if (!PointerKind) {
3803 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3804 << "type_tag_for_datatype" << 1;
3805 return;
3806 }
3807
3808 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3809
3810 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3811 Attr.getRange(),
3812 S.Context,
3813 PointerKind,
3814 MatchingCType,
3815 Attr.getLayoutCompatible(),
3816 Attr.getMustBeNull()));
3817}
3818
Chris Lattner0744e5f2008-06-29 00:23:49 +00003819//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003820// Checker-specific attribute handlers.
3821//===----------------------------------------------------------------------===//
3822
John McCallc7ad3812011-01-25 03:31:58 +00003823static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003824 return type->isDependentType() ||
3825 type->isObjCObjectPointerType() ||
3826 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003827}
3828static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003829 return type->isDependentType() ||
3830 type->isPointerType() ||
3831 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003832}
3833
Chandler Carruth1b03c872011-07-02 00:01:44 +00003834static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003835 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003836 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003837 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003838 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003839 return;
3840 }
3841
3842 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003843 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003844 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3845 cf = false;
3846 } else {
3847 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3848 cf = true;
3849 }
3850
3851 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003852 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003853 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003854 return;
3855 }
3856
3857 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003858 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003859 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003860 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003861}
3862
Chandler Carruth1b03c872011-07-02 00:01:44 +00003863static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3864 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003865 if (!isa<ObjCMethodDecl>(D)) {
3866 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003867 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003868 return;
3869 }
3870
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003871 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003872}
3873
Chandler Carruth1b03c872011-07-02 00:01:44 +00003874static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3875 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003876
John McCallc7ad3812011-01-25 03:31:58 +00003877 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003878
Chandler Carruth87c44602011-07-01 23:49:12 +00003879 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003880 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003881 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003882 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003883 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003884 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003885 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003886 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003887 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003888 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003889 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003890 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003891 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003892 return;
3893 }
Mike Stumpbf916502009-07-24 19:02:52 +00003894
John McCallc7ad3812011-01-25 03:31:58 +00003895 bool typeOK;
3896 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003897 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003898 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003899 case AttributeList::AT_NSReturnsAutoreleased:
3900 case AttributeList::AT_NSReturnsRetained:
3901 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003902 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3903 cf = false;
3904 break;
3905
Sean Hunt8e083e72012-06-19 23:57:03 +00003906 case AttributeList::AT_CFReturnsRetained:
3907 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003908 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3909 cf = true;
3910 break;
3911 }
3912
3913 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003914 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003915 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003916 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003917 }
Mike Stumpbf916502009-07-24 19:02:52 +00003918
Chandler Carruth87c44602011-07-01 23:49:12 +00003919 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003920 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003921 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003922 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003923 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003924 S.Context));
3925 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003926 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003927 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003928 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003929 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003930 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003931 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003932 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003933 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003934 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003935 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003936 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003937 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003938 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003939 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003940 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003941 return;
3942 };
3943}
3944
John McCalldc7c5ad2011-07-22 08:53:00 +00003945static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3946 const AttributeList &attr) {
3947 SourceLocation loc = attr.getLoc();
3948
3949 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3950
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003951 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003952 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003953 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003954 return;
3955 }
3956
3957 // Check that the method returns a normal pointer.
3958 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003959
3960 if (!resultType->isReferenceType() &&
3961 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003962 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3963 << SourceRange(loc)
3964 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3965
3966 // Drop the attribute.
3967 return;
3968 }
3969
3970 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003971 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003972}
3973
John McCall8dfac0b2011-09-30 05:12:12 +00003974/// Handle cf_audited_transfer and cf_unknown_transfer.
3975static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3976 if (!isa<FunctionDecl>(D)) {
3977 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003978 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003979 return;
3980 }
3981
Sean Hunt8e083e72012-06-19 23:57:03 +00003982 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00003983
3984 // Check whether there's a conflicting attribute already present.
3985 Attr *Existing;
3986 if (IsAudited) {
3987 Existing = D->getAttr<CFUnknownTransferAttr>();
3988 } else {
3989 Existing = D->getAttr<CFAuditedTransferAttr>();
3990 }
3991 if (Existing) {
3992 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3993 << A.getName()
3994 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3995 << A.getRange() << Existing->getRange();
3996 return;
3997 }
3998
3999 // All clear; add the attribute.
4000 if (IsAudited) {
4001 D->addAttr(
4002 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4003 } else {
4004 D->addAttr(
4005 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4006 }
4007}
4008
John McCallfe98da02011-09-29 07:17:38 +00004009static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4010 const AttributeList &Attr) {
4011 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4012 if (!RD || RD->isUnion()) {
4013 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004014 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004015 }
4016
4017 IdentifierInfo *ParmName = Attr.getParameterName();
4018
4019 // In Objective-C, verify that the type names an Objective-C type.
4020 // We don't want to check this outside of ObjC because people sometimes
4021 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004022 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004023 // Check for an existing type with this name.
4024 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4025 Sema::LookupOrdinaryName);
4026 if (S.LookupName(R, Sc)) {
4027 NamedDecl *Target = R.getFoundDecl();
4028 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4029 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4030 S.Diag(Target->getLocStart(), diag::note_declared_at);
4031 }
4032 }
4033 }
4034
4035 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4036 ParmName));
4037}
4038
Chandler Carruth1b03c872011-07-02 00:01:44 +00004039static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4040 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004041 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004042
Chandler Carruth87c44602011-07-01 23:49:12 +00004043 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004044 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004045}
4046
Chandler Carruth1b03c872011-07-02 00:01:44 +00004047static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4048 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004049 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004050 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004051 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004052 return;
4053 }
4054
Chandler Carruth87c44602011-07-01 23:49:12 +00004055 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004056 QualType type = vd->getType();
4057
4058 if (!type->isDependentType() &&
4059 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004060 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004061 << type;
4062 return;
4063 }
4064
4065 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4066
4067 // If we have no lifetime yet, check the lifetime we're presumably
4068 // going to infer.
4069 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4070 lifetime = type->getObjCARCImplicitLifetime();
4071
4072 switch (lifetime) {
4073 case Qualifiers::OCL_None:
4074 assert(type->isDependentType() &&
4075 "didn't infer lifetime for non-dependent type?");
4076 break;
4077
4078 case Qualifiers::OCL_Weak: // meaningful
4079 case Qualifiers::OCL_Strong: // meaningful
4080 break;
4081
4082 case Qualifiers::OCL_ExplicitNone:
4083 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004084 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004085 << (lifetime == Qualifiers::OCL_Autoreleasing);
4086 break;
4087 }
4088
Chandler Carruth87c44602011-07-01 23:49:12 +00004089 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004090 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004091}
4092
Francois Pichet11542142010-12-19 06:50:37 +00004093//===----------------------------------------------------------------------===//
4094// Microsoft specific attribute handlers.
4095//===----------------------------------------------------------------------===//
4096
Chandler Carruth1b03c872011-07-02 00:01:44 +00004097static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004098 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004099 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004100 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004101 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004102
Francois Pichet11542142010-12-19 06:50:37 +00004103 Expr *Arg = Attr.getArg(0);
4104 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004105 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004106 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4107 << "uuid" << 1;
4108 return;
4109 }
4110
Chris Lattner5f9e2722011-07-23 10:55:15 +00004111 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004112
4113 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4114 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004115
Francois Pichetd3d3be92010-12-20 01:41:49 +00004116 // Validate GUID length.
4117 if (IsCurly && StrRef.size() != 38) {
4118 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4119 return;
4120 }
4121 if (!IsCurly && StrRef.size() != 36) {
4122 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4123 return;
4124 }
4125
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004126 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004127 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004128 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004129 if (IsCurly) // Skip the optional '{'
4130 ++I;
4131
4132 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004133 if (i == 8 || i == 13 || i == 18 || i == 23) {
4134 if (*I != '-') {
4135 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4136 return;
4137 }
4138 } else if (!isxdigit(*I)) {
4139 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4140 return;
4141 }
4142 I++;
4143 }
Francois Pichet11542142010-12-19 06:50:37 +00004144
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004145 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004146 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004147 } else
Francois Pichet11542142010-12-19 06:50:37 +00004148 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004149}
4150
John McCallc052dbb2012-05-22 21:28:12 +00004151static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4152 if (S.LangOpts.MicrosoftExt) {
4153 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004154 if (Kind == AttributeList::AT_SingleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004155 D->addAttr(
4156 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004157 else if (Kind == AttributeList::AT_MultipleInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004158 D->addAttr(
4159 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004160 else if (Kind == AttributeList::AT_VirtualInheritance)
John McCallc052dbb2012-05-22 21:28:12 +00004161 D->addAttr(
4162 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
4163 } else
4164 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4165}
4166
4167static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4168 if (S.LangOpts.MicrosoftExt) {
4169 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004170 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004171 D->addAttr(
4172 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004173 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004174 D->addAttr(
4175 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004176 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004177 D->addAttr(
4178 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4179 } else
4180 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4181}
4182
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004183static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4184 if (S.LangOpts.MicrosoftExt)
4185 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4186 else
4187 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4188}
4189
Ted Kremenekb71368d2009-05-09 02:44:38 +00004190//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004191// Top Level Sema Entry Points
4192//===----------------------------------------------------------------------===//
4193
Chandler Carruth1b03c872011-07-02 00:01:44 +00004194static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4195 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004196 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004197 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4198 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4199 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004200 default:
4201 break;
4202 }
4203}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004204
Chandler Carruth1b03c872011-07-02 00:01:44 +00004205static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4206 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004207 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004208 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4209 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4210 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004211 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004212 case AttributeList::AT_AddressSpace:
4213 case AttributeList::AT_OpenCLImageAccess:
4214 case AttributeList::AT_ObjCGC:
4215 case AttributeList::AT_VectorSize:
4216 case AttributeList::AT_NeonVectorType:
4217 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004218 // Ignore these, these are type attributes, handled by
4219 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004220 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004221 case AttributeList::AT_CUDADevice:
4222 case AttributeList::AT_CUDAHost:
4223 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004224 // Ignore, this is a non-inheritable attribute, handled
4225 // by ProcessNonInheritableDeclAttr.
4226 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004227 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4228 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4229 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4230 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004231 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004232 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004233 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004234 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004235 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4236 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4237 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004238 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004239 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4240 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4241 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4242 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004243 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4244 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004245 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4246 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004247 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004248 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004249 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4250 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4251 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4252 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4253 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004254 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004255 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004256 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4257 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4258 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4259 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4260 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004261 case AttributeList::AT_ownership_returns:
4262 case AttributeList::AT_ownership_takes:
4263 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004264 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004265 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4266 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4267 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4268 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4269 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4270 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4271 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004272
Sean Hunt8e083e72012-06-19 23:57:03 +00004273 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004274 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004275 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004276 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004277
Sean Hunt8e083e72012-06-19 23:57:03 +00004278 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004279 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4280
Sean Hunt8e083e72012-06-19 23:57:03 +00004281 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004282 handleNSBridgedAttr(S, scope, D, Attr); break;
4283
Sean Hunt8e083e72012-06-19 23:57:03 +00004284 case AttributeList::AT_CFAuditedTransfer:
4285 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004286 handleCFTransferAttr(S, D, Attr); break;
4287
Ted Kremenekb71368d2009-05-09 02:44:38 +00004288 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004289 case AttributeList::AT_CFConsumed:
4290 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4291 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004292 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004293
Sean Hunt8e083e72012-06-19 23:57:03 +00004294 case AttributeList::AT_NSReturnsAutoreleased:
4295 case AttributeList::AT_NSReturnsNotRetained:
4296 case AttributeList::AT_CFReturnsNotRetained:
4297 case AttributeList::AT_NSReturnsRetained:
4298 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004299 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004300
Tanya Lattner0df579e2012-07-09 22:06:01 +00004301 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004302 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004303 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004304
Sean Hunt8e083e72012-06-19 23:57:03 +00004305 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004306 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004307
Sean Hunt8e083e72012-06-19 23:57:03 +00004308 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4309 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4310 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004311 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4312 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004313 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004314 handleArcWeakrefUnavailableAttr (S, D, Attr);
4315 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004316 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004317 handleObjCRootClassAttr(S, D, Attr);
4318 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004319 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004320 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004321 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004322 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4323 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004324 handleReturnsTwiceAttr(S, D, Attr);
4325 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004326 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4327 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4328 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004329 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004330 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4331 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4332 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4333 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004334 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004335 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004336 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004337 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004338 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004339 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004340 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004341 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004342 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4343 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4344 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4345 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4346 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4347 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4348 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4349 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4350 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004351 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004352 // Just ignore
4353 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004354 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004355 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004356 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004357 case AttributeList::AT_StdCall:
4358 case AttributeList::AT_CDecl:
4359 case AttributeList::AT_FastCall:
4360 case AttributeList::AT_ThisCall:
4361 case AttributeList::AT_Pascal:
4362 case AttributeList::AT_Pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004363 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004364 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004365 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004366 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004367 break;
John McCallc052dbb2012-05-22 21:28:12 +00004368
4369 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004370 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004371 handleMsStructAttr(S, D, Attr);
4372 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004373 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004374 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004375 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004376 case AttributeList::AT_SingleInheritance:
4377 case AttributeList::AT_MultipleInheritance:
4378 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004379 handleInheritanceAttr(S, D, Attr);
4380 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004381 case AttributeList::AT_Win64:
4382 case AttributeList::AT_Ptr32:
4383 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004384 handlePortabilityAttr(S, D, Attr);
4385 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004386 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004387 handleForceInlineAttr(S, D, Attr);
4388 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004389
4390 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004391 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004392 handleGuardedVarAttr(S, D, Attr);
4393 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004394 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004395 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004396 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004397 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004398 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004399 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004400 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004401 handleNoAddressSafetyAttr(S, D, Attr);
4402 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004403 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004404 handleNoThreadSafetyAttr(S, D, Attr);
4405 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004406 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004407 handleLockableAttr(S, D, Attr);
4408 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004409 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004410 handleGuardedByAttr(S, D, Attr);
4411 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004412 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004413 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004414 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004415 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004416 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004417 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004418 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004419 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004420 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004421 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004422 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004423 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004424 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004425 handleLockReturnedAttr(S, D, Attr);
4426 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004427 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004428 handleLocksExcludedAttr(S, D, Attr);
4429 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004430 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004431 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004432 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004433 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004434 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004435 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004436 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004437 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004438 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004439 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004440 handleUnlockFunAttr(S, D, Attr);
4441 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004442 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004443 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004444 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004445 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004446 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004447 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004448
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004449 // Type safety attributes.
4450 case AttributeList::AT_ArgumentWithTypeTag:
4451 handleArgumentWithTypeTagAttr(S, D, Attr);
4452 break;
4453 case AttributeList::AT_TypeTagForDatatype:
4454 handleTypeTagForDatatypeAttr(S, D, Attr);
4455 break;
4456
Chris Lattner803d0802008-06-29 00:43:07 +00004457 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004458 // Ask target about the attribute.
4459 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4460 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004461 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4462 diag::warn_unhandled_ms_attribute_ignored :
4463 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004464 break;
4465 }
4466}
4467
Peter Collingbourne60700392011-01-21 02:08:45 +00004468/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4469/// the attribute applies to decls. If the attribute is a type attribute, just
4470/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4471/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004472static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4473 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004474 bool NonInheritable, bool Inheritable) {
4475 if (Attr.isInvalid())
4476 return;
4477
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004478 // Type attributes are still treated as declaration attributes by
4479 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4480 // want to process them, however, because we will simply warn about ignoring
4481 // them. So instead, we will bail out early.
4482 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004483 return;
4484
4485 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004486 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004487
4488 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004489 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004490}
4491
Chris Lattner803d0802008-06-29 00:43:07 +00004492/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4493/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004494void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004495 const AttributeList *AttrList,
4496 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004497 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004498 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004499 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004500
4501 // GCC accepts
4502 // static int a9 __attribute__((weakref));
4503 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004504 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004505 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004506 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004507 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004508 }
4509}
4510
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004511// Annotation attributes are the only attributes allowed after an access
4512// specifier.
4513bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4514 const AttributeList *AttrList) {
4515 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004516 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004517 handleAnnotateAttr(*this, ASDecl, *l);
4518 } else {
4519 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4520 return true;
4521 }
4522 }
4523
4524 return false;
4525}
4526
John McCalle82247a2011-10-01 05:17:03 +00004527/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4528/// contains any decl attributes that we should warn about.
4529static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4530 for ( ; A; A = A->getNext()) {
4531 // Only warn if the attribute is an unignored, non-type attribute.
4532 if (A->isUsedAsTypeAttr()) continue;
4533 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4534
4535 if (A->getKind() == AttributeList::UnknownAttribute) {
4536 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4537 << A->getName() << A->getRange();
4538 } else {
4539 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4540 << A->getName() << A->getRange();
4541 }
4542 }
4543}
4544
4545/// checkUnusedDeclAttributes - Given a declarator which is not being
4546/// used to build a declaration, complain about any decl attributes
4547/// which might be lying around on it.
4548void Sema::checkUnusedDeclAttributes(Declarator &D) {
4549 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4550 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4551 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4552 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4553}
4554
Ryan Flynne25ff832009-07-30 03:15:39 +00004555/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004556/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004557NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4558 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004559 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004560 NamedDecl *NewD = 0;
4561 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004562 FunctionDecl *NewFD;
4563 // FIXME: Missing call to CheckFunctionDeclaration().
4564 // FIXME: Mangling?
4565 // FIXME: Is the qualifier info correct?
4566 // FIXME: Is the DeclContext correct?
4567 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4568 Loc, Loc, DeclarationName(II),
4569 FD->getType(), FD->getTypeSourceInfo(),
4570 SC_None, SC_None,
4571 false/*isInlineSpecified*/,
4572 FD->hasPrototype(),
4573 false/*isConstexprSpecified*/);
4574 NewD = NewFD;
4575
4576 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004577 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004578
4579 // Fake up parameter variables; they are declared as if this were
4580 // a typedef.
4581 QualType FDTy = FD->getType();
4582 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4583 SmallVector<ParmVarDecl*, 16> Params;
4584 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4585 AE = FT->arg_type_end(); AI != AE; ++AI) {
4586 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4587 Param->setScopeInfo(0, Params.size());
4588 Params.push_back(Param);
4589 }
David Blaikie4278c652011-09-21 18:16:56 +00004590 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004591 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004592 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4593 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004594 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004595 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004596 VD->getStorageClass(),
4597 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004598 if (VD->getQualifier()) {
4599 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004600 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004601 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004602 }
4603 return NewD;
4604}
4605
James Dennett1dfbd922012-06-14 21:40:34 +00004606/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004607/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004608void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004609 if (W.getUsed()) return; // only do this once
4610 W.setUsed(true);
4611 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4612 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004613 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004614 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4615 NDId->getName()));
4616 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004617 WeakTopLevelDecl.push_back(NewD);
4618 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4619 // to insert Decl at TU scope, sorry.
4620 DeclContext *SavedContext = CurContext;
4621 CurContext = Context.getTranslationUnitDecl();
4622 PushOnScopeChains(NewD, S);
4623 CurContext = SavedContext;
4624 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004625 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004626 }
4627}
4628
Chris Lattner0744e5f2008-06-29 00:23:49 +00004629/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4630/// it, apply them to D. This is a bit tricky because PD can have attributes
4631/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004632void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4633 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004634 // It's valid to "forward-declare" #pragma weak, in which case we
4635 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004636 if (Inheritable) {
4637 LoadExternalWeakUndeclaredIdentifiers();
4638 if (!WeakUndeclaredIdentifiers.empty()) {
4639 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4640 if (IdentifierInfo *Id = ND->getIdentifier()) {
4641 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4642 = WeakUndeclaredIdentifiers.find(Id);
4643 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4644 WeakInfo W = I->second;
4645 DeclApplyPragmaWeak(S, ND, W);
4646 WeakUndeclaredIdentifiers[Id] = W;
4647 }
John McCalld4aff0e2010-10-27 00:59:00 +00004648 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004649 }
4650 }
4651 }
4652
Chris Lattner0744e5f2008-06-29 00:23:49 +00004653 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004654 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004655 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004656
Chris Lattner0744e5f2008-06-29 00:23:49 +00004657 // Walk the declarator structure, applying decl attributes that were in a type
4658 // position to the decl itself. This handles cases like:
4659 // int *__attr__(x)** D;
4660 // when X is a decl attribute.
4661 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4662 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004663 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004664
Chris Lattner0744e5f2008-06-29 00:23:49 +00004665 // Finally, apply any attributes on the decl itself.
4666 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004667 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004668}
John McCall54abf7d2009-11-04 02:18:39 +00004669
John McCallf85e1932011-06-15 23:02:42 +00004670/// Is the given declaration allowed to use a forbidden type?
4671static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4672 // Private ivars are always okay. Unfortunately, people don't
4673 // always properly make their ivars private, even in system headers.
4674 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004675 // Function declarations in sys headers will be marked unavailable.
4676 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4677 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004678 return false;
4679
4680 // Require it to be declared in a system header.
4681 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4682}
4683
4684/// Handle a delayed forbidden-type diagnostic.
4685static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4686 Decl *decl) {
4687 if (decl && isForbiddenTypeAllowed(S, decl)) {
4688 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4689 "this system declaration uses an unsupported type"));
4690 return;
4691 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004692 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004693 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004694 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004695 // kind of forbidden type messages on unavailable functions.
4696 if (FD->hasAttr<UnavailableAttr>() &&
4697 diag.getForbiddenTypeDiagnostic() ==
4698 diag::err_arc_array_param_no_ownership) {
4699 diag.Triggered = true;
4700 return;
4701 }
4702 }
John McCallf85e1932011-06-15 23:02:42 +00004703
4704 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4705 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4706 diag.Triggered = true;
4707}
4708
John McCall92576642012-05-07 06:16:41 +00004709void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4710 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004711 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004712 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004713
John McCall92576642012-05-07 06:16:41 +00004714 // When delaying diagnostics to run in the context of a parsed
4715 // declaration, we only want to actually emit anything if parsing
4716 // succeeds.
4717 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004718
John McCall92576642012-05-07 06:16:41 +00004719 // We emit all the active diagnostics in this pool or any of its
4720 // parents. In general, we'll get one pool for the decl spec
4721 // and a child pool for each declarator; in a decl group like:
4722 // deprecated_typedef foo, *bar, baz();
4723 // only the declarator pops will be passed decls. This is correct;
4724 // we really do need to consider delayed diagnostics from the decl spec
4725 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004726 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004727 do {
John McCall13489672012-05-07 06:16:58 +00004728 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004729 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4730 // This const_cast is a bit lame. Really, Triggered should be mutable.
4731 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004732 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004733 continue;
4734
John McCalleee1d542011-02-14 07:13:47 +00004735 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004736 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004737 // Don't bother giving deprecation diagnostics if the decl is invalid.
4738 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004739 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004740 break;
4741
4742 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004743 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004744 break;
John McCallf85e1932011-06-15 23:02:42 +00004745
4746 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004747 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004748 break;
John McCall2f514482010-01-27 03:50:35 +00004749 }
4750 }
John McCall92576642012-05-07 06:16:41 +00004751 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004752}
4753
John McCall13489672012-05-07 06:16:58 +00004754/// Given a set of delayed diagnostics, re-emit them as if they had
4755/// been delayed in the current context instead of in the given pool.
4756/// Essentially, this just moves them to the current pool.
4757void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4758 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4759 assert(curPool && "re-emitting in undelayed context not supported");
4760 curPool->steal(pool);
4761}
4762
John McCall54abf7d2009-11-04 02:18:39 +00004763static bool isDeclDeprecated(Decl *D) {
4764 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004765 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004766 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004767 // A category implicitly has the availability of the interface.
4768 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4769 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004770 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4771 return false;
4772}
4773
Eli Friedmanc3b23082012-08-08 21:52:41 +00004774static void
4775DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4776 SourceLocation Loc,
4777 const ObjCInterfaceDecl *UnknownObjCClass) {
4778 DeclarationName Name = D->getDeclName();
4779 if (!Message.empty()) {
4780 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4781 S.Diag(D->getLocation(),
4782 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4783 : diag::note_previous_decl) << Name;
4784 } else if (!UnknownObjCClass) {
4785 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4786 S.Diag(D->getLocation(),
4787 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4788 : diag::note_previous_decl) << Name;
4789 } else {
4790 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4791 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4792 }
4793}
4794
John McCall9c3087b2010-08-26 02:13:20 +00004795void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004796 Decl *Ctx) {
4797 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004798 return;
4799
John McCall2f514482010-01-27 03:50:35 +00004800 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004801 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4802 DD.getDeprecationMessage(), DD.Loc,
4803 DD.getUnknownObjCClass());
John McCall54abf7d2009-11-04 02:18:39 +00004804}
4805
Chris Lattner5f9e2722011-07-23 10:55:15 +00004806void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004807 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004808 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004809 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004810 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004811 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4812 UnknownObjCClass,
4813 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004814 return;
4815 }
4816
4817 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004818 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004819 return;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004820 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass);
John McCall54abf7d2009-11-04 02:18:39 +00004821}