blob: ae394d78b9616917fdf4b5d1b6ab8e12d5a2530a [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000028using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000030
John McCall883cc2c2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
John McCall883cc2c2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedVariable,
43 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000044 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000045 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000046 ExpectedStruct,
47 ExpectedTLSVar
John McCall883cc2c2011-03-02 12:29:23 +000048};
49
Chris Lattnere5c5ee12008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Chandler Carruth87c44602011-07-01 23:49:12 +000054static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000056 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000057 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000059 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000061 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000062 Ty = decl->getUnderlyingType();
63 else
64 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000065
Chris Lattner6b6b5372008-06-26 18:38:35 +000066 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000067 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000068 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000069 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070
John McCall183700f2009-09-21 23:43:11 +000071 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000072}
73
Daniel Dunbar35682492008-09-26 04:12:28 +000074// FIXME: We should provide an abstraction around a method or function
75// to provide the following bits of information.
76
Nuno Lopesd20254f2009-12-20 23:11:08 +000077/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000078/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000079static bool isFunction(const Decl *D) {
80 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081}
82
83/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000084/// type (function or function-typed variable) or an Objective-C
85/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000086static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000087 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000088}
89
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000090/// isFunctionOrMethodOrBlock - Return true if the given decl has function
91/// type (function or function-typed variable) or an Objective-C
92/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +000093static bool isFunctionOrMethodOrBlock(const Decl *D) {
94 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000095 return true;
96 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +000097 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098 QualType Ty = V->getType();
99 return Ty->isBlockPointerType();
100 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000101 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000102}
103
John McCall711c52b2011-01-05 12:14:39 +0000104/// Return true if the given decl has a declarator that should have
105/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000107 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000108 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
109 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000110}
111
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000112/// hasFunctionProto - Return true if the given decl has a argument
113/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000114/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasFunctionProto(const Decl *D) {
116 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000118 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000119 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 return true;
121 }
122}
123
124/// getFunctionOrMethodNumArgs - Return number of function or method
125/// arguments. It is an error to call this on a K&R function (use
126/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000127static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
128 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000131 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000133}
134
Chandler Carruth87c44602011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000139 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000140
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodResultType(const Decl *D) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000146 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000148}
149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150static bool isFunctionOrMethodVariadic(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000152 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000153 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000154 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000155 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000156 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000158 }
159}
160
Chandler Carruth87c44602011-07-01 23:49:12 +0000161static bool isInstanceMethod(const Decl *D) {
162 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000163 return MethodDecl->isInstance();
164 return false;
165}
166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000168 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000169 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000171
John McCall506b57e2010-05-17 21:00:27 +0000172 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
173 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000175
John McCall506b57e2010-05-17 21:00:27 +0000176 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000177
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 // FIXME: Should we walk the chain of classes?
179 return ClsName == &Ctx.Idents.get("NSString") ||
180 ClsName == &Ctx.Idents.get("NSMutableString");
181}
182
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000183static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000184 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185 if (!PT)
186 return false;
187
Ted Kremenek6217b802009-07-29 21:53:49 +0000188 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000189 if (!RT)
190 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000193 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 return false;
195
196 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197}
198
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000199/// \brief Check if the attribute has exactly as many args as Num. May
200/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000201static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned int Num) {
203 if (Attr.getNumArgs() != Num) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
205 return false;
206 }
207
208 return true;
209}
210
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000211
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000212/// \brief Check if the attribute has at least as many args as Num. May
213/// output an error.
214static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
215 unsigned int Num) {
216 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000217 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
218 return false;
219 }
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221 return true;
222}
223
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000224/// \brief Check if IdxExpr is a valid argument index for a function or
225/// instance method D. May output an error.
226///
227/// \returns true if IdxExpr is a valid index.
228static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
229 StringRef AttrName,
230 SourceLocation AttrLoc,
231 unsigned AttrArgNum,
232 const Expr *IdxExpr,
233 uint64_t &Idx)
234{
235 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
236
237 // In C++ the implicit 'this' function parameter also counts.
238 // Parameters are counted from one.
239 const bool HasImplicitThisParam = isInstanceMethod(D);
240 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
241 const unsigned FirstIdx = 1;
242
243 llvm::APSInt IdxInt;
244 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
245 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
246 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
247 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
248 return false;
249 }
250
251 Idx = IdxInt.getLimitedValue();
252 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
253 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
254 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
255 return false;
256 }
257 Idx--; // Convert to zero-based.
258 if (HasImplicitThisParam) {
259 if (Idx == 0) {
260 S.Diag(AttrLoc,
261 diag::err_attribute_invalid_implicit_this_argument)
262 << AttrName << IdxExpr->getSourceRange();
263 return false;
264 }
265 --Idx;
266 }
267
268 return true;
269}
270
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000271///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000272/// \brief Check if passed in Decl is a field or potentially shared global var
273/// \return true if the Decl is a field or potentially shared global variable
274///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000275static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000276 if (isa<FieldDecl>(D))
277 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000278 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000279 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
280
281 return false;
282}
283
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000284/// \brief Check if the passed-in expression is of type int or bool.
285static bool isIntOrBool(Expr *Exp) {
286 QualType QT = Exp->getType();
287 return QT->isBooleanType() || QT->isIntegerType();
288}
289
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000290
291// Check to see if the type is a smart pointer of some kind. We assume
292// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000293static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
294 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
295 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
296 if (Res1.first == Res1.second)
297 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000298
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000299 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
300 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
301 if (Res2.first == Res2.second)
302 return false;
303
304 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000305}
306
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000307/// \brief Check if passed in Decl is a pointer type.
308/// Note that this function may produce an error message.
309/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000310static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
311 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000312 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000313 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000314 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000315 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000316
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000317 if (const RecordType *RT = QT->getAs<RecordType>()) {
318 // If it's an incomplete type, it could be a smart pointer; skip it.
319 // (We don't want to force template instantiation if we can avoid it,
320 // since that would alter the order in which templates are instantiated.)
321 if (RT->isIncompleteType())
322 return true;
323
324 if (threadSafetyCheckIsSmartPointer(S, RT))
325 return true;
326 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000327
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000328 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000329 << Attr.getName()->getName() << QT;
330 } else {
331 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
332 << Attr.getName();
333 }
334 return false;
335}
336
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000337/// \brief Checks that the passed in QualType either is of RecordType or points
338/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000339static const RecordType *getRecordType(QualType QT) {
340 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000341 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000342
343 // Now check if we point to record type.
344 if (const PointerType *PT = QT->getAs<PointerType>())
345 return PT->getPointeeType()->getAs<RecordType>();
346
347 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000348}
349
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000350
Jordy Rosefad5de92012-05-08 03:27:22 +0000351static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
352 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000353 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
354 if (RT->getDecl()->getAttr<LockableAttr>())
355 return true;
356 return false;
357}
358
359
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000360/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000361/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000362static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
363 QualType Ty) {
364 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000365
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000366 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000367 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000368 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000369 << Attr.getName() << Ty.getAsString();
370 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000371 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000372
Michael Hanf1aae3b2012-08-03 17:40:43 +0000373 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000374 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000375 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000376
377 // Allow smart pointers to be used as lockable objects.
378 // FIXME -- Check the type that the smart pointer points to.
379 if (threadSafetyCheckIsSmartPointer(S, RT))
380 return;
381
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000382 // Check if the type is lockable.
383 RecordDecl *RD = RT->getDecl();
384 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000385 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000386
387 // Else check if any base classes are lockable.
388 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
389 CXXBasePaths BPaths(false, false);
390 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
391 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000392 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000393
394 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
395 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000396}
397
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000398/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000399/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000400/// \param Sidx The attribute argument index to start checking with.
401/// \param ParamIdxOk Whether an argument can be indexing into a function
402/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000403static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000404 const AttributeList &Attr,
405 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000406 int Sidx = 0,
407 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000408 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000409 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000410
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000411 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000413 Args.push_back(ArgExp);
414 continue;
415 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000416
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000417 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000418 if (StrLit->getLength() == 0 ||
419 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000420 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000421 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000422 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000423 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000424 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000425
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000426 // We allow constant strings to be used as a placeholder for expressions
427 // that are not valid C++ syntax, but warn that they are ignored.
428 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
429 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000430 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000431 continue;
432 }
433
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000434 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000435
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000436 // A pointer to member expression of the form &MyClass::mu is treated
437 // specially -- we need to look at the type of the member.
438 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
439 if (UOp->getOpcode() == UO_AddrOf)
440 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
441 if (DRE->getDecl()->isCXXInstanceMember())
442 ArgTy = DRE->getDecl()->getType();
443
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000444 // First see if we can just cast to record type, or point to record type.
445 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000446
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447 // Now check if we index into a record type function param.
448 if(!RT && ParamIdxOk) {
449 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000450 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
451 if(FD && IL) {
452 unsigned int NumParams = FD->getNumParams();
453 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000454 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
455 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
456 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000457 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
458 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000459 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000460 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000461 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000462 }
463 }
464
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000465 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000466
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000467 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000468 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000469}
470
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000471//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000472// Attribute Implementations
473//===----------------------------------------------------------------------===//
474
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000475// FIXME: All this manual attribute parsing code is gross. At the
476// least add some helper functions to check most argument patterns (#
477// and types of args).
478
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000479enum ThreadAttributeDeclKind {
480 ThreadExpectedFieldOrGlobalVar,
481 ThreadExpectedFunctionOrMethod,
482 ThreadExpectedClassOrStruct
483};
484
Michael Hanf1aae3b2012-08-03 17:40:43 +0000485static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000486 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000487 assert(!Attr.isInvalid());
488
489 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000490 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000491
492 // D must be either a member field or global (potentially shared) variable.
493 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000494 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
495 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000496 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000497 }
498
Michael Handc691572012-07-23 18:48:41 +0000499 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500}
501
Michael Handc691572012-07-23 18:48:41 +0000502static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
503 if (!checkGuardedVarAttrCommon(S, D, Attr))
504 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000505
Michael Handc691572012-07-23 18:48:41 +0000506 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
507}
508
Michael Hanf1aae3b2012-08-03 17:40:43 +0000509static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000510 const AttributeList &Attr) {
511 if (!checkGuardedVarAttrCommon(S, D, Attr))
512 return;
513
514 if (!threadSafetyCheckIsPointer(S, D, Attr))
515 return;
516
517 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
518}
519
Michael Hanf1aae3b2012-08-03 17:40:43 +0000520static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
521 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000522 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000523 assert(!Attr.isInvalid());
524
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000525 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000526 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000527
528 // D must be either a member field or global (potentially shared) variable.
529 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000530 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
531 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000532 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000533 }
534
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000535 SmallVector<Expr*, 1> Args;
536 // check that all arguments are lockable objects
537 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
538 unsigned Size = Args.size();
539 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000540 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000541
Michael Handc691572012-07-23 18:48:41 +0000542 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000543
Michael Handc691572012-07-23 18:48:41 +0000544 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000545}
546
Michael Handc691572012-07-23 18:48:41 +0000547static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
548 Expr *Arg = 0;
549 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
550 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000551
Michael Handc691572012-07-23 18:48:41 +0000552 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
553}
554
Michael Hanf1aae3b2012-08-03 17:40:43 +0000555static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000556 const AttributeList &Attr) {
557 Expr *Arg = 0;
558 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
559 return;
560
561 if (!threadSafetyCheckIsPointer(S, D, Attr))
562 return;
563
564 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
565 S.Context, Arg));
566}
567
Michael Hanf1aae3b2012-08-03 17:40:43 +0000568static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000570 assert(!Attr.isInvalid());
571
572 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000573 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000574
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000575 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000576 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000577 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
578 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000579 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000580 }
581
Michael Handc691572012-07-23 18:48:41 +0000582 return true;
583}
584
585static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
586 if (!checkLockableAttrCommon(S, D, Attr))
587 return;
588
589 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
590}
591
Michael Hanf1aae3b2012-08-03 17:40:43 +0000592static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000593 const AttributeList &Attr) {
594 if (!checkLockableAttrCommon(S, D, Attr))
595 return;
596
597 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000598}
599
600static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
601 const AttributeList &Attr) {
602 assert(!Attr.isInvalid());
603
604 if (!checkAttributeNumArgs(S, Attr, 0))
605 return;
606
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000607 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000608 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
609 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000610 return;
611 }
612
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000613 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000614 S.Context));
615}
616
Kostya Serebryany71efba02012-01-24 19:25:38 +0000617static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000618 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000619 assert(!Attr.isInvalid());
620
621 if (!checkAttributeNumArgs(S, Attr, 0))
622 return;
623
624 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
626 << Attr.getName() << ExpectedFunctionOrMethod;
627 return;
628 }
629
630 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckyf50b6fe2012-07-24 01:37:23 +0000631 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000632}
633
Michael Hanf1aae3b2012-08-03 17:40:43 +0000634static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
635 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000636 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000637 assert(!Attr.isInvalid());
638
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000639 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000640 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000641
642 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000643 ValueDecl *VD = dyn_cast<ValueDecl>(D);
644 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000645 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
646 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000647 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000648 }
649
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000650 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000651 QualType QT = VD->getType();
652 if (!QT->isDependentType()) {
653 const RecordType *RT = getRecordType(QT);
654 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000655 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000656 << Attr.getName();
657 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000658 }
659 }
660
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000661 // Check that all arguments are lockable objects.
662 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000663 if (Args.size() == 0)
664 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000665
Michael Handc691572012-07-23 18:48:41 +0000666 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000667}
668
Michael Hanf1aae3b2012-08-03 17:40:43 +0000669static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000670 const AttributeList &Attr) {
671 SmallVector<Expr*, 1> Args;
672 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
673 return;
674
675 Expr **StartArg = &Args[0];
676 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000677 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000678}
679
Michael Hanf1aae3b2012-08-03 17:40:43 +0000680static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000681 const AttributeList &Attr) {
682 SmallVector<Expr*, 1> Args;
683 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
684 return;
685
686 Expr **StartArg = &Args[0];
687 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +0000688 StartArg, Args.size()));
Michael Handc691572012-07-23 18:48:41 +0000689}
690
Michael Hanf1aae3b2012-08-03 17:40:43 +0000691static bool checkLockFunAttrCommon(Sema &S, Decl *D,
692 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000693 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000694 assert(!Attr.isInvalid());
695
696 // zero or more arguments ok
697
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000698 // check that the attribute is applied to a function
699 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000700 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
701 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000702 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000703 }
704
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000705 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000706 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000707
Michael Handc691572012-07-23 18:48:41 +0000708 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000709}
710
Michael Hanf1aae3b2012-08-03 17:40:43 +0000711static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000712 const AttributeList &Attr) {
713 SmallVector<Expr*, 1> Args;
714 if (!checkLockFunAttrCommon(S, D, Attr, Args))
715 return;
716
717 unsigned Size = Args.size();
718 Expr **StartArg = Size == 0 ? 0 : &Args[0];
719 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000720 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000721 StartArg, Size));
722}
723
Michael Hanf1aae3b2012-08-03 17:40:43 +0000724static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 1> Args;
727 if (!checkLockFunAttrCommon(S, D, Attr, Args))
728 return;
729
730 unsigned Size = Args.size();
731 Expr **StartArg = Size == 0 ? 0 : &Args[0];
732 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000733 S.Context,
Michael Handc691572012-07-23 18:48:41 +0000734 StartArg, Size));
735}
736
Michael Hanf1aae3b2012-08-03 17:40:43 +0000737static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
738 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000739 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000740 assert(!Attr.isInvalid());
741
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000742 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000743 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000744
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000745 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000746 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
747 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000748 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000749 }
750
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000751 if (!isIntOrBool(Attr.getArg(0))) {
752 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Handc691572012-07-23 18:48:41 +0000753 << Attr.getName();
754 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000755 }
756
757 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000758 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000759
Michael Handc691572012-07-23 18:48:41 +0000760 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000761}
762
Michael Hanf1aae3b2012-08-03 17:40:43 +0000763static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000764 const AttributeList &Attr) {
765 SmallVector<Expr*, 2> Args;
766 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
767 return;
768
769 unsigned Size = Args.size();
770 Expr **StartArg = Size == 0 ? 0 : &Args[0];
771 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000772 S.Context,
773 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000774 StartArg, Size));
775}
776
Michael Hanf1aae3b2012-08-03 17:40:43 +0000777static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000778 const AttributeList &Attr) {
779 SmallVector<Expr*, 2> Args;
780 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
781 return;
782
783 unsigned Size = Args.size();
784 Expr **StartArg = Size == 0 ? 0 : &Args[0];
785 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000786 S.Context,
787 Attr.getArg(0),
Michael Handc691572012-07-23 18:48:41 +0000788 StartArg, Size));
789}
790
Michael Hanf1aae3b2012-08-03 17:40:43 +0000791static bool checkLocksRequiredCommon(Sema &S, Decl *D,
792 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000793 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000794 assert(!Attr.isInvalid());
795
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000796 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000797 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000798
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000799 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000800 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
801 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000802 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000803 }
804
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000805 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000806 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Handc691572012-07-23 18:48:41 +0000807 if (Args.size() == 0)
808 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000809
Michael Handc691572012-07-23 18:48:41 +0000810 return true;
811}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000812
Michael Hanf1aae3b2012-08-03 17:40:43 +0000813static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000814 const AttributeList &Attr) {
815 SmallVector<Expr*, 1> Args;
816 if (!checkLocksRequiredCommon(S, D, Attr, Args))
817 return;
818
819 Expr **StartArg = &Args[0];
820 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000821 S.Context,
822 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000823 Args.size()));
824}
825
Michael Hanf1aae3b2012-08-03 17:40:43 +0000826static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000827 const AttributeList &Attr) {
828 SmallVector<Expr*, 1> Args;
829 if (!checkLocksRequiredCommon(S, D, Attr, Args))
830 return;
831
832 Expr **StartArg = &Args[0];
833 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hanf1aae3b2012-08-03 17:40:43 +0000834 S.Context,
835 StartArg,
Michael Handc691572012-07-23 18:48:41 +0000836 Args.size()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000837}
838
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000839static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000840 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000841 assert(!Attr.isInvalid());
842
843 // zero or more arguments ok
844
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000845 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000846 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
847 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000848 return;
849 }
850
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000851 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000852 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000853 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000854 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000855 Expr **StartArg = Size == 0 ? 0 : &Args[0];
856
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000857 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000858 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000859}
860
861static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000862 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000863 assert(!Attr.isInvalid());
864
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000865 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000866 return;
867
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000868 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000869 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
870 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000871 return;
872 }
873
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000874 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000875 SmallVector<Expr*, 1> Args;
876 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
877 unsigned Size = Args.size();
878 if (Size == 0)
879 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000880
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000881 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
882 Args[0]));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000883}
884
885static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000887 assert(!Attr.isInvalid());
888
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000889 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000890 return;
891
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000893 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
894 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000895 return;
896 }
897
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000898 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000899 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000900 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000901 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000902 if (Size == 0)
903 return;
904 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000905
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000906 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000907 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000908}
909
910
Chandler Carruth1b03c872011-07-02 00:01:44 +0000911static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
912 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000913 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000914 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000915 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000916 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000917 }
Mike Stumpbf916502009-07-24 19:02:52 +0000918
Chris Lattner6b6b5372008-06-26 18:38:35 +0000919 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000920
921 Expr *sizeExpr;
922
923 // Special case where the argument is a template id.
924 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000925 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000926 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000927 UnqualifiedId id;
928 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hanf1aae3b2012-08-03 17:40:43 +0000929
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000930 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
931 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000932 if (Size.isInvalid())
933 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000934
Douglas Gregor4ac01402011-06-15 16:02:29 +0000935 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000936 } else {
937 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000938 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000939 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000940
Peter Collingbourne7a730022010-11-23 20:45:58 +0000941 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000942 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000943
944 // Instantiate/Install the vector type, and let Sema build the type for us.
945 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000946 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000947 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000948 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000949 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000950
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000951 // Remember this typedef decl, we will need it later for diagnostics.
952 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000953 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000954}
955
Chandler Carruth1b03c872011-07-02 00:01:44 +0000956static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000957 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000958 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000959 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000960
Chandler Carruth87c44602011-07-01 23:49:12 +0000961 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000962 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000963 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000964 // If the alignment is less than or equal to 8 bits, the packed attribute
965 // has no effect.
966 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000967 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000968 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000969 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000970 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000971 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000972 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000973 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000974}
975
Chandler Carruth1b03c872011-07-02 00:01:44 +0000976static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000977 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000978 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000979 else
980 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
981}
982
Chandler Carruth1b03c872011-07-02 00:01:44 +0000983static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000984 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000985 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000986 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000987
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000988 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000989 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000990 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000991 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000992 return;
993 }
994
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000995 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000996}
997
Ted Kremenek2f041d02011-09-29 07:02:25 +0000998static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
999 // The IBOutlet/IBOutletCollection attributes only apply to instance
1000 // variables or properties of Objective-C classes. The outlet must also
1001 // have an object reference type.
1002 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1003 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001004 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001005 << Attr.getName() << VD->getType() << 0;
1006 return false;
1007 }
1008 }
1009 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1010 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001011 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001012 << Attr.getName() << PD->getType() << 1;
1013 return false;
1014 }
1015 }
1016 else {
1017 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1018 return false;
1019 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001020
Ted Kremenek2f041d02011-09-29 07:02:25 +00001021 return true;
1022}
1023
Chandler Carruth1b03c872011-07-02 00:01:44 +00001024static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001025 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001026 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001027 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001028
1029 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001030 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001031
Ted Kremenek2f041d02011-09-29 07:02:25 +00001032 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +00001033}
1034
Chandler Carruth1b03c872011-07-02 00:01:44 +00001035static void handleIBOutletCollection(Sema &S, Decl *D,
1036 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001037
1038 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001039 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1041 return;
1042 }
1043
Ted Kremenek2f041d02011-09-29 07:02:25 +00001044 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001045 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001046
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001047 IdentifierInfo *II = Attr.getParameterName();
1048 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001049 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001050
John McCallb3d87482010-08-24 05:47:05 +00001051 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001052 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001053 if (!TypeRep) {
1054 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1055 return;
1056 }
John McCallb3d87482010-08-24 05:47:05 +00001057 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001058 // Diagnose use of non-object type in iboutletcollection attribute.
1059 // FIXME. Gnu attribute extension ignores use of builtin types in
1060 // attributes. So, __attribute__((iboutletcollection(char))) will be
1061 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001062 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001063 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1064 return;
1065 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +00001066 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1067 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001068}
1069
Chandler Carruthd309c812011-07-01 23:49:16 +00001070static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001071 if (const RecordType *UT = T->getAsUnionType())
1072 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1073 RecordDecl *UD = UT->getDecl();
1074 for (RecordDecl::field_iterator it = UD->field_begin(),
1075 itend = UD->field_end(); it != itend; ++it) {
1076 QualType QT = it->getType();
1077 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1078 T = QT;
1079 return;
1080 }
1081 }
1082 }
1083}
1084
Nuno Lopes587de5b2012-05-24 00:22:00 +00001085static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001086 if (!isFunctionOrMethod(D)) {
1087 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1088 << "alloc_size" << ExpectedFunctionOrMethod;
1089 return;
1090 }
1091
Nuno Lopes587de5b2012-05-24 00:22:00 +00001092 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1093 return;
1094
1095 // In C++ the implicit 'this' function parameter also counts, and they are
1096 // counted from one.
1097 bool HasImplicitThisParam = isInstanceMethod(D);
1098 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1099
1100 SmallVector<unsigned, 8> SizeArgs;
1101
1102 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1103 E = Attr.arg_end(); I!=E; ++I) {
1104 // The argument must be an integer constant expression.
1105 Expr *Ex = *I;
1106 llvm::APSInt ArgNum;
1107 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1108 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1110 << "alloc_size" << Ex->getSourceRange();
1111 return;
1112 }
1113
1114 uint64_t x = ArgNum.getZExtValue();
1115
1116 if (x < 1 || x > NumArgs) {
1117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1118 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1119 return;
1120 }
1121
1122 --x;
1123 if (HasImplicitThisParam) {
1124 if (x == 0) {
1125 S.Diag(Attr.getLoc(),
1126 diag::err_attribute_invalid_implicit_this_argument)
1127 << "alloc_size" << Ex->getSourceRange();
1128 return;
1129 }
1130 --x;
1131 }
1132
1133 // check if the function argument is of an integer type
1134 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1135 if (!T->isIntegerType()) {
1136 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1137 << "alloc_size" << Ex->getSourceRange();
1138 return;
1139 }
1140
Nuno Lopes587de5b2012-05-24 00:22:00 +00001141 SizeArgs.push_back(x);
1142 }
1143
1144 // check if the function returns a pointer
1145 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1146 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1147 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1148 }
1149
Nuno Lopes96c67d12012-06-18 16:27:56 +00001150 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1151 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001152}
1153
Chandler Carruth1b03c872011-07-02 00:01:44 +00001154static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001155 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1156 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001157 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001158 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001159 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001160 return;
1161 }
Mike Stumpbf916502009-07-24 19:02:52 +00001162
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001163 // In C++ the implicit 'this' function parameter also counts, and they are
1164 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001165 bool HasImplicitThisParam = isInstanceMethod(D);
1166 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001167
1168 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001169 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +00001170
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001171 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1172 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +00001173
1174
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001175 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001176 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001177 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001178 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1179 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1181 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001182 return;
1183 }
Mike Stumpbf916502009-07-24 19:02:52 +00001184
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001185 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001186
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001187 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +00001189 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001190 return;
1191 }
Mike Stumpbf916502009-07-24 19:02:52 +00001192
Ted Kremenek465172f2008-07-21 22:09:15 +00001193 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001194 if (HasImplicitThisParam) {
1195 if (x == 0) {
1196 S.Diag(Attr.getLoc(),
1197 diag::err_attribute_invalid_implicit_this_argument)
1198 << "nonnull" << Ex->getSourceRange();
1199 return;
1200 }
1201 --x;
1202 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001203
1204 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001205 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001206 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001207
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001208 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001209 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001210 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001211 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001212 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001213 }
Mike Stumpbf916502009-07-24 19:02:52 +00001214
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001215 NonNullArgs.push_back(x);
1216 }
Mike Stumpbf916502009-07-24 19:02:52 +00001217
1218 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1219 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001220 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001221 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1222 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001223 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001224 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +00001225 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001226 }
Mike Stumpbf916502009-07-24 19:02:52 +00001227
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001228 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001229 if (NonNullArgs.empty()) {
1230 // Warn the trivial case only if attribute is not coming from a
1231 // macro instantiation.
1232 if (Attr.getLoc().isFileID())
1233 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001234 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001235 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001236 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001237
1238 unsigned* start = &NonNullArgs[0];
1239 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001240 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001241 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +00001242 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001243}
1244
Chandler Carruth1b03c872011-07-02 00:01:44 +00001245static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001246 // This attribute must be applied to a function declaration.
1247 // The first argument to the attribute must be a string,
1248 // the name of the resource, for example "malloc".
1249 // The following arguments must be argument indexes, the arguments must be
1250 // of integer type for Returns, otherwise of pointer type.
1251 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001252 // after being held. free() should be __attribute((ownership_takes)), whereas
1253 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001254
1255 if (!AL.getParameterName()) {
1256 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1257 << AL.getName()->getName() << 1;
1258 return;
1259 }
1260 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001261 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001262 switch (AL.getKind()) {
1263 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001264 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001265 if (AL.getNumArgs() < 1) {
1266 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1267 return;
1268 }
Jordy Rose2a479922010-08-12 08:54:03 +00001269 break;
1270 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001271 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001272 if (AL.getNumArgs() < 1) {
1273 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1274 return;
1275 }
Jordy Rose2a479922010-08-12 08:54:03 +00001276 break;
1277 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001278 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001279 if (AL.getNumArgs() > 1) {
1280 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1281 << AL.getNumArgs() + 1;
1282 return;
1283 }
Jordy Rose2a479922010-08-12 08:54:03 +00001284 break;
1285 default:
1286 // This should never happen given how we are called.
1287 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001288 }
1289
Chandler Carruth87c44602011-07-01 23:49:12 +00001290 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001291 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1292 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001293 return;
1294 }
1295
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001296 // In C++ the implicit 'this' function parameter also counts, and they are
1297 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001298 bool HasImplicitThisParam = isInstanceMethod(D);
1299 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001300
Chris Lattner5f9e2722011-07-23 10:55:15 +00001301 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001302
1303 // Normalize the argument, __foo__ becomes foo.
1304 if (Module.startswith("__") && Module.endswith("__"))
1305 Module = Module.substr(2, Module.size() - 4);
1306
Chris Lattner5f9e2722011-07-23 10:55:15 +00001307 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001308
Jordy Rose2a479922010-08-12 08:54:03 +00001309 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1310 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001311
Peter Collingbourne7a730022010-11-23 20:45:58 +00001312 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001313 llvm::APSInt ArgNum(32);
1314 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1315 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1316 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1317 << AL.getName()->getName() << IdxExpr->getSourceRange();
1318 continue;
1319 }
1320
1321 unsigned x = (unsigned) ArgNum.getZExtValue();
1322
1323 if (x > NumArgs || x < 1) {
1324 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1325 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1326 continue;
1327 }
1328 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001329 if (HasImplicitThisParam) {
1330 if (x == 0) {
1331 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1332 << "ownership" << IdxExpr->getSourceRange();
1333 return;
1334 }
1335 --x;
1336 }
1337
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001338 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001339 case OwnershipAttr::Takes:
1340 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001342 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001343 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1344 // FIXME: Should also highlight argument in decl.
1345 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001346 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001347 << "pointer"
1348 << IdxExpr->getSourceRange();
1349 continue;
1350 }
1351 break;
1352 }
Sean Huntcf807c42010-08-18 23:23:40 +00001353 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001354 if (AL.getNumArgs() > 1) {
1355 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001356 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001357 llvm::APSInt ArgNum(32);
1358 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1359 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1360 S.Diag(AL.getLoc(), diag::err_ownership_type)
1361 << "ownership_returns" << "integer"
1362 << IdxExpr->getSourceRange();
1363 return;
1364 }
1365 }
1366 break;
1367 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001368 } // switch
1369
1370 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001371 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001372 i = D->specific_attr_begin<OwnershipAttr>(),
1373 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001374 i != e; ++i) {
1375 if ((*i)->getOwnKind() != K) {
1376 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1377 I!=E; ++I) {
1378 if (x == *I) {
1379 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1380 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001381 }
1382 }
1383 }
1384 }
1385 OwnershipArgs.push_back(x);
1386 }
1387
1388 unsigned* start = OwnershipArgs.data();
1389 unsigned size = OwnershipArgs.size();
1390 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001391
1392 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1393 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1394 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001395 }
Sean Huntcf807c42010-08-18 23:23:40 +00001396
Chandler Carruth87c44602011-07-01 23:49:12 +00001397 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001398 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001399}
1400
John McCall332bb2a2011-02-08 22:35:49 +00001401/// Whether this declaration has internal linkage for the purposes of
1402/// things that want to complain about things not have internal linkage.
1403static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1404 switch (D->getLinkage()) {
1405 case NoLinkage:
1406 case InternalLinkage:
1407 return true;
1408
1409 // Template instantiations that go from external to unique-external
1410 // shouldn't get diagnosed.
1411 case UniqueExternalLinkage:
1412 return true;
1413
1414 case ExternalLinkage:
1415 return false;
1416 }
1417 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001418}
1419
Chandler Carruth1b03c872011-07-02 00:01:44 +00001420static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001421 // Check the attribute arguments.
1422 if (Attr.getNumArgs() > 1) {
1423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1424 return;
1425 }
1426
Chandler Carruth87c44602011-07-01 23:49:12 +00001427 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001429 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001430 return;
1431 }
1432
Chandler Carruth87c44602011-07-01 23:49:12 +00001433 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001434
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001435 // gcc rejects
1436 // class c {
1437 // static int a __attribute__((weakref ("v2")));
1438 // static int b() __attribute__((weakref ("f3")));
1439 // };
1440 // and ignores the attributes of
1441 // void f(void) {
1442 // static int a __attribute__((weakref ("v2")));
1443 // }
1444 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001445 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001446 if (!Ctx->isFileContext()) {
1447 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001448 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001449 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001450 }
1451
1452 // The GCC manual says
1453 //
1454 // At present, a declaration to which `weakref' is attached can only
1455 // be `static'.
1456 //
1457 // It also says
1458 //
1459 // Without a TARGET,
1460 // given as an argument to `weakref' or to `alias', `weakref' is
1461 // equivalent to `weak'.
1462 //
1463 // gcc 4.4.1 will accept
1464 // int a7 __attribute__((weakref));
1465 // as
1466 // int a7 __attribute__((weak));
1467 // This looks like a bug in gcc. We reject that for now. We should revisit
1468 // it if this behaviour is actually used.
1469
John McCall332bb2a2011-02-08 22:35:49 +00001470 if (!hasEffectivelyInternalLinkage(nd)) {
1471 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001472 return;
1473 }
1474
1475 // GCC rejects
1476 // static ((alias ("y"), weakref)).
1477 // Should we? How to check that weakref is before or after alias?
1478
1479 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001480 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001481 Arg = Arg->IgnoreParenCasts();
1482 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1483
Douglas Gregor5cee1192011-07-27 05:40:30 +00001484 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1486 << "weakref" << 1;
1487 return;
1488 }
1489 // GCC will accept anything as the argument of weakref. Should we
1490 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001491 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001492 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001493 }
1494
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001495 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001496}
1497
Chandler Carruth1b03c872011-07-02 00:01:44 +00001498static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001499 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001500 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001501 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001502 return;
1503 }
Mike Stumpbf916502009-07-24 19:02:52 +00001504
Peter Collingbourne7a730022010-11-23 20:45:58 +00001505 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001506 Arg = Arg->IgnoreParenCasts();
1507 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001508
Douglas Gregor5cee1192011-07-27 05:40:30 +00001509 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001510 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001511 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001512 return;
1513 }
Mike Stumpbf916502009-07-24 19:02:52 +00001514
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001515 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001516 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1517 return;
1518 }
1519
Chris Lattner6b6b5372008-06-26 18:38:35 +00001520 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001521
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001522 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001523 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001524}
1525
Benjamin Krameree409a92012-05-12 21:10:52 +00001526static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1527 // Check the attribute arguments.
1528 if (!checkAttributeNumArgs(S, Attr, 0))
1529 return;
1530
1531 if (!isa<FunctionDecl>(D)) {
1532 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1533 << Attr.getName() << ExpectedFunction;
1534 return;
1535 }
1536
1537 if (D->hasAttr<HotAttr>()) {
1538 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1539 << Attr.getName() << "hot";
1540 return;
1541 }
1542
1543 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1544}
1545
1546static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1547 // Check the attribute arguments.
1548 if (!checkAttributeNumArgs(S, Attr, 0))
1549 return;
1550
1551 if (!isa<FunctionDecl>(D)) {
1552 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1553 << Attr.getName() << ExpectedFunction;
1554 return;
1555 }
1556
1557 if (D->hasAttr<ColdAttr>()) {
1558 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1559 << Attr.getName() << "cold";
1560 return;
1561 }
1562
1563 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1564}
1565
Chandler Carruth1b03c872011-07-02 00:01:44 +00001566static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001567 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001568 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001569 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001570
Chandler Carruth87c44602011-07-01 23:49:12 +00001571 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001573 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001574 return;
1575 }
1576
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001577 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001578}
1579
Chandler Carruth1b03c872011-07-02 00:01:44 +00001580static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1581 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001582 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001583 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001584 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1585 return;
1586 }
1587
Chandler Carruth87c44602011-07-01 23:49:12 +00001588 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001589 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001590 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001591 return;
1592 }
Mike Stumpbf916502009-07-24 19:02:52 +00001593
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001594 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001595}
1596
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001597static void handleTLSModelAttr(Sema &S, Decl *D,
1598 const AttributeList &Attr) {
1599 // Check the attribute arguments.
1600 if (Attr.getNumArgs() != 1) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1602 return;
1603 }
1604
1605 Expr *Arg = Attr.getArg(0);
1606 Arg = Arg->IgnoreParenCasts();
1607 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1608
1609 // Check that it is a string.
1610 if (!Str) {
1611 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1612 return;
1613 }
1614
1615 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1617 << Attr.getName() << ExpectedTLSVar;
1618 return;
1619 }
1620
1621 // Check that the value.
1622 StringRef Model = Str->getString();
1623 if (Model != "global-dynamic" && Model != "local-dynamic"
1624 && Model != "initial-exec" && Model != "local-exec") {
1625 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1626 return;
1627 }
1628
1629 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1630 Model));
1631}
1632
Chandler Carruth1b03c872011-07-02 00:01:44 +00001633static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001634 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001635 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001636 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1637 return;
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Chandler Carruth87c44602011-07-01 23:49:12 +00001640 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001641 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001642 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001643 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001644 return;
1645 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001646 }
1647
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001648 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001649}
1650
Chandler Carruth1b03c872011-07-02 00:01:44 +00001651static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001652 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001653 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001654 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001655
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001656 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001657}
1658
Chandler Carruth1b03c872011-07-02 00:01:44 +00001659static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001660 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001661 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001662 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001663 else
1664 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001665 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001666}
1667
Chandler Carruth1b03c872011-07-02 00:01:44 +00001668static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001669 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001670 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001671 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001672 else
1673 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001674 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001675}
1676
Chandler Carruth1b03c872011-07-02 00:01:44 +00001677static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001678 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001679
1680 if (S.CheckNoReturnAttr(attr)) return;
1681
Chandler Carruth87c44602011-07-01 23:49:12 +00001682 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001683 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001684 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001685 return;
1686 }
1687
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001688 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001689}
1690
1691bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001692 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001693 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1694 attr.setInvalid();
1695 return true;
1696 }
1697
1698 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001699}
1700
Chandler Carruth1b03c872011-07-02 00:01:44 +00001701static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1702 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001703
1704 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1705 // because 'analyzer_noreturn' does not impact the type.
1706
Chandler Carruth1731e202011-07-11 23:30:35 +00001707 if(!checkAttributeNumArgs(S, Attr, 0))
1708 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001709
Chandler Carruth87c44602011-07-01 23:49:12 +00001710 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1711 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001712 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1713 && !VD->getType()->isFunctionPointerType())) {
1714 S.Diag(Attr.getLoc(),
1715 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1716 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001717 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001718 return;
1719 }
1720 }
1721
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001722 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001723}
1724
John Thompson35cc9622010-08-09 21:53:52 +00001725// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001726static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001727/*
1728 Returning a Vector Class in Registers
1729
Eric Christopherf48f3672010-12-01 22:13:54 +00001730 According to the PPU ABI specifications, a class with a single member of
1731 vector type is returned in memory when used as the return value of a function.
1732 This results in inefficient code when implementing vector classes. To return
1733 the value in a single vector register, add the vecreturn attribute to the
1734 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001735
1736 Example:
1737
1738 struct Vector
1739 {
1740 __vector float xyzw;
1741 } __attribute__((vecreturn));
1742
1743 Vector Add(Vector lhs, Vector rhs)
1744 {
1745 Vector result;
1746 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1747 return result; // This will be returned in a register
1748 }
1749*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001750 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001752 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001753 return;
1754 }
1755
Chandler Carruth87c44602011-07-01 23:49:12 +00001756 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001757 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1758 return;
1759 }
1760
Chandler Carruth87c44602011-07-01 23:49:12 +00001761 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001762 int count = 0;
1763
1764 if (!isa<CXXRecordDecl>(record)) {
1765 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1766 return;
1767 }
1768
1769 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1770 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1771 return;
1772 }
1773
Eric Christopherf48f3672010-12-01 22:13:54 +00001774 for (RecordDecl::field_iterator iter = record->field_begin();
1775 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001776 if ((count == 1) || !iter->getType()->isVectorType()) {
1777 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1778 return;
1779 }
1780 count++;
1781 }
1782
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001783 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001784}
1785
Chandler Carruth1b03c872011-07-02 00:01:44 +00001786static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001787 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001788 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001789 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001790 return;
1791 }
1792 // FIXME: Actually store the attribute on the declaration
1793}
1794
Chandler Carruth1b03c872011-07-02 00:01:44 +00001795static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001796 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001797 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001799 return;
1800 }
Mike Stumpbf916502009-07-24 19:02:52 +00001801
Chandler Carruth87c44602011-07-01 23:49:12 +00001802 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001803 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001804 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001805 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001806 return;
1807 }
Mike Stumpbf916502009-07-24 19:02:52 +00001808
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001809 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001810}
1811
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001812static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1813 const AttributeList &Attr) {
1814 // check the attribute arguments.
1815 if (Attr.hasParameterOrArguments()) {
1816 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1817 return;
1818 }
1819
1820 if (!isa<FunctionDecl>(D)) {
1821 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1822 << Attr.getName() << ExpectedFunction;
1823 return;
1824 }
1825
1826 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1827}
1828
Chandler Carruth1b03c872011-07-02 00:01:44 +00001829static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001830 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001831 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001832 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1833 return;
1834 }
Mike Stumpbf916502009-07-24 19:02:52 +00001835
Chandler Carruth87c44602011-07-01 23:49:12 +00001836 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001837 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001838 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1839 return;
1840 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001841 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001843 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001844 return;
1845 }
Mike Stumpbf916502009-07-24 19:02:52 +00001846
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001847 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001848}
1849
Chandler Carruth1b03c872011-07-02 00:01:44 +00001850static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001851 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001852 if (Attr.getNumArgs() > 1) {
1853 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001854 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001855 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001856
1857 int priority = 65535; // FIXME: Do not hardcode such constants.
1858 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001859 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001860 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001861 if (E->isTypeDependent() || E->isValueDependent() ||
1862 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001863 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001864 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001865 return;
1866 }
1867 priority = Idx.getZExtValue();
1868 }
Mike Stumpbf916502009-07-24 19:02:52 +00001869
Chandler Carruth87c44602011-07-01 23:49:12 +00001870 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001872 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001873 return;
1874 }
1875
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001876 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001877 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001878}
1879
Chandler Carruth1b03c872011-07-02 00:01:44 +00001880static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001881 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001882 if (Attr.getNumArgs() > 1) {
1883 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001884 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001885 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001886
1887 int priority = 65535; // FIXME: Do not hardcode such constants.
1888 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001889 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001890 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001891 if (E->isTypeDependent() || E->isValueDependent() ||
1892 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001893 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001894 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001895 return;
1896 }
1897 priority = Idx.getZExtValue();
1898 }
Mike Stumpbf916502009-07-24 19:02:52 +00001899
Chandler Carruth87c44602011-07-01 23:49:12 +00001900 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001901 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001902 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001903 return;
1904 }
1905
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001906 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001907 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001908}
1909
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001910template <typename AttrTy>
1911static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1912 const char *Name) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001913 unsigned NumArgs = Attr.getNumArgs();
1914 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001915 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001916 return;
1917 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001918
1919 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001920 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001921 if (NumArgs == 1) {
1922 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001923 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001924 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001925 << Name;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001926 return;
1927 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001928 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001929 }
Mike Stumpbf916502009-07-24 19:02:52 +00001930
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001931 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001932}
1933
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001934static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1935 const AttributeList &Attr) {
1936 unsigned NumArgs = Attr.getNumArgs();
1937 if (NumArgs > 0) {
1938 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1939 return;
1940 }
1941
1942 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001943 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001944}
1945
Patrick Beardb2f68202012-04-06 18:12:22 +00001946static void handleObjCRootClassAttr(Sema &S, Decl *D,
1947 const AttributeList &Attr) {
1948 if (!isa<ObjCInterfaceDecl>(D)) {
1949 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1950 return;
1951 }
1952
1953 unsigned NumArgs = Attr.getNumArgs();
1954 if (NumArgs > 0) {
1955 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1956 return;
1957 }
1958
1959 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1960}
1961
Ted Kremenek71207fc2012-01-05 22:47:47 +00001962static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001963 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001964 if (!isa<ObjCInterfaceDecl>(D)) {
1965 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1966 return;
1967 }
1968
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001969 unsigned NumArgs = Attr.getNumArgs();
1970 if (NumArgs > 0) {
1971 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1972 return;
1973 }
1974
Ted Kremenek71207fc2012-01-05 22:47:47 +00001975 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001976 Attr.getRange(), S.Context));
1977}
1978
Jordy Rosefad5de92012-05-08 03:27:22 +00001979static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1980 IdentifierInfo *Platform,
1981 VersionTuple Introduced,
1982 VersionTuple Deprecated,
1983 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00001984 StringRef PlatformName
1985 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1986 if (PlatformName.empty())
1987 PlatformName = Platform->getName();
1988
1989 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1990 // of these steps are needed).
1991 if (!Introduced.empty() && !Deprecated.empty() &&
1992 !(Introduced <= Deprecated)) {
1993 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1994 << 1 << PlatformName << Deprecated.getAsString()
1995 << 0 << Introduced.getAsString();
1996 return true;
1997 }
1998
1999 if (!Introduced.empty() && !Obsoleted.empty() &&
2000 !(Introduced <= Obsoleted)) {
2001 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2002 << 2 << PlatformName << Obsoleted.getAsString()
2003 << 0 << Introduced.getAsString();
2004 return true;
2005 }
2006
2007 if (!Deprecated.empty() && !Obsoleted.empty() &&
2008 !(Deprecated <= Obsoleted)) {
2009 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2010 << 2 << PlatformName << Obsoleted.getAsString()
2011 << 1 << Deprecated.getAsString();
2012 return true;
2013 }
2014
2015 return false;
2016}
2017
Rafael Espindola599f1b72012-05-13 03:25:18 +00002018AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2019 IdentifierInfo *Platform,
2020 VersionTuple Introduced,
2021 VersionTuple Deprecated,
2022 VersionTuple Obsoleted,
2023 bool IsUnavailable,
2024 StringRef Message) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002025 VersionTuple MergedIntroduced = Introduced;
2026 VersionTuple MergedDeprecated = Deprecated;
2027 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002028 bool FoundAny = false;
2029
Rafael Espindola98ae8342012-05-10 02:50:16 +00002030 if (D->hasAttrs()) {
2031 AttrVec &Attrs = D->getAttrs();
2032 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2033 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2034 if (!OldAA) {
2035 ++i;
2036 continue;
2037 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002038
Rafael Espindola98ae8342012-05-10 02:50:16 +00002039 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2040 if (OldPlatform != Platform) {
2041 ++i;
2042 continue;
2043 }
2044
2045 FoundAny = true;
2046 VersionTuple OldIntroduced = OldAA->getIntroduced();
2047 VersionTuple OldDeprecated = OldAA->getDeprecated();
2048 VersionTuple OldObsoleted = OldAA->getObsoleted();
2049 bool OldIsUnavailable = OldAA->getUnavailable();
2050 StringRef OldMessage = OldAA->getMessage();
2051
2052 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2053 OldIntroduced != Introduced) ||
2054 (!OldDeprecated.empty() && !Deprecated.empty() &&
2055 OldDeprecated != Deprecated) ||
2056 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2057 OldObsoleted != Obsoleted) ||
2058 (OldIsUnavailable != IsUnavailable) ||
2059 (OldMessage != Message)) {
2060 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2061 Diag(Range.getBegin(), diag::note_previous_attribute);
2062 Attrs.erase(Attrs.begin() + i);
2063 --e;
2064 continue;
2065 }
2066
2067 VersionTuple MergedIntroduced2 = MergedIntroduced;
2068 VersionTuple MergedDeprecated2 = MergedDeprecated;
2069 VersionTuple MergedObsoleted2 = MergedObsoleted;
2070
2071 if (MergedIntroduced2.empty())
2072 MergedIntroduced2 = OldIntroduced;
2073 if (MergedDeprecated2.empty())
2074 MergedDeprecated2 = OldDeprecated;
2075 if (MergedObsoleted2.empty())
2076 MergedObsoleted2 = OldObsoleted;
2077
2078 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2079 MergedIntroduced2, MergedDeprecated2,
2080 MergedObsoleted2)) {
2081 Attrs.erase(Attrs.begin() + i);
2082 --e;
2083 continue;
2084 }
2085
2086 MergedIntroduced = MergedIntroduced2;
2087 MergedDeprecated = MergedDeprecated2;
2088 MergedObsoleted = MergedObsoleted2;
2089 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002090 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002091 }
2092
2093 if (FoundAny &&
2094 MergedIntroduced == Introduced &&
2095 MergedDeprecated == Deprecated &&
2096 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002097 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002098
Rafael Espindola98ae8342012-05-10 02:50:16 +00002099 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola3b294362012-05-06 19:56:25 +00002100 MergedDeprecated, MergedObsoleted)) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002101 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2102 Introduced, Deprecated,
2103 Obsoleted, IsUnavailable, Message);
Rafael Espindola3b294362012-05-06 19:56:25 +00002104 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002105 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002106}
2107
Chandler Carruth1b03c872011-07-02 00:01:44 +00002108static void handleAvailabilityAttr(Sema &S, Decl *D,
2109 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002110 IdentifierInfo *Platform = Attr.getParameterName();
2111 SourceLocation PlatformLoc = Attr.getParameterLoc();
2112
Rafael Espindola3b294362012-05-06 19:56:25 +00002113 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002114 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2115 << Platform;
2116
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002117 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2118 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2119 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002120 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002121 StringRef Str;
2122 const StringLiteral *SE =
2123 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2124 if (SE)
2125 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002126
Rafael Espindola599f1b72012-05-13 03:25:18 +00002127 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2128 Platform,
2129 Introduced.Version,
2130 Deprecated.Version,
2131 Obsoleted.Version,
2132 IsUnavailable, Str);
2133 if (NewAttr)
2134 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002135}
2136
Rafael Espindola599f1b72012-05-13 03:25:18 +00002137VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2138 VisibilityAttr::VisibilityType Vis) {
Rafael Espindoladd44f342012-05-10 03:01:34 +00002139 if (isa<TypedefNameDecl>(D)) {
2140 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindola599f1b72012-05-13 03:25:18 +00002141 return NULL;
Rafael Espindoladd44f342012-05-10 03:01:34 +00002142 }
Rafael Espindola98ae8342012-05-10 02:50:16 +00002143 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2144 if (ExistingAttr) {
2145 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2146 if (ExistingVis == Vis)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002147 return NULL;
Rafael Espindola98ae8342012-05-10 02:50:16 +00002148 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2149 Diag(Range.getBegin(), diag::note_previous_attribute);
2150 D->dropAttr<VisibilityAttr>();
2151 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002152 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002153}
2154
Chandler Carruth1b03c872011-07-02 00:01:44 +00002155static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002156 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002157 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002158 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002159
Peter Collingbourne7a730022010-11-23 20:45:58 +00002160 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002161 Arg = Arg->IgnoreParenCasts();
2162 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002163
Douglas Gregor5cee1192011-07-27 05:40:30 +00002164 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002165 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002166 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002167 return;
2168 }
Mike Stumpbf916502009-07-24 19:02:52 +00002169
Chris Lattner5f9e2722011-07-23 10:55:15 +00002170 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002171 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00002172
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002173 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002174 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002175 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002176 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002177 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002178 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002179 else if (TypeStr == "protected") {
2180 // Complain about attempts to use protected visibility on targets
2181 // (like Darwin) that don't support it.
2182 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2183 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2184 type = VisibilityAttr::Default;
2185 } else {
2186 type = VisibilityAttr::Protected;
2187 }
2188 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002189 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002190 return;
2191 }
Mike Stumpbf916502009-07-24 19:02:52 +00002192
Rafael Espindola599f1b72012-05-13 03:25:18 +00002193 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2194 if (NewAttr)
2195 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002196}
2197
Chandler Carruth1b03c872011-07-02 00:01:44 +00002198static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2199 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002200 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2201 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002203 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002204 return;
2205 }
2206
Chandler Carruth87c44602011-07-01 23:49:12 +00002207 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2208 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2209 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00002210 << "objc_method_family" << 1;
2211 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00002213 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002214 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002215 return;
2216 }
2217
Chris Lattner5f9e2722011-07-23 10:55:15 +00002218 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002219 ObjCMethodFamilyAttr::FamilyKind family;
2220 if (param == "none")
2221 family = ObjCMethodFamilyAttr::OMF_None;
2222 else if (param == "alloc")
2223 family = ObjCMethodFamilyAttr::OMF_alloc;
2224 else if (param == "copy")
2225 family = ObjCMethodFamilyAttr::OMF_copy;
2226 else if (param == "init")
2227 family = ObjCMethodFamilyAttr::OMF_init;
2228 else if (param == "mutableCopy")
2229 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2230 else if (param == "new")
2231 family = ObjCMethodFamilyAttr::OMF_new;
2232 else {
2233 // Just warn and ignore it. This is future-proof against new
2234 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002235 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002236 return;
2237 }
2238
John McCallf85e1932011-06-15 23:02:42 +00002239 if (family == ObjCMethodFamilyAttr::OMF_init &&
2240 !method->getResultType()->isObjCObjectPointerType()) {
2241 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2242 << method->getResultType();
2243 // Ignore the attribute.
2244 return;
2245 }
2246
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002247 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002248 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002249}
2250
Chandler Carruth1b03c872011-07-02 00:01:44 +00002251static void handleObjCExceptionAttr(Sema &S, Decl *D,
2252 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002253 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002254 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002255
Chris Lattner0db29ec2009-02-14 08:09:34 +00002256 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2257 if (OCI == 0) {
2258 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2259 return;
2260 }
Mike Stumpbf916502009-07-24 19:02:52 +00002261
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002262 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002263}
2264
Chandler Carruth1b03c872011-07-02 00:01:44 +00002265static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002266 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002268 return;
2269 }
Richard Smith162e1c12011-04-15 14:24:37 +00002270 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002271 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002272 if (!T->isCARCBridgableType()) {
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();
Ted Kremenek9af91222012-08-29 22:54:47 +00002279 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002280 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2281 return;
2282 }
2283 }
2284 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002285 // It is okay to include this attribute on properties, e.g.:
2286 //
2287 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2288 //
2289 // In this case it follows tradition and suppresses an error in the above
2290 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002291 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002292 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002293 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002294}
2295
Mike Stumpbf916502009-07-24 19:02:52 +00002296static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002297handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002298 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00002299 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002300 return;
2301 }
2302
2303 if (!isa<FunctionDecl>(D)) {
2304 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2305 return;
2306 }
2307
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002308 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002309}
2310
Chandler Carruth1b03c872011-07-02 00:01:44 +00002311static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002312 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002314 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002315 return;
2316 }
Mike Stumpbf916502009-07-24 19:02:52 +00002317
Steve Naroff9eae5762008-09-18 16:44:58 +00002318 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002319 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002320 return;
2321 }
Mike Stumpbf916502009-07-24 19:02:52 +00002322
Sean Huntcf807c42010-08-18 23:23:40 +00002323 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002324 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002325 type = BlocksAttr::ByRef;
2326 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002327 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002328 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002329 return;
2330 }
Mike Stumpbf916502009-07-24 19:02:52 +00002331
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002332 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00002333}
2334
Chandler Carruth1b03c872011-07-02 00:01:44 +00002335static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002336 // check the attribute arguments.
2337 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002338 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002339 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002340 }
2341
John McCall3323fad2011-09-09 07:56:05 +00002342 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002343 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002344 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002345 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002346 if (E->isTypeDependent() || E->isValueDependent() ||
2347 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002348 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002349 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002350 return;
2351 }
Mike Stumpbf916502009-07-24 19:02:52 +00002352
John McCall3323fad2011-09-09 07:56:05 +00002353 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002354 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2355 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002356 return;
2357 }
John McCall3323fad2011-09-09 07:56:05 +00002358
2359 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002360 }
2361
John McCall3323fad2011-09-09 07:56:05 +00002362 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002363 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002364 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002365 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002366 if (E->isTypeDependent() || E->isValueDependent() ||
2367 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002368 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002369 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002370 return;
2371 }
2372 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002373
John McCall3323fad2011-09-09 07:56:05 +00002374 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002375 // FIXME: This error message could be improved, it would be nice
2376 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002377 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2378 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002379 return;
2380 }
2381 }
2382
Chandler Carruth87c44602011-07-01 23:49:12 +00002383 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002384 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002385 if (isa<FunctionNoProtoType>(FT)) {
2386 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2387 return;
2388 }
Mike Stumpbf916502009-07-24 19:02:52 +00002389
Chris Lattner897cd902009-03-17 23:03:47 +00002390 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002391 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002392 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002393 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002394 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002395 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002396 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002397 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002398 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002399 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2400 if (!BD->isVariadic()) {
2401 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2402 return;
2403 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002404 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002405 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002406 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002407 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002408 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002409 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002410 int m = Ty->isFunctionPointerType() ? 0 : 1;
2411 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002412 return;
2413 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002414 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002416 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002417 return;
2418 }
Anders Carlsson77091822008-10-05 18:05:59 +00002419 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002421 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002422 return;
2423 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002424 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00002425 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00002426}
2427
Chandler Carruth1b03c872011-07-02 00:01:44 +00002428static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002429 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002430 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002431 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002432
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002433 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002434 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002435 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00002436 return;
2437 }
Mike Stumpbf916502009-07-24 19:02:52 +00002438
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002439 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2440 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2441 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002442 return;
2443 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002444 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2445 if (MD->getResultType()->isVoidType()) {
2446 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2447 << Attr.getName() << 1;
2448 return;
2449 }
2450
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002451 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00002452}
2453
Chandler Carruth1b03c872011-07-02 00:01:44 +00002454static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002455 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002456 if (Attr.hasParameterOrArguments()) {
2457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002458 return;
2459 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002460
Chandler Carruth87c44602011-07-01 23:49:12 +00002461 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002462 if (isa<CXXRecordDecl>(D)) {
2463 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2464 return;
2465 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002466 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2467 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002468 return;
2469 }
2470
Chandler Carruth87c44602011-07-01 23:49:12 +00002471 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002472
2473 // 'weak' only applies to declarations with external linkage.
2474 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002475 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002476 return;
2477 }
Mike Stumpbf916502009-07-24 19:02:52 +00002478
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002479 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002480}
2481
Chandler Carruth1b03c872011-07-02 00:01:44 +00002482static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002483 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002484 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002485 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002486
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002487
2488 // weak_import only applies to variable & function declarations.
2489 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002490 if (!D->canBeWeakImported(isDef)) {
2491 if (isDef)
2492 S.Diag(Attr.getLoc(),
2493 diag::warn_attribute_weak_import_invalid_on_definition)
2494 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002495 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002496 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002497 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002498 // Nothing to warn about here.
2499 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002500 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002501 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002502
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002503 return;
2504 }
2505
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002506 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002507}
2508
Tanya Lattner0df579e2012-07-09 22:06:01 +00002509// Handles reqd_work_group_size and work_group_size_hint.
2510static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002511 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002512 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2513 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2514
Nate Begeman6f3d8382009-06-26 06:32:41 +00002515 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002516 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002517
2518 unsigned WGSize[3];
2519 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002520 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002521 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002522 if (E->isTypeDependent() || E->isValueDependent() ||
2523 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002524 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattner0df579e2012-07-09 22:06:01 +00002525 << Attr.getName()->getName() << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002526 return;
2527 }
2528 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2529 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002530
2531 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2532 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2533 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2534 if (!(A->getXDim() == WGSize[0] &&
2535 A->getYDim() == WGSize[1] &&
2536 A->getZDim() == WGSize[2])) {
2537 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2538 Attr.getName();
2539 }
2540 }
2541
2542 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2543 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2544 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2545 if (!(A->getXDim() == WGSize[0] &&
2546 A->getYDim() == WGSize[1] &&
2547 A->getZDim() == WGSize[2])) {
2548 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2549 Attr.getName();
2550 }
2551 }
2552
2553 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2554 D->addAttr(::new (S.Context)
2555 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2556 WGSize[0], WGSize[1], WGSize[2]));
2557 else
2558 D->addAttr(::new (S.Context)
2559 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2560 WGSize[0], WGSize[1], WGSize[2]));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002561}
2562
Rafael Espindola599f1b72012-05-13 03:25:18 +00002563SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2564 StringRef Name) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002565 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2566 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002567 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002568 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2569 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002570 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002571 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002572 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola420efd82012-05-13 02:42:42 +00002573}
2574
Chandler Carruth1b03c872011-07-02 00:01:44 +00002575static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002576 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002577 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002578 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002579
2580 // Make sure that there is a string literal as the sections's single
2581 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002582 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002583 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002584 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002585 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002586 return;
2587 }
Mike Stump1eb44332009-09-09 15:08:12 +00002588
Chris Lattner797c3c42009-08-10 19:03:04 +00002589 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002590 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002591 if (!Error.empty()) {
2592 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2593 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002594 return;
2595 }
Mike Stump1eb44332009-09-09 15:08:12 +00002596
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002597 // This attribute cannot be applied to local variables.
2598 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2599 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2600 return;
2601 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002602 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2603 SE->getString());
2604 if (NewAttr)
2605 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002606}
2607
Chris Lattner6b6b5372008-06-26 18:38:35 +00002608
Chandler Carruth1b03c872011-07-02 00:01:44 +00002609static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002610 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002611 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002612 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002613 return;
2614 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002615
Chandler Carruth87c44602011-07-01 23:49:12 +00002616 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002617 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002618 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002619 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002620 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002621 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002622}
2623
Chandler Carruth1b03c872011-07-02 00:01:44 +00002624static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002625 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002626 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002628 return;
2629 }
Mike Stumpbf916502009-07-24 19:02:52 +00002630
Chandler Carruth87c44602011-07-01 23:49:12 +00002631 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002632 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002633 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002634 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002635 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002636 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002637}
2638
Chandler Carruth1b03c872011-07-02 00:01:44 +00002639static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002640 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002641 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002642 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002643
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002644 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002645}
2646
Chandler Carruth1b03c872011-07-02 00:01:44 +00002647static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002648 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002649 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2650 return;
2651 }
Mike Stumpbf916502009-07-24 19:02:52 +00002652
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002653 if (Attr.getNumArgs() != 0) {
2654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2655 return;
2656 }
Mike Stumpbf916502009-07-24 19:02:52 +00002657
Chandler Carruth87c44602011-07-01 23:49:12 +00002658 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002659
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002660 if (!VD || !VD->hasLocalStorage()) {
2661 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2662 return;
2663 }
Mike Stumpbf916502009-07-24 19:02:52 +00002664
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002665 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002666 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002667 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002668 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2669 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002670 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002671 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002672 Attr.getParameterName();
2673 return;
2674 }
Mike Stumpbf916502009-07-24 19:02:52 +00002675
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002676 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2677 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002678 S.Diag(Attr.getParameterLoc(),
2679 diag::err_attribute_cleanup_arg_not_function)
2680 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002681 return;
2682 }
2683
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002684 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002685 S.Diag(Attr.getParameterLoc(),
2686 diag::err_attribute_cleanup_func_must_take_one_arg)
2687 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002688 return;
2689 }
Mike Stumpbf916502009-07-24 19:02:52 +00002690
Anders Carlsson89941c12009-02-07 23:16:50 +00002691 // We're currently more strict than GCC about what function types we accept.
2692 // If this ever proves to be a problem it should be easy to fix.
2693 QualType Ty = S.Context.getPointerType(VD->getType());
2694 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002695 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2696 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002697 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002698 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2699 Attr.getParameterName() << ParamTy << Ty;
2700 return;
2701 }
Mike Stumpbf916502009-07-24 19:02:52 +00002702
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002703 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002704 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002705}
2706
Mike Stumpbf916502009-07-24 19:02:52 +00002707/// Handle __attribute__((format_arg((idx)))) attribute based on
2708/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002709static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002710 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002711 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002712
Chandler Carruth87c44602011-07-01 23:49:12 +00002713 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002714 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002715 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002716 return;
2717 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002718
2719 // In C++ the implicit 'this' function parameter also counts, and they are
2720 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002721 bool HasImplicitThisParam = isInstanceMethod(D);
2722 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002723 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002724
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002725 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002726 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002727 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002728 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2729 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002730 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2731 << "format" << 2 << IdxExpr->getSourceRange();
2732 return;
2733 }
Mike Stumpbf916502009-07-24 19:02:52 +00002734
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002735 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2736 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2737 << "format" << 2 << IdxExpr->getSourceRange();
2738 return;
2739 }
Mike Stumpbf916502009-07-24 19:02:52 +00002740
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002741 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002742
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002743 if (HasImplicitThisParam) {
2744 if (ArgIdx == 0) {
2745 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2746 << "format_arg" << IdxExpr->getSourceRange();
2747 return;
2748 }
2749 ArgIdx--;
2750 }
2751
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002752 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002753 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002754
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002755 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2756 if (not_nsstring_type &&
2757 !isCFStringType(Ty, S.Context) &&
2758 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002759 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002760 // FIXME: Should highlight the actual expression that has the wrong type.
2761 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002762 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002763 << IdxExpr->getSourceRange();
2764 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002765 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002766 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002767 if (!isNSStringType(Ty, S.Context) &&
2768 !isCFStringType(Ty, S.Context) &&
2769 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002770 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002771 // FIXME: Should highlight the actual expression that has the wrong type.
2772 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002773 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002774 << IdxExpr->getSourceRange();
2775 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002776 }
2777
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002778 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002779 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002780}
2781
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002782enum FormatAttrKind {
2783 CFStringFormat,
2784 NSStringFormat,
2785 StrftimeFormat,
2786 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002787 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002788 InvalidFormat
2789};
2790
2791/// getFormatAttrKind - Map from format attribute names to supported format
2792/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002793static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002794 return llvm::StringSwitch<FormatAttrKind>(Format)
2795 // Check for formats that get handled specially.
2796 .Case("NSString", NSStringFormat)
2797 .Case("CFString", CFStringFormat)
2798 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002799
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002800 // Otherwise, check for supported formats.
2801 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2802 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2803 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002804
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002805 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2806 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002807}
2808
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002809/// Handle __attribute__((init_priority(priority))) attributes based on
2810/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002811static void handleInitPriorityAttr(Sema &S, Decl *D,
2812 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002813 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002814 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2815 return;
2816 }
2817
Chandler Carruth87c44602011-07-01 23:49:12 +00002818 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002819 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2820 Attr.setInvalid();
2821 return;
2822 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002823 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002824 if (S.Context.getAsArrayType(T))
2825 T = S.Context.getBaseElementType(T);
2826 if (!T->getAs<RecordType>()) {
2827 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2828 Attr.setInvalid();
2829 return;
2830 }
2831
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002832 if (Attr.getNumArgs() != 1) {
2833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2834 Attr.setInvalid();
2835 return;
2836 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002837 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002838
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002839 llvm::APSInt priority(32);
2840 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2841 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2842 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2843 << "init_priority" << priorityExpr->getSourceRange();
2844 Attr.setInvalid();
2845 return;
2846 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002847 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002848 if (prioritynum < 101 || prioritynum > 65535) {
2849 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2850 << priorityExpr->getSourceRange();
2851 Attr.setInvalid();
2852 return;
2853 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002854 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002855 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002856}
2857
Rafael Espindola599f1b72012-05-13 03:25:18 +00002858FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2859 int FormatIdx, int FirstArg) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002860 // Check whether we already have an equivalent format attribute.
2861 for (specific_attr_iterator<FormatAttr>
2862 i = D->specific_attr_begin<FormatAttr>(),
2863 e = D->specific_attr_end<FormatAttr>();
2864 i != e ; ++i) {
2865 FormatAttr *f = *i;
2866 if (f->getType() == Format &&
2867 f->getFormatIdx() == FormatIdx &&
2868 f->getFirstArg() == FirstArg) {
2869 // If we don't have a valid location for this attribute, adopt the
2870 // location.
2871 if (f->getLocation().isInvalid())
2872 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002873 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002874 }
2875 }
2876
Rafael Espindola599f1b72012-05-13 03:25:18 +00002877 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2878 FirstArg);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002879}
2880
Mike Stumpbf916502009-07-24 19:02:52 +00002881/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2882/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002883static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002884
Chris Lattner545dd342008-06-28 23:36:30 +00002885 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002886 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002887 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002888 return;
2889 }
2890
Chris Lattner545dd342008-06-28 23:36:30 +00002891 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002893 return;
2894 }
2895
Chandler Carruth87c44602011-07-01 23:49:12 +00002896 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002897 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002898 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002899 return;
2900 }
2901
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002902 // In C++ the implicit 'this' function parameter also counts, and they are
2903 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002904 bool HasImplicitThisParam = isInstanceMethod(D);
2905 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002906 unsigned FirstIdx = 1;
2907
Chris Lattner5f9e2722011-07-23 10:55:15 +00002908 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002909
2910 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002911 if (Format.startswith("__") && Format.endswith("__"))
2912 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002913
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002914 // Check for supported formats.
2915 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002916
2917 if (Kind == IgnoredFormat)
2918 return;
2919
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002920 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002921 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002922 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002923 return;
2924 }
2925
2926 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002927 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002928 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002929 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2930 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002931 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002932 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002933 return;
2934 }
2935
2936 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002937 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002938 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002939 return;
2940 }
2941
2942 // FIXME: Do we need to bounds check?
2943 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002944
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002945 if (HasImplicitThisParam) {
2946 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002947 S.Diag(Attr.getLoc(),
2948 diag::err_format_attribute_implicit_this_format_string)
2949 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002950 return;
2951 }
2952 ArgIdx--;
2953 }
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Chris Lattner6b6b5372008-06-26 18:38:35 +00002955 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002956 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002957
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002958 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002959 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002960 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2961 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002962 return;
2963 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002964 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002965 // FIXME: do we need to check if the type is NSString*? What are the
2966 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002967 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002968 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002969 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2970 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002971 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002972 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002973 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002974 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002975 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002976 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2977 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002978 return;
2979 }
2980
2981 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002982 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002983 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002984 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2985 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002987 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002988 return;
2989 }
2990
2991 // check if the function is variadic if the 3rd argument non-zero
2992 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002993 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002994 ++NumArgs; // +1 for ...
2995 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002996 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002997 return;
2998 }
2999 }
3000
Chris Lattner3c73c412008-11-19 08:23:25 +00003001 // strftime requires FirstArg to be 0 because it doesn't read from any
3002 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003003 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003004 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003005 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3006 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003007 return;
3008 }
3009 // if 0 it disables parameter checking (to use with e.g. va_list)
3010 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003011 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003012 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003013 return;
3014 }
3015
Rafael Espindola599f1b72012-05-13 03:25:18 +00003016 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3017 Idx.getZExtValue(),
3018 FirstArg.getZExtValue());
3019 if (NewAttr)
3020 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003021}
3022
Chandler Carruth1b03c872011-07-02 00:01:44 +00003023static void handleTransparentUnionAttr(Sema &S, Decl *D,
3024 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003025 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003026 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003027 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003028
Chris Lattner6b6b5372008-06-26 18:38:35 +00003029
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003030 // Try to find the underlying union declaration.
3031 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003032 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003033 if (TD && TD->getUnderlyingType()->isUnionType())
3034 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3035 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003036 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003037
3038 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003040 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003041 return;
3042 }
3043
John McCall5e1cdac2011-10-07 06:10:15 +00003044 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003045 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003046 diag::warn_transparent_union_attribute_not_definition);
3047 return;
3048 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003049
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003050 RecordDecl::field_iterator Field = RD->field_begin(),
3051 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003052 if (Field == FieldEnd) {
3053 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3054 return;
3055 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003056
David Blaikie581deb32012-06-06 20:45:41 +00003057 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003058 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003059 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003060 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003061 diag::warn_transparent_union_attribute_floating)
3062 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003063 return;
3064 }
3065
3066 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3067 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3068 for (; Field != FieldEnd; ++Field) {
3069 QualType FieldType = Field->getType();
3070 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3071 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3072 // Warn if we drop the attribute.
3073 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003074 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003075 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003076 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003077 diag::warn_transparent_union_attribute_field_size_align)
3078 << isSize << Field->getDeclName() << FieldBits;
3079 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003080 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003081 diag::note_transparent_union_first_field_size_align)
3082 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003083 return;
3084 }
3085 }
3086
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003087 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003088}
3089
Chandler Carruth1b03c872011-07-02 00:01:44 +00003090static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003091 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003092 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003093 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003094
Peter Collingbourne7a730022010-11-23 20:45:58 +00003095 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003096 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003097
Chris Lattner6b6b5372008-06-26 18:38:35 +00003098 // Make sure that there is a string literal as the annotation's single
3099 // argument.
3100 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00003101 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00003102 return;
3103 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003104
3105 // Don't duplicate annotations that are already set.
3106 for (specific_attr_iterator<AnnotateAttr>
3107 i = D->specific_attr_begin<AnnotateAttr>(),
3108 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3109 if ((*i)->getAnnotation() == SE->getString())
3110 return;
3111 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003112 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00003113 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003114}
3115
Chandler Carruth1b03c872011-07-02 00:01:44 +00003116static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003117 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003118 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00003119 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003120 return;
3121 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003122
Sean Huntbbd37c62009-11-21 08:43:09 +00003123 //FIXME: The C++0x version of this attribute has more limited applicabilty
3124 // than GNU's, and should error out when it is used to specify a
3125 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00003126
Chris Lattner545dd342008-06-28 23:36:30 +00003127 if (Attr.getNumArgs() == 0) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003128 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3129 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003130 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131 }
Mike Stumpbf916502009-07-24 19:02:52 +00003132
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003133 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3134 Attr.isDeclspecAttribute());
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003135}
3136
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003137void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3138 bool isDeclSpec) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00003139 // FIXME: Handle pack-expansions here.
3140 if (DiagnoseUnexpandedParameterPack(E))
3141 return;
3142
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003143 if (E->isTypeDependent() || E->isValueDependent()) {
3144 // Save dependent expressions in the AST to be instantiated.
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003145 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3146 isDeclSpec));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003147 return;
3148 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003149
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003150 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00003151 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003152 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003153 ExprResult ICE
3154 = VerifyIntegerConstantExpression(E, &Alignment,
3155 diag::err_aligned_attribute_argument_not_int,
3156 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003157 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003158 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003159 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003160 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3161 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003162 return;
3163 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003164 if (isDeclSpec) {
3165 // We've already verified it's a power of 2, now let's make sure it's
3166 // 8192 or less.
3167 if (Alignment.getZExtValue() > 8192) {
3168 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3169 << E->getSourceRange();
3170 return;
3171 }
3172 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003173
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003174 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3175 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003176}
3177
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003178void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3179 bool isDeclSpec) {
Sean Huntcf807c42010-08-18 23:23:40 +00003180 // FIXME: Cache the number on the Attr object if non-dependent?
3181 // FIXME: Perform checking of type validity
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003182 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3183 isDeclSpec));
Sean Huntcf807c42010-08-18 23:23:40 +00003184 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003185}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003186
Chandler Carruthd309c812011-07-01 23:49:16 +00003187/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003188/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003189///
Mike Stumpbf916502009-07-24 19:02:52 +00003190/// Despite what would be logical, the mode attribute is a decl attribute, not a
3191/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3192/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003193static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003194 // This attribute isn't documented, but glibc uses it. It changes
3195 // the width of an int or unsigned int to the specified size.
3196
3197 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003198 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003199 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003200
Chris Lattnerfbf13472008-06-27 22:18:37 +00003201
3202 IdentifierInfo *Name = Attr.getParameterName();
3203 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003204 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003205 return;
3206 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003207
Chris Lattner5f9e2722011-07-23 10:55:15 +00003208 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003209
3210 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003211 if (Str.startswith("__") && Str.endswith("__"))
3212 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003213
3214 unsigned DestWidth = 0;
3215 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003216 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003217 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003218 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003219 switch (Str[0]) {
3220 case 'Q': DestWidth = 8; break;
3221 case 'H': DestWidth = 16; break;
3222 case 'S': DestWidth = 32; break;
3223 case 'D': DestWidth = 64; break;
3224 case 'X': DestWidth = 96; break;
3225 case 'T': DestWidth = 128; break;
3226 }
3227 if (Str[1] == 'F') {
3228 IntegerMode = false;
3229 } else if (Str[1] == 'C') {
3230 IntegerMode = false;
3231 ComplexMode = true;
3232 } else if (Str[1] != 'I') {
3233 DestWidth = 0;
3234 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003235 break;
3236 case 4:
3237 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3238 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003239 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003240 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003241 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003242 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003243 break;
3244 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003245 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003246 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003247 break;
3248 }
3249
3250 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003251 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003252 OldTy = TD->getUnderlyingType();
3253 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3254 OldTy = VD->getType();
3255 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003256 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003257 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003258 return;
3259 }
Eli Friedman73397492009-03-03 06:41:03 +00003260
John McCall183700f2009-09-21 23:43:11 +00003261 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003262 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3263 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003264 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003265 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3266 } else if (ComplexMode) {
3267 if (!OldTy->isComplexType())
3268 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3269 } else {
3270 if (!OldTy->isFloatingType())
3271 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3272 }
3273
Mike Stump390b4cc2009-05-16 07:39:55 +00003274 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3275 // and friends, at least with glibc.
3276 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3277 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003278 // FIXME: Make sure floating-point mappings are accurate
3279 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003280 QualType NewTy;
3281 switch (DestWidth) {
3282 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003283 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003284 return;
3285 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003286 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003287 return;
3288 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003289 if (!IntegerMode) {
3290 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3291 return;
3292 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003293 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003294 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003295 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003296 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003297 break;
3298 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003299 if (!IntegerMode) {
3300 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3301 return;
3302 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003303 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003304 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003305 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003306 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003307 break;
3308 case 32:
3309 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003310 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003311 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003312 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003313 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003314 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003315 break;
3316 case 64:
3317 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003318 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003319 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003320 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003321 NewTy = S.Context.LongTy;
3322 else
3323 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003324 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003325 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003326 NewTy = S.Context.UnsignedLongTy;
3327 else
3328 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003329 break;
Eli Friedman73397492009-03-03 06:41:03 +00003330 case 96:
3331 NewTy = S.Context.LongDoubleTy;
3332 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003333 case 128:
3334 if (!IntegerMode) {
3335 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3336 return;
3337 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00003338 if (OldTy->isSignedIntegerType())
3339 NewTy = S.Context.Int128Ty;
3340 else
3341 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00003342 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003343 }
3344
Eli Friedman73397492009-03-03 06:41:03 +00003345 if (ComplexMode) {
3346 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003347 }
3348
3349 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00003350 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00003351 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00003352 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00003353 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003354 cast<ValueDecl>(D)->setType(NewTy);
3355}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003356
Chandler Carruth1b03c872011-07-02 00:01:44 +00003357static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003358 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003359 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003360 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003361
Nick Lewycky78d1a102012-07-24 01:40:49 +00003362 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3363 if (!VD->hasGlobalStorage())
3364 S.Diag(Attr.getLoc(),
3365 diag::warn_attribute_requires_functions_or_static_globals)
3366 << Attr.getName();
3367 } else if (!isFunctionOrMethod(D)) {
3368 S.Diag(Attr.getLoc(),
3369 diag::warn_attribute_requires_functions_or_static_globals)
3370 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003371 return;
3372 }
Mike Stumpbf916502009-07-24 19:02:52 +00003373
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003374 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00003375}
3376
Chandler Carruth1b03c872011-07-02 00:01:44 +00003377static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003378 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003379 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003380 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003381
Mike Stumpbf916502009-07-24 19:02:52 +00003382
Chandler Carruth87c44602011-07-01 23:49:12 +00003383 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003384 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003385 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003386 return;
3387 }
Mike Stumpbf916502009-07-24 19:02:52 +00003388
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003389 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003390}
3391
Chandler Carruth1b03c872011-07-02 00:01:44 +00003392static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3393 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003394 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003395 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003396 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003397
Chris Lattner7255a2d2010-06-22 00:03:40 +00003398
Chandler Carruth87c44602011-07-01 23:49:12 +00003399 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003400 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003401 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003402 return;
3403 }
3404
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003405 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003406 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003407}
3408
Chandler Carruth1b03c872011-07-02 00:01:44 +00003409static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003410 if (S.LangOpts.CUDA) {
3411 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003412 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003413 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3414 return;
3415 }
3416
Chandler Carruth87c44602011-07-01 23:49:12 +00003417 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003418 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003419 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003420 return;
3421 }
3422
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003423 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003424 } else {
3425 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3426 }
3427}
3428
Chandler Carruth1b03c872011-07-02 00:01:44 +00003429static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003430 if (S.LangOpts.CUDA) {
3431 // check the attribute arguments.
3432 if (Attr.getNumArgs() != 0) {
3433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3434 return;
3435 }
3436
Chandler Carruth87c44602011-07-01 23:49:12 +00003437 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003439 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003440 return;
3441 }
3442
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003443 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003444 } else {
3445 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3446 }
3447}
3448
Chandler Carruth1b03c872011-07-02 00:01:44 +00003449static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003450 if (S.LangOpts.CUDA) {
3451 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003452 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003453 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003454
Chandler Carruth87c44602011-07-01 23:49:12 +00003455 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003456 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003457 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003458 return;
3459 }
3460
Chandler Carruth87c44602011-07-01 23:49:12 +00003461 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003462 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003463 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003464 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3465 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3466 << FD->getType()
3467 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3468 "void");
3469 } else {
3470 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3471 << FD->getType();
3472 }
3473 return;
3474 }
3475
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003476 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003477 } else {
3478 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3479 }
3480}
3481
Chandler Carruth1b03c872011-07-02 00:01:44 +00003482static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003483 if (S.LangOpts.CUDA) {
3484 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003485 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003486 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003487
Peter Collingbourneced76712010-12-01 03:15:31 +00003488
Chandler Carruth87c44602011-07-01 23:49:12 +00003489 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003490 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003491 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003492 return;
3493 }
3494
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003495 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003496 } else {
3497 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3498 }
3499}
3500
Chandler Carruth1b03c872011-07-02 00:01:44 +00003501static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003502 if (S.LangOpts.CUDA) {
3503 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003504 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003505 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003506
Peter Collingbourneced76712010-12-01 03:15:31 +00003507
Chandler Carruth87c44602011-07-01 23:49:12 +00003508 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003509 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003510 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003511 return;
3512 }
3513
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003514 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00003515 } else {
3516 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3517 }
3518}
3519
Chandler Carruth1b03c872011-07-02 00:01:44 +00003520static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003521 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003522 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003523 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003524
Chandler Carruth87c44602011-07-01 23:49:12 +00003525 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003526 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003527 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003528 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003529 return;
3530 }
Mike Stumpbf916502009-07-24 19:02:52 +00003531
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003532 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003533 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003534 return;
3535 }
Mike Stumpbf916502009-07-24 19:02:52 +00003536
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003537 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003538}
3539
Chandler Carruth1b03c872011-07-02 00:01:44 +00003540static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003541 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003542
Chandler Carruth87c44602011-07-01 23:49:12 +00003543 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003544 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3545 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003546 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003547 return;
3548
Chandler Carruth87c44602011-07-01 23:49:12 +00003549 if (!isa<ObjCMethodDecl>(D)) {
3550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3551 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003552 return;
3553 }
3554
Chandler Carruth87c44602011-07-01 23:49:12 +00003555 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003556 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003557 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003558 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003559 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003560 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003561 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003562 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003563 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003564 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003565 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003566 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003567 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003568 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003569 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003570 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003571 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003572 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003573 switch (CC) {
3574 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003575 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003576 break;
3577 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003578 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003579 break;
3580 default:
3581 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003582 }
3583
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003584 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003585 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003586 default:
3587 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003588 }
3589}
3590
Chandler Carruth1b03c872011-07-02 00:01:44 +00003591static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003592 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003593 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003594}
3595
John McCall711c52b2011-01-05 12:14:39 +00003596bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3597 if (attr.isInvalid())
3598 return true;
3599
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003600 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3601 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3602 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00003603 attr.setInvalid();
3604 return true;
3605 }
3606
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003607 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3608 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003609 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003610 case AttributeList::AT_CDecl: CC = CC_C; break;
3611 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3612 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3613 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3614 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3615 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003616 Expr *Arg = attr.getArg(0);
3617 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003618 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003619 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3620 << "pcs" << 1;
3621 attr.setInvalid();
3622 return true;
3623 }
3624
Chris Lattner5f9e2722011-07-23 10:55:15 +00003625 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003626 if (StrRef == "aapcs") {
3627 CC = CC_AAPCS;
3628 break;
3629 } else if (StrRef == "aapcs-vfp") {
3630 CC = CC_AAPCS_VFP;
3631 break;
3632 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003633
3634 attr.setInvalid();
3635 Diag(attr.getLoc(), diag::err_invalid_pcs);
3636 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003637 }
David Blaikie7530c032012-01-17 06:56:22 +00003638 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003639 }
3640
3641 return false;
3642}
3643
Chandler Carruth1b03c872011-07-02 00:01:44 +00003644static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003645 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003646
3647 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003648 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003649 return;
3650
Chandler Carruth87c44602011-07-01 23:49:12 +00003651 if (!isa<ObjCMethodDecl>(D)) {
3652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3653 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003654 return;
3655 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003656
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003657 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003658}
3659
3660/// Checks a regparm attribute, returning true if it is ill-formed and
3661/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003662bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3663 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003664 return true;
3665
Chandler Carruth87c44602011-07-01 23:49:12 +00003666 if (Attr.getNumArgs() != 1) {
3667 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3668 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003669 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003670 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003671
Chandler Carruth87c44602011-07-01 23:49:12 +00003672 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003673 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003674 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003675 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003676 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003677 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003678 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003679 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003680 }
3681
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003682 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003683 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003684 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003685 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003686 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003687 }
3688
John McCall711c52b2011-01-05 12:14:39 +00003689 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003690 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003691 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003692 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003693 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003694 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003695 }
3696
John McCall711c52b2011-01-05 12:14:39 +00003697 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003698}
3699
Chandler Carruth1b03c872011-07-02 00:01:44 +00003700static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003701 if (S.LangOpts.CUDA) {
3702 // check the attribute arguments.
3703 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003704 // FIXME: 0 is not okay.
3705 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003706 return;
3707 }
3708
Chandler Carruth87c44602011-07-01 23:49:12 +00003709 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003710 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003711 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003712 return;
3713 }
3714
3715 Expr *MaxThreadsExpr = Attr.getArg(0);
3716 llvm::APSInt MaxThreads(32);
3717 if (MaxThreadsExpr->isTypeDependent() ||
3718 MaxThreadsExpr->isValueDependent() ||
3719 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3720 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3721 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3722 return;
3723 }
3724
3725 llvm::APSInt MinBlocks(32);
3726 if (Attr.getNumArgs() > 1) {
3727 Expr *MinBlocksExpr = Attr.getArg(1);
3728 if (MinBlocksExpr->isTypeDependent() ||
3729 MinBlocksExpr->isValueDependent() ||
3730 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3731 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3732 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3733 return;
3734 }
3735 }
3736
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003737 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003738 MaxThreads.getZExtValue(),
3739 MinBlocks.getZExtValue()));
3740 } else {
3741 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3742 }
3743}
3744
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00003745static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3746 const AttributeList &Attr) {
3747 StringRef AttrName = Attr.getName()->getName();
3748 if (!Attr.getParameterName()) {
3749 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3750 << Attr.getName() << /* arg num = */ 1;
3751 return;
3752 }
3753
3754 if (Attr.getNumArgs() != 2) {
3755 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3756 << /* required args = */ 3;
3757 return;
3758 }
3759
3760 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3761
3762 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3763 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3764 << Attr.getName() << ExpectedFunctionOrMethod;
3765 return;
3766 }
3767
3768 uint64_t ArgumentIdx;
3769 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3770 Attr.getLoc(), 2,
3771 Attr.getArg(0), ArgumentIdx))
3772 return;
3773
3774 uint64_t TypeTagIdx;
3775 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3776 Attr.getLoc(), 3,
3777 Attr.getArg(1), TypeTagIdx))
3778 return;
3779
3780 bool IsPointer = (AttrName == "pointer_with_type_tag");
3781 if (IsPointer) {
3782 // Ensure that buffer has a pointer type.
3783 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3784 if (!BufferTy->isPointerType()) {
3785 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3786 << AttrName;
3787 }
3788 }
3789
3790 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3791 S.Context,
3792 ArgumentKind,
3793 ArgumentIdx,
3794 TypeTagIdx,
3795 IsPointer));
3796}
3797
3798static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3799 const AttributeList &Attr) {
3800 IdentifierInfo *PointerKind = Attr.getParameterName();
3801 if (!PointerKind) {
3802 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3803 << "type_tag_for_datatype" << 1;
3804 return;
3805 }
3806
3807 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3808
3809 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3810 Attr.getRange(),
3811 S.Context,
3812 PointerKind,
3813 MatchingCType,
3814 Attr.getLayoutCompatible(),
3815 Attr.getMustBeNull()));
3816}
3817
Chris Lattner0744e5f2008-06-29 00:23:49 +00003818//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003819// Checker-specific attribute handlers.
3820//===----------------------------------------------------------------------===//
3821
John McCallc7ad3812011-01-25 03:31:58 +00003822static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003823 return type->isDependentType() ||
3824 type->isObjCObjectPointerType() ||
3825 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003826}
3827static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003828 return type->isDependentType() ||
3829 type->isPointerType() ||
3830 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003831}
3832
Chandler Carruth1b03c872011-07-02 00:01:44 +00003833static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003834 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003835 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003836 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003837 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003838 return;
3839 }
3840
3841 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00003842 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003843 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3844 cf = false;
3845 } else {
3846 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3847 cf = true;
3848 }
3849
3850 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003851 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003852 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003853 return;
3854 }
3855
3856 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003857 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003858 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003859 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003860}
3861
Chandler Carruth1b03c872011-07-02 00:01:44 +00003862static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3863 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003864 if (!isa<ObjCMethodDecl>(D)) {
3865 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003866 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003867 return;
3868 }
3869
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003870 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003871}
3872
Chandler Carruth1b03c872011-07-02 00:01:44 +00003873static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3874 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003875
John McCallc7ad3812011-01-25 03:31:58 +00003876 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003877
Chandler Carruth87c44602011-07-01 23:49:12 +00003878 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003879 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003880 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00003881 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00003882 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00003883 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3884 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003885 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003886 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003887 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003888 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003889 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003890 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003891 return;
3892 }
Mike Stumpbf916502009-07-24 19:02:52 +00003893
John McCallc7ad3812011-01-25 03:31:58 +00003894 bool typeOK;
3895 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003896 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003897 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003898 case AttributeList::AT_NSReturnsAutoreleased:
3899 case AttributeList::AT_NSReturnsRetained:
3900 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003901 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3902 cf = false;
3903 break;
3904
Sean Hunt8e083e72012-06-19 23:57:03 +00003905 case AttributeList::AT_CFReturnsRetained:
3906 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00003907 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3908 cf = true;
3909 break;
3910 }
3911
3912 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003913 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003914 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003915 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003916 }
Mike Stumpbf916502009-07-24 19:02:52 +00003917
Chandler Carruth87c44602011-07-01 23:49:12 +00003918 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003919 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003920 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00003921 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003922 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003923 S.Context));
3924 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003925 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003926 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003927 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003928 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003929 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003930 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003931 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003932 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003933 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003934 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003935 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003936 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003937 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003938 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003939 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003940 return;
3941 };
3942}
3943
John McCalldc7c5ad2011-07-22 08:53:00 +00003944static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3945 const AttributeList &attr) {
3946 SourceLocation loc = attr.getLoc();
3947
3948 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3949
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003950 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003951 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003952 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003953 return;
3954 }
3955
3956 // Check that the method returns a normal pointer.
3957 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003958
3959 if (!resultType->isReferenceType() &&
3960 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003961 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3962 << SourceRange(loc)
3963 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3964
3965 // Drop the attribute.
3966 return;
3967 }
3968
3969 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003970 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003971}
3972
Fariborz Jahanian84101132012-09-07 23:46:23 +00003973static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3974 const AttributeList &attr) {
3975 SourceLocation loc = attr.getLoc();
3976 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3977
3978 if (!method) {
3979 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3980 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
3981 return;
3982 }
3983 DeclContext *DC = method->getDeclContext();
3984 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3985 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3986 << attr.getName() << 0;
3987 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3988 return;
3989 }
3990 if (method->getMethodFamily() == OMF_dealloc) {
3991 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3992 << attr.getName() << 1;
3993 return;
3994 }
3995
3996 method->addAttr(
3997 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
3998}
3999
John McCall8dfac0b2011-09-30 05:12:12 +00004000/// Handle cf_audited_transfer and cf_unknown_transfer.
4001static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4002 if (!isa<FunctionDecl>(D)) {
4003 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004004 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004005 return;
4006 }
4007
Sean Hunt8e083e72012-06-19 23:57:03 +00004008 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004009
4010 // Check whether there's a conflicting attribute already present.
4011 Attr *Existing;
4012 if (IsAudited) {
4013 Existing = D->getAttr<CFUnknownTransferAttr>();
4014 } else {
4015 Existing = D->getAttr<CFAuditedTransferAttr>();
4016 }
4017 if (Existing) {
4018 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4019 << A.getName()
4020 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4021 << A.getRange() << Existing->getRange();
4022 return;
4023 }
4024
4025 // All clear; add the attribute.
4026 if (IsAudited) {
4027 D->addAttr(
4028 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4029 } else {
4030 D->addAttr(
4031 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4032 }
4033}
4034
John McCallfe98da02011-09-29 07:17:38 +00004035static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4036 const AttributeList &Attr) {
4037 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4038 if (!RD || RD->isUnion()) {
4039 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004040 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004041 }
4042
4043 IdentifierInfo *ParmName = Attr.getParameterName();
4044
4045 // In Objective-C, verify that the type names an Objective-C type.
4046 // We don't want to check this outside of ObjC because people sometimes
4047 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004048 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004049 // Check for an existing type with this name.
4050 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4051 Sema::LookupOrdinaryName);
4052 if (S.LookupName(R, Sc)) {
4053 NamedDecl *Target = R.getFoundDecl();
4054 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4055 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4056 S.Diag(Target->getLocStart(), diag::note_declared_at);
4057 }
4058 }
4059 }
4060
4061 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4062 ParmName));
4063}
4064
Chandler Carruth1b03c872011-07-02 00:01:44 +00004065static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4066 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004067 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004068
Chandler Carruth87c44602011-07-01 23:49:12 +00004069 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004070 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004071}
4072
Chandler Carruth1b03c872011-07-02 00:01:44 +00004073static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4074 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004075 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004076 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004077 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004078 return;
4079 }
4080
Chandler Carruth87c44602011-07-01 23:49:12 +00004081 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004082 QualType type = vd->getType();
4083
4084 if (!type->isDependentType() &&
4085 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004086 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004087 << type;
4088 return;
4089 }
4090
4091 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4092
4093 // If we have no lifetime yet, check the lifetime we're presumably
4094 // going to infer.
4095 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4096 lifetime = type->getObjCARCImplicitLifetime();
4097
4098 switch (lifetime) {
4099 case Qualifiers::OCL_None:
4100 assert(type->isDependentType() &&
4101 "didn't infer lifetime for non-dependent type?");
4102 break;
4103
4104 case Qualifiers::OCL_Weak: // meaningful
4105 case Qualifiers::OCL_Strong: // meaningful
4106 break;
4107
4108 case Qualifiers::OCL_ExplicitNone:
4109 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004110 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004111 << (lifetime == Qualifiers::OCL_Autoreleasing);
4112 break;
4113 }
4114
Chandler Carruth87c44602011-07-01 23:49:12 +00004115 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004116 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00004117}
4118
Francois Pichet11542142010-12-19 06:50:37 +00004119//===----------------------------------------------------------------------===//
4120// Microsoft specific attribute handlers.
4121//===----------------------------------------------------------------------===//
4122
Chandler Carruth1b03c872011-07-02 00:01:44 +00004123static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004124 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00004125 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00004126 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00004127 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004128
Francois Pichet11542142010-12-19 06:50:37 +00004129 Expr *Arg = Attr.getArg(0);
4130 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004131 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004132 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4133 << "uuid" << 1;
4134 return;
4135 }
4136
Chris Lattner5f9e2722011-07-23 10:55:15 +00004137 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004138
4139 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4140 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004141
Francois Pichetd3d3be92010-12-20 01:41:49 +00004142 // Validate GUID length.
4143 if (IsCurly && StrRef.size() != 38) {
4144 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4145 return;
4146 }
4147 if (!IsCurly && StrRef.size() != 36) {
4148 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4149 return;
4150 }
4151
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004152 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00004153 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00004154 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00004155 if (IsCurly) // Skip the optional '{'
4156 ++I;
4157
4158 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004159 if (i == 8 || i == 13 || i == 18 || i == 23) {
4160 if (*I != '-') {
4161 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4162 return;
4163 }
4164 } else if (!isxdigit(*I)) {
4165 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4166 return;
4167 }
4168 I++;
4169 }
Francois Pichet11542142010-12-19 06:50:37 +00004170
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004171 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00004172 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00004173 } else
Francois Pichet11542142010-12-19 06:50:37 +00004174 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00004175}
4176
John McCallc052dbb2012-05-22 21:28:12 +00004177static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Joao Matos679fc932012-09-04 17:18:12 +00004178 if (S.LangOpts.MicrosoftExt) {
4179 AttributeList::Kind Kind = Attr.getKind();
4180 if (Kind == AttributeList::AT_SingleInheritance)
4181 D->addAttr(
4182 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4183 else if (Kind == AttributeList::AT_MultipleInheritance)
4184 D->addAttr(
4185 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4186 else if (Kind == AttributeList::AT_VirtualInheritance)
4187 D->addAttr(
4188 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
4189 } else
John McCallc052dbb2012-05-22 21:28:12 +00004190 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4191}
4192
4193static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4194 if (S.LangOpts.MicrosoftExt) {
4195 AttributeList::Kind Kind = Attr.getKind();
Sean Hunt8e083e72012-06-19 23:57:03 +00004196 if (Kind == AttributeList::AT_Ptr32)
John McCallc052dbb2012-05-22 21:28:12 +00004197 D->addAttr(
4198 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004199 else if (Kind == AttributeList::AT_Ptr64)
John McCallc052dbb2012-05-22 21:28:12 +00004200 D->addAttr(
4201 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Sean Hunt8e083e72012-06-19 23:57:03 +00004202 else if (Kind == AttributeList::AT_Win64)
John McCallc052dbb2012-05-22 21:28:12 +00004203 D->addAttr(
4204 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4205 } else
4206 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4207}
4208
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004209static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4210 if (S.LangOpts.MicrosoftExt)
4211 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4212 else
4213 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4214}
4215
Ted Kremenekb71368d2009-05-09 02:44:38 +00004216//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004217// Top Level Sema Entry Points
4218//===----------------------------------------------------------------------===//
4219
Chandler Carruth1b03c872011-07-02 00:01:44 +00004220static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4221 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00004222 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004223 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4224 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4225 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00004226 default:
4227 break;
4228 }
4229}
Abramo Bagnarae215f722010-04-30 13:10:51 +00004230
Chandler Carruth1b03c872011-07-02 00:01:44 +00004231static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4232 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00004233 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004234 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4235 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4236 case AttributeList::AT_IBOutletCollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004237 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004238 case AttributeList::AT_AddressSpace:
4239 case AttributeList::AT_OpenCLImageAccess:
4240 case AttributeList::AT_ObjCGC:
4241 case AttributeList::AT_VectorSize:
4242 case AttributeList::AT_NeonVectorType:
4243 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpbf916502009-07-24 19:02:52 +00004244 // Ignore these, these are type attributes, handled by
4245 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004246 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004247 case AttributeList::AT_CUDADevice:
4248 case AttributeList::AT_CUDAHost:
4249 case AttributeList::AT_Overloadable:
Peter Collingbourne60700392011-01-21 02:08:45 +00004250 // Ignore, this is a non-inheritable attribute, handled
4251 // by ProcessNonInheritableDeclAttr.
4252 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004253 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4254 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4255 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4256 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004257 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004258 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004259 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004260 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004261 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4262 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4263 case AttributeList::AT_CarriesDependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004264 handleDependencyAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004265 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4266 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4267 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4268 case AttributeList::AT_Deprecated:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004269 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4270 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004271 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4272 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004273 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004274 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004275 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4276 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4277 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4278 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4279 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004280 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004281 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004282 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4283 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4284 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4285 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4286 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004287 case AttributeList::AT_ownership_returns:
4288 case AttributeList::AT_ownership_takes:
4289 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004290 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004291 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4292 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4293 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4294 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4295 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4296 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4297 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004298
Sean Hunt8e083e72012-06-19 23:57:03 +00004299 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004300 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004301 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004302 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004303
Sean Hunt8e083e72012-06-19 23:57:03 +00004304 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004305 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4306
Fariborz Jahanian84101132012-09-07 23:46:23 +00004307 case AttributeList::AT_ObjCRequiresSuper:
4308 handleObjCRequiresSuperAttr(S, D, Attr); break;
4309
Sean Hunt8e083e72012-06-19 23:57:03 +00004310 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004311 handleNSBridgedAttr(S, scope, D, Attr); break;
4312
Sean Hunt8e083e72012-06-19 23:57:03 +00004313 case AttributeList::AT_CFAuditedTransfer:
4314 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004315 handleCFTransferAttr(S, D, Attr); break;
4316
Ted Kremenekb71368d2009-05-09 02:44:38 +00004317 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004318 case AttributeList::AT_CFConsumed:
4319 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4320 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004321 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004322
Sean Hunt8e083e72012-06-19 23:57:03 +00004323 case AttributeList::AT_NSReturnsAutoreleased:
4324 case AttributeList::AT_NSReturnsNotRetained:
4325 case AttributeList::AT_CFReturnsNotRetained:
4326 case AttributeList::AT_NSReturnsRetained:
4327 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004328 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004329
Tanya Lattner0df579e2012-07-09 22:06:01 +00004330 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004331 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004332 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004333
Sean Hunt8e083e72012-06-19 23:57:03 +00004334 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004335 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004336
Sean Hunt8e083e72012-06-19 23:57:03 +00004337 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4338 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4339 case AttributeList::AT_Unavailable:
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004340 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4341 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004342 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004343 handleArcWeakrefUnavailableAttr (S, D, Attr);
4344 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004345 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004346 handleObjCRootClassAttr(S, D, Attr);
4347 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004348 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004349 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004350 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004351 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4352 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004353 handleReturnsTwiceAttr(S, D, Attr);
4354 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004355 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4356 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4357 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004358 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004359 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4360 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4361 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4362 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004363 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004364 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004365 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004366 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004367 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004368 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004369 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004370 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004371 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4372 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4373 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4374 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4375 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4376 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4377 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4378 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4379 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004380 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004381 // Just ignore
4382 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004383 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004384 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004385 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004386 case AttributeList::AT_StdCall:
4387 case AttributeList::AT_CDecl:
4388 case AttributeList::AT_FastCall:
4389 case AttributeList::AT_ThisCall:
4390 case AttributeList::AT_Pascal:
4391 case AttributeList::AT_Pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004392 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004393 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004394 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004395 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004396 break;
John McCallc052dbb2012-05-22 21:28:12 +00004397
4398 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004399 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004400 handleMsStructAttr(S, D, Attr);
4401 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004402 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004403 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004404 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004405 case AttributeList::AT_SingleInheritance:
4406 case AttributeList::AT_MultipleInheritance:
4407 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004408 handleInheritanceAttr(S, D, Attr);
4409 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004410 case AttributeList::AT_Win64:
4411 case AttributeList::AT_Ptr32:
4412 case AttributeList::AT_Ptr64:
John McCallc052dbb2012-05-22 21:28:12 +00004413 handlePortabilityAttr(S, D, Attr);
4414 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004415 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004416 handleForceInlineAttr(S, D, Attr);
4417 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004418
4419 // Thread safety attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004420 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004421 handleGuardedVarAttr(S, D, Attr);
4422 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004423 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004424 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004425 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004426 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004427 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004428 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004429 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany71efba02012-01-24 19:25:38 +00004430 handleNoAddressSafetyAttr(S, D, Attr);
4431 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004432 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004433 handleNoThreadSafetyAttr(S, D, Attr);
4434 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004435 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004436 handleLockableAttr(S, D, Attr);
4437 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004438 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004439 handleGuardedByAttr(S, D, Attr);
4440 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004441 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004442 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004443 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004444 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004445 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004446 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004447 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004448 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004449 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004450 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004451 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004452 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004453 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004454 handleLockReturnedAttr(S, D, Attr);
4455 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004456 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004457 handleLocksExcludedAttr(S, D, Attr);
4458 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004459 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004460 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004461 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004462 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004463 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004464 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004465 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004466 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004467 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004468 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004469 handleUnlockFunAttr(S, D, Attr);
4470 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004471 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004472 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004473 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004474 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004475 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004476 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004477
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004478 // Type safety attributes.
4479 case AttributeList::AT_ArgumentWithTypeTag:
4480 handleArgumentWithTypeTagAttr(S, D, Attr);
4481 break;
4482 case AttributeList::AT_TypeTagForDatatype:
4483 handleTypeTagForDatatypeAttr(S, D, Attr);
4484 break;
4485
Chris Lattner803d0802008-06-29 00:43:07 +00004486 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004487 // Ask target about the attribute.
4488 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4489 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004490 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4491 diag::warn_unhandled_ms_attribute_ignored :
4492 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004493 break;
4494 }
4495}
4496
Peter Collingbourne60700392011-01-21 02:08:45 +00004497/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4498/// the attribute applies to decls. If the attribute is a type attribute, just
4499/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4500/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00004501static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4502 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00004503 bool NonInheritable, bool Inheritable) {
4504 if (Attr.isInvalid())
4505 return;
4506
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004507 // Type attributes are still treated as declaration attributes by
4508 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4509 // want to process them, however, because we will simply warn about ignoring
4510 // them. So instead, we will bail out early.
4511 if (Attr.isMSTypespecAttribute())
Peter Collingbourne60700392011-01-21 02:08:45 +00004512 return;
4513
4514 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004515 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004516
4517 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00004518 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00004519}
4520
Chris Lattner803d0802008-06-29 00:43:07 +00004521/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4522/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004523void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004524 const AttributeList *AttrList,
4525 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004526 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00004527 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola9b79fc92012-05-07 23:58:18 +00004528 }
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004529
4530 // GCC accepts
4531 // static int a9 __attribute__((weakref));
4532 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00004533 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004534 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004535 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004536 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004537 }
4538}
4539
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004540// Annotation attributes are the only attributes allowed after an access
4541// specifier.
4542bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4543 const AttributeList *AttrList) {
4544 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004545 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004546 handleAnnotateAttr(*this, ASDecl, *l);
4547 } else {
4548 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4549 return true;
4550 }
4551 }
4552
4553 return false;
4554}
4555
John McCalle82247a2011-10-01 05:17:03 +00004556/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4557/// contains any decl attributes that we should warn about.
4558static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4559 for ( ; A; A = A->getNext()) {
4560 // Only warn if the attribute is an unignored, non-type attribute.
4561 if (A->isUsedAsTypeAttr()) continue;
4562 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4563
4564 if (A->getKind() == AttributeList::UnknownAttribute) {
4565 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4566 << A->getName() << A->getRange();
4567 } else {
4568 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4569 << A->getName() << A->getRange();
4570 }
4571 }
4572}
4573
4574/// checkUnusedDeclAttributes - Given a declarator which is not being
4575/// used to build a declaration, complain about any decl attributes
4576/// which might be lying around on it.
4577void Sema::checkUnusedDeclAttributes(Declarator &D) {
4578 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4579 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4580 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4581 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4582}
4583
Ryan Flynne25ff832009-07-30 03:15:39 +00004584/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004585/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004586NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4587 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004588 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004589 NamedDecl *NewD = 0;
4590 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004591 FunctionDecl *NewFD;
4592 // FIXME: Missing call to CheckFunctionDeclaration().
4593 // FIXME: Mangling?
4594 // FIXME: Is the qualifier info correct?
4595 // FIXME: Is the DeclContext correct?
4596 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4597 Loc, Loc, DeclarationName(II),
4598 FD->getType(), FD->getTypeSourceInfo(),
4599 SC_None, SC_None,
4600 false/*isInlineSpecified*/,
4601 FD->hasPrototype(),
4602 false/*isConstexprSpecified*/);
4603 NewD = NewFD;
4604
4605 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004606 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004607
4608 // Fake up parameter variables; they are declared as if this were
4609 // a typedef.
4610 QualType FDTy = FD->getType();
4611 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4612 SmallVector<ParmVarDecl*, 16> Params;
4613 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4614 AE = FT->arg_type_end(); AI != AE; ++AI) {
4615 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4616 Param->setScopeInfo(0, Params.size());
4617 Params.push_back(Param);
4618 }
David Blaikie4278c652011-09-21 18:16:56 +00004619 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004620 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004621 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4622 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004623 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004624 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00004625 VD->getStorageClass(),
4626 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00004627 if (VD->getQualifier()) {
4628 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004629 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004630 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004631 }
4632 return NewD;
4633}
4634
James Dennett1dfbd922012-06-14 21:40:34 +00004635/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004636/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004637void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004638 if (W.getUsed()) return; // only do this once
4639 W.setUsed(true);
4640 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4641 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00004642 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00004643 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4644 NDId->getName()));
4645 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004646 WeakTopLevelDecl.push_back(NewD);
4647 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4648 // to insert Decl at TU scope, sorry.
4649 DeclContext *SavedContext = CurContext;
4650 CurContext = Context.getTranslationUnitDecl();
4651 PushOnScopeChains(NewD, S);
4652 CurContext = SavedContext;
4653 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00004654 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00004655 }
4656}
4657
Chris Lattner0744e5f2008-06-29 00:23:49 +00004658/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4659/// it, apply them to D. This is a bit tricky because PD can have attributes
4660/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00004661void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4662 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00004663 // It's valid to "forward-declare" #pragma weak, in which case we
4664 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00004665 if (Inheritable) {
4666 LoadExternalWeakUndeclaredIdentifiers();
4667 if (!WeakUndeclaredIdentifiers.empty()) {
4668 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4669 if (IdentifierInfo *Id = ND->getIdentifier()) {
4670 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4671 = WeakUndeclaredIdentifiers.find(Id);
4672 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4673 WeakInfo W = I->second;
4674 DeclApplyPragmaWeak(S, ND, W);
4675 WeakUndeclaredIdentifiers[Id] = W;
4676 }
John McCalld4aff0e2010-10-27 00:59:00 +00004677 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004678 }
4679 }
4680 }
4681
Chris Lattner0744e5f2008-06-29 00:23:49 +00004682 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00004683 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00004684 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004685
Chris Lattner0744e5f2008-06-29 00:23:49 +00004686 // Walk the declarator structure, applying decl attributes that were in a type
4687 // position to the decl itself. This handles cases like:
4688 // int *__attr__(x)** D;
4689 // when X is a decl attribute.
4690 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4691 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004692 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004693
Chris Lattner0744e5f2008-06-29 00:23:49 +00004694 // Finally, apply any attributes on the decl itself.
4695 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004696 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004697}
John McCall54abf7d2009-11-04 02:18:39 +00004698
John McCallf85e1932011-06-15 23:02:42 +00004699/// Is the given declaration allowed to use a forbidden type?
4700static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4701 // Private ivars are always okay. Unfortunately, people don't
4702 // always properly make their ivars private, even in system headers.
4703 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004704 // Function declarations in sys headers will be marked unavailable.
4705 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4706 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004707 return false;
4708
4709 // Require it to be declared in a system header.
4710 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4711}
4712
4713/// Handle a delayed forbidden-type diagnostic.
4714static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4715 Decl *decl) {
4716 if (decl && isForbiddenTypeAllowed(S, decl)) {
4717 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4718 "this system declaration uses an unsupported type"));
4719 return;
4720 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004721 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004722 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00004723 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004724 // kind of forbidden type messages on unavailable functions.
4725 if (FD->hasAttr<UnavailableAttr>() &&
4726 diag.getForbiddenTypeDiagnostic() ==
4727 diag::err_arc_array_param_no_ownership) {
4728 diag.Triggered = true;
4729 return;
4730 }
4731 }
John McCallf85e1932011-06-15 23:02:42 +00004732
4733 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4734 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4735 diag.Triggered = true;
4736}
4737
John McCall92576642012-05-07 06:16:41 +00004738void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4739 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00004740 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00004741 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00004742
John McCall92576642012-05-07 06:16:41 +00004743 // When delaying diagnostics to run in the context of a parsed
4744 // declaration, we only want to actually emit anything if parsing
4745 // succeeds.
4746 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00004747
John McCall92576642012-05-07 06:16:41 +00004748 // We emit all the active diagnostics in this pool or any of its
4749 // parents. In general, we'll get one pool for the decl spec
4750 // and a child pool for each declarator; in a decl group like:
4751 // deprecated_typedef foo, *bar, baz();
4752 // only the declarator pops will be passed decls. This is correct;
4753 // we really do need to consider delayed diagnostics from the decl spec
4754 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00004755 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00004756 do {
John McCall13489672012-05-07 06:16:58 +00004757 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00004758 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4759 // This const_cast is a bit lame. Really, Triggered should be mutable.
4760 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00004761 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004762 continue;
4763
John McCalleee1d542011-02-14 07:13:47 +00004764 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004765 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004766 // Don't bother giving deprecation diagnostics if the decl is invalid.
4767 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00004768 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004769 break;
4770
4771 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00004772 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004773 break;
John McCallf85e1932011-06-15 23:02:42 +00004774
4775 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00004776 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00004777 break;
John McCall2f514482010-01-27 03:50:35 +00004778 }
4779 }
John McCall92576642012-05-07 06:16:41 +00004780 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00004781}
4782
John McCall13489672012-05-07 06:16:58 +00004783/// Given a set of delayed diagnostics, re-emit them as if they had
4784/// been delayed in the current context instead of in the given pool.
4785/// Essentially, this just moves them to the current pool.
4786void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4787 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4788 assert(curPool && "re-emitting in undelayed context not supported");
4789 curPool->steal(pool);
4790}
4791
John McCall54abf7d2009-11-04 02:18:39 +00004792static bool isDeclDeprecated(Decl *D) {
4793 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004794 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004795 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004796 // A category implicitly has the availability of the interface.
4797 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4798 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004799 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4800 return false;
4801}
4802
Eli Friedmanc3b23082012-08-08 21:52:41 +00004803static void
4804DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4805 SourceLocation Loc,
4806 const ObjCInterfaceDecl *UnknownObjCClass) {
4807 DeclarationName Name = D->getDeclName();
4808 if (!Message.empty()) {
4809 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4810 S.Diag(D->getLocation(),
4811 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4812 : diag::note_previous_decl) << Name;
4813 } else if (!UnknownObjCClass) {
4814 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4815 S.Diag(D->getLocation(),
4816 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4817 : diag::note_previous_decl) << Name;
4818 } else {
4819 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4820 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4821 }
4822}
4823
John McCall9c3087b2010-08-26 02:13:20 +00004824void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004825 Decl *Ctx) {
4826 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004827 return;
4828
John McCall2f514482010-01-27 03:50:35 +00004829 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004830 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4831 DD.getDeprecationMessage(), DD.Loc,
4832 DD.getUnknownObjCClass());
John McCall54abf7d2009-11-04 02:18:39 +00004833}
4834
Chris Lattner5f9e2722011-07-23 10:55:15 +00004835void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004836 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004837 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004838 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004839 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004840 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4841 UnknownObjCClass,
4842 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004843 return;
4844 }
4845
4846 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004847 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004848 return;
Eli Friedmanc3b23082012-08-08 21:52:41 +00004849 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass);
John McCall54abf7d2009-11-04 02:18:39 +00004850}