blob: c8af3a430a2118204ccd6ee9c1809e5c7e6e5654 [file] [log] [blame]
Chris Lattner2c6fcf52008-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 McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000018#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000022#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000024#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000025#include "clang/Sema/Lookup.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000027using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000028using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000029
John McCall5fca7ea2011-03-02 12:29:23 +000030/// These constants match the enumerated choices of
31/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000032enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000033 ExpectedFunction,
34 ExpectedUnion,
35 ExpectedVariableOrFunction,
36 ExpectedFunctionOrMethod,
37 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000038 ExpectedFunctionMethodOrBlock,
John McCall5fca7ea2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000041 ExpectedVariable,
42 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000043 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000044 ExpectedFieldOrGlobalVar,
45 ExpectedStruct
John McCall5fca7ea2011-03-02 12:29:23 +000046};
47
Chris Lattner58418ff2008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000063
Chris Lattner2c6fcf52008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068
John McCall9dd450b2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000070}
71
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000072// FIXME: We should provide an abstraction around a method or function
73// to provide the following bits of information.
74
Nuno Lopes518e3702009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000086}
87
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088/// isFunctionOrMethodOrBlock - Return true if the given decl has function
89/// type (function or function-typed variable) or an Objective-C
90/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000100}
101
John McCall3882ace2011-01-05 12:14:39 +0000102/// Return true if the given decl has a declarator that should have
103/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000108}
109
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000110/// hasFunctionProto - Return true if the given decl has a argument
111/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000118 return true;
119 }
120}
121
122/// getFunctionOrMethodNumArgs - Return number of function or method
123/// arguments. It is an error to call this on a K&R function (use
124/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000131}
132
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000140}
141
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000146}
147
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000154 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000168 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000169
John McCall96fa4842010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000172 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
John McCall96fa4842010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000175
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
Daniel Dunbar980c6692008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000189
Daniel Dunbar980c6692008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000197/// \brief Check if the attribute has exactly as many args as Num. May
198/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000199static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
200 unsigned int Num) {
201 if (Attr.getNumArgs() != Num) {
202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
203 return false;
204 }
205
206 return true;
207}
208
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000209
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000210/// \brief Check if the attribute has at least as many args as Num. May
211/// output an error.
212static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
213 unsigned int Num) {
214 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000215 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
216 return false;
217 }
218
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000219 return true;
220}
221
222///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000223/// \brief Check if passed in Decl is a field or potentially shared global var
224/// \return true if the Decl is a field or potentially shared global variable
225///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000226static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000227 if (isa<FieldDecl>(D))
228 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000229 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000230 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
231
232 return false;
233}
234
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000235/// \brief Check if the passed-in expression is of type int or bool.
236static bool isIntOrBool(Expr *Exp) {
237 QualType QT = Exp->getType();
238 return QT->isBooleanType() || QT->isIntegerType();
239}
240
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000241
242// Check to see if the type is a smart pointer of some kind. We assume
243// it's a smart pointer if it defines both operator-> and operator*.
244static bool threadSafetyCheckIsSmartPointer(Sema &S, const QualType QT) {
245 if (const RecordType *RT = QT->getAs<RecordType>()) {
246 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
247 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
248 if (Res1.first == Res1.second)
249 return false;
250
251 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
252 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
253 if (Res2.first != Res2.second)
254 return true;
255 }
256 return false;
257}
258
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000259///
260/// \brief Check if passed in Decl is a pointer type.
261/// Note that this function may produce an error message.
262/// \return true if the Decl is a pointer type; false otherwise
263///
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000264static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
265 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000266 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000267 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000268 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000269 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000270
271 if (threadSafetyCheckIsSmartPointer(S, QT))
272 return true;
273
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000274 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000275 << Attr.getName()->getName() << QT;
276 } else {
277 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
278 << Attr.getName();
279 }
280 return false;
281}
282
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000283/// \brief Checks that the passed in QualType either is of RecordType or points
284/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000285static const RecordType *getRecordType(QualType QT) {
286 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000287 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000288
289 // Now check if we point to record type.
290 if (const PointerType *PT = QT->getAs<PointerType>())
291 return PT->getPointeeType()->getAs<RecordType>();
292
293 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000294}
295
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000296/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000297/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000298static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
299 QualType Ty) {
300 const RecordType *RT = getRecordType(Ty);
301
302 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000303 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000304 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000305 << Attr.getName() << Ty.getAsString();
306 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000307 }
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000308 // Don't check for lockable if the class hasn't been defined yet.
309 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000310 return;
311 // Warn if the type is not lockable.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000312 if (!RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000313 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000314 << Attr.getName() << Ty.getAsString();
315 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000316 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000317}
318
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000319/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000320/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000321/// \param Sidx The attribute argument index to start checking with.
322/// \param ParamIdxOk Whether an argument can be indexing into a function
323/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000324static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000325 const AttributeList &Attr,
326 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000327 int Sidx = 0,
328 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000329 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000330 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000331
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000332 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000333 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000334 Args.push_back(ArgExp);
335 continue;
336 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000337
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000338 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
339 // Ignore empty strings without warnings
340 if (StrLit->getLength() == 0)
341 continue;
342
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000343 // We allow constant strings to be used as a placeholder for expressions
344 // that are not valid C++ syntax, but warn that they are ignored.
345 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
346 Attr.getName();
347 continue;
348 }
349
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000350 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000351
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000352 // A pointer to member expression of the form &MyClass::mu is treated
353 // specially -- we need to look at the type of the member.
354 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
355 if (UOp->getOpcode() == UO_AddrOf)
356 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
357 if (DRE->getDecl()->isCXXInstanceMember())
358 ArgTy = DRE->getDecl()->getType();
359
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000360 // First see if we can just cast to record type, or point to record type.
361 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000362
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000363 // Now check if we index into a record type function param.
364 if(!RT && ParamIdxOk) {
365 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000366 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
367 if(FD && IL) {
368 unsigned int NumParams = FD->getNumParams();
369 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000370 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
371 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
372 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000373 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
374 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000375 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000376 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000377 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000378 }
379 }
380
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000381 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000382
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000383 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000384 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000385}
386
Chris Lattner58418ff2008-06-29 00:16:31 +0000387//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000388// Attribute Implementations
389//===----------------------------------------------------------------------===//
390
Daniel Dunbar032db472008-07-31 22:40:48 +0000391// FIXME: All this manual attribute parsing code is gross. At the
392// least add some helper functions to check most argument patterns (#
393// and types of args).
394
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000395static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
396 bool pointer = false) {
397 assert(!Attr.isInvalid());
398
399 if (!checkAttributeNumArgs(S, Attr, 0))
400 return;
401
402 // D must be either a member field or global (potentially shared) variable.
403 if (!mayBeSharedVariable(D)) {
404 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000405 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000406 return;
407 }
408
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000409 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000410 return;
411
412 if (pointer)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000413 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000414 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000415 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000416}
417
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000418static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000419 bool pointer = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000420 assert(!Attr.isInvalid());
421
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000422 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000423 return;
424
425 // D must be either a member field or global (potentially shared) variable.
426 if (!mayBeSharedVariable(D)) {
427 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000428 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000429 return;
430 }
431
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000432 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000433 return;
434
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000435 SmallVector<Expr*, 1> Args;
436 // check that all arguments are lockable objects
437 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
438 unsigned Size = Args.size();
439 if (Size != 1)
440 return;
441 Expr *Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000442
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000443 if (pointer)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000444 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000445 S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000446 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000447 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000448}
449
450
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000451static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
452 bool scoped = false) {
453 assert(!Attr.isInvalid());
454
455 if (!checkAttributeNumArgs(S, Attr, 0))
456 return;
457
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000458 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000459 if (!isa<CXXRecordDecl>(D)) {
460 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
461 << Attr.getName() << ExpectedClass;
462 return;
463 }
464
465 if (scoped)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000466 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000467 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000468 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000469}
470
471static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
472 const AttributeList &Attr) {
473 assert(!Attr.isInvalid());
474
475 if (!checkAttributeNumArgs(S, Attr, 0))
476 return;
477
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000478 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000479 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
480 << Attr.getName() << ExpectedFunctionOrMethod;
481 return;
482 }
483
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000484 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000485 S.Context));
486}
487
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000488static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000489 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000490 assert(!Attr.isInvalid());
491
492 if (!checkAttributeNumArgs(S, Attr, 0))
493 return;
494
495 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
496 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
497 << Attr.getName() << ExpectedFunctionOrMethod;
498 return;
499 }
500
501 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
502 S.Context));
503}
504
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000505static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
506 bool before) {
507 assert(!Attr.isInvalid());
508
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000509 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000510 return;
511
512 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000513 ValueDecl *VD = dyn_cast<ValueDecl>(D);
514 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000515 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000517 return;
518 }
519
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000521 QualType QT = VD->getType();
522 if (!QT->isDependentType()) {
523 const RecordType *RT = getRecordType(QT);
524 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000525 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000526 << Attr.getName();
527 return;
528 }
529 }
530
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000531 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000532 // Check that all arguments are lockable objects.
533 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000534 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000535 if (Size == 0)
536 return;
537 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000538
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000539 if (before)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000540 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000541 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000542 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000543 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000544 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000545}
546
547static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000548 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000549 assert(!Attr.isInvalid());
550
551 // zero or more arguments ok
552
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000553 // check that the attribute is applied to a function
554 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000555 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
556 << Attr.getName() << ExpectedFunctionOrMethod;
557 return;
558 }
559
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000560 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000561 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000562 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000563 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000564 Expr **StartArg = Size == 0 ? 0 : &Args[0];
565
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000566 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000567 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000568 S.Context, StartArg,
569 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000570 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000571 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000572 S.Context, StartArg,
573 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000574}
575
576static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000577 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000578 assert(!Attr.isInvalid());
579
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000580 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000581 return;
582
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000583 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
585 << Attr.getName() << ExpectedFunctionOrMethod;
586 return;
587 }
588
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589 if (!isIntOrBool(Attr.getArg(0))) {
590 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
591 << Attr.getName();
592 return;
593 }
594
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000595 SmallVector<Expr*, 2> Args;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000597 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000598 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000599 Expr **StartArg = Size == 0 ? 0 : &Args[0];
600
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000601 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000602 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000603 S.Context,
Caitlin Sadowskibf06c722011-09-15 17:50:19 +0000604 Attr.getArg(0),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000605 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000606 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000607 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowskibf06c722011-09-15 17:50:19 +0000608 S.Context,
609 Attr.getArg(0),
610 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000611}
612
613static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000614 bool exclusive = false) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000615 assert(!Attr.isInvalid());
616
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000617 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000618 return;
619
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000621 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
622 << Attr.getName() << ExpectedFunctionOrMethod;
623 return;
624 }
625
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000626 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000627 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000628 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000629 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000630 if (Size == 0)
631 return;
632 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000633
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000634 if (exclusive)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000635 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000636 S.Context, StartArg,
637 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000638 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000639 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000640 S.Context, StartArg,
641 Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000642}
643
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000644static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000645 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000646 assert(!Attr.isInvalid());
647
648 // zero or more arguments ok
649
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000650 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000651 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
652 << Attr.getName() << ExpectedFunctionOrMethod;
653 return;
654 }
655
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000656 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000657 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000658 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000659 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000660 Expr **StartArg = Size == 0 ? 0 : &Args[0];
661
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000662 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000663 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000664}
665
666static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000667 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000668 assert(!Attr.isInvalid());
669
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000670 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000671 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000672 Expr *Arg = Attr.getArg(0);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000673
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000674 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
676 << Attr.getName() << ExpectedFunctionOrMethod;
677 return;
678 }
679
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000680 if (Arg->isTypeDependent())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000681 return;
682
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000683 // check that the argument is lockable object
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000684 checkForLockableRecord(S, D, Attr, Arg->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000685
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000686 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000687}
688
689static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000690 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000691 assert(!Attr.isInvalid());
692
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000693 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000694 return;
695
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000696 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
698 << Attr.getName() << ExpectedFunctionOrMethod;
699 return;
700 }
701
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000702 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000703 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000704 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000705 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000706 if (Size == 0)
707 return;
708 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000709
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000710 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000711 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000712}
713
714
Chandler Carruthedc2c642011-07-02 00:01:44 +0000715static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
716 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000717 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000718 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000719 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000720 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000721 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000722
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000723 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000724
725 Expr *sizeExpr;
726
727 // Special case where the argument is a template id.
728 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000729 CXXScopeSpec SS;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000730 SourceLocation TemplateKWLoc;
John McCalle66edc12009-11-24 19:00:30 +0000731 UnqualifiedId id;
732 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor39c02722011-06-15 16:02:29 +0000733
Abramo Bagnara7945c982012-01-27 09:46:47 +0000734 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
735 false, false);
Douglas Gregor39c02722011-06-15 16:02:29 +0000736 if (Size.isInvalid())
737 return;
738
739 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000740 } else {
741 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000742 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor758a8692009-06-17 21:51:59 +0000743 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000744
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000745 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000746 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000747
748 // Instantiate/Install the vector type, and let Sema build the type for us.
749 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000750 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000751 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000752 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000753 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000754
Douglas Gregor758a8692009-06-17 21:51:59 +0000755 // Remember this typedef decl, we will need it later for diagnostics.
756 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000757 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000758}
759
Chandler Carruthedc2c642011-07-02 00:01:44 +0000760static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000761 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000762 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000763 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000764
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000765 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000766 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000767 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000768 // If the alignment is less than or equal to 8 bits, the packed attribute
769 // has no effect.
770 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000771 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000772 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000773 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000774 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000775 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000776 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000777 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000778}
779
Chandler Carruthedc2c642011-07-02 00:01:44 +0000780static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000781 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000782 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000783 else
784 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
785}
786
Chandler Carruthedc2c642011-07-02 00:01:44 +0000787static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000788 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000789 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000790 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000791
Ted Kremenek1f672822010-02-18 03:08:58 +0000792 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000793 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000794 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000795 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000796 return;
797 }
798
Ted Kremenekd68ec812011-02-04 06:54:16 +0000799 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000800}
801
Ted Kremenek7fd17232011-09-29 07:02:25 +0000802static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
803 // The IBOutlet/IBOutletCollection attributes only apply to instance
804 // variables or properties of Objective-C classes. The outlet must also
805 // have an object reference type.
806 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
807 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +0000808 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +0000809 << Attr.getName() << VD->getType() << 0;
810 return false;
811 }
812 }
813 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
814 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +0000815 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +0000816 << Attr.getName() << PD->getType() << 1;
817 return false;
818 }
819 }
820 else {
821 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
822 return false;
823 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +0000824
Ted Kremenek7fd17232011-09-29 07:02:25 +0000825 return true;
826}
827
Chandler Carruthedc2c642011-07-02 00:01:44 +0000828static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000829 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000830 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +0000831 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +0000832
833 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +0000834 return;
Ted Kremenek1f672822010-02-18 03:08:58 +0000835
Ted Kremenek7fd17232011-09-29 07:02:25 +0000836 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000837}
838
Chandler Carruthedc2c642011-07-02 00:01:44 +0000839static void handleIBOutletCollection(Sema &S, Decl *D,
840 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000841
842 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000843 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
845 return;
846 }
847
Ted Kremenek7fd17232011-09-29 07:02:25 +0000848 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +0000849 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +0000850
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000851 IdentifierInfo *II = Attr.getParameterName();
852 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +0000853 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000854
John McCallba7bf592010-08-24 05:47:05 +0000855 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000856 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000857 if (!TypeRep) {
858 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
859 return;
860 }
John McCallba7bf592010-08-24 05:47:05 +0000861 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000862 // Diagnose use of non-object type in iboutletcollection attribute.
863 // FIXME. Gnu attribute extension ignores use of builtin types in
864 // attributes. So, __attribute__((iboutletcollection(char))) will be
865 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +0000866 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000867 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
868 return;
869 }
Argyrios Kyrtzidis8db67df2011-09-13 18:41:59 +0000870 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
871 QT, Attr.getParameterLoc()));
Ted Kremenek26bde772010-05-19 17:38:06 +0000872}
873
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000874static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000875 if (const RecordType *UT = T->getAsUnionType())
876 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
877 RecordDecl *UD = UT->getDecl();
878 for (RecordDecl::field_iterator it = UD->field_begin(),
879 itend = UD->field_end(); it != itend; ++it) {
880 QualType QT = it->getType();
881 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
882 T = QT;
883 return;
884 }
885 }
886 }
887}
888
Chandler Carruthedc2c642011-07-02 00:01:44 +0000889static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000890 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
891 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000892 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000894 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000895 return;
896 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000897
Chandler Carruth743682b2010-11-16 08:35:43 +0000898 // In C++ the implicit 'this' function parameter also counts, and they are
899 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000900 bool HasImplicitThisParam = isInstanceMethod(D);
901 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000902
903 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000904 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000905
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000906 for (AttributeList::arg_iterator I=Attr.arg_begin(),
907 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000908
909
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000910 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000911 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000912 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000913 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
914 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000915 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
916 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000917 return;
918 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000919
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000920 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000921
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000922 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000923 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000924 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000925 return;
926 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000927
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000928 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000929 if (HasImplicitThisParam) {
930 if (x == 0) {
931 S.Diag(Attr.getLoc(),
932 diag::err_attribute_invalid_implicit_this_argument)
933 << "nonnull" << Ex->getSourceRange();
934 return;
935 }
936 --x;
937 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000938
939 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000940 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000941 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000942
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000943 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000944 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000945 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000946 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000947 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000948 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000949
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000950 NonNullArgs.push_back(x);
951 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000952
953 // If no arguments were specified to __attribute__((nonnull)) then all pointer
954 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000955 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000956 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
957 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000958 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000959 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000960 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000961 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000962
Ted Kremenek22813f42010-10-21 18:49:36 +0000963 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000964 if (NonNullArgs.empty()) {
965 // Warn the trivial case only if attribute is not coming from a
966 // macro instantiation.
967 if (Attr.getLoc().isFileID())
968 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000969 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000970 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000971 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000972
973 unsigned* start = &NonNullArgs[0];
974 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000975 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000976 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000977 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000978}
979
Chandler Carruthedc2c642011-07-02 00:01:44 +0000980static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000981 // This attribute must be applied to a function declaration.
982 // The first argument to the attribute must be a string,
983 // the name of the resource, for example "malloc".
984 // The following arguments must be argument indexes, the arguments must be
985 // of integer type for Returns, otherwise of pointer type.
986 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000987 // after being held. free() should be __attribute((ownership_takes)), whereas
988 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000989
990 if (!AL.getParameterName()) {
991 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
992 << AL.getName()->getName() << 1;
993 return;
994 }
995 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000996 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000997 switch (AL.getKind()) {
998 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000999 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001000 if (AL.getNumArgs() < 1) {
1001 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1002 return;
1003 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001004 break;
1005 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001006 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001007 if (AL.getNumArgs() < 1) {
1008 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1009 return;
1010 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001011 break;
1012 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001013 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001014 if (AL.getNumArgs() > 1) {
1015 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1016 << AL.getNumArgs() + 1;
1017 return;
1018 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001019 break;
1020 default:
1021 // This should never happen given how we are called.
1022 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001023 }
1024
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001025 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001026 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1027 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001028 return;
1029 }
1030
Chandler Carruth743682b2010-11-16 08:35:43 +00001031 // In C++ the implicit 'this' function parameter also counts, and they are
1032 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001033 bool HasImplicitThisParam = isInstanceMethod(D);
1034 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001035
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001036 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001037
1038 // Normalize the argument, __foo__ becomes foo.
1039 if (Module.startswith("__") && Module.endswith("__"))
1040 Module = Module.substr(2, Module.size() - 4);
1041
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001042 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001043
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001044 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1045 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001046
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001047 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001048 llvm::APSInt ArgNum(32);
1049 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1050 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1051 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1052 << AL.getName()->getName() << IdxExpr->getSourceRange();
1053 continue;
1054 }
1055
1056 unsigned x = (unsigned) ArgNum.getZExtValue();
1057
1058 if (x > NumArgs || x < 1) {
1059 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1060 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1061 continue;
1062 }
1063 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001064 if (HasImplicitThisParam) {
1065 if (x == 0) {
1066 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1067 << "ownership" << IdxExpr->getSourceRange();
1068 return;
1069 }
1070 --x;
1071 }
1072
Ted Kremenekd21139a2010-07-31 01:52:11 +00001073 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001074 case OwnershipAttr::Takes:
1075 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001076 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001077 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001078 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1079 // FIXME: Should also highlight argument in decl.
1080 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001081 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001082 << "pointer"
1083 << IdxExpr->getSourceRange();
1084 continue;
1085 }
1086 break;
1087 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001088 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001089 if (AL.getNumArgs() > 1) {
1090 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001091 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001092 llvm::APSInt ArgNum(32);
1093 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1094 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1095 S.Diag(AL.getLoc(), diag::err_ownership_type)
1096 << "ownership_returns" << "integer"
1097 << IdxExpr->getSourceRange();
1098 return;
1099 }
1100 }
1101 break;
1102 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001103 } // switch
1104
1105 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001106 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001107 i = D->specific_attr_begin<OwnershipAttr>(),
1108 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001109 i != e; ++i) {
1110 if ((*i)->getOwnKind() != K) {
1111 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1112 I!=E; ++I) {
1113 if (x == *I) {
1114 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1115 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001116 }
1117 }
1118 }
1119 }
1120 OwnershipArgs.push_back(x);
1121 }
1122
1123 unsigned* start = OwnershipArgs.data();
1124 unsigned size = OwnershipArgs.size();
1125 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001126
1127 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1128 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1129 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001130 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001131
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001132 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001133 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001134}
1135
John McCall7a198ce2011-02-08 22:35:49 +00001136/// Whether this declaration has internal linkage for the purposes of
1137/// things that want to complain about things not have internal linkage.
1138static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1139 switch (D->getLinkage()) {
1140 case NoLinkage:
1141 case InternalLinkage:
1142 return true;
1143
1144 // Template instantiations that go from external to unique-external
1145 // shouldn't get diagnosed.
1146 case UniqueExternalLinkage:
1147 return true;
1148
1149 case ExternalLinkage:
1150 return false;
1151 }
1152 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +00001153}
1154
Chandler Carruthedc2c642011-07-02 00:01:44 +00001155static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001156 // Check the attribute arguments.
1157 if (Attr.getNumArgs() > 1) {
1158 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1159 return;
1160 }
1161
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001162 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001163 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001164 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001165 return;
1166 }
1167
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001168 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001169
Rafael Espindolac18086a2010-02-23 22:00:30 +00001170 // gcc rejects
1171 // class c {
1172 // static int a __attribute__((weakref ("v2")));
1173 // static int b() __attribute__((weakref ("f3")));
1174 // };
1175 // and ignores the attributes of
1176 // void f(void) {
1177 // static int a __attribute__((weakref ("v2")));
1178 // }
1179 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001180 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001181 if (!Ctx->isFileContext()) {
1182 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001183 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001184 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001185 }
1186
1187 // The GCC manual says
1188 //
1189 // At present, a declaration to which `weakref' is attached can only
1190 // be `static'.
1191 //
1192 // It also says
1193 //
1194 // Without a TARGET,
1195 // given as an argument to `weakref' or to `alias', `weakref' is
1196 // equivalent to `weak'.
1197 //
1198 // gcc 4.4.1 will accept
1199 // int a7 __attribute__((weakref));
1200 // as
1201 // int a7 __attribute__((weak));
1202 // This looks like a bug in gcc. We reject that for now. We should revisit
1203 // it if this behaviour is actually used.
1204
John McCall7a198ce2011-02-08 22:35:49 +00001205 if (!hasEffectivelyInternalLinkage(nd)) {
1206 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001207 return;
1208 }
1209
1210 // GCC rejects
1211 // static ((alias ("y"), weakref)).
1212 // Should we? How to check that weakref is before or after alias?
1213
1214 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001215 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001216 Arg = Arg->IgnoreParenCasts();
1217 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1218
Douglas Gregorfb65e592011-07-27 05:40:30 +00001219 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001220 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1221 << "weakref" << 1;
1222 return;
1223 }
1224 // GCC will accept anything as the argument of weakref. Should we
1225 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001226 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001227 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001228 }
1229
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001230 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001231}
1232
Chandler Carruthedc2c642011-07-02 00:01:44 +00001233static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001234 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001235 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001236 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001237 return;
1238 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001239
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001240 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001241 Arg = Arg->IgnoreParenCasts();
1242 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001243
Douglas Gregorfb65e592011-07-27 05:40:30 +00001244 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001246 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001247 return;
1248 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001249
Douglas Gregore8bbc122011-09-02 00:18:52 +00001250 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001251 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1252 return;
1253 }
1254
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001255 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001256
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001257 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001258 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001259}
1260
Chandler Carruthedc2c642011-07-02 00:01:44 +00001261static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001262 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001263 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001264 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001265
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001266 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001267 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001268 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001269 return;
1270 }
1271
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001272 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001273}
1274
Chandler Carruthedc2c642011-07-02 00:01:44 +00001275static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1276 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001277 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001278 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1280 return;
1281 }
1282
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001283 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001284 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001285 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001286 return;
1287 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001288
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001289 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001290}
1291
Chandler Carruthedc2c642011-07-02 00:01:44 +00001292static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001293 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001294 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001295 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1296 return;
1297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001299 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001300 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001301 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001302 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001303 return;
1304 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001305 }
1306
Ted Kremenek08479ae2009-08-15 00:51:46 +00001307 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001308}
1309
Chandler Carruthedc2c642011-07-02 00:01:44 +00001310static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001311 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001312 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001313 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001314
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001315 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001316}
1317
Chandler Carruthedc2c642011-07-02 00:01:44 +00001318static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001319 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001320 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001321 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001322 else
1323 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001324 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001325}
1326
Chandler Carruthedc2c642011-07-02 00:01:44 +00001327static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001328 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001329 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001330 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001331 else
1332 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001333 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001334}
1335
Chandler Carruthedc2c642011-07-02 00:01:44 +00001336static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001337 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001338
1339 if (S.CheckNoReturnAttr(attr)) return;
1340
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001341 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001342 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001343 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001344 return;
1345 }
1346
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001347 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001348}
1349
1350bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001351 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001352 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1353 attr.setInvalid();
1354 return true;
1355 }
1356
1357 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001358}
1359
Chandler Carruthedc2c642011-07-02 00:01:44 +00001360static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1361 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001362
1363 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1364 // because 'analyzer_noreturn' does not impact the type.
1365
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001366 if(!checkAttributeNumArgs(S, Attr, 0))
1367 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001368
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001369 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1370 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001371 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1372 && !VD->getType()->isFunctionPointerType())) {
1373 S.Diag(Attr.getLoc(),
1374 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1375 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001376 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001377 return;
1378 }
1379 }
1380
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001381 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001382}
1383
John Thompsoncdb847ba2010-08-09 21:53:52 +00001384// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001385static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001386/*
1387 Returning a Vector Class in Registers
1388
Eric Christopherbc638a82010-12-01 22:13:54 +00001389 According to the PPU ABI specifications, a class with a single member of
1390 vector type is returned in memory when used as the return value of a function.
1391 This results in inefficient code when implementing vector classes. To return
1392 the value in a single vector register, add the vecreturn attribute to the
1393 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001394
1395 Example:
1396
1397 struct Vector
1398 {
1399 __vector float xyzw;
1400 } __attribute__((vecreturn));
1401
1402 Vector Add(Vector lhs, Vector rhs)
1403 {
1404 Vector result;
1405 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1406 return result; // This will be returned in a register
1407 }
1408*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001409 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001410 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001411 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001412 return;
1413 }
1414
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001415 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001416 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1417 return;
1418 }
1419
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001420 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001421 int count = 0;
1422
1423 if (!isa<CXXRecordDecl>(record)) {
1424 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1425 return;
1426 }
1427
1428 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1429 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1430 return;
1431 }
1432
Eric Christopherbc638a82010-12-01 22:13:54 +00001433 for (RecordDecl::field_iterator iter = record->field_begin();
1434 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001435 if ((count == 1) || !iter->getType()->isVectorType()) {
1436 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1437 return;
1438 }
1439 count++;
1440 }
1441
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001442 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001443}
1444
Chandler Carruthedc2c642011-07-02 00:01:44 +00001445static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001446 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001448 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001449 return;
1450 }
1451 // FIXME: Actually store the attribute on the declaration
1452}
1453
Chandler Carruthedc2c642011-07-02 00:01:44 +00001454static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001455 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001456 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001458 return;
1459 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001460
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001461 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1462 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001464 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001465 return;
1466 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001467
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001468 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001469}
1470
Rafael Espindola70107f92011-10-03 14:59:42 +00001471static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1472 const AttributeList &Attr) {
1473 // check the attribute arguments.
1474 if (Attr.hasParameterOrArguments()) {
1475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1476 return;
1477 }
1478
1479 if (!isa<FunctionDecl>(D)) {
1480 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1481 << Attr.getName() << ExpectedFunction;
1482 return;
1483 }
1484
1485 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1486}
1487
Chandler Carruthedc2c642011-07-02 00:01:44 +00001488static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001489 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001490 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1492 return;
1493 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001494
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001495 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001496 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001497 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1498 return;
1499 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001500 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001501 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001502 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001503 return;
1504 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001505
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001506 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001507}
1508
Chandler Carruthedc2c642011-07-02 00:01:44 +00001509static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001510 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001511 if (Attr.getNumArgs() > 1) {
1512 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001513 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001514 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001515
1516 int priority = 65535; // FIXME: Do not hardcode such constants.
1517 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001518 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001519 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001520 if (E->isTypeDependent() || E->isValueDependent() ||
1521 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001522 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001523 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001524 return;
1525 }
1526 priority = Idx.getZExtValue();
1527 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001528
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001529 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001531 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001532 return;
1533 }
1534
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001535 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001536 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001537}
1538
Chandler Carruthedc2c642011-07-02 00:01:44 +00001539static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001540 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001541 if (Attr.getNumArgs() > 1) {
1542 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001543 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001544 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001545
1546 int priority = 65535; // FIXME: Do not hardcode such constants.
1547 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001548 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001549 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001550 if (E->isTypeDependent() || E->isValueDependent() ||
1551 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001553 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001554 return;
1555 }
1556 priority = Idx.getZExtValue();
1557 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001558
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001559 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001560 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001561 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001562 return;
1563 }
1564
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001565 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001566 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001567}
1568
Chandler Carruthedc2c642011-07-02 00:01:44 +00001569static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001570 unsigned NumArgs = Attr.getNumArgs();
1571 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001572 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001573 return;
1574 }
Chris Lattner190aa102011-02-24 05:42:24 +00001575
Fariborz Jahanian551063102010-10-06 21:18:44 +00001576 // Handle the case where deprecated attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001577 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001578 if (NumArgs == 1) {
1579 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001580 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001581 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1582 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001583 return;
1584 }
Chris Lattner190aa102011-02-24 05:42:24 +00001585 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001586 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001587
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001588 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001589}
1590
Chandler Carruthedc2c642011-07-02 00:01:44 +00001591static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001592 unsigned NumArgs = Attr.getNumArgs();
1593 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001594 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001595 return;
1596 }
Chris Lattner190aa102011-02-24 05:42:24 +00001597
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001598 // Handle the case where unavailable attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001599 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001600 if (NumArgs == 1) {
1601 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001602 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001603 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001604 diag::err_attribute_not_string) << "unavailable";
1605 return;
1606 }
Chris Lattner190aa102011-02-24 05:42:24 +00001607 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001608 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001609 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001610}
1611
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001612static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1613 const AttributeList &Attr) {
1614 unsigned NumArgs = Attr.getNumArgs();
1615 if (NumArgs > 0) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1617 return;
1618 }
1619
1620 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001621 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001622}
1623
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001624static void handleObjCRootClassAttr(Sema &S, Decl *D,
1625 const AttributeList &Attr) {
1626 if (!isa<ObjCInterfaceDecl>(D)) {
1627 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1628 return;
1629 }
1630
1631 unsigned NumArgs = Attr.getNumArgs();
1632 if (NumArgs > 0) {
1633 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1634 return;
1635 }
1636
1637 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1638}
1639
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001640static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001641 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001642 if (!isa<ObjCInterfaceDecl>(D)) {
1643 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1644 return;
1645 }
1646
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001647 unsigned NumArgs = Attr.getNumArgs();
1648 if (NumArgs > 0) {
1649 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1650 return;
1651 }
1652
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001653 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001654 Attr.getRange(), S.Context));
1655}
1656
Chandler Carruthedc2c642011-07-02 00:01:44 +00001657static void handleAvailabilityAttr(Sema &S, Decl *D,
1658 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001659 IdentifierInfo *Platform = Attr.getParameterName();
1660 SourceLocation PlatformLoc = Attr.getParameterLoc();
1661
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001662 StringRef PlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001663 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1664 if (PlatformName.empty()) {
1665 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1666 << Platform;
1667
1668 PlatformName = Platform->getName();
1669 }
1670
1671 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1672 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1673 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001674 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001675
Douglas Gregor05a51ae2011-08-10 16:09:55 +00001676 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001677 // of these steps are needed).
1678 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor6f47e5c2011-08-10 15:31:35 +00001679 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001680 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1681 << 1 << PlatformName << Deprecated.Version.getAsString()
1682 << 0 << Introduced.Version.getAsString();
1683 return;
1684 }
1685
1686 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor6f47e5c2011-08-10 15:31:35 +00001687 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001688 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1689 << 2 << PlatformName << Obsoleted.Version.getAsString()
1690 << 0 << Introduced.Version.getAsString();
1691 return;
1692 }
1693
1694 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor6f47e5c2011-08-10 15:31:35 +00001695 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001696 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1697 << 2 << PlatformName << Obsoleted.Version.getAsString()
1698 << 1 << Deprecated.Version.getAsString();
1699 return;
1700 }
1701
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001702 StringRef Str;
1703 const StringLiteral *SE =
1704 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1705 if (SE)
1706 Str = SE->getString();
1707
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001708 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001709 Platform,
1710 Introduced.Version,
1711 Deprecated.Version,
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001712 Obsoleted.Version,
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001713 IsUnavailable,
1714 Str));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001715}
1716
Chandler Carruthedc2c642011-07-02 00:01:44 +00001717static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001718 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001719 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001720 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001721
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001722 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001723 Arg = Arg->IgnoreParenCasts();
1724 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001725
Douglas Gregorfb65e592011-07-27 05:40:30 +00001726 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001727 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001728 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001729 return;
1730 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001731
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001732 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001733 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001734
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001735 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001736 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001737 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001738 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001739 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001740 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00001741 else if (TypeStr == "protected") {
1742 // Complain about attempts to use protected visibility on targets
1743 // (like Darwin) that don't support it.
1744 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1745 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1746 type = VisibilityAttr::Default;
1747 } else {
1748 type = VisibilityAttr::Protected;
1749 }
1750 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001751 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001752 return;
1753 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001754
Rafael Espindolaa01ff782012-05-01 20:58:29 +00001755 Decl *PrevDecl;
1756 if (isa<FunctionDecl>(D))
1757 PrevDecl = D->getMostRecentDecl()->getPreviousDecl();
1758 else
1759 PrevDecl = D->getCanonicalDecl();
1760
1761 VisibilityAttr *PrevAttr = PrevDecl ? PrevDecl->getAttr<VisibilityAttr>() : 0;
Rafael Espindola4c3db232012-04-26 01:26:03 +00001762 if (PrevAttr) {
1763 VisibilityAttr::VisibilityType PrevVisibility = PrevAttr->getVisibility();
1764 if (PrevVisibility != type) {
1765 S.Diag(Attr.getLoc(), diag::err_mismatched_visibilit);
1766 S.Diag(PrevAttr->getLocation(), diag::note_previous_attribute);
1767 return;
1768 }
1769 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001770 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001771}
1772
Chandler Carruthedc2c642011-07-02 00:01:44 +00001773static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1774 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001775 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1776 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001778 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001779 return;
1780 }
1781
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001782 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1783 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1784 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001785 << "objc_method_family" << 1;
1786 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001787 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001788 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001789 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001790 return;
1791 }
1792
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001793 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001794 ObjCMethodFamilyAttr::FamilyKind family;
1795 if (param == "none")
1796 family = ObjCMethodFamilyAttr::OMF_None;
1797 else if (param == "alloc")
1798 family = ObjCMethodFamilyAttr::OMF_alloc;
1799 else if (param == "copy")
1800 family = ObjCMethodFamilyAttr::OMF_copy;
1801 else if (param == "init")
1802 family = ObjCMethodFamilyAttr::OMF_init;
1803 else if (param == "mutableCopy")
1804 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1805 else if (param == "new")
1806 family = ObjCMethodFamilyAttr::OMF_new;
1807 else {
1808 // Just warn and ignore it. This is future-proof against new
1809 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001810 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001811 return;
1812 }
1813
John McCall31168b02011-06-15 23:02:42 +00001814 if (family == ObjCMethodFamilyAttr::OMF_init &&
1815 !method->getResultType()->isObjCObjectPointerType()) {
1816 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1817 << method->getResultType();
1818 // Ignore the attribute.
1819 return;
1820 }
1821
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001822 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00001823 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001824}
1825
Chandler Carruthedc2c642011-07-02 00:01:44 +00001826static void handleObjCExceptionAttr(Sema &S, Decl *D,
1827 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001828 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00001829 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001830
Chris Lattner677a3582009-02-14 08:09:34 +00001831 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1832 if (OCI == 0) {
1833 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1834 return;
1835 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001836
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001837 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001838}
1839
Chandler Carruthedc2c642011-07-02 00:01:44 +00001840static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001841 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001842 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001843 return;
1844 }
Richard Smithdda56e42011-04-15 14:24:37 +00001845 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001846 QualType T = TD->getUnderlyingType();
1847 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001848 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001849 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1850 return;
1851 }
1852 }
Ted Kremenek05e916b2012-03-01 01:40:32 +00001853 else if (!isa<ObjCPropertyDecl>(D)) {
1854 // It is okay to include this attribute on properties, e.g.:
1855 //
1856 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1857 //
1858 // In this case it follows tradition and suppresses an error in the above
1859 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00001860 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00001861 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001862 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001863}
1864
Mike Stumpd3bb5572009-07-24 19:02:52 +00001865static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00001866handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001867 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001868 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001869 return;
1870 }
1871
1872 if (!isa<FunctionDecl>(D)) {
1873 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1874 return;
1875 }
1876
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001877 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001878}
1879
Chandler Carruthedc2c642011-07-02 00:01:44 +00001880static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001881 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001882 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001883 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001884 return;
1885 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001886
Steve Naroff3405a732008-09-18 16:44:58 +00001887 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001889 return;
1890 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001891
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001892 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001893 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001894 type = BlocksAttr::ByRef;
1895 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001896 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001897 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001898 return;
1899 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001900
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001901 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001902}
1903
Chandler Carruthedc2c642011-07-02 00:01:44 +00001904static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001905 // check the attribute arguments.
1906 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001907 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001908 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001909 }
1910
John McCallb46f2872011-09-09 07:56:05 +00001911 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001912 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001913 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001914 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001915 if (E->isTypeDependent() || E->isValueDependent() ||
1916 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001917 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001918 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001919 return;
1920 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001921
John McCallb46f2872011-09-09 07:56:05 +00001922 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001923 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1924 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001925 return;
1926 }
John McCallb46f2872011-09-09 07:56:05 +00001927
1928 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00001929 }
1930
John McCallb46f2872011-09-09 07:56:05 +00001931 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001932 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001933 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001934 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001935 if (E->isTypeDependent() || E->isValueDependent() ||
1936 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001937 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001938 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001939 return;
1940 }
1941 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001942
John McCallb46f2872011-09-09 07:56:05 +00001943 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001944 // FIXME: This error message could be improved, it would be nice
1945 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001946 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1947 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001948 return;
1949 }
1950 }
1951
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001952 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00001953 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001954 if (isa<FunctionNoProtoType>(FT)) {
1955 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1956 return;
1957 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001958
Chris Lattner9363e312009-03-17 23:03:47 +00001959 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001960 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001961 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001962 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001963 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001964 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001965 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001966 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001967 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00001968 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1969 if (!BD->isVariadic()) {
1970 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1971 return;
1972 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001973 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001974 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001975 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001976 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00001977 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001978 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001979 int m = Ty->isFunctionPointerType() ? 0 : 1;
1980 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001981 return;
1982 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001983 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001984 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001985 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001986 return;
1987 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001988 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001989 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001990 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001991 return;
1992 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001993 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00001994 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001995}
1996
Chandler Carruthedc2c642011-07-02 00:01:44 +00001997static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00001998 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001999 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002000 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002001
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002002 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002003 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002004 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00002005 return;
2006 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002007
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002008 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2009 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2010 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002011 return;
2012 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002013 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2014 if (MD->getResultType()->isVoidType()) {
2015 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2016 << Attr.getName() << 1;
2017 return;
2018 }
2019
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002020 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002021}
2022
Chandler Carruthedc2c642011-07-02 00:01:44 +00002023static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002024 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002025 if (Attr.hasParameterOrArguments()) {
2026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002027 return;
2028 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002029
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002030 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002031 if (isa<CXXRecordDecl>(D)) {
2032 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2033 return;
2034 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002035 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2036 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002037 return;
2038 }
2039
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002040 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002041
2042 // 'weak' only applies to declarations with external linkage.
2043 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002044 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002045 return;
2046 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002047
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002048 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002049}
2050
Chandler Carruthedc2c642011-07-02 00:01:44 +00002051static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002052 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002053 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002054 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002055
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002056
2057 // weak_import only applies to variable & function declarations.
2058 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002059 if (!D->canBeWeakImported(isDef)) {
2060 if (isDef)
2061 S.Diag(Attr.getLoc(),
2062 diag::warn_attribute_weak_import_invalid_on_definition)
2063 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002064 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002065 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002066 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002067 // Nothing to warn about here.
2068 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002069 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002070 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002071
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002072 return;
2073 }
2074
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002075 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002076}
2077
Chandler Carruthedc2c642011-07-02 00:01:44 +00002078static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2079 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00002080 // Attribute has 3 arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002081 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begemanf2758702009-06-26 06:32:41 +00002082 return;
Nate Begemanf2758702009-06-26 06:32:41 +00002083
2084 unsigned WGSize[3];
2085 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002086 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002087 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002088 if (E->isTypeDependent() || E->isValueDependent() ||
2089 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002090 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2091 << "reqd_work_group_size" << E->getSourceRange();
2092 return;
2093 }
2094 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2095 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002096 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002097 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00002098 WGSize[2]));
2099}
2100
Chandler Carruthedc2c642011-07-02 00:01:44 +00002101static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002102 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002103 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002104 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002105
2106 // Make sure that there is a string literal as the sections's single
2107 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002108 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002109 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002110 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002111 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002112 return;
2113 }
Mike Stump11289f42009-09-09 15:08:12 +00002114
Chris Lattner30ba6742009-08-10 19:03:04 +00002115 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002116 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002117 if (!Error.empty()) {
2118 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2119 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002120 return;
2121 }
Mike Stump11289f42009-09-09 15:08:12 +00002122
Chris Lattner20aee9b2010-01-12 20:58:53 +00002123 // This attribute cannot be applied to local variables.
2124 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2125 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2126 return;
2127 }
2128
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002129 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002130 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00002131}
2132
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002133
Chandler Carruthedc2c642011-07-02 00:01:44 +00002134static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002135 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002136 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002137 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002138 return;
2139 }
Douglas Gregor88336832011-06-15 05:45:11 +00002140
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002141 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002142 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002143 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002144 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002145 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002146 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002147}
2148
Chandler Carruthedc2c642011-07-02 00:01:44 +00002149static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002150 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002151 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002152 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002153 return;
2154 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002155
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002156 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002157 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002158 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002159 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002160 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002161 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002162}
2163
Chandler Carruthedc2c642011-07-02 00:01:44 +00002164static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002165 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002166 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002167 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002168
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002169 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002170}
2171
Chandler Carruthedc2c642011-07-02 00:01:44 +00002172static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002173 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002174 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2175 return;
2176 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002177
Anders Carlssond277d792009-01-31 01:16:18 +00002178 if (Attr.getNumArgs() != 0) {
2179 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2180 return;
2181 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002182
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002183 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002184
Anders Carlssond277d792009-01-31 01:16:18 +00002185 if (!VD || !VD->hasLocalStorage()) {
2186 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2187 return;
2188 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002189
Anders Carlssond277d792009-01-31 01:16:18 +00002190 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002191 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002192 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002193 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2194 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002195 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002196 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002197 Attr.getParameterName();
2198 return;
2199 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002200
Anders Carlssond277d792009-01-31 01:16:18 +00002201 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2202 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002203 S.Diag(Attr.getParameterLoc(),
2204 diag::err_attribute_cleanup_arg_not_function)
2205 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002206 return;
2207 }
2208
Anders Carlssond277d792009-01-31 01:16:18 +00002209 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002210 S.Diag(Attr.getParameterLoc(),
2211 diag::err_attribute_cleanup_func_must_take_one_arg)
2212 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002213 return;
2214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002215
Anders Carlsson723f55d2009-02-07 23:16:50 +00002216 // We're currently more strict than GCC about what function types we accept.
2217 // If this ever proves to be a problem it should be easy to fix.
2218 QualType Ty = S.Context.getPointerType(VD->getType());
2219 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002220 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2221 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002222 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002223 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2224 Attr.getParameterName() << ParamTy << Ty;
2225 return;
2226 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002227
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002228 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002229 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002230}
2231
Mike Stumpd3bb5572009-07-24 19:02:52 +00002232/// Handle __attribute__((format_arg((idx)))) attribute based on
2233/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002234static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002235 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002236 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002237
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002238 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002239 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002240 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002241 return;
2242 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002243
2244 // In C++ the implicit 'this' function parameter also counts, and they are
2245 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002246 bool HasImplicitThisParam = isInstanceMethod(D);
2247 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002248 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002249
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002250 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002251 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002252 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002253 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2254 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002255 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2256 << "format" << 2 << IdxExpr->getSourceRange();
2257 return;
2258 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002259
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002260 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2262 << "format" << 2 << IdxExpr->getSourceRange();
2263 return;
2264 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002265
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002266 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002267
Chandler Carruth743682b2010-11-16 08:35:43 +00002268 if (HasImplicitThisParam) {
2269 if (ArgIdx == 0) {
2270 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2271 << "format_arg" << IdxExpr->getSourceRange();
2272 return;
2273 }
2274 ArgIdx--;
2275 }
2276
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002277 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002278 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002279
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002280 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2281 if (not_nsstring_type &&
2282 !isCFStringType(Ty, S.Context) &&
2283 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002284 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002285 // FIXME: Should highlight the actual expression that has the wrong type.
2286 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002287 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002288 << IdxExpr->getSourceRange();
2289 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002290 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002291 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002292 if (!isNSStringType(Ty, S.Context) &&
2293 !isCFStringType(Ty, S.Context) &&
2294 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002295 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002296 // FIXME: Should highlight the actual expression that has the wrong type.
2297 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002298 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002299 << IdxExpr->getSourceRange();
2300 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002301 }
2302
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002303 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002304 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002305}
2306
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002307enum FormatAttrKind {
2308 CFStringFormat,
2309 NSStringFormat,
2310 StrftimeFormat,
2311 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002312 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002313 InvalidFormat
2314};
2315
2316/// getFormatAttrKind - Map from format attribute names to supported format
2317/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002318static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002319 // Check for formats that get handled specially.
2320 if (Format == "NSString")
2321 return NSStringFormat;
2322 if (Format == "CFString")
2323 return CFStringFormat;
2324 if (Format == "strftime")
2325 return StrftimeFormat;
2326
2327 // Otherwise, check for supported formats.
2328 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupasfc0da1a2012-01-27 09:14:17 +00002329 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00002330 Format == "zcmn_err" ||
2331 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002332 return SupportedFormat;
2333
Duncan Sandsde4fe352010-03-23 14:44:19 +00002334 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2335 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00002336 return IgnoredFormat;
2337
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002338 return InvalidFormat;
2339}
2340
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002341/// Handle __attribute__((init_priority(priority))) attributes based on
2342/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002343static void handleInitPriorityAttr(Sema &S, Decl *D,
2344 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002345 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002346 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2347 return;
2348 }
2349
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002350 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002351 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2352 Attr.setInvalid();
2353 return;
2354 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002355 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002356 if (S.Context.getAsArrayType(T))
2357 T = S.Context.getBaseElementType(T);
2358 if (!T->getAs<RecordType>()) {
2359 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2360 Attr.setInvalid();
2361 return;
2362 }
2363
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002364 if (Attr.getNumArgs() != 1) {
2365 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2366 Attr.setInvalid();
2367 return;
2368 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002369 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002370
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002371 llvm::APSInt priority(32);
2372 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2373 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2374 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2375 << "init_priority" << priorityExpr->getSourceRange();
2376 Attr.setInvalid();
2377 return;
2378 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002379 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002380 if (prioritynum < 101 || prioritynum > 65535) {
2381 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2382 << priorityExpr->getSourceRange();
2383 Attr.setInvalid();
2384 return;
2385 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002386 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002387 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002388}
2389
Mike Stumpd3bb5572009-07-24 19:02:52 +00002390/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2391/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002392static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002393
Chris Lattner4a927cb2008-06-28 23:36:30 +00002394 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002395 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002396 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002397 return;
2398 }
2399
Chris Lattner4a927cb2008-06-28 23:36:30 +00002400 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002401 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002402 return;
2403 }
2404
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002405 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002406 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002407 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002408 return;
2409 }
2410
Chandler Carruth743682b2010-11-16 08:35:43 +00002411 // In C++ the implicit 'this' function parameter also counts, and they are
2412 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002413 bool HasImplicitThisParam = isInstanceMethod(D);
2414 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002415 unsigned FirstIdx = 1;
2416
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002417 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002418
2419 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002420 if (Format.startswith("__") && Format.endswith("__"))
2421 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002422
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002423 // Check for supported formats.
2424 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002425
2426 if (Kind == IgnoredFormat)
2427 return;
2428
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002429 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002430 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002431 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002432 return;
2433 }
2434
2435 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002436 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002437 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002438 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2439 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002440 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002441 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002442 return;
2443 }
2444
2445 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002447 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002448 return;
2449 }
2450
2451 // FIXME: Do we need to bounds check?
2452 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002453
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002454 if (HasImplicitThisParam) {
2455 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002456 S.Diag(Attr.getLoc(),
2457 diag::err_format_attribute_implicit_this_format_string)
2458 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002459 return;
2460 }
2461 ArgIdx--;
2462 }
Mike Stump11289f42009-09-09 15:08:12 +00002463
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002464 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002465 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002466
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002467 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002468 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002469 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2470 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002471 return;
2472 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002473 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002474 // FIXME: do we need to check if the type is NSString*? What are the
2475 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002476 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002477 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002478 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2479 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002480 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002481 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002482 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002483 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002484 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002485 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2486 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002487 return;
2488 }
2489
2490 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002491 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002492 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002493 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2494 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002495 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002496 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002497 return;
2498 }
2499
2500 // check if the function is variadic if the 3rd argument non-zero
2501 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002502 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002503 ++NumArgs; // +1 for ...
2504 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002505 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002506 return;
2507 }
2508 }
2509
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002510 // strftime requires FirstArg to be 0 because it doesn't read from any
2511 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002512 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002513 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002514 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2515 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002516 return;
2517 }
2518 // if 0 it disables parameter checking (to use with e.g. va_list)
2519 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002521 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002522 return;
2523 }
2524
Douglas Gregor88336832011-06-15 05:45:11 +00002525 // Check whether we already have an equivalent format attribute.
2526 for (specific_attr_iterator<FormatAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002527 i = D->specific_attr_begin<FormatAttr>(),
2528 e = D->specific_attr_end<FormatAttr>();
Douglas Gregor88336832011-06-15 05:45:11 +00002529 i != e ; ++i) {
2530 FormatAttr *f = *i;
2531 if (f->getType() == Format &&
2532 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2533 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2534 // If we don't have a valid location for this attribute, adopt the
2535 // location.
2536 if (f->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002537 f->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002538 return;
2539 }
2540 }
2541
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002542 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002543 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002544 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002545}
2546
Chandler Carruthedc2c642011-07-02 00:01:44 +00002547static void handleTransparentUnionAttr(Sema &S, Decl *D,
2548 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002549 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002550 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002551 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002552
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002553
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002554 // Try to find the underlying union declaration.
2555 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002556 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002557 if (TD && TD->getUnderlyingType()->isUnionType())
2558 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2559 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002560 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002561
2562 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002563 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002564 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002565 return;
2566 }
2567
John McCallf937c022011-10-07 06:10:15 +00002568 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002569 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002570 diag::warn_transparent_union_attribute_not_definition);
2571 return;
2572 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002573
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002574 RecordDecl::field_iterator Field = RD->field_begin(),
2575 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002576 if (Field == FieldEnd) {
2577 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2578 return;
2579 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002580
David Blaikie2d7c57e2012-04-30 02:36:29 +00002581 FieldDecl *FirstField = &*Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002582 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002583 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002584 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002585 diag::warn_transparent_union_attribute_floating)
2586 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002587 return;
2588 }
2589
2590 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2591 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2592 for (; Field != FieldEnd; ++Field) {
2593 QualType FieldType = Field->getType();
2594 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2595 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2596 // Warn if we drop the attribute.
2597 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002598 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002599 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002600 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002601 diag::warn_transparent_union_attribute_field_size_align)
2602 << isSize << Field->getDeclName() << FieldBits;
2603 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002604 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002605 diag::note_transparent_union_first_field_size_align)
2606 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002607 return;
2608 }
2609 }
2610
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002611 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002612}
2613
Chandler Carruthedc2c642011-07-02 00:01:44 +00002614static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002615 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002616 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002617 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002618
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002619 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002620 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002622 // Make sure that there is a string literal as the annotation's single
2623 // argument.
2624 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002625 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002626 return;
2627 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002628
2629 // Don't duplicate annotations that are already set.
2630 for (specific_attr_iterator<AnnotateAttr>
2631 i = D->specific_attr_begin<AnnotateAttr>(),
2632 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2633 if ((*i)->getAnnotation() == SE->getString())
2634 return;
2635 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002636 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002637 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002638}
2639
Chandler Carruthedc2c642011-07-02 00:01:44 +00002640static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002641 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002642 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644 return;
2645 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002646
2647 //FIXME: The C++0x version of this attribute has more limited applicabilty
2648 // than GNU's, and should error out when it is used to specify a
2649 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650
Chris Lattner4a927cb2008-06-28 23:36:30 +00002651 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002652 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002653 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002654 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002655
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002656 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002657}
2658
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002659void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002660 // FIXME: Handle pack-expansions here.
2661 if (DiagnoseUnexpandedParameterPack(E))
2662 return;
2663
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002664 if (E->isTypeDependent() || E->isValueDependent()) {
2665 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002666 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002667 return;
2668 }
2669
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002670 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002671 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002672 llvm::APSInt Alignment(32);
Richard Smithf4c51d92012-02-04 09:53:13 +00002673 ExprResult ICE =
2674 VerifyIntegerConstantExpression(E, &Alignment,
2675 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2676 /*AllowFold*/ false);
2677 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002678 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002679 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002680 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2681 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002682 return;
2683 }
2684
Richard Smithf4c51d92012-02-04 09:53:13 +00002685 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002686}
2687
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002688void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002689 // FIXME: Cache the number on the Attr object if non-dependent?
2690 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002691 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002692 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002693}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002694
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002695/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002696/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002697///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002698/// Despite what would be logical, the mode attribute is a decl attribute, not a
2699/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2700/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002701static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002702 // This attribute isn't documented, but glibc uses it. It changes
2703 // the width of an int or unsigned int to the specified size.
2704
2705 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002706 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002707 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002708
Chris Lattneracbc2d22008-06-27 22:18:37 +00002709
2710 IdentifierInfo *Name = Attr.getParameterName();
2711 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002712 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002713 return;
2714 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002715
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002716 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002717
2718 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002719 if (Str.startswith("__") && Str.endswith("__"))
2720 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002721
2722 unsigned DestWidth = 0;
2723 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002724 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002725 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002726 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002727 switch (Str[0]) {
2728 case 'Q': DestWidth = 8; break;
2729 case 'H': DestWidth = 16; break;
2730 case 'S': DestWidth = 32; break;
2731 case 'D': DestWidth = 64; break;
2732 case 'X': DestWidth = 96; break;
2733 case 'T': DestWidth = 128; break;
2734 }
2735 if (Str[1] == 'F') {
2736 IntegerMode = false;
2737 } else if (Str[1] == 'C') {
2738 IntegerMode = false;
2739 ComplexMode = true;
2740 } else if (Str[1] != 'I') {
2741 DestWidth = 0;
2742 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002743 break;
2744 case 4:
2745 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2746 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002747 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002748 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002749 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002750 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002751 break;
2752 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002753 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002754 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002755 break;
2756 }
2757
2758 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002759 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002760 OldTy = TD->getUnderlyingType();
2761 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2762 OldTy = VD->getType();
2763 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002764 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002765 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002766 return;
2767 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002768
John McCall9dd450b2009-09-21 23:43:11 +00002769 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002770 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2771 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002772 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002773 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2774 } else if (ComplexMode) {
2775 if (!OldTy->isComplexType())
2776 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2777 } else {
2778 if (!OldTy->isFloatingType())
2779 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2780 }
2781
Mike Stump87c57ac2009-05-16 07:39:55 +00002782 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2783 // and friends, at least with glibc.
2784 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2785 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002786 // FIXME: Make sure floating-point mappings are accurate
2787 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002788 QualType NewTy;
2789 switch (DestWidth) {
2790 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002791 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002792 return;
2793 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002794 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002795 return;
2796 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002797 if (!IntegerMode) {
2798 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2799 return;
2800 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002801 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002802 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002803 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002804 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002805 break;
2806 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002807 if (!IntegerMode) {
2808 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2809 return;
2810 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002811 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002812 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002813 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002814 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002815 break;
2816 case 32:
2817 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002818 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002819 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002820 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002821 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002822 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002823 break;
2824 case 64:
2825 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002826 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002827 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00002828 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00002829 NewTy = S.Context.LongTy;
2830 else
2831 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002832 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00002833 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00002834 NewTy = S.Context.UnsignedLongTy;
2835 else
2836 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002837 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002838 case 96:
2839 NewTy = S.Context.LongDoubleTy;
2840 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002841 case 128:
2842 if (!IntegerMode) {
2843 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2844 return;
2845 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002846 if (OldTy->isSignedIntegerType())
2847 NewTy = S.Context.Int128Ty;
2848 else
2849 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002850 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002851 }
2852
Eli Friedman4735374e2009-03-03 06:41:03 +00002853 if (ComplexMode) {
2854 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002855 }
2856
2857 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00002858 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00002859 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002860 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002861 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002862 cast<ValueDecl>(D)->setType(NewTy);
2863}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002864
Chandler Carruthedc2c642011-07-02 00:01:44 +00002865static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002866 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002867 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00002868 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00002869
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002870 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002872 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002873 return;
2874 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002875
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002876 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002877}
2878
Chandler Carruthedc2c642011-07-02 00:01:44 +00002879static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00002880 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002881 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00002882 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002883
Mike Stumpd3bb5572009-07-24 19:02:52 +00002884
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002885 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002886 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002887 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002888 return;
2889 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002890
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002891 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002892}
2893
Chandler Carruthedc2c642011-07-02 00:01:44 +00002894static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2895 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002896 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002897 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00002898 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002899
Chris Lattner3c77a352010-06-22 00:03:40 +00002900
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002901 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002902 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002903 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002904 return;
2905 }
2906
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002907 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002908 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002909}
2910
Chandler Carruthedc2c642011-07-02 00:01:44 +00002911static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002912 if (S.LangOpts.CUDA) {
2913 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002914 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002915 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2916 return;
2917 }
2918
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002919 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002920 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002921 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002922 return;
2923 }
2924
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002925 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002926 } else {
2927 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2928 }
2929}
2930
Chandler Carruthedc2c642011-07-02 00:01:44 +00002931static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002932 if (S.LangOpts.CUDA) {
2933 // check the attribute arguments.
2934 if (Attr.getNumArgs() != 0) {
2935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2936 return;
2937 }
2938
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002939 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002941 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002942 return;
2943 }
2944
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002945 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002946 } else {
2947 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2948 }
2949}
2950
Chandler Carruthedc2c642011-07-02 00:01:44 +00002951static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002952 if (S.LangOpts.CUDA) {
2953 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002954 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002955 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002956
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002957 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002958 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002959 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002960 return;
2961 }
2962
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002963 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002964 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002965 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002966 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2967 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2968 << FD->getType()
2969 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2970 "void");
2971 } else {
2972 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2973 << FD->getType();
2974 }
2975 return;
2976 }
2977
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002978 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002979 } else {
2980 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2981 }
2982}
2983
Chandler Carruthedc2c642011-07-02 00:01:44 +00002984static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002985 if (S.LangOpts.CUDA) {
2986 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002987 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002988 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002989
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002990
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002991 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002992 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002993 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002994 return;
2995 }
2996
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002997 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002998 } else {
2999 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3000 }
3001}
3002
Chandler Carruthedc2c642011-07-02 00:01:44 +00003003static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003004 if (S.LangOpts.CUDA) {
3005 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003006 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003007 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003008
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003009
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003010 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003011 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003012 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003013 return;
3014 }
3015
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003016 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003017 } else {
3018 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3019 }
3020}
3021
Chandler Carruthedc2c642011-07-02 00:01:44 +00003022static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003023 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003024 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003025 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003026
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003027 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003028 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003029 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003030 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003031 return;
3032 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003033
Douglas Gregor35b57532009-10-27 21:01:01 +00003034 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003035 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003036 return;
3037 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003038
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003039 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003040}
3041
Chandler Carruthedc2c642011-07-02 00:01:44 +00003042static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003043 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003044
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003045 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003046 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3047 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003048 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003049 return;
3050
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003051 if (!isa<ObjCMethodDecl>(D)) {
3052 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3053 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003054 return;
3055 }
3056
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003057 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00003058 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003059 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003060 return;
3061 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003062 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003063 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00003064 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003065 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003066 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003067 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003068 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003069 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003070 case AttributeList::AT_pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003071 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003072 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003073 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003074 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003075 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003076 if (!Str || !Str->isAscii()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003078 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003079 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003080 return;
3081 }
3082
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003083 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003084 PcsAttr::PCSType PCS;
3085 if (StrRef == "aapcs")
3086 PCS = PcsAttr::AAPCS;
3087 else if (StrRef == "aapcs-vfp")
3088 PCS = PcsAttr::AAPCS_VFP;
3089 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003090 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3091 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003092 return;
3093 }
3094
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003095 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003096 }
Abramo Bagnara50099372010-04-30 13:10:51 +00003097 default:
3098 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003099 }
3100}
3101
Chandler Carruthedc2c642011-07-02 00:01:44 +00003102static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003103 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003104 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003105}
3106
John McCall3882ace2011-01-05 12:14:39 +00003107bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3108 if (attr.isInvalid())
3109 return true;
3110
Ted Kremenek1551d552011-04-15 05:49:29 +00003111 if ((attr.getNumArgs() != 0 &&
3112 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3113 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00003114 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3115 attr.setInvalid();
3116 return true;
3117 }
3118
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003119 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3120 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003121 switch (attr.getKind()) {
3122 case AttributeList::AT_cdecl: CC = CC_C; break;
3123 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3124 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3125 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3126 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003127 case AttributeList::AT_pcs: {
3128 Expr *Arg = attr.getArg(0);
3129 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003130 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003131 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3132 << "pcs" << 1;
3133 attr.setInvalid();
3134 return true;
3135 }
3136
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003137 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003138 if (StrRef == "aapcs") {
3139 CC = CC_AAPCS;
3140 break;
3141 } else if (StrRef == "aapcs-vfp") {
3142 CC = CC_AAPCS_VFP;
3143 break;
3144 }
3145 // FALLS THROUGH
3146 }
David Blaikie8a40f702012-01-17 06:56:22 +00003147 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003148 }
3149
3150 return false;
3151}
3152
Chandler Carruthedc2c642011-07-02 00:01:44 +00003153static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003154 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003155
3156 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003157 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003158 return;
3159
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003160 if (!isa<ObjCMethodDecl>(D)) {
3161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3162 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003163 return;
3164 }
Eli Friedman7044b762009-03-27 21:06:47 +00003165
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003166 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003167}
3168
3169/// Checks a regparm attribute, returning true if it is ill-formed and
3170/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003171bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3172 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003173 return true;
3174
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003175 if (Attr.getNumArgs() != 1) {
3176 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3177 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003178 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003179 }
Eli Friedman7044b762009-03-27 21:06:47 +00003180
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003181 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003182 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003183 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003184 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003185 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003186 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003187 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003188 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003189 }
3190
Douglas Gregore8bbc122011-09-02 00:18:52 +00003191 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003192 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003193 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003194 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003195 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003196 }
3197
John McCall3882ace2011-01-05 12:14:39 +00003198 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003199 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003200 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003201 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003202 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003203 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003204 }
3205
John McCall3882ace2011-01-05 12:14:39 +00003206 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003207}
3208
Chandler Carruthedc2c642011-07-02 00:01:44 +00003209static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003210 if (S.LangOpts.CUDA) {
3211 // check the attribute arguments.
3212 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003213 // FIXME: 0 is not okay.
3214 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003215 return;
3216 }
3217
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003218 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003219 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003220 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003221 return;
3222 }
3223
3224 Expr *MaxThreadsExpr = Attr.getArg(0);
3225 llvm::APSInt MaxThreads(32);
3226 if (MaxThreadsExpr->isTypeDependent() ||
3227 MaxThreadsExpr->isValueDependent() ||
3228 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3230 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3231 return;
3232 }
3233
3234 llvm::APSInt MinBlocks(32);
3235 if (Attr.getNumArgs() > 1) {
3236 Expr *MinBlocksExpr = Attr.getArg(1);
3237 if (MinBlocksExpr->isTypeDependent() ||
3238 MinBlocksExpr->isValueDependent() ||
3239 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3241 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3242 return;
3243 }
3244 }
3245
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003246 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003247 MaxThreads.getZExtValue(),
3248 MinBlocks.getZExtValue()));
3249 } else {
3250 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3251 }
3252}
3253
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003254//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003255// Checker-specific attribute handlers.
3256//===----------------------------------------------------------------------===//
3257
John McCalled433932011-01-25 03:31:58 +00003258static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003259 return type->isDependentType() ||
3260 type->isObjCObjectPointerType() ||
3261 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003262}
3263static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003264 return type->isDependentType() ||
3265 type->isPointerType() ||
3266 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003267}
3268
Chandler Carruthedc2c642011-07-02 00:01:44 +00003269static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003270 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003271 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003272 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003273 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003274 return;
3275 }
3276
3277 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003278 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00003279 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3280 cf = false;
3281 } else {
3282 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3283 cf = true;
3284 }
3285
3286 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003287 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003288 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003289 return;
3290 }
3291
3292 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003293 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003294 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003295 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003296}
3297
Chandler Carruthedc2c642011-07-02 00:01:44 +00003298static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3299 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003300 if (!isa<ObjCMethodDecl>(D)) {
3301 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003302 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003303 return;
3304 }
3305
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003306 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003307}
3308
Chandler Carruthedc2c642011-07-02 00:01:44 +00003309static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3310 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003311
John McCalled433932011-01-25 03:31:58 +00003312 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003313
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003314 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003315 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003316 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00003317 returnType = PD->getType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003318 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003319 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00003320 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003321 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003322 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003323 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003324 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003325 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003326 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003327 return;
3328 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003329
John McCalled433932011-01-25 03:31:58 +00003330 bool typeOK;
3331 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003332 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003333 default: llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003334 case AttributeList::AT_ns_returns_autoreleased:
3335 case AttributeList::AT_ns_returns_retained:
3336 case AttributeList::AT_ns_returns_not_retained:
3337 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3338 cf = false;
3339 break;
3340
3341 case AttributeList::AT_cf_returns_retained:
3342 case AttributeList::AT_cf_returns_not_retained:
3343 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3344 cf = true;
3345 break;
3346 }
3347
3348 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003349 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003350 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003351 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003352 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003353
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003354 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003355 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003356 llvm_unreachable("invalid ownership attribute");
John McCalled433932011-01-25 03:31:58 +00003357 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003358 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003359 S.Context));
3360 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00003361 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003362 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003363 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003364 return;
3365 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003366 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003367 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003368 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003369 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003370 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003371 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003372 return;
3373 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003374 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003375 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003376 return;
3377 };
3378}
3379
John McCallcf166702011-07-22 08:53:00 +00003380static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3381 const AttributeList &attr) {
3382 SourceLocation loc = attr.getLoc();
3383
3384 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3385
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00003386 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00003387 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003388 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003389 return;
3390 }
3391
3392 // Check that the method returns a normal pointer.
3393 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003394
3395 if (!resultType->isReferenceType() &&
3396 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003397 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3398 << SourceRange(loc)
3399 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3400
3401 // Drop the attribute.
3402 return;
3403 }
3404
3405 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003406 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003407}
3408
John McCall32f5fe12011-09-30 05:12:12 +00003409/// Handle cf_audited_transfer and cf_unknown_transfer.
3410static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3411 if (!isa<FunctionDecl>(D)) {
3412 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003413 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00003414 return;
3415 }
3416
3417 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3418
3419 // Check whether there's a conflicting attribute already present.
3420 Attr *Existing;
3421 if (IsAudited) {
3422 Existing = D->getAttr<CFUnknownTransferAttr>();
3423 } else {
3424 Existing = D->getAttr<CFAuditedTransferAttr>();
3425 }
3426 if (Existing) {
3427 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3428 << A.getName()
3429 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3430 << A.getRange() << Existing->getRange();
3431 return;
3432 }
3433
3434 // All clear; add the attribute.
3435 if (IsAudited) {
3436 D->addAttr(
3437 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3438 } else {
3439 D->addAttr(
3440 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3441 }
3442}
3443
John McCallf1e8b342011-09-29 07:17:38 +00003444static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3445 const AttributeList &Attr) {
3446 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3447 if (!RD || RD->isUnion()) {
3448 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003449 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003450 }
3451
3452 IdentifierInfo *ParmName = Attr.getParameterName();
3453
3454 // In Objective-C, verify that the type names an Objective-C type.
3455 // We don't want to check this outside of ObjC because people sometimes
3456 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003457 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003458 // Check for an existing type with this name.
3459 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3460 Sema::LookupOrdinaryName);
3461 if (S.LookupName(R, Sc)) {
3462 NamedDecl *Target = R.getFoundDecl();
3463 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3464 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3465 S.Diag(Target->getLocStart(), diag::note_declared_at);
3466 }
3467 }
3468 }
3469
3470 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3471 ParmName));
3472}
3473
Chandler Carruthedc2c642011-07-02 00:01:44 +00003474static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3475 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003476 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003477
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003478 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003479 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003480}
3481
Chandler Carruthedc2c642011-07-02 00:01:44 +00003482static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3483 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003484 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003485 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003486 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003487 return;
3488 }
3489
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003490 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003491 QualType type = vd->getType();
3492
3493 if (!type->isDependentType() &&
3494 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003495 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003496 << type;
3497 return;
3498 }
3499
3500 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3501
3502 // If we have no lifetime yet, check the lifetime we're presumably
3503 // going to infer.
3504 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3505 lifetime = type->getObjCARCImplicitLifetime();
3506
3507 switch (lifetime) {
3508 case Qualifiers::OCL_None:
3509 assert(type->isDependentType() &&
3510 "didn't infer lifetime for non-dependent type?");
3511 break;
3512
3513 case Qualifiers::OCL_Weak: // meaningful
3514 case Qualifiers::OCL_Strong: // meaningful
3515 break;
3516
3517 case Qualifiers::OCL_ExplicitNone:
3518 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003519 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003520 << (lifetime == Qualifiers::OCL_Autoreleasing);
3521 break;
3522 }
3523
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003524 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003525 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00003526}
3527
Charles Davis163855f2010-02-16 18:27:26 +00003528static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman0c84ebb2012-02-23 22:46:33 +00003529 switch (Attr.getKind()) {
3530 default:
3531 return false;
3532 case AttributeList::AT_dllimport:
3533 case AttributeList::AT_dllexport:
3534 case AttributeList::AT_uuid:
3535 case AttributeList::AT_deprecated:
3536 case AttributeList::AT_noreturn:
3537 case AttributeList::AT_nothrow:
3538 case AttributeList::AT_naked:
3539 case AttributeList::AT_noinline:
3540 return true;
3541 }
Francois Picheta83957a2010-12-19 06:50:37 +00003542}
3543
3544//===----------------------------------------------------------------------===//
3545// Microsoft specific attribute handlers.
3546//===----------------------------------------------------------------------===//
3547
Chandler Carruthedc2c642011-07-02 00:01:44 +00003548static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00003549 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00003550 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003551 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00003552 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003553
Francois Picheta83957a2010-12-19 06:50:37 +00003554 Expr *Arg = Attr.getArg(0);
3555 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003556 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00003557 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3558 << "uuid" << 1;
3559 return;
3560 }
3561
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003562 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00003563
3564 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3565 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003566
Francois Pichet7da11662010-12-20 01:41:49 +00003567 // Validate GUID length.
3568 if (IsCurly && StrRef.size() != 38) {
3569 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3570 return;
3571 }
3572 if (!IsCurly && StrRef.size() != 36) {
3573 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3574 return;
3575 }
3576
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003577 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00003578 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003579 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00003580 if (IsCurly) // Skip the optional '{'
3581 ++I;
3582
3583 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00003584 if (i == 8 || i == 13 || i == 18 || i == 23) {
3585 if (*I != '-') {
3586 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3587 return;
3588 }
3589 } else if (!isxdigit(*I)) {
3590 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3591 return;
3592 }
3593 I++;
3594 }
Francois Picheta83957a2010-12-19 06:50:37 +00003595
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003596 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00003597 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00003598 } else
Francois Picheta83957a2010-12-19 06:50:37 +00003599 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00003600}
3601
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003602//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003603// Top Level Sema Entry Points
3604//===----------------------------------------------------------------------===//
3605
Chandler Carruthedc2c642011-07-02 00:01:44 +00003606static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3607 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003608 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003609 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3610 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3611 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003612 default:
3613 break;
3614 }
3615}
Abramo Bagnara50099372010-04-30 13:10:51 +00003616
Chandler Carruthedc2c642011-07-02 00:01:44 +00003617static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3618 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003619 switch (Attr.getKind()) {
Michael Han4a045172012-03-07 00:12:16 +00003620 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3621 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3622 case AttributeList::AT_iboutletcollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003623 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003624 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003625 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00003626 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00003627 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00003628 case AttributeList::AT_neon_vector_type:
3629 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003630 // Ignore these, these are type attributes, handled by
3631 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003632 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00003633 case AttributeList::AT_device:
3634 case AttributeList::AT_host:
3635 case AttributeList::AT_overloadable:
3636 // Ignore, this is a non-inheritable attribute, handled
3637 // by ProcessNonInheritableDeclAttr.
3638 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003639 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3640 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003641 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003642 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00003643 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003644 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3645 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3646 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003647 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003648 handleDependencyAttr (S, D, Attr); break;
3649 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3650 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3651 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3652 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3653 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003654 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003655 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003656 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003657 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3658 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3659 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3660 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003661 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003662 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003663 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003664 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3665 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3666 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3667 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3668 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00003669 case AttributeList::AT_ownership_returns:
3670 case AttributeList::AT_ownership_takes:
3671 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003672 handleOwnershipAttr (S, D, Attr); break;
3673 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3674 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3675 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3676 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3677 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003678
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003679 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003680 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003681 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003682 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003683
John McCallcf166702011-07-22 08:53:00 +00003684 case AttributeList::AT_objc_returns_inner_pointer:
3685 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3686
John McCallf1e8b342011-09-29 07:17:38 +00003687 case AttributeList::AT_ns_bridged:
3688 handleNSBridgedAttr(S, scope, D, Attr); break;
3689
John McCall32f5fe12011-09-30 05:12:12 +00003690 case AttributeList::AT_cf_audited_transfer:
3691 case AttributeList::AT_cf_unknown_transfer:
3692 handleCFTransferAttr(S, D, Attr); break;
3693
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003694 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003695 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003696 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003697 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003698 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003699
3700 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003701 case AttributeList::AT_ns_returns_not_retained:
3702 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003703 case AttributeList::AT_ns_returns_retained:
3704 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003705 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003706
Michael Han4a045172012-03-07 00:12:16 +00003707 case AttributeList::AT_reqd_work_group_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003708 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003709
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003710 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003711 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003712
Chandler Carruthedc2c642011-07-02 00:01:44 +00003713 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003714 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003715 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3716 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Han4a045172012-03-07 00:12:16 +00003717 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00003718 handleArcWeakrefUnavailableAttr (S, D, Attr);
3719 break;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00003720 case AttributeList::AT_objc_root_class:
3721 handleObjCRootClassAttr(S, D, Attr);
3722 break;
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00003723 case AttributeList::AT_objc_requires_property_definitions:
3724 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00003725 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003726 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindola70107f92011-10-03 14:59:42 +00003727 case AttributeList::AT_returns_twice:
3728 handleReturnsTwiceAttr(S, D, Attr);
3729 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003730 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3731 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3732 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003733 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003734 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3735 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3736 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003737 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003738 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003739 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003740 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003741 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003742 break;
John McCall86bc21f2011-03-02 11:33:24 +00003743 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003744 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003745 break;
Michael Han4a045172012-03-07 00:12:16 +00003746 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003747 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3748 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3749 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3750 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3751 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3752 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3753 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3754 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003755 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003756 // Just ignore
3757 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003758 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003759 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003760 break;
John McCallab26cfa2010-02-05 21:31:56 +00003761 case AttributeList::AT_stdcall:
3762 case AttributeList::AT_cdecl:
3763 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003764 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003765 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003766 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003767 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003768 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003769 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003770 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003771 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003772 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003773 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003774 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003775
3776 // Thread safety attributes:
3777 case AttributeList::AT_guarded_var:
3778 handleGuardedVarAttr(S, D, Attr);
3779 break;
3780 case AttributeList::AT_pt_guarded_var:
3781 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3782 break;
3783 case AttributeList::AT_scoped_lockable:
3784 handleLockableAttr(S, D, Attr, /*scoped = */true);
3785 break;
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00003786 case AttributeList::AT_no_address_safety_analysis:
3787 handleNoAddressSafetyAttr(S, D, Attr);
3788 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003789 case AttributeList::AT_no_thread_safety_analysis:
3790 handleNoThreadSafetyAttr(S, D, Attr);
3791 break;
3792 case AttributeList::AT_lockable:
3793 handleLockableAttr(S, D, Attr);
3794 break;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00003795 case AttributeList::AT_guarded_by:
3796 handleGuardedByAttr(S, D, Attr);
3797 break;
3798 case AttributeList::AT_pt_guarded_by:
3799 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3800 break;
3801 case AttributeList::AT_exclusive_lock_function:
3802 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3803 break;
3804 case AttributeList::AT_exclusive_locks_required:
3805 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3806 break;
3807 case AttributeList::AT_exclusive_trylock_function:
3808 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3809 break;
3810 case AttributeList::AT_lock_returned:
3811 handleLockReturnedAttr(S, D, Attr);
3812 break;
3813 case AttributeList::AT_locks_excluded:
3814 handleLocksExcludedAttr(S, D, Attr);
3815 break;
3816 case AttributeList::AT_shared_lock_function:
3817 handleLockFunAttr(S, D, Attr);
3818 break;
3819 case AttributeList::AT_shared_locks_required:
3820 handleLocksRequiredAttr(S, D, Attr);
3821 break;
3822 case AttributeList::AT_shared_trylock_function:
3823 handleTrylockFunAttr(S, D, Attr);
3824 break;
3825 case AttributeList::AT_unlock_function:
3826 handleUnlockFunAttr(S, D, Attr);
3827 break;
3828 case AttributeList::AT_acquired_before:
3829 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3830 break;
3831 case AttributeList::AT_acquired_after:
3832 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3833 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00003834
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003835 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003836 // Ask target about the attribute.
3837 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3838 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00003839 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3840 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003841 break;
3842 }
3843}
3844
Peter Collingbourneb331b262011-01-21 02:08:45 +00003845/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3846/// the attribute applies to decls. If the attribute is a type attribute, just
3847/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3848/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00003849static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3850 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003851 bool NonInheritable, bool Inheritable) {
3852 if (Attr.isInvalid())
3853 return;
3854
3855 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3856 // FIXME: Try to deal with other __declspec attributes!
3857 return;
3858
3859 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003860 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003861
3862 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003863 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003864}
3865
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003866/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3867/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00003868void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003869 const AttributeList *AttrList,
3870 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003871 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003872 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00003873 }
3874
3875 // GCC accepts
3876 // static int a9 __attribute__((weakref));
3877 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003878 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003879 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00003880 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00003881 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003882 }
3883}
3884
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003885// Annotation attributes are the only attributes allowed after an access
3886// specifier.
3887bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3888 const AttributeList *AttrList) {
3889 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3890 if (l->getKind() == AttributeList::AT_annotate) {
3891 handleAnnotateAttr(*this, ASDecl, *l);
3892 } else {
3893 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3894 return true;
3895 }
3896 }
3897
3898 return false;
3899}
3900
John McCall42856de2011-10-01 05:17:03 +00003901/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3902/// contains any decl attributes that we should warn about.
3903static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3904 for ( ; A; A = A->getNext()) {
3905 // Only warn if the attribute is an unignored, non-type attribute.
3906 if (A->isUsedAsTypeAttr()) continue;
3907 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3908
3909 if (A->getKind() == AttributeList::UnknownAttribute) {
3910 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3911 << A->getName() << A->getRange();
3912 } else {
3913 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3914 << A->getName() << A->getRange();
3915 }
3916 }
3917}
3918
3919/// checkUnusedDeclAttributes - Given a declarator which is not being
3920/// used to build a declaration, complain about any decl attributes
3921/// which might be lying around on it.
3922void Sema::checkUnusedDeclAttributes(Declarator &D) {
3923 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3924 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3925 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3926 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3927}
3928
Ryan Flynn7d470f32009-07-30 03:15:39 +00003929/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3930/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedmance3e2c82011-09-07 04:05:06 +00003931NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3932 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00003933 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003934 NamedDecl *NewD = 0;
3935 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00003936 FunctionDecl *NewFD;
3937 // FIXME: Missing call to CheckFunctionDeclaration().
3938 // FIXME: Mangling?
3939 // FIXME: Is the qualifier info correct?
3940 // FIXME: Is the DeclContext correct?
3941 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3942 Loc, Loc, DeclarationName(II),
3943 FD->getType(), FD->getTypeSourceInfo(),
3944 SC_None, SC_None,
3945 false/*isInlineSpecified*/,
3946 FD->hasPrototype(),
3947 false/*isConstexprSpecified*/);
3948 NewD = NewFD;
3949
3950 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00003951 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00003952
3953 // Fake up parameter variables; they are declared as if this were
3954 // a typedef.
3955 QualType FDTy = FD->getType();
3956 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3957 SmallVector<ParmVarDecl*, 16> Params;
3958 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3959 AE = FT->arg_type_end(); AI != AE; ++AI) {
3960 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3961 Param->setScopeInfo(0, Params.size());
3962 Params.push_back(Param);
3963 }
David Blaikie9c70e042011-09-21 18:16:56 +00003964 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00003965 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003966 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3967 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003968 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00003969 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003970 VD->getStorageClass(),
3971 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00003972 if (VD->getQualifier()) {
3973 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003974 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003975 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003976 }
3977 return NewD;
3978}
3979
3980/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3981/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00003982void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00003983 if (W.getUsed()) return; // only do this once
3984 W.setUsed(true);
3985 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3986 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00003987 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003988 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3989 NDId->getName()));
3990 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00003991 WeakTopLevelDecl.push_back(NewD);
3992 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3993 // to insert Decl at TU scope, sorry.
3994 DeclContext *SavedContext = CurContext;
3995 CurContext = Context.getTranslationUnitDecl();
3996 PushOnScopeChains(NewD, S);
3997 CurContext = SavedContext;
3998 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003999 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004000 }
4001}
4002
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004003/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4004/// it, apply them to D. This is a bit tricky because PD can have attributes
4005/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004006void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4007 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004008 // It's valid to "forward-declare" #pragma weak, in which case we
4009 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004010 if (Inheritable) {
4011 LoadExternalWeakUndeclaredIdentifiers();
4012 if (!WeakUndeclaredIdentifiers.empty()) {
4013 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4014 if (IdentifierInfo *Id = ND->getIdentifier()) {
4015 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4016 = WeakUndeclaredIdentifiers.find(Id);
4017 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4018 WeakInfo W = I->second;
4019 DeclApplyPragmaWeak(S, ND, W);
4020 WeakUndeclaredIdentifiers[Id] = W;
4021 }
John McCall6fe02402010-10-27 00:59:00 +00004022 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004023 }
4024 }
4025 }
4026
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004027 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004028 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004029 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004030
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004031 // Walk the declarator structure, applying decl attributes that were in a type
4032 // position to the decl itself. This handles cases like:
4033 // int *__attr__(x)** D;
4034 // when X is a decl attribute.
4035 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4036 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004037 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004038
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004039 // Finally, apply any attributes on the decl itself.
4040 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004041 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004042}
John McCall28a6aea2009-11-04 02:18:39 +00004043
John McCall31168b02011-06-15 23:02:42 +00004044/// Is the given declaration allowed to use a forbidden type?
4045static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4046 // Private ivars are always okay. Unfortunately, people don't
4047 // always properly make their ivars private, even in system headers.
4048 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004049 // Function declarations in sys headers will be marked unavailable.
4050 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4051 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004052 return false;
4053
4054 // Require it to be declared in a system header.
4055 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4056}
4057
4058/// Handle a delayed forbidden-type diagnostic.
4059static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4060 Decl *decl) {
4061 if (decl && isForbiddenTypeAllowed(S, decl)) {
4062 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4063 "this system declaration uses an unsupported type"));
4064 return;
4065 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004066 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004067 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4068 // FIXME. we may want to supress diagnostics for all
4069 // kind of forbidden type messages on unavailable functions.
4070 if (FD->hasAttr<UnavailableAttr>() &&
4071 diag.getForbiddenTypeDiagnostic() ==
4072 diag::err_arc_array_param_no_ownership) {
4073 diag.Triggered = true;
4074 return;
4075 }
4076 }
John McCall31168b02011-06-15 23:02:42 +00004077
4078 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4079 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4080 diag.Triggered = true;
4081}
4082
John McCallc1465822011-02-14 07:13:47 +00004083// This duplicates a vector push_back but hides the need to know the
4084// size of the type.
4085void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4086 assert(StackSize <= StackCapacity);
4087
4088 // Grow the stack if necessary.
4089 if (StackSize == StackCapacity) {
4090 unsigned newCapacity = 2 * StackCapacity + 2;
4091 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4092 const char *oldBuffer = (const char*) Stack;
4093
4094 if (StackCapacity)
4095 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4096
4097 delete[] oldBuffer;
4098 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4099 StackCapacity = newCapacity;
4100 }
4101
4102 assert(StackSize < StackCapacity);
4103 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00004104}
4105
John McCallc1465822011-02-14 07:13:47 +00004106void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4107 Decl *decl) {
4108 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00004109
John McCallc1465822011-02-14 07:13:47 +00004110 // Check the invariants.
4111 assert(DD.StackSize >= state.SavedStackSize);
4112 assert(state.SavedStackSize >= DD.ActiveStackBase);
4113 assert(DD.ParsingDepth > 0);
4114
4115 // Drop the parsing depth.
4116 DD.ParsingDepth--;
4117
4118 // If there are no active diagnostics, we're done.
4119 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00004120 return;
4121
John McCall86121512010-01-27 03:50:35 +00004122 // We only want to actually emit delayed diagnostics when we
4123 // successfully parsed a decl.
John McCall18a962b2012-01-26 20:04:03 +00004124 if (decl) {
John McCallc1465822011-02-14 07:13:47 +00004125 // We emit all the active diagnostics, not just those starting
4126 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00004127 // decl spec and another for each declarator; in a decl group like:
4128 // deprecated_typedef foo, *bar, baz();
4129 // only the declarator pops will be passed decls. This is correct;
4130 // we really do need to consider delayed diagnostics from the decl spec
4131 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00004132 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4133 DelayedDiagnostic &diag = DD.Stack[i];
4134 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004135 continue;
4136
John McCallc1465822011-02-14 07:13:47 +00004137 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004138 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004139 // Don't bother giving deprecation diagnostics if the decl is invalid.
4140 if (!decl->isInvalidDecl())
4141 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004142 break;
4143
4144 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00004145 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004146 break;
John McCall31168b02011-06-15 23:02:42 +00004147
4148 case DelayedDiagnostic::ForbiddenType:
4149 handleDelayedForbiddenType(S, diag, decl);
4150 break;
John McCall86121512010-01-27 03:50:35 +00004151 }
4152 }
4153 }
4154
John McCall1064d7e2010-03-16 05:22:47 +00004155 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00004156 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor899b68f2011-03-23 15:13:44 +00004157 DD.Stack[i].Destroy();
John McCall1064d7e2010-03-16 05:22:47 +00004158
John McCallc1465822011-02-14 07:13:47 +00004159 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00004160}
4161
4162static bool isDeclDeprecated(Decl *D) {
4163 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004164 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004165 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004166 // A category implicitly has the availability of the interface.
4167 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4168 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004169 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4170 return false;
4171}
4172
John McCallb45a1e72010-08-26 02:13:20 +00004173void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004174 Decl *Ctx) {
4175 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004176 return;
4177
John McCall86121512010-01-27 03:50:35 +00004178 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004179 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00004180 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004181 << DD.getDeprecationDecl()->getDeclName()
4182 << DD.getDeprecationMessage();
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004183 else if (DD.getUnknownObjCClass()) {
4184 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4185 << DD.getDeprecationDecl()->getDeclName();
4186 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4187 }
Fariborz Jahanian551063102010-10-06 21:18:44 +00004188 else
4189 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00004190 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00004191}
4192
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004193void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004194 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004195 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00004196 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004197 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004198 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4199 UnknownObjCClass,
4200 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004201 return;
4202 }
4203
4204 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004205 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004206 return;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004207 if (!Message.empty()) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00004208 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4209 << Message;
Fariborz Jahanian08a1eb72012-04-23 20:30:52 +00004210 Diag(D->getLocation(),
4211 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4212 : diag::note_previous_decl) << D->getDeclName();
4213 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004214 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00004215 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004216 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004217 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004218 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00004219 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4220 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004221 }
John McCall28a6aea2009-11-04 02:18:39 +00004222}